blob: aeab2e90c5ccc6af3b4456665ba6d0eef44ebe95 [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."""
dnj@chromium.org8829c522015-10-24 03:25:05 +000059 print 'Fetching infra@%s into %s, may take a couple of minutes...' % (
60 branch, TARGET_DIR)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000061 if not os.path.isdir(TARGET_DIR):
62 os.mkdir(TARGET_DIR)
63 if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')):
64 subprocess.check_call(
65 [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'],
66 cwd=TARGET_DIR,
67 stdout=subprocess.PIPE)
68 subprocess.check_call(
dnj@chromium.org8829c522015-10-24 03:25:05 +000069 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000070 cwd=TARGET_DIR,
71 stdout=subprocess.PIPE)
72
73
74def get_available_tools():
75 tools = []
76 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools')
77 for root, _, files in os.walk(starting):
78 if '__main__.py' in files:
79 tools.append(root[len(starting)+1:].replace(os.path.sep, '.'))
dsansome@chromium.org0878b052016-02-18 04:51:21 +000080 return sorted(tools)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000081
82
83def run(args):
84 if args:
85 tool_name = args[0]
86 cmd = [
87 sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'),
88 'infra.tools.%s' % tool_name]
89 cmd.extend(args[1:])
90 return subprocess.call(cmd)
91
92 tools = get_available_tools()
93 print """usage: cit.py <name of tool> [args for tool]
94
95 Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*"
96
97 Available tools are:
98 """
99 for tool in tools:
100 print ' * %s' % tool
101
102
103def main():
dnj@chromium.org8829c522015-10-24 03:25:05 +0000104 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.")
105 parser.add_argument('-b', '--infra-branch', default='deployed',
106 help="The name of the 'infra' branch to use (default is %(default)s).")
107 parser.add_argument('args', nargs=argparse.REMAINDER)
108
109 args, extras = parser.parse_known_args()
110 if args.args and args.args[0] == '--':
111 args.args.pop(0)
112 if extras:
113 args.args = extras + args.args
114
115 if need_to_update(args.infra_branch):
116 ensure_infra(args.infra_branch)
117 return run(args.args)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000118
119if __name__ == '__main__':
120 sys.exit(main())