Mercurial > hg > config
annotate python/hgrc.py @ 506:2a5eee9418ba
hgrc
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 31 Aug 2013 21:17:16 -0700 |
parents | ab405b339112 |
children | bb6e21338c5c |
rev | line source |
---|---|
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
350 | 3 """ |
4 Script for modifying hgrc files. | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
433 | 6 If no arguments specified, the repository given by `hg root` is used. |
505 | 7 |
8 [WIP] If http(s):// arguments are given, create hgrc file from such a thing | |
350 | 9 """ |
433 | 10 |
351
971e7deca495
got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents:
350
diff
changeset
|
11 # imports |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 import optparse |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 import os |
433 | 14 import subprocess |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 import sys |
477 | 16 import urlparse |
484 | 17 from collections import OrderedDict |
437 | 18 from ConfigParser import RawConfigParser as ConfigParser |
489 | 19 from StringIO import StringIO |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 |
484 | 21 ### global methods |
22 | |
506 | 23 def isHTTP(path): |
24 """is path an {http,https}:// URL?""" | |
25 return urlparse.urlsplit(path)[0] in ('http', 'https') | |
26 | |
480 | 27 class section(object): |
482 | 28 def __init__(self, section_name, *section_names): |
480 | 29 self.sections = [section_name] |
30 self.sections.extend(section_names) | |
486 | 31 def __call__(self, function): |
482 | 32 def wrapped(parser, *args, **kwargs): |
33 for section in self.sections: | |
34 if section not in parser.sections(): | |
35 parser.add_section(section) | |
486 | 36 function(parser, *args, **kwargs) |
482 | 37 return wrapped |
480 | 38 |
506 | 39 |
480 | 40 @section('paths') |
479 | 41 def set_default(parser, default): |
42 """set [paths]:default""" | |
488 | 43 parser.set('paths', 'default', default) |
487 | 44 |
482 | 45 @section('paths') |
467 | 46 def set_default_push(parser, default_push): |
47 """ | |
478 | 48 set [paths]:default-push to `default_push` |
467 | 49 """ |
478 | 50 parser.set('paths', 'default-push', default_push) |
51 | |
468 | 52 def set_default_push_to_ssh(parser): |
53 """ | |
478 | 54 set `[paths]:default-push` to that given by `[paths]:default` but |
474 | 55 turn the protocol to 'ssh' |
477 | 56 If `[paths]:default` is not there, do nothing. |
57 Returns True if written, otherwise False | |
468 | 58 """ |
467 | 59 |
477 | 60 # get [paths]:default value |
61 if 'paths' not in parser.sections(): | |
62 return False | |
63 if not parser.has_option('paths', 'default'): | |
64 return False | |
65 default = parser.get('paths', 'default') | |
475 | 66 |
477 | 67 # parse URL |
68 scheme, netloc, path, query, anchor = urlparse.urlsplit(default) | |
69 ssh_url = urlparse.urlunsplit(('ssh', netloc, path, query, anchor)) | |
70 | |
478 | 71 # set |
72 set_default_push(parser, ssh_url) | |
73 return True # XXX could instead be url to set to or old value | |
74 | |
473 | 75 |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 def main(args=sys.argv[1:]): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 |
433 | 78 # parse command line arguments |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 usage = '%prog [options] repository <repository> <...>' |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 parser = optparse.OptionParser(usage=usage, description=__doc__) |
433 | 81 parser.add_option('-l', '--list', dest='list_hgrc', |
350 | 82 action='store_true', default=False, |
433 | 83 help="list full path to hgrc files") |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 parser.add_option('--ssh', dest='default_push_ssh', |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 action='store_true', default=False, |
350 | 86 help="use `default` entries for `default-push`") |
353 | 87 parser.add_option('--push', '--default-push', dest='default_push', |
88 help="set [paths] default-push location") | |
484 | 89 parser.add_option('-d', '--default', dest='default', |
479 | 90 help="set [paths] default entry") |
478 | 91 parser.add_option('-p', '--print', dest='print_ini', |
92 action='store_true', default=False, | |
93 help="print .ini contents") | |
489 | 94 parser.add_option('--dry-run', dest='dry_run', |
95 action='store_true', default=False, | |
96 help="don't write to disk") | |
437 | 97 options, args = parser.parse_args(args) |
433 | 98 |
467 | 99 # sanitization |
100 if options.default_push and options.default_push_ssh: | |
101 parser.error("Cannot set --push and --ssh") | |
102 | |
433 | 103 # if not specified, use repo from `hg root` |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 if not args: |
437 | 105 args = [subprocess.check_output(['hg', 'root']).strip()] |
433 | 106 |
107 # if not specified, set a default action | |
108 default_action = 'default_push_ssh' | |
481 | 109 available_actions = ('default', |
110 'default_push', | |
111 'default_push_ssh', | |
484 | 112 'print_ini', |
481 | 113 'list_hgrc', |
465 | 114 ) |
481 | 115 actions = [(name, getattr(options, name)) |
116 for name in available_actions | |
485 | 117 if getattr(options, name)] |
465 | 118 if not actions: |
490 | 119 # add a default action for our convenience |
481 | 120 actions = [('default_push_ssh', True)] |
484 | 121 actions = OrderedDict(actions) |
490 | 122 if not actions: |
123 parser.error("Please specify an action") | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
124 |
506 | 125 # find all hgrc files and URLs |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
126 hgrc = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
127 missing = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
128 not_hg = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
129 not_a_directory = [] |
506 | 130 urls = [] |
350 | 131 errors = {'Missing path': missing, |
132 'Not a mercurial directory': not_hg, | |
133 'Not a directory': not_a_directory, | |
134 } | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
135 for path in args: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
136 if not os.path.exists(path): |
506 | 137 if isHTTP(path): |
138 hgrc.append(path) | |
139 urls.append(path) | |
140 continue | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
141 missing.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
142 path = os.path.abspath(os.path.normpath(path)) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
143 if os.path.isdir(path): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
144 basename = os.path.basename(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
145 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
146 if basename == '.hg': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
147 hgrcpath = os.path.join(path, 'hgrc') |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
148 elif os.path.exists(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
149 if not os.path.isdir(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
150 not_a_directory.append(subhgdir) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
151 continue |
437 | 152 hgrcpath = os.path.join(subhgdir, 'hgrc') |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
153 else: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
154 not_hg.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
155 continue |
437 | 156 hgrc.append(hgrcpath) |
350 | 157 else: |
158 assert os.path.isfile(path), "%s is not a file, exiting" % path | |
437 | 159 hgrc.append(path) |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
160 |
353 | 161 # raise errors if encountered |
437 | 162 if filter(None, errors.values()): |
353 | 163 for key, value in errors.items(): |
164 if value: | |
165 print '%s: %s' % (key, ', '.join(value)) | |
166 parser.exit(1) | |
167 | |
352 | 168 # construct ConfigParser objects and |
169 # ensure that all the files are parseable | |
170 config = {} | |
171 for path in hgrc: | |
353 | 172 config[path] = ConfigParser() |
352 | 173 if isinstance(path, basestring): |
353 | 174 if os.path.exists(path): |
175 config[path].read(path) | |
506 | 176 elif path in urls: |
177 if 'default' not in actions: | |
178 set_default(config[path], path) | |
353 | 179 |
433 | 180 # print the chosen hgrc paths |
484 | 181 if 'list_hgrc' in actions: |
351
971e7deca495
got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents:
350
diff
changeset
|
182 print '\n'.join(hgrc) |
433 | 183 |
482 | 184 # remove from actions list |
484 | 185 actions.pop('list_hgrc', None) |
481 | 186 |
470 | 187 # map of actions -> functions; |
188 # XXX this is pretty improv; to be improved | |
471 | 189 action_map = {'default_push_ssh': set_default_push_to_ssh, |
484 | 190 'default_push': set_default_push, |
191 'default': set_default | |
471 | 192 } |
470 | 193 |
484 | 194 # cache for later (XXX) |
506 | 195 print_ini = actions.pop('print_ini', bool(urls)) |
484 | 196 |
465 | 197 # alter .hgrc files |
486 | 198 for action_name, parameter in actions.items(): |
468 | 199 |
471 | 200 # XXX crappy |
473 | 201 method = action_map[action_name] |
476 | 202 if action_name == 'default_push_ssh': |
203 parameter = None | |
471 | 204 |
205 # apply to all files | |
470 | 206 for path, ini in config.items(): |
481 | 207 |
208 # call method with parser | |
486 | 209 if parameter is None: |
210 method(ini) | |
211 else: | |
473 | 212 method(ini, parameter) |
213 | |
478 | 214 # print .hgrc files, if specified |
484 | 215 if print_ini: |
489 | 216 values = [] |
484 | 217 for path, ini in config.items(): |
489 | 218 _buffer = StringIO() |
219 ini.write(_buffer) | |
506 | 220 value = _buffer.getvalue().strip() |
221 if len(config) == 1: | |
222 values = [value] | |
223 else: | |
224 values.append('+++ %s\n%s' % (path, value)) | |
489 | 225 print '\n'.join(values) |
226 | |
227 # write .ini files | |
228 for path, ini in config.items(): | |
506 | 229 if path in urls: |
230 continue | |
489 | 231 with file(path, 'w') as f: |
232 ini.write(f) | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
233 |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
234 if __name__ == '__main__': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
235 main() |