Chromium Code Reviews| Index: scripts/slave/check_target_existence.py |
| diff --git a/scripts/slave/check_target_existence.py b/scripts/slave/check_target_existence.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..ce831a07037c3636e09d7931b6c50321f6d4d47b |
| --- /dev/null |
| +++ b/scripts/slave/check_target_existence.py |
| @@ -0,0 +1,104 @@ |
| +#!/usr/bin/env python |
|
iannucci
2016/03/09 19:01:52
let's add this under the findit module in the 'res
stgao
2016/03/09 20:09:11
Nice! I didn't know about self.resource.
|
| +# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""A tool to check whether a target exists in the ninja build graph or not. |
| + |
| + Before this is run, ninja build files should already be generated or updated. |
| + |
| + For a list of command line options, call this script with '--help'. |
| +""" |
| + |
| +import json |
| +import optparse |
| +import os |
| +import subprocess |
| +import sys |
| + |
| + |
| +def targets_exist(targets, target_build_dir): |
| + """Returns True if the given targets exist in the ninja build file. |
| + |
| + Args: |
| + targets (list): A list of target names. |
| + target_build_dir (str): The path to the target build directory. |
| + """ |
| + print 'Checking targets: %s' % ', '.join(targets) |
| + cmd = ['ninja', '-C', target_build_dir, '-t', 'query'] |
|
Nico
2016/03/08 19:41:36
(i'm assuming you checked that this does the right
stgao
2016/03/09 20:09:11
Yes, it works as the code expected here. I checked
|
| + cmd.extend(targets) |
| + with open(os.devnull, 'w') as f: # Mute normal output, only care about error. |
| + p = subprocess.Popen(cmd, stdout=f) |
| + return_code = p.wait() |
| + print 'return code: %s' % return_code |
| + return return_code == 0 |
| + |
| + |
| +def check_targets(targets, target_build_dir): |
| + """Returns a dict of two lists to indicate which targets exist or not. |
| + |
| + Args: |
| + targets (list): A list of target names. |
| + target_build_dir (str): The path to the target build directory. |
| + """ |
| + # Check all the targets at once first in one ninja command. |
| + # If a target among a few doesn't exist, ninja will exit immediately and not |
| + # process the remaining targets. In that case, we have to check them one by |
| + # one instead. |
| + all_existing = targets_exist(targets, target_build_dir) |
| + if all_existing: # All targets exists. |
| + return {'found': targets, 'not_found': []} |
| + elif len(targets) == 1: # None of the targets exists. |
| + return {'found': [], 'not_found': targets} |
| + else: # Some targets do not exist. |
| + found = [] |
| + not_found = [] |
| + for target in targets: |
| + if targets_exist([target], target_build_dir): |
| + found.append(target) |
| + else: |
| + not_found.append(target) |
| + |
| + return { |
| + 'found': found, |
| + 'not_found': not_found, |
| + } |
| + |
| + |
| +def parse_args(): |
| + option_parser = optparse.OptionParser() |
| + option_parser.add_option('--target-build-dir', |
| + help='The target build directory. eg: out/Release.') |
| + option_parser.add_option('--target', action='append', default=[], |
| + help='Specify the target to be checked. eg: ' |
| + 'browser_tests, obj/path/to/Source.o, or ' |
| + 'gen/path/to/generated.cc, etc.') |
| + option_parser.add_option('--json-output', |
| + help='Optional. Specify a file to dump the result ' |
| + 'as json.') |
| + options, _ = option_parser.parse_args() |
| + return options.target_build_dir, options.target, options.json_output |
| + |
| + |
| +def main(): |
| + target_build_dir, targets, json_output = parse_args() |
| + if not (target_build_dir and targets): |
| + print 'Invalid parameter.' |
| + print 'target-build-dir: %s' % target_build_dir |
| + print 'targets:' |
| + for target in targets: |
| + print ' %s' % target |
| + return 1 |
| + |
| + result = check_targets(targets, target_build_dir) |
| + if json_output: |
| + with open(json_output, 'wb') as f: |
| + json.dump(result, f, indent=2) |
| + else: |
| + print json.dumps(result, indent=2) |
| + |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |