hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [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.* |
| 12 | """ |
| 13 | |
| 14 | # TODO(hinoka): Use cipd/glyco instead of git/gclient. |
| 15 | |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 16 | import argparse |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 17 | import sys |
| 18 | import os |
| 19 | import subprocess |
| 20 | import re |
| 21 | |
| 22 | |
| 23 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 24 | GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py') |
mmoss@chromium.org | c349971 | 2015-11-25 01:04:01 +0000 | [diff] [blame] | 25 | TARGET_DIR = os.path.expanduser(os.path.join('~', '.chrome-infra')) |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 26 | INFRA_DIR = os.path.join(TARGET_DIR, 'infra') |
| 27 | |
| 28 | |
| 29 | def get_git_rev(target, branch): |
| 30 | return subprocess.check_output( |
| 31 | ['git', 'log', '--format=%B', '-n1', branch], cwd=target) |
| 32 | |
| 33 | |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 34 | def need_to_update(branch): |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 35 | """Checks to see if we need to update the ~/.chrome-infra/infra checkout.""" |
| 36 | try: |
| 37 | cmd = [sys.executable, GCLIENT, 'revinfo'] |
| 38 | subprocess.check_call( |
| 39 | cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.PIPE) |
| 40 | except subprocess.CalledProcessError: |
| 41 | return True # Gclient failed, definitely need to update. |
| 42 | except OSError: |
| 43 | return True # Gclient failed, definitely need to update. |
| 44 | |
iannucci@chromium.org | 3add4b6 | 2015-11-17 20:27:56 +0000 | [diff] [blame] | 45 | if not os.path.isdir(INFRA_DIR): |
| 46 | return True |
| 47 | |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 48 | local_rev = get_git_rev(INFRA_DIR, 'HEAD') |
| 49 | |
| 50 | subprocess.check_call( |
| 51 | ['git', 'fetch', 'origin'], cwd=INFRA_DIR, |
| 52 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 53 | origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,)) |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 54 | return origin_rev != local_rev |
| 55 | |
| 56 | |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 57 | def ensure_infra(branch): |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 58 | """Ensures that infra.git is present in ~/.chrome-infra.""" |
tandrii | 56396af | 2016-06-17 09:50:46 -0700 | [diff] [blame] | 59 | sys.stderr.write( |
| 60 | 'Fetching infra@%s into %s, may take a couple of minutes...' % ( |
| 61 | branch, TARGET_DIR)) |
| 62 | sys.stderr.flush() |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 63 | if not os.path.isdir(TARGET_DIR): |
| 64 | os.mkdir(TARGET_DIR) |
| 65 | if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')): |
| 66 | subprocess.check_call( |
| 67 | [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'], |
| 68 | cwd=TARGET_DIR, |
| 69 | stdout=subprocess.PIPE) |
| 70 | subprocess.check_call( |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 71 | [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)], |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 72 | cwd=TARGET_DIR, |
| 73 | stdout=subprocess.PIPE) |
tandrii | 56396af | 2016-06-17 09:50:46 -0700 | [diff] [blame] | 74 | sys.stderr.write(' done.\n') |
| 75 | sys.stderr.flush() |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def get_available_tools(): |
| 79 | tools = [] |
| 80 | starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') |
| 81 | for root, _, files in os.walk(starting): |
| 82 | if '__main__.py' in files: |
| 83 | tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) |
dsansome@chromium.org | 0878b05 | 2016-02-18 04:51:21 +0000 | [diff] [blame] | 84 | return sorted(tools) |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def run(args): |
| 88 | if args: |
| 89 | tool_name = args[0] |
| 90 | cmd = [ |
| 91 | sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'), |
| 92 | 'infra.tools.%s' % tool_name] |
| 93 | cmd.extend(args[1:]) |
| 94 | return subprocess.call(cmd) |
| 95 | |
| 96 | tools = get_available_tools() |
| 97 | print """usage: cit.py <name of tool> [args for tool] |
| 98 | |
| 99 | Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*" |
| 100 | |
| 101 | Available tools are: |
| 102 | """ |
| 103 | for tool in tools: |
| 104 | print ' * %s' % tool |
| 105 | |
| 106 | |
| 107 | def main(): |
dnj@chromium.org | 8829c52 | 2015-10-24 03:25:05 +0000 | [diff] [blame] | 108 | parser = argparse.ArgumentParser("Chrome Infrastructure CLI.") |
| 109 | parser.add_argument('-b', '--infra-branch', default='deployed', |
| 110 | help="The name of the 'infra' branch to use (default is %(default)s).") |
| 111 | parser.add_argument('args', nargs=argparse.REMAINDER) |
| 112 | |
| 113 | args, extras = parser.parse_known_args() |
| 114 | if args.args and args.args[0] == '--': |
| 115 | args.args.pop(0) |
| 116 | if extras: |
| 117 | args.args = extras + args.args |
| 118 | |
| 119 | if need_to_update(args.infra_branch): |
| 120 | ensure_infra(args.infra_branch) |
| 121 | return run(args.args) |
hinoka@chromium.org | bdc4728 | 2015-07-17 22:05:19 +0000 | [diff] [blame] | 122 | |
| 123 | if __name__ == '__main__': |
| 124 | sys.exit(main()) |