Fixing a race condition in landmines.py
[chromium-blink-merge.git] / build / landmines.py
blobdb5e54d081dced725036fa2b707edf23313bd5f6
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 a hook. If it detects that the build should
8 be clobbered, it will touch the file <build_dir>/.landmine_triggered. The
9 various build scripts will then check for the presence of this file and clobber
10 accordingly. The script will also emit the reasons for the clobber to stdout.
12 A landmine is tripped when a builder checks out a different revision, and the
13 diff between the new landmines and the old ones is non-null. At this point, the
14 build is clobbered.
15 """
17 import difflib
18 import errno
19 import logging
20 import optparse
21 import os
22 import sys
23 import subprocess
24 import time
26 import landmine_utils
29 SRC_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
32 def get_target_build_dir(build_tool, target, is_iphone=False):
33 """
34 Returns output directory absolute path dependent on build and targets.
35 Examples:
36 r'c:\b\build\slave\win\build\src\out\Release'
37 '/mnt/data/b/build/slave/linux/build/src/out/Debug'
38 '/b/build/slave/ios_rel_device/build/src/xcodebuild/Release-iphoneos'
40 Keep this function in sync with tools/build/scripts/slave/compile.py
41 """
42 ret = None
43 if build_tool == 'xcode':
44 ret = os.path.join(SRC_DIR, 'xcodebuild',
45 target + ('-iphoneos' if is_iphone else ''))
46 elif build_tool in ['make', 'ninja', 'ninja-ios']: # TODO: Remove ninja-ios.
47 ret = os.path.join(SRC_DIR, 'out', target)
48 elif build_tool in ['msvs', 'vs', 'ib']:
49 ret = os.path.join(SRC_DIR, 'build', target)
50 else:
51 raise NotImplementedError('Unexpected GYP_GENERATORS (%s)' % build_tool)
52 return os.path.abspath(ret)
55 def set_up_landmines(target, new_landmines):
56 """Does the work of setting, planting, and triggering landmines."""
57 out_dir = get_target_build_dir(landmine_utils.builder(), target,
58 landmine_utils.platform() == 'ios')
60 landmines_path = os.path.join(out_dir, '.landmines')
61 try:
62 os.makedirs(out_dir)
63 except OSError as e:
64 if e.errno == errno.EEXIST:
65 pass
67 if not os.path.exists(landmines_path):
68 with open(landmines_path, 'w') as f:
69 f.writelines(new_landmines)
70 else:
71 triggered = os.path.join(out_dir, '.landmines_triggered')
72 with open(landmines_path, 'r') as f:
73 old_landmines = f.readlines()
74 if old_landmines != new_landmines:
75 old_date = time.ctime(os.stat(landmines_path).st_ctime)
76 diff = difflib.unified_diff(old_landmines, new_landmines,
77 fromfile='old_landmines', tofile='new_landmines',
78 fromfiledate=old_date, tofiledate=time.ctime(), n=0)
80 with open(triggered, 'w') as f:
81 f.writelines(diff)
82 elif os.path.exists(triggered):
83 # Remove false triggered landmines.
84 os.remove(triggered)
87 def process_options():
88 """Returns a list of landmine emitting scripts."""
89 parser = optparse.OptionParser()
90 parser.add_option(
91 '-s', '--landmine-scripts', action='append',
92 default=[os.path.join(SRC_DIR, 'build', 'get_landmines.py')],
93 help='Path to the script which emits landmines to stdout. The target '
94 'is passed to this script via option -t. Note that an extra '
95 'script can be specified via an env var EXTRA_LANDMINES_SCRIPT.')
96 parser.add_option('-v', '--verbose', action='store_true',
97 default=('LANDMINES_VERBOSE' in os.environ),
98 help=('Emit some extra debugging information (default off). This option '
99 'is also enabled by the presence of a LANDMINES_VERBOSE environment '
100 'variable.'))
102 options, args = parser.parse_args()
104 if args:
105 parser.error('Unknown arguments %s' % args)
107 logging.basicConfig(
108 level=logging.DEBUG if options.verbose else logging.ERROR)
110 extra_script = os.environ.get('EXTRA_LANDMINES_SCRIPT')
111 if extra_script:
112 return options.landmine_scripts + [extra_script]
113 else:
114 return options.landmine_scripts
117 def main():
118 landmine_scripts = process_options()
120 if landmine_utils.builder() in ('dump_dependency_json', 'eclipse'):
121 return 0
123 for target in ('Debug', 'Release', 'Debug_x64', 'Release_x64'):
124 landmines = []
125 for s in landmine_scripts:
126 proc = subprocess.Popen([sys.executable, s, '-t', target],
127 stdout=subprocess.PIPE)
128 output, _ = proc.communicate()
129 landmines.extend([('%s\n' % l.strip()) for l in output.splitlines()])
130 set_up_landmines(target, landmines)
132 return 0
135 if __name__ == '__main__':
136 sys.exit(main())