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