comparison python/setup_repo.py @ 285:fd0e81fdf1fa

making setup_repo almost a real program
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 10 May 2013 00:29:17 -0700
parents 5a6f89cc6bc4
children 8964b285c2f7
comparison
equal deleted inserted replaced
284:35b30d8fccac 285:fd0e81fdf1fa
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2
3 """
4 setup a repository
5
6 Parameters:
7 - host URL
8 - location on host disk
9 """
2 10
3 import os 11 import os
4 import subprocess 12 import subprocess
5 import sys 13 import sys
14 import urlparse
6 15
16 from subprocess import check_call as call
7 from optparse import OptionParser 17 from optparse import OptionParser
8 18
19
20 # global variables
9 HOST='k0s.org' 21 HOST='k0s.org'
10 HGRC="""[paths] 22 HGRC="""[paths]
11 default = http://%(host)s/%(repo)s/%(name)s 23 default = http://%(host)s/%(repo)s/%(name)s
12 default-push = ssh://%(host)s/%(repo)s/%(name)s 24 default-push = ssh://%(host)s/%(repo)s/%(name)s
13 """ 25 """
14 26
15 def call(command, *args, **kw): 27 def call(command, *args, **kw):
16 code = subprocess.call(command, *args, **kw) 28
17 if isinstance(command, basestring): 29 code = subprocess.call(command, *args, **kw)
18 cmdstr = command 30 if isinstance(command, basestring):
19 else: 31 cmdstr = command
20 cmdstr = ' '.join(command) 32 else:
21 print cmdstr 33 cmdstr = ' '.join(command)
22 if code: 34 print cmdstr
23 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) 35 if code:
24 36 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
37
25 def main(args=sys.argv[1:]): 38 def main(args=sys.argv[1:]):
26 39
27 parser = OptionParser('%prog [options] location') 40 # parse command line arguments
28 parser.add_option('-m', '--message', 41 parser = OptionParser('%prog [options] location')
29 help='commit message') 42 parser.add_option('-m', '--message', dest='message',
30 options, args = parser.parse_args(args) 43 help='commit message')
31 44 parser.add_option('-u', '--remote-url', dest='remote_url',
32 if len(args) != 1: 45 default="http://k0s.org/hg/",
33 parser.print_usage() 46 help="URL of host hg repository collection [Default: %default]")
34 parser.exit() 47 parser.add_options('-p', '--remote-path', dest='remote_path',
48 help="remote path of hg repository links; otherwise taken from --remote-url, if specified")
49 options, args = parser.parse_args(args)
50 if len(args) != 1:
51 parser.print_usage()
52 parser.exit()
53 # TODO: sanity check for remote_url, remote_path
35 54
36 repo = args[0].strip('/') 55 # setup repository
37 56 name = os.path.basename(directory)
38 directory = os.getcwd() 57 call(['hg', 'init'])
39 name = os.path.basename(directory) 58 call(['hg', 'add'])
40 os.chdir('..') 59 call(['hg', 'commit', '-m', options.message or 'initial commit of %s' %name])
41 call(['scp', '-r', name, '%s:~/%s/' % (HOST, repo)]) 60
42 call(['ssh', HOST, "cd %s/%s && hg init && hg add && hg ci -m '%s'" % (repo, name, options.message or 'initial commit of %s' % name)]) 61 # setup remote, if specified
43 os.chdir(directory) 62 if options.remote_url:
44 call(['hg', 'init']) 63
45 call(['hg', 'pull', 'http://%s/%s/%s' % (HOST, repo, name)]) 64 # parse remote URL
46 call(['hg', 'update', '-C']) 65 host, netloc, path, query, anchor = urlparse.urlsplit(options.remote_url)
47 f = file(os.path.join('.hg', 'hgrc'), 'a') 66 if options.remote_path:
48 print >> f, HGRC % { 'host': HOST, 'repo': repo, 'name': name} 67 remote_host = host
68 if ':' in remote_path:
69 remote_host, remote_path = remote_path.split(':', 1)
70 else:
71 remote_path = path
72
73 # setup remote repository
74 call(['ssh', HOST, "cd %s/%s && hg init && hg add && hg ci -m '%s'" % (repo, name, options.message or 'initial commit of %s' % name)])
75
76 # write local .hgrc file
77 # TODO: use ConfigParser
78 f = file(os.path.join('.hg', 'hgrc'), 'w')
79 print >> f, HGRC % { 'host': HOST, 'repo': repo, 'name': name}
49 80
50 if __name__ == '__main__': 81 if __name__ == '__main__':
51 main() 82 main()