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