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.
5 from __future__ import print_function
15 def MakeDirectory(dir_path):
22 def DeleteDirectory(dir_path):
23 if os.path.exists(dir_path):
24 shutil.rmtree(dir_path)
28 MakeDirectory(os.path.dirname(path))
33 def FindInDirectory(directory, filter_string):
35 for root, _, filenames in os.walk(directory):
36 matched_files = fnmatch.filter(filenames, filter_string)
37 files.extend((os.path.join(root, f) for f in matched_files))
41 def FindInDirectories(directories, filter_string):
43 for directory in directories:
44 all_files.extend(FindInDirectory(directory, filter_string))
48 # This can be used in most cases like subprocess.check_call. The output,
49 # particularly when the command fails, better highlights the command's failure.
50 # This call will directly exit on a failure in the subprocess so that no python
51 # stacktrace is printed after the output of the failed command (and will
52 # instead print a python stack trace before the output of the failed command)
53 def CheckCallDie(args, suppress_output=False, cwd=None):
57 if os.name == 'posix':
58 child = subprocess.Popen(args,
59 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
61 child = subprocess.Popen(args,
62 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, shell=True)
64 stdout, _ = child.communicate()
67 stacktrace = traceback.extract_stack()
68 print(''.join(traceback.format_list(stacktrace)), file=sys.stderr)
69 # A user should be able to simply copy and paste the command that failed
71 copyable_command = ' '.join(map(pipes.quote, args))
72 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; '
73 + copyable_command + ' )')
74 print('Command failed:', copyable_command, '\n', file=sys.stderr)
79 # Directly exit to avoid printing stacktrace.
80 sys.exit(child.returncode)
83 if stdout and not suppress_output:
88 def GetModifiedTime(path):
89 # For a symlink, the modified time should be the greater of the link's
90 # modified time and the modified time of the target.
91 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime)