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