OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import contextlib | 5 import contextlib |
6 import fnmatch | 6 import fnmatch |
7 import json | 7 import json |
8 import os | 8 import os |
9 import pipes | 9 import pipes |
10 import shlex | 10 import shlex |
11 import shutil | 11 import shutil |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
14 import tempfile | 14 import tempfile |
15 import zipfile | 15 import zipfile |
16 | 16 |
17 CHROMIUM_SRC = os.path.join(os.path.dirname(__file__), | 17 CHROMIUM_SRC = os.path.normpath( |
18 os.pardir, os.pardir, os.pardir, os.pardir) | 18 os.path.join(os.path.dirname(__file__), |
| 19 os.pardir, os.pardir, os.pardir, os.pardir)) |
19 COLORAMA_ROOT = os.path.join(CHROMIUM_SRC, | 20 COLORAMA_ROOT = os.path.join(CHROMIUM_SRC, |
20 'third_party', 'colorama', 'src') | 21 'third_party', 'colorama', 'src') |
21 | 22 |
22 | 23 |
23 @contextlib.contextmanager | 24 @contextlib.contextmanager |
24 def TempDir(): | 25 def TempDir(): |
25 dirname = tempfile.mkdtemp() | 26 dirname = tempfile.mkdtemp() |
26 try: | 27 try: |
27 yield dirname | 28 yield dirname |
28 finally: | 29 finally: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 return shlex.split(gyp_string) | 76 return shlex.split(gyp_string) |
76 | 77 |
77 | 78 |
78 def CheckOptions(options, parser, required=None): | 79 def CheckOptions(options, parser, required=None): |
79 if not required: | 80 if not required: |
80 return | 81 return |
81 for option_name in required: | 82 for option_name in required: |
82 if getattr(options, option_name) is None: | 83 if getattr(options, option_name) is None: |
83 parser.error('--%s is required' % option_name.replace('_', '-')) | 84 parser.error('--%s is required' % option_name.replace('_', '-')) |
84 | 85 |
| 86 |
85 def WriteJson(obj, path, only_if_changed=False): | 87 def WriteJson(obj, path, only_if_changed=False): |
86 old_dump = None | 88 old_dump = None |
87 if os.path.exists(path): | 89 if os.path.exists(path): |
88 with open(path, 'r') as oldfile: | 90 with open(path, 'r') as oldfile: |
89 old_dump = oldfile.read() | 91 old_dump = oldfile.read() |
90 | 92 |
91 new_dump = json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')) | 93 new_dump = json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')) |
92 | 94 |
93 if not only_if_changed or old_dump != new_dump: | 95 if not only_if_changed or old_dump != new_dump: |
94 with open(path, 'w') as outfile: | 96 with open(path, 'w') as outfile: |
95 outfile.write(new_dump) | 97 outfile.write(new_dump) |
96 | 98 |
| 99 |
97 def ReadJson(path): | 100 def ReadJson(path): |
98 with open(path, 'r') as jsonfile: | 101 with open(path, 'r') as jsonfile: |
99 return json.load(jsonfile) | 102 return json.load(jsonfile) |
100 | 103 |
101 | 104 |
102 class CalledProcessError(Exception): | 105 class CalledProcessError(Exception): |
103 """This exception is raised when the process run by CheckOutput | 106 """This exception is raised when the process run by CheckOutput |
104 exits with a non-zero exit code.""" | 107 exits with a non-zero exit code.""" |
105 | 108 |
106 def __init__(self, cwd, args, output): | 109 def __init__(self, cwd, args, output): |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 208 |
206 | 209 |
207 def PrintWarning(message): | 210 def PrintWarning(message): |
208 print 'WARNING: ' + message | 211 print 'WARNING: ' + message |
209 | 212 |
210 | 213 |
211 def PrintBigWarning(message): | 214 def PrintBigWarning(message): |
212 print '***** ' * 8 | 215 print '***** ' * 8 |
213 PrintWarning(message) | 216 PrintWarning(message) |
214 print '***** ' * 8 | 217 print '***** ' * 8 |
| 218 |
| 219 |
| 220 def GetPythonDependencies(): |
| 221 """Gets the paths of imported non-system python modules. |
| 222 |
| 223 A path is assumed to be a "system" import if it is outside of chromium's |
| 224 src/. The paths will be relative to the current directory. |
| 225 """ |
| 226 module_paths = (m.__file__ for m in sys.modules.itervalues() |
| 227 if m is not None and hasattr(m, '__file__')) |
| 228 |
| 229 abs_module_paths = map(os.path.abspath, module_paths) |
| 230 |
| 231 non_system_module_paths = [ |
| 232 p for p in abs_module_paths if p.startswith(CHROMIUM_SRC)] |
| 233 def ConvertPycToPy(s): |
| 234 if s.endswith('.pyc'): |
| 235 return s[:-1] |
| 236 return s |
| 237 |
| 238 non_system_module_paths = map(ConvertPycToPy, non_system_module_paths) |
| 239 non_system_module_paths = map(os.path.relpath, non_system_module_paths) |
| 240 return sorted(set(non_system_module_paths)) |
| 241 |
| 242 |
| 243 def AddDepfileOption(parser): |
| 244 parser.add_option('--depfile', |
| 245 help='Path to depfile. This must be specified as the ' |
| 246 'action\'s first output.') |
| 247 |
| 248 |
| 249 def WriteDepfile(path, dependencies): |
| 250 with open(path, 'w') as depfile: |
| 251 depfile.write(path) |
| 252 depfile.write(': ') |
| 253 depfile.write(' '.join(dependencies)) |
| 254 depfile.write('\n') |
OLD | NEW |