-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.py
executable file
·597 lines (451 loc) · 16.6 KB
/
deploy.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8
# maintainer: rgaudin
'''
Q&D script to setup rsms apps, mvp way.
** What does it do ?
* clone and/or update your main rapidsms fork
* clone and/or update other rapidsms forks
* links your third-parties apps into your fork
** How it works
1. Create a config file (install.ini)
2. launch the script
done.
** Config file
* INI file format
* each [section] represent a repository
* [main] section represent __your__ main fork (target)
* [main] is mandatory
* [self] section (optional) relates to the rapidsms-impl folder.
* [sections] accepts several options.
Options:
* url (mandatory string): the URL of the git repository to clone
* name (recommended string): the name used to refer to that repository.
* rev (optional int): the revision to pull
* install (optional boolean): whether or not to install app (setup.py)
* patch (optional string): target repository (section name) and patch file
separated by a comma. Example: pygsm, pygsm.patch
* patches (optional string): list of patch-formated instructions (see above)
separated by a pipe.
Example: rapidsms, email-backend.patch | pygsm, patches/pygsm-ussd.patch
* filecopy (optional string): repository (section name), file, destination
separated by commas. Example: rapidsms, patches/new.py, lib/rapidsms
* filecopies (optional string): list of filecopy-formated instructions
(see above) separated by a pipe.
Example: rapidsms, new.py, lib/rapidsms | pygsm, patches/gsm.py, lib/gsm
** Script usage
1. Go to the parent of your soon-to-be rapidsms folder
2. write or copy your config file as install.ini
3. launch: chmod +x deploy.py
4. launch: ./deploy.py
* If you have repositories with install=True,
sudo will prompt for password (after downloads)
* script accepts two optional parameters:
-c filename : specify a config file instead of install.ini
-t path : specify a target path instead of .
'''
import sys
import os
import copy
import getopt
from ConfigParser import ConfigParser, NoOptionError
SELF_PATH = sys.path[0]
class Repository(object):
''' holds repository information '''
isself = False
ident = None
url = None
name = None
branch = None
rev = None
tag = None
install = False
patch = None
patches = None
filecopies = None
patch_target = None
fcopy = None
fcopy_target = None
fcopy_ftarget = None
def __init__(self, url=None, name=None):
if url and isinstance(url, str):
self.url = url
if name and isinstance(name, str):
self.name = name
def get_rev(self):
''' return command-line friendly revision string '''
if self.rev == None:
return ""
else:
return self.rev
def __str__(self):
return "%s/%s" % (self.name, self.url)
def __unicode__(self):
return self.__str__()
class FeederRepository(Repository):
''' Repository which holds apps to link '''
apps = []
def __init__(self, url=None, name=None):
super(FeederRepository, self).__init__(url, name)
self.apps = []
def add_app(self, name):
''' add named app to the list '''
self.apps.append(name)
class GitCommander(object):
''' grab, updates and links repositories based on configuration '''
main = None
others = []
root = None
others_dir_name = 'sources'
others_dir = None
python_install_cmd = "sudo python"
def __init__(self, main=None, others=None, virtualenv=None):
# Add main repository
if main and isinstance(main, Repository):
self.main = main
if others and isinstance(others, (list, tuple)):
for other in others:
self.others.append(other)
self.root = os.getcwd()
if virtualenv:
GitCommander.python_install_cmd = os.path.join(virtualenv, "bin/python")
def build(self):
''' launchs all build steps sequencialy '''
# make paths
self.make_paths()
# clone repositories
self.clone_repos()
# apply patches
self.apply_patches()
# copy additions
self.copy_additions()
# create symlinks
self.make_symlinks()
# install repositories
self.install_repos()
def make_paths(self):
''' creates sources placeholder '''
print "Creating paths"
# make folder for others
try:
os.mkdir(self.others_dir_name)
except OSError, e:
if e.errno == 17:
print " Path %s already exists." % self.others_dir_name
pass
else:
raise
# store repo folders
self.others_dir = os.path.join(self.root, self.others_dir_name)
self.main_dir = os.path.join(self.root, self.main.name)
def make_symlinks(self):
''' creates symlinks in main repo to others' apps '''
main_apps_dir = os.path.join(self.main_dir, 'apps')
for rep in self.others:
if rep.apps.__len__() == 0:
continue
print " Linkings apps from %s" % rep.name
rep_dir = os.path.join(self.others_dir, rep.name)
apps_dir = os.path.join(rep_dir, 'apps')
for app in rep.apps:
app_dir = os.path.join(apps_dir, app)
dst_dir = os.path.join(main_apps_dir, app)
try:
os.unlink(dst_dir)
except:
pass
try:
os.symlink(app_dir, dst_dir)
except OSError, e:
# simlink exist
if e.errno == 17:
print " symlink %s exists." % app
else:
raise
def clone_repos(self):
''' clones referenced repositories '''
repos = copy.copy(self.others)
repos.append(self.main)
for rep in repos:
if rep.isself:
continue
print "Entering repository %s" % rep.name
init_dir = os.getcwd()
# move to others folder
if isinstance(rep, FeederRepository):
os.chdir(self.others_dir)
# update repo if exists
if not os.path.exists(rep.name):
print " Cloning git repository"
os.system("git clone %(url)s %(dir)s" % {'url': rep.url, \
'dir': rep.name})
else:
print " repository %s exists." % rep.name
print " Updating repository"
GitCommander.update_repo(os.path.join(os.getcwd(), rep.name), \
rep.get_rev(), rep.tag, rep.branch)
# move back
os.chdir(init_dir)
def install_repos(self):
for rep in self.others:
if rep.install:
folder = os.path.join(self.others_dir, rep.name)
GitCommander.install(folder)
def apply_patches(self):
for rep in self.others:
if rep.patch and rep.patch_target:
target = self.repo_by_ident(rep.patch_target)
if target == None:
print " Error with patch location."
continue
folder = os.path.join(self.others_dir, target.name)
rep_dir = os.path.join(self.others_dir, rep.name)
GitCommander.patch(folder, os.path.join(rep_dir, rep.patch))
if rep.patches:
print " Applying multiple patches (%s)" % rep.patches.__len__()
for patch_target, patch in rep.patches:
target = self.repo_by_ident(patch_target)
if target == None:
print " Error with patch location."
continue
folder = os.path.join(self.others_dir, target.name)
if rep.isself:
rep_dir = SELF_PATH
else:
rep_dir = os.path.join(self.others_dir, rep.name)
GitCommander.patch(folder, os.path.join(rep_dir, patch))
def copy_additions(self):
print "File Additions"
for rep in self.others:
if rep.fcopy and rep.fcopy_target and rep.fcopy_ftarget:
print " Copying addition"
target = self.repo_by_ident(rep.fcopy_target)
if target == None:
print " Error with filecopy location."
continue
folder = os.path.join(self.others_dir, target.name)
if rep.isself:
rep_dir = SELF_PATH
else:
rep_dir = os.path.join(self.others_dir, rep.name)
GitCommander.copy(rep_dir, rep.fcopy, \
os.path.join(folder, rep.fcopy_ftarget))
if rep.filecopies:
print " Copying %s additions" % rep.filecopies.__len__()
for ffcopy_target, ffcopy, ffcopy_ftarget in rep.filecopies:
target = self.repo_by_ident(ffcopy_target)
if target == None:
print " Error with filecopy location."
continue
folder = os.path.join(self.others_dir, target.name)
if rep.isself:
rep_dir = SELF_PATH
else:
rep_dir = os.path.join(self.others_dir, rep.name)
GitCommander.copy(rep_dir, ffcopy, \
os.path.join(folder, ffcopy_ftarget))
@classmethod
def install(cls, folder):
init_dir = os.getcwd()
os.chdir(folder)
print " Installing repository at %s" % folder
os.system("%s ./setup.py install" % GitCommander.python_install_cmd)
os.chdir(init_dir)
@classmethod
def patch(cls, folder, patch):
init_dir = os.getcwd()
os.chdir(folder)
print " Patching repository at %s" % folder
os.system("patch -p1 < %s" % patch)
os.chdir(init_dir)
@classmethod
def copy(cls, folder, source, target):
init_dir = os.getcwd()
os.chdir(folder)
print " Copy %s at %s" % (source, target)
os.system("cp -rv %s %s/" % (source, target))
os.chdir(init_dir)
@classmethod
def update_repo(cls, folder, rev, tag=None, branch=None):
''' pulls new changes from a git repository '''
init_dir = os.getcwd()
os.chdir(folder)
# reset in case it's an existing repo
# retrieve tags
os.system("git fetch --tags --keep")
# update
os.system("git pull")
# select branch if applicable
if branch:
os.system("git checkout --track -b %(branch)s origin/%(branch)s" \
% {'branch': branch})
# update
os.system("git pull")
if rev:
os.system("git checkout -b %(rev)s %(rev)s" % {'rev': rev})
if tag:
os.system("git checkout -b %(tag)s %(tag)s" % {'tag': tag})
os.system("git reset --hard")
os.chdir(init_dir)
def repo_by_ident(self, ident):
for rep in self.others:
if rep.ident == ident:
return rep
return None
class GitConfig(ConfigParser):
''' configures respositories from a config file '''
def __init__(self, path=None):
ConfigParser.__init__(self)
self.repos = []
self.main_repo = None
if path:
self.readfp(open(path))
self.config_repos()
def config_repos(self):
''' configures reposotory objects based on config '''
for repo_name in self.sections():
ismain = repo_name == 'main'
repo = self.config_repo(repo_name, ismain)
if ismain:
self.main_repo = repo
else:
self.repos.append(repo)
def config_repo(self, ident, main=False):
''' configure a named repository '''
# get name ; if none, default to section name
try:
name = self.get(ident, 'name')
except NoOptionError:
name = ident
# url is mandatory
try:
url = self.get(ident, 'url', None)
except:
if not name == 'self':
raise
url = None
# revision is optional
try:
rev = self.getint(ident, 'rev')
except (NoOptionError, ValueError):
#rev = self.get(ident, 'rev')
rev = None
# branch is optional
try:
branch = self.get(ident, 'branch').strip()
except NoOptionError:
#rev = self.get(ident, 'rev')
branch = None
# install is optional
try:
install = self.getboolean(ident, 'install')
except NoOptionError:
install = False
# apps is optional (feeder only)
try:
apps = self.get(ident, 'apps')
apps = apps.replace(' ', '').split(',')
except:
apps = []
# patch is optional
try:
patch = self.get(ident, 'patch')
patch_target, patch = patch.replace(' ', '').split(',')
except:
patch = None
patch_target = None
# fcopy is optional
try:
fcopy = self.get(ident, 'filecopy')
fcopy_target, fcopy, fcopy_ftarget = \
fcopy.replace(' ', '').split(',')
except:
fcopy = None
fcopy_target = None
fcopy_ftarget = None
# patches is optional
try:
patches = self.get(ident, 'patches')
patchest = []
for tu in patches.replace(' ', '').split('|'):
x = tu.split(',')
patchest.append((x[0], x[1]))
except:
patchest = None
# filecopies is optional
try:
filecopies = self.get(ident, 'filecopies')
filecopiesst = []
for tu in filecopies.replace(' ', '').split('|'):
x = tu.split(',')
filecopiesst.append((x[0], x[1], x[2]))
except:
filecopiesst = None
if main:
repo = Repository(url=url, name=name)
else:
repo = FeederRepository(url=url, name=name)
if name == 'self':
repo.isself = True
repo.ident = ident
repo.rev = rev
repo.branch = branch
repo.install = install
repo.patch = patch
repo.patch_target = patch_target
repo.patches = patchest
repo.fcopy = fcopy
repo.fcopy_target = fcopy_target
repo.fcopy_ftarget = fcopy_ftarget
repo.filecopies = filecopiesst
try:
repo.apps = apps
except:
pass
return repo
def usage(me):
print u"Usage: %s [-c file, --config=file, -t path, --target=path, -e env_path, ] \n\n \
\
-c, --config= Use provided configuration file \n \
-t, -target= Use provided path as home for repositories\n \
-e, --virtualenv= Install required lib in the currently activated virtal env \
" % me
def main():
config_file = 'install.ini'
target = os.getcwd()
virtual_env = None
try:
opts, args = getopt.getopt(sys.argv[1:],
"hc:t:e:",
["help", "config=", "target=", "virtualenv="])
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(2)
virtualenv = None
for o, a in opts:
print o, a
if o in ("-h", "--help"):
usage(sys.argv[0])
sys.exit()
elif o in ("-c", "--config"):
print o, a
config_file = a
elif o in ("-t", "--target"):
target = a
elif o in ("-e", "--virualenv"):
virtualenv = a
else:
assert False, "Unhandled option"
print config_file
if not os.path.exists(config_file) \
or not os.path.exists(target) \
or virtualenv and not os.path.exists(virtualenv):
print "Error. File does not exist."
sys.exit(1)
# read config file
config = GitConfig(config_file)
# build folder and clones and everything
commander = GitCommander(main=config.main_repo, others=config.repos, virtualenv=virtualenv)
commander.build()
if __name__ == '__main__':
main()