blob: 55331f4f5ea89b21341f931d53833db8e77bc002 [file] [log] [blame]
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +00001#!/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
9This 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.org8829c522015-10-24 03:25:05 +000016import argparse
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000017import sys
18import os
19import subprocess
20import re
21
22
23SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
24GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py')
25TARGET_DIR = os.path.expanduser('~/.chrome-infra')
26INFRA_DIR = os.path.join(TARGET_DIR, 'infra')
27
28
29def get_git_rev(target, branch):
30 return subprocess.check_output(
31 ['git', 'log', '--format=%B', '-n1', branch], cwd=target)
32
33
dnj@chromium.org8829c522015-10-24 03:25:05 +000034def need_to_update(branch):
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000035 """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
45 local_rev = get_git_rev(INFRA_DIR, 'HEAD')
46
47 subprocess.check_call(
48 ['git', 'fetch', 'origin'], cwd=INFRA_DIR,
49 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dnj@chromium.org8829c522015-10-24 03:25:05 +000050 origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000051 return origin_rev != local_rev
52
53
dnj@chromium.org8829c522015-10-24 03:25:05 +000054def ensure_infra(branch):
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000055 """Ensures that infra.git is present in ~/.chrome-infra."""
dnj@chromium.org8829c522015-10-24 03:25:05 +000056 print 'Fetching infra@%s into %s, may take a couple of minutes...' % (
57 branch, TARGET_DIR)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000058 if not os.path.isdir(TARGET_DIR):
59 os.mkdir(TARGET_DIR)
60 if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')):
61 subprocess.check_call(
62 [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'],
63 cwd=TARGET_DIR,
64 stdout=subprocess.PIPE)
65 subprocess.check_call(
dnj@chromium.org8829c522015-10-24 03:25:05 +000066 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000067 cwd=TARGET_DIR,
68 stdout=subprocess.PIPE)
69
70
71def get_available_tools():
72 tools = []
73 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools')
74 for root, _, files in os.walk(starting):
75 if '__main__.py' in files:
76 tools.append(root[len(starting)+1:].replace(os.path.sep, '.'))
77 return tools
78
79
80def run(args):
81 if args:
82 tool_name = args[0]
83 cmd = [
84 sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'),
85 'infra.tools.%s' % tool_name]
86 cmd.extend(args[1:])
87 return subprocess.call(cmd)
88
89 tools = get_available_tools()
90 print """usage: cit.py <name of tool> [args for tool]
91
92 Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*"
93
94 Available tools are:
95 """
96 for tool in tools:
97 print ' * %s' % tool
98
99
100def main():
dnj@chromium.org8829c522015-10-24 03:25:05 +0000101 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.")
102 parser.add_argument('-b', '--infra-branch', default='deployed',
103 help="The name of the 'infra' branch to use (default is %(default)s).")
104 parser.add_argument('args', nargs=argparse.REMAINDER)
105
106 args, extras = parser.parse_known_args()
107 if args.args and args.args[0] == '--':
108 args.args.pop(0)
109 if extras:
110 args.args = extras + args.args
111
112 if need_to_update(args.infra_branch):
113 ensure_infra(args.infra_branch)
114 return run(args.args)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000115
116if __name__ == '__main__':
117 sys.exit(main())