-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcit.py
executable file
·104 lines (79 loc) · 2.92 KB
/
cit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Wrapper for updating and calling infra.git tools.
This tool does a two things:
* Maintains a infra.git checkout pinned at "deployed" in the home dir
* Acts as an alias to infra.tools.*
"""
# TODO(hinoka): Use cipd/glyco instead of git/gclient.
import sys
import os
import subprocess
import re
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
GCLIENT = os.path.join(SCRIPT_DIR, 'gclient.py')
TARGET_DIR = os.path.expanduser('~/.chrome-infra')
INFRA_DIR = os.path.join(TARGET_DIR, 'infra')
def get_git_rev(target, branch):
return subprocess.check_output(
['git', 'log', '--format=%B', '-n1', branch], cwd=target)
def need_to_update():
"""Checks to see if we need to update the ~/.chrome-infra/infra checkout."""
try:
cmd = [sys.executable, GCLIENT, 'revinfo']
subprocess.check_call(
cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.PIPE)
except subprocess.CalledProcessError:
return True # Gclient failed, definitely need to update.
except OSError:
return True # Gclient failed, definitely need to update.
local_rev = get_git_rev(INFRA_DIR, 'HEAD')
subprocess.check_call(
['git', 'fetch', 'origin'], cwd=INFRA_DIR,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
origin_rev = get_git_rev(INFRA_DIR, 'origin/deployed')
return origin_rev != local_rev
def ensure_infra():
"""Ensures that infra.git is present in ~/.chrome-infra."""
print 'Fetching infra into %s, may take a couple of minutes...' % TARGET_DIR
if not os.path.isdir(TARGET_DIR):
os.mkdir(TARGET_DIR)
if not os.path.exists(os.path.join(TARGET_DIR, '.gclient')):
subprocess.check_call(
[sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'],
cwd=TARGET_DIR,
stdout=subprocess.PIPE)
subprocess.check_call(
[sys.executable, GCLIENT, 'sync', '--revision', 'origin/deployed'],
cwd=TARGET_DIR,
stdout=subprocess.PIPE)
def get_available_tools():
tools = []
starting = os.path.join(TARGET_DIR, 'infra', 'infra', 'tools')
for root, _, files in os.walk(starting):
if '__main__.py' in files:
tools.append(root[len(starting)+1:].replace(os.path.sep, '.'))
return tools
def run(args):
if args:
tool_name = args[0]
cmd = [
sys.executable, os.path.join(TARGET_DIR, 'infra', 'run.py'),
'infra.tools.%s' % tool_name]
cmd.extend(args[1:])
return subprocess.call(cmd)
tools = get_available_tools()
print """usage: cit.py <name of tool> [args for tool]
Wrapper for maintaining and calling tools in "infra.git/run.py infra.tools.*"
Available tools are:
"""
for tool in tools:
print ' * %s' % tool
def main():
if need_to_update():
ensure_infra()
return run(sys.argv[1:])
if __name__ == '__main__':
sys.exit(main())