annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
3 """
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
4 setup a repository
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
5
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
6 Parameters:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
7 - host URL
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
8 - location on host disk
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
9 """
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
10
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import os
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import subprocess
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import sys
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
14 import urlparse
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 from optparse import OptionParser
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
403
62eb670a408f dont roll your own
Jeff Hammel <jhammel@mozilla.com>
parents: 402
diff changeset
18 call = subprocess.check_output
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
19
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
20 def init_repo(directory):
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
21 """setup repository"""
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
22 call(['hg', 'init', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
23 call(['hg', 'add', '-R', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
24 call(['hg', 'commit', '-m', options.message, '-R', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
25
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
26 def setup_remote(local_repo, remote_url, push='ssh', remote_path=None):
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
27 """
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
28 setup a remote repository for local_repo
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
29 - remote_url : public (pull) URL for remote repository
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
30 - push : protocol for push
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
31 - remote_path : remote path of hg repository links; otherwise taken from --remote-url, if specified
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
32 """
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
33
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
34 # parse remote URL
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
35 host, netloc, path, query, anchor = urlparse.urlsplit(options.remote_url)
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
36 if options.remote_path:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
37 remote_host = host
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
38 if ':' in remote_path:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
39 remote_host, remote_path = remote_path.split(':', 1)
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
40 else:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
41 remote_path = path
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
42
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
43 # setup remote repository
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
44 remote_dir = '%s/%s' % (path, name)
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
45 call(['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)])
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
46
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
47 def write_hgrc(directory, host, repo, name):
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
48 """write hgrc file"""
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
49
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
50 HGRC="""[paths]
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
51 default = http://%(host)s/%(repo)s/%(name)s
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
52 default-push = ssh://%(host)s/%(repo)s/%(name)s
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
53 """
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
54
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
55 path = os.path.join(directory, '.hg', 'hgrc')
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
56 # TODO: use ConfigParser
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
57 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
58 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name}
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
59
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
60
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 def main(args=sys.argv[1:]):
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
63 # parse command line arguments
403
62eb670a408f dont roll your own
Jeff Hammel <jhammel@mozilla.com>
parents: 402
diff changeset
64 parser = OptionParser('%prog [options] directory')
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
65 parser.add_option('-m', '--message', dest='message',
401
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
66 default='initial commit',
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
67 help='commit message [Default: %default]')
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
68 parser.add_option('-u', '--remote-url', dest='remote_url',
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
69 default="http://k0s.org/hg/",
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
70 help="URL of host hg repository collection [Default: %default]")
400
Jeff Hammel <jhammel@mozilla.com>
parents: 285
diff changeset
71 parser.add_option('-p', '--remote-path', dest='remote_path',
Jeff Hammel <jhammel@mozilla.com>
parents: 285
diff changeset
72 help="remote path of hg repository links; otherwise taken from --remote-url, if specified")
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
73 parser.add_option('-o', '--remote-only', dest='remote_only',
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
74 action='store_true', default=False,
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
75 help="setup remote server only")
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
76 options, args = parser.parse_args(args)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
77 if len(args) != 1:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
78 parser.print_usage()
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
79 parser.exit()
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
80 directory = args[0]
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
82 # initialize repository
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
83 if not options.remote_only:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
84 init_repo(directory)
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
85
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
86 # setup remote, if specified
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
87 name = os.path.basename(directory)
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
88 if options.remote_url:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
89
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
90 setup_remote(directory, options.remote_url, remote_path=options.remote_path)
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91
401
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
92 # push changes
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
93 call(['hg', 'push', '-R', directory])
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
94
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 if __name__ == '__main__':
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 main()