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