-
Notifications
You must be signed in to change notification settings - Fork 1
/
murmgr.py
378 lines (274 loc) · 9.32 KB
/
murmgr.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
__author__ = 'Alexander Weigl'
import os
import urllib
import sys
import subprocess
import colorama
import click
import yaml
from path import path
colorama.init()
REPO_LAYOUT_VERSION_SUPPORTED = 1
EMPTY_USER_PACKAGE = "https://github.com/CognitionGuidedSurgery/mup-empty.git"
"URL to the git repository, to create empty packages"
REPOSITORY_TEMPLATE = "https://gist.githubusercontent.com/areku/e4d587de7bc8588f151a/raw/"
"template for MSML_REPO_CONFIG"
MSML_REPO_CONFIG = "msml-repo.yaml"
"name of the msml repository file"
MSML_PACKAGE_CONFIG = "msml-package.yaml"
""
DOC ="""
"""
import yaml.representer
yaml.add_representer(path, yaml.representer.SafeRepresenter.represent_unicode)
class AppConfig(object):
def __init__(self):
self.verbosity = 0
self.repository = None
self.always_yes = False
def in_repository(self):
if not self.repository:
p = path(".") / MSML_REPO_CONFIG
if p.exists():
self.repository = path(".").abspath()
return self.repository and self.repository.exists()
appconfig = AppConfig()
"""Global application config"""
# region santa's helper
class NotInRepositoryError(BaseException): pass
def execute(command, **kwargs):
cmd = command.format(**kwargs)
do(cmd)
retcode = subprocess.call(cmd, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=os.getcwd())
if retcode > 0:
error("Could not execute: %s" % cmd)
def read_yaml(filename):
with file(filename) as fp:
return yaml.safe_load(fp.read())
def store_yaml(obj, filename):
import StringIO
try: # safety first, do not open file before we can safely dump
s = StringIO.StringIO()
yaml.dump(obj, s)
with file(filename, 'w') as fp:
fp.write(s.getvalue())
except IOError:
raise
def get_repo_config(repository = None, filename = None):
if repository and not filename:
filename = repository / MSML_REPO_CONFIG
elif appconfig.in_repository() and not filename:
filename = appconfig.repository / MSML_REPO_CONFIG
if not filename:
raise NotInRepositoryError()
config = read_yaml(filename)
if int(config['repo_layout_version']) != REPO_LAYOUT_VERSION_SUPPORTED:
error("Repository layout version is not %d" % REPO_LAYOUT_VERSION_SUPPORTED)
sys.exit(1)
return config
def save_repo_config(config, repository=None, filename=None):
assert config
if repository and not filename:
filename = repository / MSML_REPO_CONFIG
elif appconfig.in_repository():
filename = appconfig.repository / MSML_REPO_CONFIG
store_yaml(config, filename)
def error(message):
click.echo(click.style("ERROR: ", fg='red') + message, err=True)
def warning(message):
click.echo(click.style("WARNING: ", fg='yellow') + message, err=True)
def do(message):
click.echo(click.style(">>> ", fg='blue') + message, err=False)
def go_into_repository():
if appconfig.in_repository():
os.chdir(appconfig.repository)
else:
raise NotInRepositoryError
def get_package_config(name=None, folder=None):
def prefix_paths(prefix, seq):
return map(lambda x: (prefix / x).abspath(), seq)
def prefix_paths_inplace(config, key):
config[key] = prefix_paths(folder.abspath(), config.get(key, []))
if name:
folder = appconfig.repository / name
if not folder:
raise BaseException("Not valid package folder given.")
config = read_yaml(filename=folder / MSML_PACKAGE_CONFIG)
config['folder'] = folder.abspath()
prefix_paths_inplace(config, 'alphabet-directories')
prefix_paths_inplace(config, 'binary-search-path')
prefix_paths_inplace(config, 'python-path')
return config
def get_packages():
try:
config = get_repo_config()
packages = config.get('packages', [])
for p in packages:
yield get_package_config(p)
except NotInRepositoryError:
error("Not in a repository")
# endregion
def ask_yes_no(prompt, default = True):
if appconfig.always_yes:
return default
else:
d = "Yn" if default else "yN"
val = click.prompt(prompt+" [%s]" % d)
if val in ('y', 'Y'):
return True
elif val in ('n','N'):
return False
else:
return default
@click.group()
@click.option("-v", "--verbose", count=True)
@click.option("-r", "--repository", help="give a path to an repository", type=path)
@click.option("-y", "--always-yes/--always-no", default=False)
def cli(verbose=0, repository=None, always_yes = False):
appconfig.verbosity = verbose
appconfig.always_yes = always_yes
if repository:
appconfig.repository = path(repository).abspath()
else:
try:
appconfig.repository = path(os.environ['MSML_REPOSITORY']).abspath()
except KeyError:
appconfig.repository = None
@click.command()
def help():
"show a detail and long help message"
click.echo_via_pager(DOC)
@click.command()
@click.argument("name")
@click.pass_context
def new_package(ctx, name):
"""creates a new user package.
"""
p = path(name).abspath()
curdir = path(".").abspath()
try:
os.mkdir(p)
os.chdir(p)
execute("git init")
execute("git remote add base {repo}", repo=EMPTY_USER_PACKAGE)
execute("git pull base master")
os.chdir(curdir)
if appconfig.in_repository():
if ask_yes_no("Activate user package %s" % name):
ctx.invoke(activate_package, filepath=p)
except OSError:
click.echo(click.style("Folder '%s' already exists!" % name, fg='red'), err=True)
@click.command()
@click.argument("filepath")
def activate_package(filepath):
config = get_repo_config()
fp = path(filepath).relpath(appconfig.repository)
if not fp.exists():
warning("The folder '%s' does not exists!")
l = config.get('packages', list())
if l is None:
l = list()
try:
if fp not in l: # no duplicates
l.append(str(fp))
else:
warning("%s already in the list of active packages" % filepath)
except AttributeError: # not a list
l = [fp]
config['packages'] = l
save_repo_config(config)
@click.command()
@click.argument("filepath")
def deactivate_package(filepath):
config = get_repo_config()
fp = filepath.abspath()
l = config.get('packages', list())
try:
config['packages'] = filter(
lambda x: path(x).abspath() != fp, l
)
except TypeError:
error("Wrong data type detected!")
raise
save_repo_config(config)
@click.command()
@click.argument("name")
def new_repository(name):
try:
os.mkdir(name)
os.chdir(name)
do("Create git repository")
execute("git init")
do("Downloading configuration template")
urllib.urlretrieve(REPOSITORY_TEMPLATE, MSML_REPO_CONFIG)
except OSError:
error("Folder '%s' already exists!" % name)
@click.command()
@click.argument("url")
@click.option("-n", "--name")
@click.pass_context
def download_package(ctx, url, name = None):
"""
"""
if not name:
import re
pattern = re.compile(r"(.+)/(?P<name>.+?).git")
name = str(pattern.search(url).group('name'))
try:
go_into_repository()
do("Enable submodule %s" % name)
execute("git submodule add {repo} {name}" , repo = url, name = name)
ctx.invoke(activate_package, filepath = path('.')/name)
except NotInRepositoryError:
error("Not in a msml repository")
@click.command()
def update_repository():
try:
go_into_repository()
execute("git submodule foreach git pull origin master")
except NotInRepositoryError:
error("Not in a msml repository")
@click.command()
def show():
def echo_filepaths(category, seq):
click.echo(click.style(category, fg='blue'))
rep = len(appconfig.repository.abspath())-1
for p in seq:
click.echo(
"\t"+
click.style(p[:rep], fg='yellow')
+click.style(p[rep:], fg='yellow', bold=True)
)
if appconfig.in_repository():
config = get_repo_config()
alphabet_dirs = []
bin_dirs = []
py_dirs = []
for package in get_packages():
py_dirs += package.get('python-path', [])
bin_dirs += package.get('binary-search-path', [])
alphabet_dirs += package.get('alphabet-directories', [])
click.echo(
click.style(package['name'],fg='blue')
+click.style('@',fg='yellow')
+click.style(package['version'],fg='red')
+"\tin " + package['folder']
)
echo_filepaths("Alphabet", alphabet_dirs)
echo_filepaths("Binary", bin_dirs)
echo_filepaths("Python", py_dirs)
if appconfig.verbosity > 1:
import pprint
click.echo("Repository Config:")
pprint.pprint(config)
else:
error("Not in a repository!")
cli.add_command(new_package)
cli.add_command(new_repository)
cli.add_command(show)
cli.add_command(activate_package)
cli.add_command(deactivate_package)
cli.add_command(update_repository)
cli.add_command(download_package)
if __name__ == "__main__":
cli()