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 fnmatch | 5 import fnmatch |
6 import json | 6 import json |
7 import os | 7 import os |
8 import pipes | 8 import pipes |
9 import shlex | 9 import shlex |
10 import shutil | 10 import shutil |
11 import subprocess | 11 import subprocess |
12 import sys | 12 import sys |
13 import traceback | |
14 | 13 |
15 | 14 |
16 def MakeDirectory(dir_path): | 15 def MakeDirectory(dir_path): |
17 try: | 16 try: |
18 os.makedirs(dir_path) | 17 os.makedirs(dir_path) |
19 except OSError: | 18 except OSError: |
20 pass | 19 pass |
21 | 20 |
22 | 21 |
23 def DeleteDirectory(dir_path): | 22 def DeleteDirectory(dir_path): |
24 if os.path.exists(dir_path): | 23 if os.path.exists(dir_path): |
25 shutil.rmtree(dir_path) | 24 shutil.rmtree(dir_path) |
26 | 25 |
27 | 26 |
28 def Touch(path): | 27 def Touch(path): |
29 MakeDirectory(os.path.dirname(path)) | 28 MakeDirectory(os.path.dirname(path)) |
30 with open(path, 'a'): | 29 with open(path, 'a'): |
31 os.utime(path, None) | 30 os.utime(path, None) |
32 | 31 |
33 | 32 |
34 def FindInDirectory(directory, filter): | 33 def FindInDirectory(directory, filename_filter): |
35 files = [] | 34 files = [] |
36 for root, dirnames, filenames in os.walk(directory): | 35 for root, _dirnames, filenames in os.walk(directory): |
37 matched_files = fnmatch.filter(filenames, filter) | 36 matched_files = fnmatch.filter(filenames, filename_filter) |
38 files.extend((os.path.join(root, f) for f in matched_files)) | 37 files.extend((os.path.join(root, f) for f in matched_files)) |
39 return files | 38 return files |
40 | 39 |
41 | 40 |
42 def FindInDirectories(directories, filter): | 41 def FindInDirectories(directories, filename_filter): |
43 all_files = [] | 42 all_files = [] |
44 for directory in directories: | 43 for directory in directories: |
45 all_files.extend(FindInDirectory(directory, filter)) | 44 all_files.extend(FindInDirectory(directory, filename_filter)) |
46 return all_files | 45 return all_files |
47 | 46 |
48 | 47 |
49 def ParseGypList(gyp_string): | 48 def ParseGypList(gyp_string): |
50 # The ninja generator doesn't support $ in strings, so use ## to | 49 # The ninja generator doesn't support $ in strings, so use ## to |
51 # represent $. | 50 # represent $. |
52 # TODO(cjhopman): Remove when | 51 # TODO(cjhopman): Remove when |
53 # https://code.google.com/p/gyp/issues/detail?id=327 | 52 # https://code.google.com/p/gyp/issues/detail?id=327 |
54 # is addressed. | 53 # is addressed. |
55 gyp_string = gyp_string.replace('##', '$') | 54 gyp_string = gyp_string.replace('##', '$') |
56 return shlex.split(gyp_string) | 55 return shlex.split(gyp_string) |
57 | 56 |
58 | 57 |
59 def CheckOptions(options, parser, required=[]): | 58 def CheckOptions(options, parser, required=None): |
| 59 if not required: |
| 60 return |
60 for option_name in required: | 61 for option_name in required: |
61 if not getattr(options, option_name): | 62 if not getattr(options, option_name): |
62 parser.error('--%s is required' % option_name.replace('_', '-')) | 63 parser.error('--%s is required' % option_name.replace('_', '-')) |
63 | 64 |
64 def WriteJson(obj, path, only_if_changed=False): | 65 def WriteJson(obj, path, only_if_changed=False): |
65 old_dump = None | 66 old_dump = None |
66 if os.path.exists(path): | 67 if os.path.exists(path): |
67 with open(path, 'r') as oldfile: | 68 with open(path, 'r') as oldfile: |
68 old_dump = oldfile.read() | 69 old_dump = oldfile.read() |
69 | 70 |
70 new_dump = json.dumps(obj) | 71 new_dump = json.dumps(obj) |
71 | 72 |
72 if not only_if_changed or old_dump != new_dump: | 73 if not only_if_changed or old_dump != new_dump: |
73 with open(path, 'w') as outfile: | 74 with open(path, 'w') as outfile: |
74 outfile.write(new_dump) | 75 outfile.write(new_dump) |
75 | 76 |
76 def ReadJson(path): | 77 def ReadJson(path): |
77 with open(path, 'r') as jsonfile: | 78 with open(path, 'r') as jsonfile: |
78 return json.load(jsonfile) | 79 return json.load(jsonfile) |
79 | 80 |
80 | 81 |
81 class CalledProcessError(Exception): | 82 class CalledProcessError(Exception): |
82 """This exception is raised when the process run by CheckOutput | 83 """This exception is raised when the process run by CheckOutput |
83 exits with a non-zero exit code.""" | 84 exits with a non-zero exit code.""" |
84 | 85 |
85 def __init__(self, cwd, args, output): | 86 def __init__(self, cwd, args, output): |
| 87 super(CalledProcessError, self).__init__() |
86 self.cwd = cwd | 88 self.cwd = cwd |
87 self.args = args | 89 self.args = args |
88 self.output = output | 90 self.output = output |
89 | 91 |
90 def __str__(self): | 92 def __str__(self): |
91 # A user should be able to simply copy and paste the command that failed | 93 # A user should be able to simply copy and paste the command that failed |
92 # into their shell. | 94 # into their shell. |
93 copyable_command = '( cd {}; {} )'.format(os.path.abspath(self.cwd), | 95 copyable_command = '( cd {}; {} )'.format(os.path.abspath(self.cwd), |
94 ' '.join(map(pipes.quote, self.args))) | 96 ' '.join(map(pipes.quote, self.args))) |
95 return 'Command failed: {}\n{}'.format(copyable_command, self.output) | 97 return 'Command failed: {}\n{}'.format(copyable_command, self.output) |
(...skipping 26 matching lines...) Expand all Loading... |
122 # For a symlink, the modified time should be the greater of the link's | 124 # For a symlink, the modified time should be the greater of the link's |
123 # modified time and the modified time of the target. | 125 # modified time and the modified time of the target. |
124 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime) | 126 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime) |
125 | 127 |
126 | 128 |
127 def IsTimeStale(output, inputs): | 129 def IsTimeStale(output, inputs): |
128 if not os.path.exists(output): | 130 if not os.path.exists(output): |
129 return True | 131 return True |
130 | 132 |
131 output_time = GetModifiedTime(output) | 133 output_time = GetModifiedTime(output) |
132 for input in inputs: | 134 for i in inputs: |
133 if GetModifiedTime(input) > output_time: | 135 if GetModifiedTime(i) > output_time: |
134 return True | 136 return True |
135 return False | 137 return False |
136 | 138 |
137 | 139 |
138 def IsDeviceReady(): | 140 def IsDeviceReady(): |
139 device_state = CheckOutput(['adb', 'get-state']) | 141 device_state = CheckOutput(['adb', 'get-state']) |
140 return device_state.strip() == 'device' | 142 return device_state.strip() == 'device' |
141 | 143 |
142 | 144 |
143 def PrintWarning(message): | 145 def PrintWarning(message): |
144 print 'WARNING: ' + message | 146 print 'WARNING: ' + message |
145 | 147 |
146 | 148 |
147 def PrintBigWarning(message): | 149 def PrintBigWarning(message): |
148 print '***** ' * 8 | 150 print '***** ' * 8 |
149 PrintWarning(message) | 151 PrintWarning(message) |
150 print '***** ' * 8 | 152 print '***** ' * 8 |
OLD | NEW |