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