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