Mercurial > hg > config
annotate python/setup_repo.py @ 448:056befdec66c
python/setup_repo.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 10 Aug 2013 15:19:39 -0700 |
parents | daa9356d614f |
children | f39d37973d4b |
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 | 11 # TODO: |
12 # There are patterns here... | |
13 # - actions | |
14 # - ... | |
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 |
403 | 23 call = subprocess.check_output |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
24 |
404 | 25 def init_repo(directory): |
26 """setup repository""" | |
27 call(['hg', 'init', directory]) | |
28 call(['hg', 'add', '-R', directory]) | |
29 call(['hg', 'commit', '-m', options.message, '-R', directory]) | |
30 | |
406 | 31 def write_hgrc(directory, host, repo, name): |
32 """write hgrc file""" | |
33 | |
34 HGRC="""[paths] | |
35 default = http://%(host)s/%(repo)s/%(name)s | |
36 default-push = ssh://%(host)s/%(repo)s/%(remote_path)s | |
37 """ | |
38 | |
39 path = os.path.join(directory, '.hg', 'hgrc') | |
40 # TODO: use ConfigParser | |
41 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f: | |
42 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name} | |
43 | |
44 | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
45 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
|
46 """ |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
47 setup a remote repository for local_repo |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
48 - remote_url : public (pull) URL for remote repository |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
49 - push : protocol for push |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
50 - 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
|
51 """ |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
52 |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
53 # parse remote URL |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
54 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
|
55 if options.remote_path: |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
56 remote_host = host |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
57 if ':' in remote_path: |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
58 remote_host, remote_path = remote_path.split(':', 1) |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
59 else: |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
60 remote_path = path |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
61 |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
62 # setup remote repository |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
63 remote_dir = '%s/%s' % (path, name) |
447 | 64 command = ['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)] |
65 call(command) | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
66 |
404 | 67 |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 def main(args=sys.argv[1:]): |
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
70 # parse command line arguments |
403 | 71 parser = OptionParser('%prog [options] directory') |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
72 parser.add_option('-m', '--message', dest='message', |
401 | 73 default='initial commit', |
74 help='commit message [Default: %default]') | |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
75 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
|
76 default="http://k0s.org/hg/", |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
77 help="URL of host hg repository collection [Default: %default]") |
400 | 78 parser.add_option('-p', '--remote-path', dest='remote_path', |
79 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
|
80 parser.add_option('-o', '--remote-only', dest='remote_only', |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
81 action='store_true', default=False, |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
82 help="setup remote server only") |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
83 options, args = parser.parse_args(args) |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 |
404 | 85 # initialize repository |
447 | 86 if options.remote_only: |
87 assert options.remote_url | |
88 else: | |
89 if len(args) != 1: | |
90 parser.print_usage() | |
91 parser.exit() | |
92 directory = args[0] | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
93 init_repo(directory) |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
94 |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
95 # setup remote, if specified |
404 | 96 name = os.path.basename(directory) |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
97 if options.remote_url: |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
98 |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
99 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
|
100 |
401 | 101 # push changes |
102 call(['hg', 'push', '-R', directory]) | |
103 | |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 if __name__ == '__main__': |
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
105 main() |