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 | |
| 16 | import sys |
| 17 | import os |
| 18 | import subprocess |
| 19 | import re |
| 20 | |
| 21 | |
| 22 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 23 | GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py') |
| 24 | TARGET_DIR = os.path.expanduser('~/.chrome-infra') |
| 25 | INFRA_DIR = os.path.join(TARGET_DIR, 'infra') |
| 26 | |
| 27 | |
| 28 | def get_git_rev(target, branch): |
| 29 | return subprocess.check_output( |
| 30 | ['git', 'log', '--format=%B', '-n1', branch], cwd=target) |
| 31 | |
| 32 | |
| 33 | def need_to_update(): |
| 34 | """Checks to see if we need to update the ~/.chrome-infra/infra checkout.""" |
| 35 | try: |
| 36 | cmd = [sys.executable, GCLIENT, 'revinfo'] |
| 37 | subprocess.check_call( |
| 38 | cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.PIPE) |
| 39 | except subprocess.CalledProcessError: |
| 40 | return True # Gclient failed, definitely need to update. |
| 41 | except OSError: |
| 42 | return True # Gclient failed, definitely need to update. |
| 43 | |
| 44 | local_rev = get_git_rev(INFRA_DIR, 'HEAD') |
| 45 | |
| 46 | subprocess.check_call( |
| 47 | ['git', 'fetch', 'origin'], cwd=INFRA_DIR, |
| 48 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 49 | origin_rev = get_git_rev(INFRA_DIR, 'origin/deployed') |
| 50 | return origin_rev != local_rev |
| 51 | |
| 52 | |
| 53 | def ensure_infra(): |
| 54 | """Ensures that infra.git is present in ~/.chrome-infra.""" |
| 55 | print 'Fetching infra into %s, may take a couple of minutes...' % TARGET_DIR |
| 56 | if not os.path.isdir(TARGET_DIR): |
| 57 | os.mkdir(TARGET_DIR) |
| 58 | if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')): |
| 59 | subprocess.check_call( |
| 60 | [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'], |
| 61 | cwd=TARGET_DIR, |
| 62 | stdout=subprocess.PIPE) |
| 63 | subprocess.check_call( |
| 64 | [sys.executable, GCLIENT, 'sync', '--revision', 'deployed'], |
| 65 | cwd=TARGET_DIR, |
| 66 | stdout=subprocess.PIPE) |
| 67 | |
| 68 | |
| 69 | def get_available_tools(): |
| 70 | tools = [] |
| 71 | starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') |
| 72 | for root, _, files in os.walk(starting): |
| 73 | if '__main__.py' in files: |
| 74 | tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) |
| 75 | return tools |
| 76 | |
| 77 | |
| 78 | def run(args): |
| 79 | if args: |
| 80 | tool_name = args[0] |
| 81 | cmd = [ |
| 82 | sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'), |
| 83 | 'infra.tools.%s' % tool_name] |
| 84 | cmd.extend(args[1:]) |
| 85 | return subprocess.call(cmd) |
| 86 | |
| 87 | tools = get_available_tools() |
| 88 | print """usage: cit.py <name of tool> [args for tool] |
| 89 | |
| 90 | Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*" |
| 91 | |
| 92 | Available tools are: |
| 93 | """ |
| 94 | for tool in tools: |
| 95 | print ' * %s' % tool |
| 96 | |
| 97 | |
| 98 | def main(): |
| 99 | if need_to_update(): |
| 100 | ensure_infra() |
| 101 | return run(sys.argv[1:]) |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | sys.exit(main()) |