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