annotate python/setup_repo.py @ 458:483b4237d9a2

python/setup_repo.py
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 10 Aug 2013 18:10:16 -0700
parents cdad4c445993
children 32667483d1c5
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
448
056befdec66c python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 447
diff changeset
11 # TODO:
056befdec66c python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 447
diff changeset
12 # There are patterns here...
056befdec66c python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 447
diff changeset
13 # - actions
454
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
14 # - --dry-run/call()
448
056befdec66c python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 447
diff changeset
15
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 import os
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 import subprocess
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 import sys
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
19 import urlparse
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 from optparse import OptionParser
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
456
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
23 string = (basestring,)
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
24
457
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
25 # global variables that probably shouldn't be
458
483b4237d9a2 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 457
diff changeset
26 dry_run = True # XXX instead, should have runner class
457
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
27 verbose = True
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
28
454
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
29 def call(command, *args, **kwargs):
456
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
30
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
31 if isinstance(command, string):
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
32 command_str = command
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
33 else:
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
34 command_str = ' '.join(command)
7f1dfb36a0fe python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 455
diff changeset
35 print 'Running:\n%s' % (command_str)
454
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
36 return subprocess.check_output(*args, **kwargs)
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
37
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
38 def init_repo(directory):
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
39 """setup repository"""
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
40 call(['hg', 'init', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
41 call(['hg', 'add', '-R', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
42 call(['hg', 'commit', '-m', options.message, '-R', directory])
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
43
406
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
44 def write_hgrc(directory, host, repo, name):
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
45 """write hgrc file"""
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
46
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
47 HGRC="""[paths]
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
48 default = http://%(host)s/%(repo)s/%(name)s
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
49 default-push = ssh://%(host)s/%(repo)s/%(remote_path)s
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
50 """
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
51
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
52 path = os.path.join(directory, '.hg', 'hgrc')
457
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
53 # TODO: use ConfigParser and i.e
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
54 # http://k0s.org/hg/config/file/95afeaf9c42d/python/hgrc.py
406
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
55 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f:
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
56 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name}
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
57
Jeff Hammel <jhammel@mozilla.com>
parents: 405
diff changeset
58
452
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
59 def setup_remote(local_repo, remote_url, push='ssh', remote_path=None, name=None):
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
60 """
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
61 setup a remote repository for local_repo
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
62 - remote_url : public (pull) URL for remote repository
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
63 - push : protocol for push
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
64 - 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
65 """
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
66
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
67 # parse remote URL
455
81a237bcf3b6 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 454
diff changeset
68 scheme, netloc, path, query, anchor = urlparse.urlsplit(remote_url)
452
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
69 path = path.rstrip('/')
455
81a237bcf3b6 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 454
diff changeset
70 host = netloc # -> host, port, etc
452
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
71
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
72 # derive name
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
73 if name is None:
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
74 if '/' in path:
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
75 prefix, name = path.rsplit('/', 1)
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
76 else:
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
77 name = path
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
78 assert name
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
79
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
80 # get remote path, if specified
451
96859ac4d23f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 450
diff changeset
81 if remote_path:
96859ac4d23f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 450
diff changeset
82
96859ac4d23f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 450
diff changeset
83 # split off remote host and path
96859ac4d23f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 450
diff changeset
84 # XXX is a separate host necessary?
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
85 remote_host = host
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
86 if ':' in remote_path:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
87 remote_host, remote_path = remote_path.split(':', 1)
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
88 else:
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
89 remote_path = path
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
90
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
91 # setup remote repository
452
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
92 remote_dir = '~/%s/%s' % (path.lstrip('/'), name)
447
daa9356d614f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 406
diff changeset
93 command = ['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)]
450
56fa913491a1 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 449
diff changeset
94 print command
56fa913491a1 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 449
diff changeset
95 # call(command)
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
96
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
97
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 def main(args=sys.argv[1:]):
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
100 # parse command line arguments
403
62eb670a408f dont roll your own
Jeff Hammel <jhammel@mozilla.com>
parents: 402
diff changeset
101 parser = OptionParser('%prog [options] directory')
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
102 parser.add_option('-m', '--message', dest='message',
401
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
103 default='initial commit',
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
104 help='commit message [Default: %default]')
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
105 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
106 default="http://k0s.org/hg/",
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
107 help="URL of host hg repository collection [Default: %default]")
400
Jeff Hammel <jhammel@mozilla.com>
parents: 285
diff changeset
108 parser.add_option('-p', '--remote-path', dest='remote_path',
Jeff Hammel <jhammel@mozilla.com>
parents: 285
diff changeset
109 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
110 parser.add_option('-o', '--remote-only', dest='remote_only',
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
111 action='store_true', default=False,
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
112 help="setup remote server only")
457
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
113 parser.add_option('--dry-run', dest='dry_run',
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
114 action='store_true', default=True,
cdad4c445993 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 456
diff changeset
115 help="")
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
116 options, args = parser.parse_args(args)
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117
449
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
118 # sanitization
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
119 if not args:
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
120 args = [os.getcwd()]
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
121 if len(args) != 1:
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
122 parser.print_usage()
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
123 parser.exit()
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
124 directory = args[0]
458
483b4237d9a2 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 457
diff changeset
125 globals()['dry_run'] = options.dry_run
449
f39d37973d4b python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 448
diff changeset
126
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
127 # initialize repository
447
daa9356d614f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 406
diff changeset
128 if options.remote_only:
daa9356d614f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 406
diff changeset
129 assert options.remote_url
daa9356d614f python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 406
diff changeset
130 else:
405
815eb5c796e9 more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents: 404
diff changeset
131 init_repo(directory)
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
132
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
133 # setup remote, if specified
404
fb304dcd1e64 factor to its own method
Jeff Hammel <jhammel@mozilla.com>
parents: 403
diff changeset
134 name = os.path.basename(directory)
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
135 if options.remote_url:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
136
450
56fa913491a1 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 449
diff changeset
137 # setup remote repository
452
7d6bd51f0323 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 451
diff changeset
138 setup_remote(directory, options.remote_url, name=name, remote_path=options.remote_path)
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
139
401
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
140 # push changes
454
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
141 command = ['hg', 'push', '-R', directory]
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
142 print command
f7c49c61f092 python/setup_repo.py
Jeff Hammel <jhammel@mozilla.com>
parents: 452
diff changeset
143 # call(command)
401
f652f9d4765b flesh out
Jeff Hammel <jhammel@mozilla.com>
parents: 400
diff changeset
144
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
145 if __name__ == '__main__':
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
146 main()