blob: 177b7ed07d7dbd72509614ffed19d23fd02a6cc8 [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.*
hinoka9a0de0b2016-07-14 13:00:00 -070012* Acts as an alias to infra.git/cipd/<executable>
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000013"""
14
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080015# TODO(hinoka,iannucci): Pre-pack infra tools in cipd package with vpython spec.
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000016
dnj@chromium.org8829c522015-10-24 03:25:05 +000017import argparse
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000018import sys
19import os
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000020import re
21
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080022import subprocess2 as subprocess
23
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000024
25SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
26GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py')
mmoss@chromium.orgc3499712015-11-25 01:04:01 +000027TARGET_DIR = os.path.expanduser(os.path.join('~', '.chrome-infra'))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000028INFRA_DIR = os.path.join(TARGET_DIR, 'infra')
29
30
31def get_git_rev(target, branch):
32 return subprocess.check_output(
33 ['git', 'log', '--format=%B', '-n1', branch], cwd=target)
34
35
dnj@chromium.org8829c522015-10-24 03:25:05 +000036def need_to_update(branch):
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000037 """Checks to see if we need to update the ~/.chrome-infra/infra checkout."""
38 try:
39 cmd = [sys.executable, GCLIENT, 'revinfo']
40 subprocess.check_call(
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080041 cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.VOID)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000042 except subprocess.CalledProcessError:
43 return True # Gclient failed, definitely need to update.
44 except OSError:
45 return True # Gclient failed, definitely need to update.
46
iannucci@chromium.org3add4b62015-11-17 20:27:56 +000047 if not os.path.isdir(INFRA_DIR):
48 return True
49
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000050 local_rev = get_git_rev(INFRA_DIR, 'HEAD')
51
52 subprocess.check_call(
53 ['git', 'fetch', 'origin'], cwd=INFRA_DIR,
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080054 stdout=subprocess.VOID, stderr=subprocess.STDOUT)
dnj@chromium.org8829c522015-10-24 03:25:05 +000055 origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000056 return origin_rev != local_rev
57
58
dnj@chromium.org8829c522015-10-24 03:25:05 +000059def ensure_infra(branch):
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000060 """Ensures that infra.git is present in ~/.chrome-infra."""
tandrii56396af2016-06-17 09:50:46 -070061 sys.stderr.write(
62 'Fetching infra@%s into %s, may take a couple of minutes...' % (
63 branch, TARGET_DIR))
64 sys.stderr.flush()
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000065 if not os.path.isdir(TARGET_DIR):
66 os.mkdir(TARGET_DIR)
67 if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')):
68 subprocess.check_call(
69 [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'],
70 cwd=TARGET_DIR,
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080071 stdout=subprocess.VOID)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000072 subprocess.check_call(
dnj@chromium.org8829c522015-10-24 03:25:05 +000073 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000074 cwd=TARGET_DIR,
Robert Iannucci5d6b00f2018-01-12 15:43:21 -080075 stdout=subprocess.VOID)
tandrii56396af2016-06-17 09:50:46 -070076 sys.stderr.write(' done.\n')
77 sys.stderr.flush()
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000078
79
hinoka9a0de0b2016-07-14 13:00:00 -070080def is_exe(filename):
81 """Given a full filepath, return true if the file is an executable."""
82 if sys.platform.startswith('win'):
83 return filename.endswith('.exe')
84 else:
85 return os.path.isfile(filename) and os.access(filename, os.X_OK)
86
87
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000088def get_available_tools():
hinoka9a0de0b2016-07-14 13:00:00 -070089 """Returns a tuple of (list of infra tools, list of cipd tools)"""
90 infra_tools = []
91 cipd_tools = []
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +000092 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools')
93 for root, _, files in os.walk(starting):
94 if '__main__.py' in files:
hinoka9a0de0b2016-07-14 13:00:00 -070095 infra_tools.append(root[len(starting)+1:].replace(os.path.sep, '.'))
96 cipd = os.path.join(TARGET_DIR, 'infra', 'cipd')
97 for fn in os.listdir(cipd):
98 if is_exe(os.path.join(cipd, fn)):
99 cipd_tools.append(fn)
100 return (sorted(infra_tools), sorted(cipd_tools))
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000101
102
dsansome72decfe2016-08-01 20:55:46 -0700103def usage():
hinoka9a0de0b2016-07-14 13:00:00 -0700104 infra_tools, cipd_tools = get_available_tools()
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000105 print """usage: cit.py <name of tool> [args for tool]
106
hinoka9a0de0b2016-07-14 13:00:00 -0700107 Wrapper for maintaining and calling tools in:
108 "infra.git/run.py infra.tools.*"
109 "infra.git/cipd/*"
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000110
hinoka9a0de0b2016-07-14 13:00:00 -0700111 Available infra tools are:"""
112 for tool in infra_tools:
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000113 print ' * %s' % tool
114
hinoka9a0de0b2016-07-14 13:00:00 -0700115 print """
116 Available cipd tools are:"""
117 for tool in cipd_tools:
118 print ' * %s' % tool
119
120
dsansome72decfe2016-08-01 20:55:46 -0700121def run(args):
122 if not args:
123 return usage()
124
125 tool_name = args[0]
126 # Check to see if it is a infra tool first.
127 infra_dir = os.path.join(
128 TARGET_DIR, 'infra', 'infra', 'tools', *tool_name.split('.'))
129 cipd_file = os.path.join(TARGET_DIR, 'infra', 'cipd', tool_name)
130 if sys.platform.startswith('win'):
131 cipd_file += '.exe'
132 if (os.path.isdir(infra_dir)
133 and os.path.isfile(os.path.join(infra_dir, '__main__.py'))):
134 cmd = [
135 sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'),
136 'infra.tools.%s' % tool_name]
137 elif os.path.isfile(cipd_file) and is_exe(cipd_file):
138 cmd = [cipd_file]
139 else:
140 print >>sys.stderr, 'Unknown tool "%s"' % tool_name
141 return usage()
142
143 # Add the remaining arguments.
144 cmd.extend(args[1:])
145 return subprocess.call(cmd)
146
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000147
148def main():
dnj@chromium.org8829c522015-10-24 03:25:05 +0000149 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.")
Ryan Tsengde1ed192017-03-07 18:43:43 -0800150 parser.add_argument('-b', '--infra-branch', default='cit',
dnj@chromium.org8829c522015-10-24 03:25:05 +0000151 help="The name of the 'infra' branch to use (default is %(default)s).")
152 parser.add_argument('args', nargs=argparse.REMAINDER)
153
154 args, extras = parser.parse_known_args()
155 if args.args and args.args[0] == '--':
156 args.args.pop(0)
157 if extras:
158 args.args = extras + args.args
159
160 if need_to_update(args.infra_branch):
161 ensure_infra(args.infra_branch)
162 return run(args.args)
hinoka@chromium.orgbdc47282015-07-17 22:05:19 +0000163
164if __name__ == '__main__':
165 sys.exit(main())