OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 """Wrapper for updating and calling infra.git tools. | 7 """Wrapper for updating and calling infra.git tools. |
8 | 8 |
9 This tool does a two things: | 9 This tool does a two things: |
10 * Maintains a infra.git checkout pinned at "deployed" in the home dir | 10 * Maintains a infra.git checkout pinned at "deployed" in the home dir |
11 * Acts as an alias to infra.tools.* | 11 * Acts as an alias to infra.tools.* |
| 12 * Acts as an alias to infra.git/cipd/<executable> |
12 """ | 13 """ |
13 | 14 |
14 # TODO(hinoka): Use cipd/glyco instead of git/gclient. | 15 # TODO(hinoka): Use cipd/glyco instead of git/gclient. |
15 | 16 |
16 import argparse | 17 import argparse |
17 import sys | 18 import sys |
18 import os | 19 import os |
19 import subprocess | 20 import subprocess |
20 import re | 21 import re |
21 | 22 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 cwd=TARGET_DIR, | 69 cwd=TARGET_DIR, |
69 stdout=subprocess.PIPE) | 70 stdout=subprocess.PIPE) |
70 subprocess.check_call( | 71 subprocess.check_call( |
71 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)], | 72 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)], |
72 cwd=TARGET_DIR, | 73 cwd=TARGET_DIR, |
73 stdout=subprocess.PIPE) | 74 stdout=subprocess.PIPE) |
74 sys.stderr.write(' done.\n') | 75 sys.stderr.write(' done.\n') |
75 sys.stderr.flush() | 76 sys.stderr.flush() |
76 | 77 |
77 | 78 |
| 79 def is_exe(filename): |
| 80 """Given a full filepath, return true if the file is an executable.""" |
| 81 if sys.platform.startswith('win'): |
| 82 return filename.endswith('.exe') |
| 83 else: |
| 84 return os.path.isfile(filename) and os.access(filename, os.X_OK) |
| 85 |
| 86 |
78 def get_available_tools(): | 87 def get_available_tools(): |
79 tools = [] | 88 """Returns a tuple of (list of infra tools, list of cipd tools)""" |
| 89 infra_tools = [] |
| 90 cipd_tools = [] |
80 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') | 91 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') |
81 for root, _, files in os.walk(starting): | 92 for root, _, files in os.walk(starting): |
82 if '__main__.py' in files: | 93 if '__main__.py' in files: |
83 tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) | 94 infra_tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) |
84 return sorted(tools) | 95 cipd = os.path.join(TARGET_DIR, 'infra', 'cipd') |
| 96 for fn in os.listdir(cipd): |
| 97 if is_exe(os.path.join(cipd, fn)): |
| 98 cipd_tools.append(fn) |
| 99 return (sorted(infra_tools), sorted(cipd_tools)) |
85 | 100 |
86 | 101 |
87 def run(args): | 102 def run(args): |
88 if args: | 103 if args: |
89 tool_name = args[0] | 104 tool_name = args[0] |
90 cmd = [ | 105 # Check to see if it is a infra tool first. |
91 sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'), | 106 infra_dir = os.path.join( |
92 'infra.tools.%s' % tool_name] | 107 TARGET_DIR, 'infra', 'infra', 'tools', *tool_name.split('.')) |
| 108 cipd_file = os.path.join(TARGET_DIR, 'infra', 'cipd', tool_name) |
| 109 if sys.platform.startswith('win'): |
| 110 cipd_file += '.exe' |
| 111 if (os.path.isdir(infra_dir) |
| 112 and os.path.isfile(os.path.join(infra_dir, '__main__.py'))): |
| 113 cmd = [ |
| 114 sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'), |
| 115 'infra.tools.%s' % tool_name] |
| 116 elif os.path.isfile(cipd_file) and is_exe(cipd_file): |
| 117 cmd = [cipd_file] |
| 118 |
| 119 # Add the remaining arguments. |
93 cmd.extend(args[1:]) | 120 cmd.extend(args[1:]) |
94 return subprocess.call(cmd) | 121 return subprocess.call(cmd) |
95 | 122 |
96 tools = get_available_tools() | 123 infra_tools, cipd_tools = get_available_tools() |
97 print """usage: cit.py <name of tool> [args for tool] | 124 print """usage: cit.py <name of tool> [args for tool] |
98 | 125 |
99 Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*" | 126 Wrapper for maintaining and calling tools in: |
| 127 "infra.git/run.py infra.tools.*" |
| 128 "infra.git/cipd/*" |
100 | 129 |
101 Available tools are: | 130 Available infra tools are:""" |
102 """ | 131 for tool in infra_tools: |
103 for tool in tools: | |
104 print ' * %s' % tool | 132 print ' * %s' % tool |
105 | 133 |
| 134 print """ |
| 135 Available cipd tools are:""" |
| 136 for tool in cipd_tools: |
| 137 print ' * %s' % tool |
| 138 |
| 139 |
106 | 140 |
107 def main(): | 141 def main(): |
108 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.") | 142 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.") |
109 parser.add_argument('-b', '--infra-branch', default='deployed', | 143 parser.add_argument('-b', '--infra-branch', default='deployed', |
110 help="The name of the 'infra' branch to use (default is %(default)s).") | 144 help="The name of the 'infra' branch to use (default is %(default)s).") |
111 parser.add_argument('args', nargs=argparse.REMAINDER) | 145 parser.add_argument('args', nargs=argparse.REMAINDER) |
112 | 146 |
113 args, extras = parser.parse_known_args() | 147 args, extras = parser.parse_known_args() |
114 if args.args and args.args[0] == '--': | 148 if args.args and args.args[0] == '--': |
115 args.args.pop(0) | 149 args.args.pop(0) |
116 if extras: | 150 if extras: |
117 args.args = extras + args.args | 151 args.args = extras + args.args |
118 | 152 |
119 if need_to_update(args.infra_branch): | 153 if need_to_update(args.infra_branch): |
120 ensure_infra(args.infra_branch) | 154 ensure_infra(args.infra_branch) |
121 return run(args.args) | 155 return run(args.args) |
122 | 156 |
123 if __name__ == '__main__': | 157 if __name__ == '__main__': |
124 sys.exit(main()) | 158 sys.exit(main()) |
OLD | NEW |