blob: 177b7ed07d7dbd72509614ffed19d23fd02a6cc8 [file] [log] [blame]
[email protected]bdc47282015-07-17 22:05:191#!/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 20:00:0012* Acts as an alias to infra.git/cipd/<executable>
[email protected]bdc47282015-07-17 22:05:1913"""
14
Robert Iannucci5d6b00f2018-01-12 23:43:2115# TODO(hinoka,iannucci): Pre-pack infra tools in cipd package with vpython spec.
[email protected]bdc47282015-07-17 22:05:1916
[email protected]8829c522015-10-24 03:25:0517import argparse
[email protected]bdc47282015-07-17 22:05:1918import sys
19import os
[email protected]bdc47282015-07-17 22:05:1920import re
21
Robert Iannucci5d6b00f2018-01-12 23:43:2122import subprocess2 as subprocess
23
[email protected]bdc47282015-07-17 22:05:1924
25SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
26GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py')
[email protected]c3499712015-11-25 01:04:0127TARGET_DIR = os.path.expanduser(os.path.join('~', '.chrome-infra'))
[email protected]bdc47282015-07-17 22:05:1928INFRA_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
[email protected]8829c522015-10-24 03:25:0536def need_to_update(branch):
[email protected]bdc47282015-07-17 22:05:1937 """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 23:43:2141 cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.VOID)
[email protected]bdc47282015-07-17 22:05:1942 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
[email protected]3add4b62015-11-17 20:27:5647 if not os.path.isdir(INFRA_DIR):
48 return True
49
[email protected]bdc47282015-07-17 22:05:1950 local_rev = get_git_rev(INFRA_DIR, 'HEAD')
51
52 subprocess.check_call(
53 ['git', 'fetch', 'origin'], cwd=INFRA_DIR,
Robert Iannucci5d6b00f2018-01-12 23:43:2154 stdout=subprocess.VOID, stderr=subprocess.STDOUT)
[email protected]8829c522015-10-24 03:25:0555 origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,))
[email protected]bdc47282015-07-17 22:05:1956 return origin_rev != local_rev
57
58
[email protected]8829c522015-10-24 03:25:0559def ensure_infra(branch):
[email protected]bdc47282015-07-17 22:05:1960 """Ensures that infra.git is present in ~/.chrome-infra."""
tandrii56396af2016-06-17 16:50:4661 sys.stderr.write(
62 'Fetching infra@%s into %s, may take a couple of minutes...' % (
63 branch, TARGET_DIR))
64 sys.stderr.flush()
[email protected]bdc47282015-07-17 22:05:1965 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 23:43:2171 stdout=subprocess.VOID)
[email protected]bdc47282015-07-17 22:05:1972 subprocess.check_call(
[email protected]8829c522015-10-24 03:25:0573 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
[email protected]bdc47282015-07-17 22:05:1974 cwd=TARGET_DIR,
Robert Iannucci5d6b00f2018-01-12 23:43:2175 stdout=subprocess.VOID)
tandrii56396af2016-06-17 16:50:4676 sys.stderr.write(' done.\n')
77 sys.stderr.flush()
[email protected]bdc47282015-07-17 22:05:1978
79
hinoka9a0de0b2016-07-14 20:00:0080def 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
[email protected]bdc47282015-07-17 22:05:1988def get_available_tools():
hinoka9a0de0b2016-07-14 20:00:0089 """Returns a tuple of (list of infra tools, list of cipd tools)"""
90 infra_tools = []
91 cipd_tools = []
[email protected]bdc47282015-07-17 22:05:1992 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 20:00:0095 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))
[email protected]bdc47282015-07-17 22:05:19101
102
dsansome72decfe2016-08-02 03:55:46103def usage():
hinoka9a0de0b2016-07-14 20:00:00104 infra_tools, cipd_tools = get_available_tools()
[email protected]bdc47282015-07-17 22:05:19105 print """usage: cit.py <name of tool> [args for tool]
106
hinoka9a0de0b2016-07-14 20:00:00107 Wrapper for maintaining and calling tools in:
108 "infra.git/run.py infra.tools.*"
109 "infra.git/cipd/*"
[email protected]bdc47282015-07-17 22:05:19110
hinoka9a0de0b2016-07-14 20:00:00111 Available infra tools are:"""
112 for tool in infra_tools:
[email protected]bdc47282015-07-17 22:05:19113 print ' * %s' % tool
114
hinoka9a0de0b2016-07-14 20:00:00115 print """
116 Available cipd tools are:"""
117 for tool in cipd_tools:
118 print ' * %s' % tool
119
120
dsansome72decfe2016-08-02 03:55:46121def 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
[email protected]bdc47282015-07-17 22:05:19147
148def main():
[email protected]8829c522015-10-24 03:25:05149 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.")
Ryan Tsengde1ed192017-03-08 02:43:43150 parser.add_argument('-b', '--infra-branch', default='cit',
[email protected]8829c522015-10-24 03:25:05151 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)
[email protected]bdc47282015-07-17 22:05:19163
164if __name__ == '__main__':
165 sys.exit(main())