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