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