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