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