[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2015 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. |
| 5 | |
| 6 | |
| 7 | """Wrapper for updating and calling infra.git tools. |
| 8 | |
| 9 | This tool does a two things: |
| 10 | * Maintains a infra.git checkout pinned at "deployed" in the home dir |
| 11 | * Acts as an alias to infra.tools.* |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 12 | * Acts as an alias to infra.git/cipd/<executable> |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 13 | """ |
| 14 | |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 15 | # TODO(hinoka,iannucci): Pre-pack infra tools in cipd package with vpython spec. |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 16 | |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 17 | import argparse |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 18 | import sys |
| 19 | import os |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 20 | import re |
| 21 | |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 22 | import subprocess2 as subprocess |
| 23 | |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 24 | |
| 25 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 26 | GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py') |
[email protected] | c349971 | 2015-11-25 01:04:01 | [diff] [blame] | 27 | TARGET_DIR = os.path.expanduser(os.path.join('~', '.chrome-infra')) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 28 | INFRA_DIR = os.path.join(TARGET_DIR, 'infra') |
| 29 | |
| 30 | |
| 31 | def get_git_rev(target, branch): |
| 32 | return subprocess.check_output( |
| 33 | ['git', 'log', '--format=%B', '-n1', branch], cwd=target) |
| 34 | |
| 35 | |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 36 | def need_to_update(branch): |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 37 | """Checks to see if we need to update the ~/.chrome-infra/infra checkout.""" |
| 38 | try: |
| 39 | cmd = [sys.executable, GCLIENT, 'revinfo'] |
| 40 | subprocess.check_call( |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 41 | cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.VOID) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 42 | except subprocess.CalledProcessError: |
| 43 | return True # Gclient failed, definitely need to update. |
| 44 | except OSError: |
| 45 | return True # Gclient failed, definitely need to update. |
| 46 | |
[email protected] | 3add4b6 | 2015-11-17 20:27:56 | [diff] [blame] | 47 | if not os.path.isdir(INFRA_DIR): |
| 48 | return True |
| 49 | |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 50 | local_rev = get_git_rev(INFRA_DIR, 'HEAD') |
| 51 | |
| 52 | subprocess.check_call( |
| 53 | ['git', 'fetch', 'origin'], cwd=INFRA_DIR, |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 54 | stdout=subprocess.VOID, stderr=subprocess.STDOUT) |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 55 | origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,)) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 56 | return origin_rev != local_rev |
| 57 | |
| 58 | |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 59 | def ensure_infra(branch): |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 60 | """Ensures that infra.git is present in ~/.chrome-infra.""" |
tandrii | 56396af | 2016-06-17 16:50:46 | [diff] [blame] | 61 | sys.stderr.write( |
| 62 | 'Fetching infra@%s into %s, may take a couple of minutes...' % ( |
| 63 | branch, TARGET_DIR)) |
| 64 | sys.stderr.flush() |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 65 | if not os.path.isdir(TARGET_DIR): |
| 66 | os.mkdir(TARGET_DIR) |
| 67 | if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')): |
| 68 | subprocess.check_call( |
| 69 | [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'], |
| 70 | cwd=TARGET_DIR, |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 71 | stdout=subprocess.VOID) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 72 | subprocess.check_call( |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 73 | [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)], |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 74 | cwd=TARGET_DIR, |
Robert Iannucci | 5d6b00f | 2018-01-12 23:43:21 | [diff] [blame] | 75 | stdout=subprocess.VOID) |
tandrii | 56396af | 2016-06-17 16:50:46 | [diff] [blame] | 76 | sys.stderr.write(' done.\n') |
| 77 | sys.stderr.flush() |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 78 | |
| 79 | |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 80 | def is_exe(filename): |
| 81 | """Given a full filepath, return true if the file is an executable.""" |
| 82 | if sys.platform.startswith('win'): |
| 83 | return filename.endswith('.exe') |
| 84 | else: |
| 85 | return os.path.isfile(filename) and os.access(filename, os.X_OK) |
| 86 | |
| 87 | |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 88 | def get_available_tools(): |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 89 | """Returns a tuple of (list of infra tools, list of cipd tools)""" |
| 90 | infra_tools = [] |
| 91 | cipd_tools = [] |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 92 | starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') |
| 93 | for root, _, files in os.walk(starting): |
| 94 | if '__main__.py' in files: |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 95 | infra_tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) |
| 96 | cipd = os.path.join(TARGET_DIR, 'infra', 'cipd') |
| 97 | for fn in os.listdir(cipd): |
| 98 | if is_exe(os.path.join(cipd, fn)): |
| 99 | cipd_tools.append(fn) |
| 100 | return (sorted(infra_tools), sorted(cipd_tools)) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 101 | |
| 102 | |
dsansome | 72decfe | 2016-08-02 03:55:46 | [diff] [blame] | 103 | def usage(): |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 104 | infra_tools, cipd_tools = get_available_tools() |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 105 | print """usage: cit.py <name of tool> [args for tool] |
| 106 | |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 107 | Wrapper for maintaining and calling tools in: |
| 108 | "infra.git/run.py infra.tools.*" |
| 109 | "infra.git/cipd/*" |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 110 | |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 111 | Available infra tools are:""" |
| 112 | for tool in infra_tools: |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 113 | print ' * %s' % tool |
| 114 | |
hinoka | 9a0de0b | 2016-07-14 20:00:00 | [diff] [blame] | 115 | print """ |
| 116 | Available cipd tools are:""" |
| 117 | for tool in cipd_tools: |
| 118 | print ' * %s' % tool |
| 119 | |
| 120 | |
dsansome | 72decfe | 2016-08-02 03:55:46 | [diff] [blame] | 121 | def run(args): |
| 122 | if not args: |
| 123 | return usage() |
| 124 | |
| 125 | tool_name = args[0] |
| 126 | # Check to see if it is a infra tool first. |
| 127 | infra_dir = os.path.join( |
| 128 | TARGET_DIR, 'infra', 'infra', 'tools', *tool_name.split('.')) |
| 129 | cipd_file = os.path.join(TARGET_DIR, 'infra', 'cipd', tool_name) |
| 130 | if sys.platform.startswith('win'): |
| 131 | cipd_file += '.exe' |
| 132 | if (os.path.isdir(infra_dir) |
| 133 | and os.path.isfile(os.path.join(infra_dir, '__main__.py'))): |
| 134 | cmd = [ |
| 135 | sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'), |
| 136 | 'infra.tools.%s' % tool_name] |
| 137 | elif os.path.isfile(cipd_file) and is_exe(cipd_file): |
| 138 | cmd = [cipd_file] |
| 139 | else: |
| 140 | print >>sys.stderr, 'Unknown tool "%s"' % tool_name |
| 141 | return usage() |
| 142 | |
| 143 | # Add the remaining arguments. |
| 144 | cmd.extend(args[1:]) |
| 145 | return subprocess.call(cmd) |
| 146 | |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 147 | |
| 148 | def main(): |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 149 | parser = argparse.ArgumentParser("Chrome Infrastructure CLI.") |
Ryan Tseng | de1ed19 | 2017-03-08 02:43:43 | [diff] [blame] | 150 | parser.add_argument('-b', '--infra-branch', default='cit', |
[email protected] | 8829c52 | 2015-10-24 03:25:05 | [diff] [blame] | 151 | help="The name of the 'infra' branch to use (default is %(default)s).") |
| 152 | parser.add_argument('args', nargs=argparse.REMAINDER) |
| 153 | |
| 154 | args, extras = parser.parse_known_args() |
| 155 | if args.args and args.args[0] == '--': |
| 156 | args.args.pop(0) |
| 157 | if extras: |
| 158 | args.args = extras + args.args |
| 159 | |
| 160 | if need_to_update(args.infra_branch): |
| 161 | ensure_infra(args.infra_branch) |
| 162 | return run(args.args) |
[email protected] | bdc4728 | 2015-07-17 22:05:19 | [diff] [blame] | 163 | |
| 164 | if __name__ == '__main__': |
| 165 | sys.exit(main()) |