comparison python/setup_repo.py @ 405:815eb5c796e9

more stubbing; separate into functions
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 30 Jul 2013 18:43:53 -0700
parents fb304dcd1e64
children 38dd72d72086
comparison
equal deleted inserted replaced
404:fb304dcd1e64 405:815eb5c796e9
13 import sys 13 import sys
14 import urlparse 14 import urlparse
15 15
16 from optparse import OptionParser 16 from optparse import OptionParser
17 17
18
19 # global variables
20 HOST='k0s.org'
21 HGRC="""[paths]
22 default = http://%(host)s/%(repo)s/%(name)s
23 default-push = ssh://%(host)s/%(repo)s/%(name)s
24 """
25
26 call = subprocess.check_output 18 call = subprocess.check_output
27 19
28 def init_repo(directory): 20 def init_repo(directory):
29 """setup repository""" 21 """setup repository"""
30 call(['hg', 'init', directory]) 22 call(['hg', 'init', directory])
31 call(['hg', 'add', '-R', directory]) 23 call(['hg', 'add', '-R', directory])
32 call(['hg', 'commit', '-m', options.message, '-R', directory]) 24 call(['hg', 'commit', '-m', options.message, '-R', directory])
25
26 def setup_remote(local_repo, remote_url, push='ssh', remote_path=None):
27 """
28 setup a remote repository for local_repo
29 - remote_url : public (pull) URL for remote repository
30 - push : protocol for push
31 - remote_path : remote path of hg repository links; otherwise taken from --remote-url, if specified
32 """
33
34 # parse remote URL
35 host, netloc, path, query, anchor = urlparse.urlsplit(options.remote_url)
36 if options.remote_path:
37 remote_host = host
38 if ':' in remote_path:
39 remote_host, remote_path = remote_path.split(':', 1)
40 else:
41 remote_path = path
42
43 # setup remote repository
44 remote_dir = '%s/%s' % (path, name)
45 call(['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)])
46
47 def write_hgrc(directory, host, repo, name):
48 """write hgrc file"""
49
50 HGRC="""[paths]
51 default = http://%(host)s/%(repo)s/%(name)s
52 default-push = ssh://%(host)s/%(repo)s/%(name)s
53 """
54
55 path = os.path.join(directory, '.hg', 'hgrc')
56 # TODO: use ConfigParser
57 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f:
58 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name}
33 59
34 60
35 def main(args=sys.argv[1:]): 61 def main(args=sys.argv[1:]):
36 62
37 # parse command line arguments 63 # parse command line arguments
42 parser.add_option('-u', '--remote-url', dest='remote_url', 68 parser.add_option('-u', '--remote-url', dest='remote_url',
43 default="http://k0s.org/hg/", 69 default="http://k0s.org/hg/",
44 help="URL of host hg repository collection [Default: %default]") 70 help="URL of host hg repository collection [Default: %default]")
45 parser.add_option('-p', '--remote-path', dest='remote_path', 71 parser.add_option('-p', '--remote-path', dest='remote_path',
46 help="remote path of hg repository links; otherwise taken from --remote-url, if specified") 72 help="remote path of hg repository links; otherwise taken from --remote-url, if specified")
47 parser.add_option 73 parser.add_option('-o', '--remote-only', dest='remote_only',
74 action='store_true', default=False,
75 help="setup remote server only")
48 options, args = parser.parse_args(args) 76 options, args = parser.parse_args(args)
49 if len(args) != 1: 77 if len(args) != 1:
50 parser.print_usage() 78 parser.print_usage()
51 parser.exit() 79 parser.exit()
52 directory = args[0] 80 directory = args[0]
53 81
54 # initialize repository 82 # initialize repository
55 init_repo(directory) 83 if not options.remote_only:
84 init_repo(directory)
56 85
57 # setup remote, if specified 86 # setup remote, if specified
58 name = os.path.basename(directory) 87 name = os.path.basename(directory)
59 if options.remote_url: 88 if options.remote_url:
60 89
61 # parse remote URL 90 setup_remote(directory, options.remote_url, remote_path=options.remote_path)
62 host, netloc, path, query, anchor = urlparse.urlsplit(options.remote_url)
63 if options.remote_path:
64 remote_host = host
65 if ':' in remote_path:
66 remote_host, remote_path = remote_path.split(':', 1)
67 else:
68 remote_path = path
69
70 # setup remote repository
71 remote_dir = '%s/%s' % (path, name)
72 call(['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)])
73
74 # write local .hgrc file
75 # TODO: use ConfigParser
76 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f:
77 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name}
78 91
79 # push changes 92 # push changes
80 call(['hg', 'push', '-R', directory]) 93 call(['hg', 'push', '-R', directory])
81 94
82 if __name__ == '__main__': 95 if __name__ == '__main__':