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
3 # found in the LICENSE file.
16 def MakeDirectory(dir_path):
23 def DeleteDirectory(dir_path):
24 if os.path.exists(dir_path):
25 shutil.rmtree(dir_path)
29 MakeDirectory(os.path.dirname(path))
34 def FindInDirectory(directory, filter):
36 for root, dirnames, filenames in os.walk(directory):
37 matched_files = fnmatch.filter(filenames, filter)
38 files.extend((os.path.join(root, f) for f in matched_files))
42 def FindInDirectories(directories, filter):
44 for directory in directories:
45 all_files.extend(FindInDirectory(directory, filter))
49 def ParseGypList(gyp_string):
50 # The ninja generator doesn't support $ in strings, so use ## to
52 # TODO(cjhopman): Remove when
53 # https://code.google.com/p/gyp/issues/detail?id=327
55 gyp_string = gyp_string.replace('##', '$')
56 return shlex.split(gyp_string)
59 def CheckOptions(options, parser, required=[]):
60 for option_name in required:
61 if not getattr(options, option_name):
62 parser.error('--%s is required' % option_name.replace('_', '-'))
64 def WriteJson(obj, path, only_if_changed=False):
66 if os.path.exists(path):
67 with open(path, 'r') as oldfile:
68 old_dump = oldfile.read()
70 new_dump = json.dumps(obj)
72 if not only_if_changed or old_dump != new_dump:
73 with open(path, 'w') as outfile:
74 outfile.write(new_dump)
77 with open(path, 'r') as jsonfile:
78 return json.load(jsonfile)
81 # This can be used in most cases like subprocess.check_call. The output,
82 # particularly when the command fails, better highlights the command's failure.
83 # This call will directly exit on a failure in the subprocess so that no python
84 # stacktrace is printed after the output of the failed command (and will
85 # instead print a python stack trace before the output of the failed command)
86 def CheckCallDie(args, suppress_output=False, cwd=None):
90 child = subprocess.Popen(args,
91 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
93 stdout, _ = child.communicate()
96 stacktrace = traceback.extract_stack()
97 print >> sys.stderr, ''.join(traceback.format_list(stacktrace))
98 # A user should be able to simply copy and paste the command that failed
100 copyable_command = ' '.join(map(pipes.quote, args))
101 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; '
102 + copyable_command + ' )')
103 print >> sys.stderr, 'Command failed:', copyable_command, '\n'
108 # Directly exit to avoid printing stacktrace.
109 sys.exit(child.returncode)
112 if stdout and not suppress_output:
117 def GetModifiedTime(path):
118 # For a symlink, the modified time should be the greater of the link's
119 # modified time and the modified time of the target.
120 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime)
123 def IsTimeStale(output, inputs):
124 if not os.path.exists(output):
127 output_time = GetModifiedTime(output)
129 if GetModifiedTime(input) > output_time:
135 device_state = CheckCallDie(['adb', 'get-state'], suppress_output=True)
136 return device_state.strip() == 'device'
139 def PrintWarning(message):
140 print 'WARNING: ' + message
143 def PrintBigWarning(message):
145 PrintWarning(message)