forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmac_util.py
95 lines (74 loc) · 2.13 KB
/
mac_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import distutils.version
import json
import logging
import subprocess
from typing import Union, Tuple
LOGGER = logging.getLogger(__name__)
def version():
"""Invokes sw_vers -productVersion
Raises:
subprocess.CalledProcessError on exit codes non zero
Returns:
e.g. 13.2.1
"""
cmd = [
'sw_vers',
'-productVersion',
]
# output sample:
# 13.2.1
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT).decode('utf-8')
return output
def is_macos_13_or_higher():
"""Returns true if the current MacOS version is 13 or higher"""
return distutils.version.LooseVersion(
'13.0') <= distutils.version.LooseVersion(version())
def stop_usbmuxd():
"""stops the current usbmuxd process"""
cmd = [
'sudo',
'/bin/launchctl',
'stop',
'com.apple.usbmuxd',
]
subprocess.check_call(cmd)
def run_codesign_check(dir_path):
"""Runs codesign check on a directory
Returns:
success (boolean), error (subprocess.CalledProcessError)
"""
try:
cmd = [
'codesign',
'--verify',
'--verbose=9',
'--deep',
'--strict=all',
dir_path,
]
subprocess.check_call(
cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
return False, e
return True, None
error_type = Union[subprocess.CalledProcessError, json.JSONDecodeError]
plist_as_dict_return_type = Union[Tuple[dict, None], Tuple[None, error_type]]
def plist_as_dict(abs_path: str) -> plist_as_dict_return_type:
"""Converts plist to python dictionary.
Args:
abs_path (str) absolute path of the string to convert.
Returns:
Plist (dictionary),
error (subprocess.CalledProcessError, json.JSONDecodeError)
"""
try:
plist = json.loads(
subprocess.check_output(
['plutil', '-convert', 'json', '-o', '-', abs_path]))
return plist, None
except (subprocess.CalledProcessError, json.JSONDecodeError) as e:
return None, e