-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
101 lines (77 loc) · 2.71 KB
/
utils.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
96
97
98
99
100
101
#!/usr/bin/env python
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=not-callable
import os
import shutil
import stat
import subprocess
import sys
THIS_DIR = os.path.realpath(os.path.dirname(__file__))
def remove(path):
if os.path.islink(path):
os.unlink(path)
elif os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
rm_tree(path)
def rm_tree(treeDir):
def chmod_and_retry(func, path, _):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
return func(path)
raise IOError("rmtree on %s failed" % path)
shutil.rmtree(treeDir, onerror=chmod_and_retry)
def android_path(*args):
return os.path.realpath(os.path.join(THIS_DIR, '../../', *args))
def llvm_path(*args):
return os.path.realpath(os.path.join(THIS_DIR, '../llvm', *args))
def out_path(*args):
out_dir = os.environ.get('OUT_DIR', android_path('out'))
return os.path.realpath(os.path.join(out_dir, *args))
def build_os_type():
if sys.platform.startswith('linux'):
return 'linux-x86'
else:
return 'darwin-x86'
def host_is_linux():
return build_os_type() == 'linux-x86'
def host_is_darwin():
return build_os_type() == 'darwin-x86'
def yes_or_no(prompt, default=True):
prompt += " (Y/n)" if default else " (y/N)"
prompt += ": "
while True:
reply = str(raw_input(prompt)).lower().strip()
if len(reply) == 0:
return default
elif reply[0] == 'y':
return True
elif reply[0] == 'n':
return False
else:
print "Unrecognized reply, try again"
def check_call_d(args, stdout=None, stderr=None, cwd=None, dry_run=False):
if not dry_run:
return subprocess.check_call(args, stdout=stdout, stderr=stderr,
cwd=cwd)
else:
print "Project " + os.path.basename(cwd) + ": " + ' '.join(args)
def check_output_d(args, stderr=None, cwd=None, dry_run=False):
if not dry_run:
return subprocess.check_output(args, stderr=stderr, cwd=cwd)
else:
print "Project " + os.path.basename(cwd) + ": " + ' '.join(args)