-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygit.py
More file actions
294 lines (234 loc) · 8.6 KB
/
mygit.py
File metadata and controls
294 lines (234 loc) · 8.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from glob import glob
from inspect import getargspec
from subprocess import Popen, PIPE, STDOUT
# usefull to retain the proper version
# on system that have several python versions
python_exe = sys.executable
class NoSuchCommandError(Exception):
"""The command does not exist."""
class ArgumentError(Exception):
"""Error parsing command arguments."""
def shell_quote(arguments):
def quote(string):
return "\\'".join("'" + p + "'" for p in string.split("'"))
return " ".join(map(quote, arguments))
def system(*arguments):
return os.system(shell_quote(arguments))
proc = Popen(shell_quote(arguments), stdout=PIPE, stderr=PIPE)
return_code = proc.wait()
if return_code == 0:
return proc.stdout.read()
else:
raise RuntimeError('System command returned an error')
def answer(*arguments):
proc = Popen(shell_quote(arguments), shell=True, stdout=PIPE)
return proc.stdout.readlines()
def validate_arguments(fun, args, name=None):
name = name or fun.__name__
argspec = getargspec(fun)[0]
defaults = getargspec(fun)[3]
if defaults is None:
num_def = 0
else:
num_def = len(defaults)
if len(args) < (len(argspec) - num_def):
raise ArgumentError("Missing arguments for %s: %s" % (
name, ", ".join(argspec[len(args):])))
def with_dir(dirname, fun):
cwd = os.getcwd()
result = None
try:
print("--- In directory: %s" % dirname)
os.chdir(dirname)
result = fun()
finally:
os.chdir(cwd)
return result
def with_all_dirs(fun):
[with_dir(dirname, lambda: fun(dirname))
for dirname in glob("*")
if os.path.isdir(dirname)]
def with_repos(*shell_eval):
"""Execute an arbitrary shell command for all reposistories."""
with_all_dirs(lambda dirname: system(*shell_eval))
def create_simple_git_command(command):
def _git_x():
with_all_dirs(lambda dirname: system("git", command))
_git_x.__name__ = command
_git_x.__doc__ = "Run git %s for all repositories" % command
return _git_x
def get_repo_url_from_config(filename):
fh = open(filename, 'r')
urls = [line.split("=")[1].strip()
for line in fh
if line.strip().startswith("url")]
return urls
def list_repos():
"""List repository urls for all repositories."""
for filename in glob('*/.git/config'):
print("\n".join(get_repo_url_from_config(filename)))
def find_distmeta_files(app):
files = []
for p in os.listdir(app):
if p not in ('testproj', 'example', 'tests', 'test'):
filename = os.path.join(app, p, 'distmeta.py')
if os.path.exists(filename):
files.append(filename)
filename = os.path.join(app, p, '__init__.py')
if os.path.exists(filename):
files.append(filename)
return files
def new_package(repo_name, server='chishop'):
"""Bump version and upload new package to chishop."""
import re
version = r'VERSION = \((\d+), (\d+), (\d+)\)'
filenames = find_distmeta_files(repo_name)
if not filenames:
raise RuntimeError("__init__.py or distmeta.py files not found")
def get_current_branch():
result = answer("git", "branch")
for branch in result:
if branch.strip().startswith('*'):
return branch.strip()[1:].strip()
return None
current_branch = with_dir(repo_name, get_current_branch)
if not current_branch:
raise RuntimeError("Coudn't find current branch on the repository %s" % repo_name)
else:
print("Current branch name is (%s)" % current_branch)
filename = None
for filename in filenames:
fh = open(filename, 'r')
content = fh.read()
fh.close()
search_result = re.search(version, content)
if search_result:
break
else:
raise RuntimeError("Regexp %s didn't match anything in files %s" % (
version, str(filenames)))
minor_version_number = int(search_result.group(3)) + 1
new_version = 'VERSION = (%d, %d, %d)' % (
int(search_result.group(1)),
int(search_result.group(2)),
minor_version_number
)
tag_name = 'v%d.%d.%d' % (
int(search_result.group(1)),
int(search_result.group(2)),
minor_version_number
)
new_content = re.sub(version, new_version, content)
fh = open(filename, 'w')
fh.write(new_content)
fh.close()
print("Version number bumped; commiting changes (%s)" % tag_name)
def commit_and_tag():
system("git", "commit", "-a", "-m", "New version %s" % (
new_version.lower()))
system("git", "push")
system("git", "tag", tag_name)
system("git", "push", '--tags')
with_dir(repo_name, commit_and_tag)
urls = get_repo_url_from_config(os.path.join(repo_name, '.git/config'))
def clone_package():
print("Checkout the repository in /tmp/")
system("git", "clone", urls[0], 'new_package')
def upload_package():
system("git", "checkout", current_branch)
print("Uploading new package to %s" % server)
system(python_exe, "setup.py", "sdist", "upload", "-r", server)
with_dir("new_package", upload_package)
system("rm", "-Rf", "new_package")
with_dir("/tmp/", clone_package)
print("Done.")
def commit(message):
"""Commit changes in all repositories."""
commit_cmd = """git commit -a -m "%s" """
with_all_dirs(lambda dirname: os.system(commit_cmd % message))
def checkout(branch):
"""Try to checkout a branch on all repositories."""
checkout_cmd = """git checkout "%s" """
with_all_dirs(lambda dirname: os.system(checkout_cmd % branch))
def branch(branch_name):
"""Try to create a branch on all repositories."""
branch_cmd = """git branch "%s" """
with_all_dirs(lambda dirname: os.system(branch_cmd % branch_name))
def add(filename):
"""Try to add a file to all repositories."""
add_cmd = """git add "%s" """
def add():
os.system(add_cmd % filename)
def test(dirname):
for path in os.listdir('.'):
if os.path.isdir(path) and os.path.exists(path+'/models.py'):
with_dir(path, add)
with_all_dirs(test)
def compile_messages():
"""Stupid brut force methods to compile messages."""
def comp():
system("django-admin.py", "compilemessages")
def test(dirname):
for path in os.listdir('.'):
if os.path.isdir(path) and os.path.exists(path+'/models.py'):
with_dir(path, comp)
with_all_dirs(test)
def make_messages(lang=False):
"""Stupid brut force methods to generate messages."""
def make():
if lang:
system("django-admin.py", "makemessages", "-l", lang)
system("django-admin.py", "makemessages", "-d", "djangojs", "-l", lang)
else:
system("django-admin.py", "makemessages", "-a")
system("django-admin.py", "makemessages", "-d", "djangojs", "-a")
def test(dirname):
for path in os.listdir('.'):
if os.path.isdir(path) and os.path.exists(path+'/models.py'):
with_dir(path, make)
with_all_dirs(test)
commands = {
"commit": commit,
"pull": create_simple_git_command("pull"),
"push": create_simple_git_command("push"),
"status": create_simple_git_command("status"),
"diff": create_simple_git_command("diff"),
"add": add,
"checkout":checkout,
"branch":branch,
"with_repos": with_repos,
"list_repos": list_repos,
"new_package": new_package,
"compile_messages": compile_messages,
"make_messages":make_messages,
}
def usage(syntax):
sys.stderr.write(syntax + "\n")
exit(0)
def help(command_name=None):
if command_name:
command = commands[command_name]
sys.stderr.write("Usage: %s %s\n\n%s\n" % (
command_name,
" ".join("<%s>" % arg
for arg in getargspec(command)[0]),
command.__doc__))
commands["help"] = help
def main(*args):
syntax = """Syntax: %s [%s] [command arguments]""" % (
os.path.basename(args[0]), "|".join(commands.keys()))
len(args) < 2 and usage(syntax)
program_name, command_name = args[:2]
arguments = args[2:]
try:
fun = commands[command_name]
except KeyError:
raise NoSuchCommandError("No such command: %s" % command_name)
validate_arguments(fun, arguments, name=command_name)
result = fun(*arguments)
if __name__ == "__main__":
main(*sys.argv)