blob: a83caf7a939c621f221fff6229cc6d2903f26b2e [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')
mmoss@chromium.orgc3499712015-11-25 01:04:01 +000025TARGET_DIR = os.path.expanduser(os.path.join('~', '.chrome-infra'))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000026INFRA_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
iannucci@chromium.org3add4b62015-11-17 20:27:56 +000045 if not os.path.isdir(INFRA_DIR):
46 return True
47
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000048 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.org8829c522015-10-24 03:25:05 +000053 origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000054 return origin_rev != local_rev
55
56
dnj@chromium.org8829c522015-10-24 03:25:05 +000057def ensure_infra(branch):
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000058 """Ensures that infra.git is present in ~/.chrome-infra."""
tandrii56396af2016-06-17 09:50:46 -070059 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.orgbdc47282015-07-17 22:05:19 +000063 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.org8829c522015-10-24 03:25:05 +000071 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000072 cwd=TARGET_DIR,
73 stdout=subprocess.PIPE)
tandrii56396af2016-06-17 09:50:46 -070074 sys.stderr.write(' done.\n')
75 sys.stderr.flush()
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000076
77
78def 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.org0878b052016-02-18 04:51:21 +000084 return sorted(tools)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000085
86
87def 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
107def main():
dnj@chromium.org8829c522015-10-24 03:25:05 +0000108 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.orgbdc47282015-07-17 22:05:19 +0000122
123if __name__ == '__main__':
124 sys.exit(main())