Mercurial > hg > config
annotate python/setup_repo.py @ 455:81a237bcf3b6
python/setup_repo.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 10 Aug 2013 17:57:48 -0700 |
parents | f7c49c61f092 |
children | 7f1dfb36a0fe |
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 | |
454 | 14 # - --dry-run/call() |
448 | 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 |
454 | 23 def call(command, *args, **kwargs): |
24 return subprocess.check_output(*args, **kwargs) | |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
25 |
404 | 26 def init_repo(directory): |
27 """setup repository""" | |
28 call(['hg', 'init', directory]) | |
29 call(['hg', 'add', '-R', directory]) | |
30 call(['hg', 'commit', '-m', options.message, '-R', directory]) | |
31 | |
406 | 32 def write_hgrc(directory, host, repo, name): |
33 """write hgrc file""" | |
34 | |
35 HGRC="""[paths] | |
36 default = http://%(host)s/%(repo)s/%(name)s | |
37 default-push = ssh://%(host)s/%(repo)s/%(remote_path)s | |
38 """ | |
39 | |
40 path = os.path.join(directory, '.hg', 'hgrc') | |
41 # TODO: use ConfigParser | |
42 with file(os.path.join(directory, '.hg', 'hgrc'), 'w') as f: | |
43 print >> f, HGRC % { 'host': host, 'repo': repo, 'name': name} | |
44 | |
45 | |
452 | 46 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
|
47 """ |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
48 setup a remote repository for local_repo |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
49 - remote_url : public (pull) URL for remote repository |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
50 - push : protocol for push |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
51 - 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
|
52 """ |
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 # parse remote URL |
455 | 55 scheme, netloc, path, query, anchor = urlparse.urlsplit(remote_url) |
452 | 56 path = path.rstrip('/') |
455 | 57 host = netloc # -> host, port, etc |
452 | 58 |
59 # derive name | |
60 if name is None: | |
61 if '/' in path: | |
62 prefix, name = path.rsplit('/', 1) | |
63 else: | |
64 name = path | |
65 assert name | |
66 | |
67 # get remote path, if specified | |
451 | 68 if remote_path: |
69 | |
70 # split off remote host and path | |
71 # XXX is a separate host necessary? | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
72 remote_host = host |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
73 if ':' in remote_path: |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
74 remote_host, remote_path = remote_path.split(':', 1) |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
75 else: |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
76 remote_path = path |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
77 |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
78 # setup remote repository |
452 | 79 remote_dir = '~/%s/%s' % (path.lstrip('/'), name) |
447 | 80 command = ['ssh', host, "mkdir -p %s && cd %s && hg init" % (remote_dir, remote_dir)] |
450 | 81 print command |
82 # call(command) | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
83 |
404 | 84 |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 def main(args=sys.argv[1:]): |
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
86 |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
87 # parse command line arguments |
403 | 88 parser = OptionParser('%prog [options] directory') |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
89 parser.add_option('-m', '--message', dest='message', |
401 | 90 default='initial commit', |
91 help='commit message [Default: %default]') | |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
92 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
|
93 default="http://k0s.org/hg/", |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
94 help="URL of host hg repository collection [Default: %default]") |
400 | 95 parser.add_option('-p', '--remote-path', dest='remote_path', |
96 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
|
97 parser.add_option('-o', '--remote-only', dest='remote_only', |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
98 action='store_true', default=False, |
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
99 help="setup remote server only") |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
100 options, args = parser.parse_args(args) |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
101 |
449 | 102 # sanitization |
103 if not args: | |
104 args = [os.getcwd()] | |
105 if len(args) != 1: | |
106 parser.print_usage() | |
107 parser.exit() | |
108 directory = args[0] | |
109 | |
404 | 110 # initialize repository |
447 | 111 if options.remote_only: |
112 assert options.remote_url | |
113 else: | |
405
815eb5c796e9
more stubbing; separate into functions
Jeff Hammel <jhammel@mozilla.com>
parents:
404
diff
changeset
|
114 init_repo(directory) |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
115 |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
116 # setup remote, if specified |
404 | 117 name = os.path.basename(directory) |
285
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
118 if options.remote_url: |
fd0e81fdf1fa
making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
72
diff
changeset
|
119 |
450 | 120 # setup remote repository |
452 | 121 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
|
122 |
401 | 123 # push changes |
454 | 124 command = ['hg', 'push', '-R', directory] |
125 print command | |
126 # call(command) | |
401 | 127 |
72
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
128 if __name__ == '__main__': |
5a6f89cc6bc4
add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
129 main() |