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