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 """ | 12 """ |
13 | 13 |
14 # TODO(hinoka): Use cipd/glyco instead of git/gclient. | 14 # TODO(hinoka): Use cipd/glyco instead of git/gclient. |
15 | 15 |
| 16 import argparse |
16 import sys | 17 import sys |
17 import os | 18 import os |
18 import subprocess | 19 import subprocess |
19 import re | 20 import re |
20 | 21 |
21 | 22 |
22 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 23 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
23 GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py') | 24 GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py') |
24 TARGET_DIR = os.path.expanduser('~/.chrome-infra') | 25 TARGET_DIR = os.path.expanduser('~/.chrome-infra') |
25 INFRA_DIR = os.path.join(TARGET_DIR, 'infra') | 26 INFRA_DIR = os.path.join(TARGET_DIR, 'infra') |
26 | 27 |
27 | 28 |
28 def get_git_rev(target, branch): | 29 def get_git_rev(target, branch): |
29 return subprocess.check_output( | 30 return subprocess.check_output( |
30 ['git', 'log', '--format=%B', '-n1', branch], cwd=target) | 31 ['git', 'log', '--format=%B', '-n1', branch], cwd=target) |
31 | 32 |
32 | 33 |
33 def need_to_update(): | 34 def need_to_update(branch): |
34 """Checks to see if we need to update the ~/.chrome-infra/infra checkout.""" | 35 """Checks to see if we need to update the ~/.chrome-infra/infra checkout.""" |
35 try: | 36 try: |
36 cmd = [sys.executable, GCLIENT, 'revinfo'] | 37 cmd = [sys.executable, GCLIENT, 'revinfo'] |
37 subprocess.check_call( | 38 subprocess.check_call( |
38 cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.PIPE) | 39 cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.PIPE) |
39 except subprocess.CalledProcessError: | 40 except subprocess.CalledProcessError: |
40 return True # Gclient failed, definitely need to update. | 41 return True # Gclient failed, definitely need to update. |
41 except OSError: | 42 except OSError: |
42 return True # Gclient failed, definitely need to update. | 43 return True # Gclient failed, definitely need to update. |
43 | 44 |
44 local_rev = get_git_rev(INFRA_DIR, 'HEAD') | 45 local_rev = get_git_rev(INFRA_DIR, 'HEAD') |
45 | 46 |
46 subprocess.check_call( | 47 subprocess.check_call( |
47 ['git', 'fetch', 'origin'], cwd=INFRA_DIR, | 48 ['git', 'fetch', 'origin'], cwd=INFRA_DIR, |
48 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 49 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
49 origin_rev = get_git_rev(INFRA_DIR, 'origin/deployed') | 50 origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,)) |
50 return origin_rev != local_rev | 51 return origin_rev != local_rev |
51 | 52 |
52 | 53 |
53 def ensure_infra(): | 54 def ensure_infra(branch): |
54 """Ensures that infra.git is present in ~/.chrome-infra.""" | 55 """Ensures that infra.git is present in ~/.chrome-infra.""" |
55 print 'Fetching infra into %s, may take a couple of minutes...' % TARGET_DIR | 56 print 'Fetching infra@%s into %s, may take a couple of minutes...' % ( |
| 57 branch, TARGET_DIR) |
56 if not os.path.isdir(TARGET_DIR): | 58 if not os.path.isdir(TARGET_DIR): |
57 os.mkdir(TARGET_DIR) | 59 os.mkdir(TARGET_DIR) |
58 if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')): | 60 if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')): |
59 subprocess.check_call( | 61 subprocess.check_call( |
60 [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'], | 62 [sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'], |
61 cwd=TARGET_DIR, | 63 cwd=TARGET_DIR, |
62 stdout=subprocess.PIPE) | 64 stdout=subprocess.PIPE) |
63 subprocess.check_call( | 65 subprocess.check_call( |
64 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/deployed'], | 66 [sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)], |
65 cwd=TARGET_DIR, | 67 cwd=TARGET_DIR, |
66 stdout=subprocess.PIPE) | 68 stdout=subprocess.PIPE) |
67 | 69 |
68 | 70 |
69 def get_available_tools(): | 71 def get_available_tools(): |
70 tools = [] | 72 tools = [] |
71 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') | 73 starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools') |
72 for root, _, files in os.walk(starting): | 74 for root, _, files in os.walk(starting): |
73 if '__main__.py' in files: | 75 if '__main__.py' in files: |
74 tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) | 76 tools.append(root[len(starting)+1:].replace(os.path.sep, '.')) |
(...skipping 14 matching lines...) Expand all Loading... |
89 | 91 |
90 Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*" | 92 Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*" |
91 | 93 |
92 Available tools are: | 94 Available tools are: |
93 """ | 95 """ |
94 for tool in tools: | 96 for tool in tools: |
95 print ' * %s' % tool | 97 print ' * %s' % tool |
96 | 98 |
97 | 99 |
98 def main(): | 100 def main(): |
99 if need_to_update(): | 101 parser = argparse.ArgumentParser("Chrome Infrastructure CLI.") |
100 ensure_infra() | 102 parser.add_argument('-b', '--infra-branch', default='deployed', |
101 return run(sys.argv[1:]) | 103 help="The name of the 'infra' branch to use (default is %(default)s).") |
| 104 parser.add_argument('args', nargs=argparse.REMAINDER) |
| 105 |
| 106 args, extras = parser.parse_known_args() |
| 107 if args.args and args.args[0] == '--': |
| 108 args.args.pop(0) |
| 109 if extras: |
| 110 args.args = extras + args.args |
| 111 |
| 112 if need_to_update(args.infra_branch): |
| 113 ensure_infra(args.infra_branch) |
| 114 return run(args.args) |
102 | 115 |
103 if __name__ == '__main__': | 116 if __name__ == '__main__': |
104 sys.exit(main()) | 117 sys.exit(main()) |
OLD | NEW |