forked from yan12125/python3-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
102 lines (76 loc) · 2.93 KB
/
Copy pathutil.py
File metadata and controls
102 lines (76 loc) · 2.93 KB
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
96
97
98
99
100
101
102
import logging
import os
import pathlib
import shlex
import shutil
from pathlib import Path
from subprocess import check_call, check_output, run, PIPE
from typing import Any, Dict, List, Text, Union
from .env import (
android_api_level as default_android_api_level,
target_arch as default_target_arch,
verify_source,
)
from . import arch
BASE = pathlib.Path(__file__).parents[1]
logger = logging.getLogger(__name__)
_PathType = Union[bytes, Text, os.PathLike]
def tostring(value: Union[List[_PathType], _PathType]) -> str:
if isinstance(value, list):
value = ' '.join(map(os.fspath, value))
return os.fspath(value)
def target_arch() -> arch.Arch:
platform_name = os.getenv('ANDROID_PLATFORM', default_target_arch)
return getattr(arch, platform_name)()
def android_api_level() -> int:
try:
return int(os.environ['ANDROID_API_LEVEL'])
except KeyError:
return default_android_api_level
def rmtree(path: Path) -> None:
print(f'Removing {os.path.relpath(path)}')
try:
shutil.rmtree(path)
except NotADirectoryError:
os.unlink(path)
except FileNotFoundError:
print(f'{os.path.relpath(path)} not found, skipping...')
# XXX: renamed to run? cwd is no longer required
def run_in_dir(cmd: List[str], cwd: _PathType = None, env: Dict[str, Any] = None, mode='run'):
if cwd is None:
cwd = BASE
print(f'Running in {os.path.relpath(cwd)}: ' + ' '.join([shlex.quote(str(arg)) for arg in cmd]))
real_env = os.environ.copy()
if env is not None:
for key, value in env.items():
real_env[key] = tostring(value)
if mode == 'run':
check_call(cmd, cwd=cwd, env=real_env)
elif mode == 'result':
return check_output(cmd, cwd=cwd, env=real_env).decode('utf-8')
elif mode == 'result_noerror':
p = run(cmd, stdout=PIPE, stderr=PIPE, cwd=cwd, env=real_env)
return (b'\n'.join([p.stderr + p.stdout])).decode('utf-8')
class VerificationFailure(Exception):
pass
def gpg_verify_file(sig_filename, filename, validpgpkeys):
if not verify_source:
return
try:
import gnupg
except ImportError:
logger.error('Failed to import gnupg. Please install python-gnupg or '
'set verify_source = False in pybuild/env.py')
raise SystemExit
gpg = gnupg.GPG()
# python-gnupg uses latin-1 by default, which breaks localized date
# strings in gpg command outputs
gpg.encoding = 'utf-8'
with open(filename, 'rb') as f:
data = f.read()
verify_result = gpg.verify_data(str(sig_filename), data)
if verify_result.status not in ('signature good', 'signature valid'):
raise VerificationFailure(verify_result.status)
if verify_result.pubkey_fingerprint not in validpgpkeys:
raise VerificationFailure(f'Signing key {verify_result.pubkey_fingerprint} '
f'not in validpgpkeys {validpgpkeys}')