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