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