-
Notifications
You must be signed in to change notification settings - Fork 38
/
update-starlark.py
executable file
·151 lines (116 loc) · 4.21 KB
/
update-starlark.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
"""
git remote add bazel https://github.com/bazelbuild/bazel
git fetch bazel --depth 1
git subtree add --prefix .tmp/bazel bazel master --squash
git fetch bazel master && git subtree pull --prefix .tmp/bazel bazel master --squash
"""
import os
import subprocess
import sys
class cd(object):
"""Context manager for changing the current working directory"""
def __init__(self, new_path):
self.new_path = os.path.expanduser(new_path)
def __enter__(self):
self.saved_path = os.getcwd()
os.chdir(self.new_path)
return self.new_path
def __exit__(self, etype, value, traceback):
os.chdir(self.saved_path)
os.makedirs('.tmp', exist_ok=True)
def get_submodules(root):
"""return submodules relative to root"""
return [
os.path.join(root, '.tmp', 'bazel'),
]
def is_repo(d):
"""is d a git repo?"""
if not os.path.exists(os.path.join(d, '.git')):
return False
proc = subprocess.Popen('git status',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
cwd=d,
)
status, _ = proc.communicate()
return status == 0
def check_submodule_status(root=None):
"""check submodule status
Has three return values:
'missing' - submodules are absent
'unclean' - submodules have unstaged changes
'clean' - all submodules are up to date
"""
if hasattr(sys, "frozen"):
# frozen via py2exe or similar, don't bother
return 'clean'
if not root:
root = os.getcwd()
if not is_repo(root):
# not in git, assume clean
return 'clean'
submodules = get_submodules(root)
for submodule in submodules:
if not os.path.exists(submodule):
return 'missing'
# Popen can't handle unicode cwd on Windows Python 2
if sys.platform == 'win32' and sys.version_info[0] < 3 \
and not isinstance(root, bytes):
root = root.encode(sys.getfilesystemencoding() or 'ascii')
# check with git submodule status
proc = subprocess.Popen('git submodule status',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
cwd=root,
)
status, _ = proc.communicate()
status = status.decode("ascii", "replace")
for line in status.splitlines():
if line.startswith('-'):
return 'missing'
elif line.startswith('+'):
return 'unclean'
return 'clean'
def update_submodules(repo_dir):
"""update submodules in a repo"""
subprocess.check_call("git submodule init", cwd=repo_dir, shell=True)
subprocess.check_call("git submodule update --depth 1 --recursive", cwd=repo_dir, shell=True)
def add_submodule(url, path):
subprocess.check_call(['git', 'submodule', 'add', '--depth', '1', '-f', '--', url, path])
def remove_submodule(path):
subprocess.check_call(['git', 'submodule', 'deinit', path])
subprocess.check_call(['git', 'rm', path])
bazel_repo = "https://github.com/bazelbuild/bazel"
for _submodule_dir in get_submodules(os.getcwd()):
submodule_dir = _submodule_dir.replace(os.getcwd() + '/', '')
add_submodule(bazel_repo, submodule_dir)
update_submodules(submodule_dir)
with cd(_submodule_dir) as cwd:
subprocess.check_call(["git", "pull"])
# if os.path.exists('bazel') and os.path.exists('.tmp/bazel/.git'):
# cmds = (
# ["git", "fetch", "bazel", "master"],
# ["git", "subtree", "pull", "--prefix", ".tmp/bazel", "bazel", "master", "--squash"],
# )
# else:
# cmds = (
# ["git", "remote", "add", "bazel", bazel_repo],
# ["git", "fetch", "bazel", "--depth", "1"],
# ["git", "subtree", "add", "--prefix", ".tmp/bazel", "bazel", "master", "--squash"],
# )
#
# for cmd in cmds:
# subprocess.call(cmd)
for target_dir in ('src/main', 'src/test',):
subprocess.call([
'rsync',
'-avz',
'--progress',
'--exclude=BUILD',
f'.tmp/bazel/{target_dir}/java/net/',
f'libstarlark/{target_dir}/java/net/',
'--delete',
], cwd=os.getcwd())