Instrumenting internals of AccountIdFetcher::Start to locate the source of jankiness.
[chromium-blink-merge.git] / build / landmines.py
blobe1ce6f35d4ae45cc9907305d02cfc1e803a7ad12
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """
7 This script runs every build as the first hook (See DEPS). If it detects that
8 the build should be clobbered, it will delete the contents of the build
9 directory.
11 A landmine is tripped when a builder checks out a different revision, and the
12 diff between the new landmines and the old ones is non-null. At this point, the
13 build is clobbered.
14 """
16 import difflib
17 import errno
18 import gyp_environment
19 import logging
20 import optparse
21 import os
22 import shutil
23 import sys
24 import subprocess
25 import time
27 import landmine_utils
30 SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
33 def get_build_dir(build_tool, is_iphone=False):
34 """
35 Returns output directory absolute path dependent on build and targets.
36 Examples:
37 r'c:\b\build\slave\win\build\src\out'
38 '/mnt/data/b/build/slave/linux/build/src/out'
39 '/b/build/slave/ios_rel_device/build/src/xcodebuild'
41 Keep this function in sync with tools/build/scripts/slave/compile.py
42 """
43 ret = None
44 if build_tool == 'xcode':
45 ret = os.path.join(SRC_DIR, 'xcodebuild')
46 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
47 if ('CHROMIUM_OUT_DIR' not in os.environ and
48 'output_dir' in landmine_utils.gyp_generator_flags()):
49 output_dir = landmine_utils.gyp_generator_flags()['output_dir']
50 else:
51 output_dir = os.environ.get('CHROMIUM_OUT_DIR', 'out')
52 ret = os.path.join(SRC_DIR, output_dir)
53 else:
54 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
55 return os.path.abspath(ret)
58 def clobber_if_necessary(new_landmines):
59 """Does the work of setting, planting, and triggering landmines."""
60 out_dir = get_build_dir(landmine_utils.builder())
61 landmines_path = os.path.normpath(os.path.join(out_dir, '..', '.landmines'))
62 try:
63 os.makedirs(out_dir)
64 except OSError as e:
65 if e.errno == errno.EEXIST:
66 pass
68 if os.path.exists(landmines_path):
69 with open(landmines_path, 'r') as f:
70 old_landmines = f.readlines()
71 if old_landmines != new_landmines:
72 old_date = time.ctime(os.stat(landmines_path).st_ctime)
73 diff = difflib.unified_diff(old_landmines, new_landmines,
74 fromfile='old_landmines', tofile='new_landmines',
75 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
76 sys.stdout.write('Clobbering due to:\n')
77 sys.stdout.writelines(diff)
79 # Clobber contents of build directory but not directory itself: some
80 # checkouts have the build directory mounted.
81 for f in os.listdir(out_dir):
82 path = os.path.join(out_dir, f)
83 if os.path.isfile(path):
84 os.unlink(path)
85 elif os.path.isdir(path):
86 shutil.rmtree(path)
88 # Save current set of landmines for next time.
89 with open(landmines_path, 'w') as f:
90 f.writelines(new_landmines)
93 def process_options():
94 """Returns a list of landmine emitting scripts."""
95 parser = optparse.OptionParser()
96 parser.add_option(
97 '-s', '--landmine-scripts', action='append',
98 default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')],
99 help='Path to the script which emits landmines to stdout. The target '
100 'is passed to this script via option -t. Note that an extra '
101 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
102 parser.add_option('-v', '--verbose', action='store_true',
103 default=('LANDMINES_VERBOSE' in os.environ),
104 help=('Emit some extra debugging information (default off). This option '
105 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
106 'variable.'))
108 options, args = parser.parse_args()
110 if args:
111 parser.error('Unknown arguments %s' % args)
113 logging.basicConfig(
114 level=logging.DEBUG if options.verbose else logging.ERROR)
116 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
117 if extra_script:
118 return options.landmine_scripts + [extra_script]
119 else:
120 return options.landmine_scripts
123 def main():
124 landmine_scripts = process_options()
126 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
127 return 0
129 gyp_environment.SetEnvironment()
131 landmines = []
132 for s in landmine_scripts:
133 proc = subprocess.Popen([sys.executable, s], stdout=subprocess.PIPE)
134 output, _ = proc.communicate()
135 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
136 clobber_if_necessary(landmines)
138 return 0
141 if __name__ == '__main__':
142 sys.exit(main())