1252efc2bf280b599c57e8b5ec4ea56e26207e43
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / gyp / util / build_utils.py
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.
4
5 from __future__ import print_function
6 import fnmatch
7 import os
8 import pipes
9 import shutil
10 import subprocess
11 import sys
12 import traceback
13
14
15 def MakeDirectory(dir_path):
16   try:
17     os.makedirs(dir_path)
18   except OSError:
19     pass
20
21
22 def DeleteDirectory(dir_path):
23   if os.path.exists(dir_path):
24     shutil.rmtree(dir_path)
25
26
27 def Touch(path):
28   MakeDirectory(os.path.dirname(path))
29   with open(path, 'a'):
30     os.utime(path, None)
31
32
33 def FindInDirectory(directory, filter_string):
34   files = []
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))
38   return files
39
40
41 def FindInDirectories(directories, filter_string):
42   all_files = []
43   for directory in directories:
44     all_files.extend(FindInDirectory(directory, filter_string))
45   return all_files
46
47
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):
54   if not cwd:
55     cwd = os.getcwd()
56
57   if os.name == 'posix':
58     child = subprocess.Popen(args,
59         stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
60   else:
61     child = subprocess.Popen(args,
62         stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, shell=True)
63
64   stdout, _ = child.communicate()
65
66   if child.returncode:
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
70     # into their shell.
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)
75
76     if stdout:
77       print(stdout)
78
79     # Directly exit to avoid printing stacktrace.
80     sys.exit(child.returncode)
81
82   else:
83     if stdout and not suppress_output:
84       print(stdout)
85     return stdout
86
87
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)