Mercurial > hg > config
annotate python/hgrc.py @ 473:da087c5724d4
python/hgrc.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 10 Aug 2013 19:11:11 -0700 |
parents | 57dfffa7e44a |
children | 3326aea03a3b |
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 |
437 | 14 from ConfigParser import RawConfigParser as ConfigParser |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 |
468 | 16 #@parser # decorator makes this x-form path -> ConfigParser automagically |
467 | 17 def set_default_push(parser, default_push): |
18 """ | |
19 set [paths]:default_push to `default_push` | |
20 """ | |
21 pass | |
22 | |
468 | 23 def set_default_push_to_ssh(parser): |
24 """ | |
467 | 25 pass |
468 | 26 """ |
467 | 27 |
473 | 28 # get default path |
29 default = '' | |
30 | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 def main(args=sys.argv[1:]): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 |
433 | 33 # parse command line arguments |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 usage = '%prog [options] repository <repository> <...>' |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 parser = optparse.OptionParser(usage=usage, description=__doc__) |
433 | 36 parser.add_option('-l', '--list', dest='list_hgrc', |
350 | 37 action='store_true', default=False, |
433 | 38 help="list full path to hgrc files") |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 parser.add_option('--ssh', dest='default_push_ssh', |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 action='store_true', default=False, |
350 | 41 help="use `default` entries for `default-push`") |
353 | 42 parser.add_option('--push', '--default-push', dest='default_push', |
43 help="set [paths] default-push location") | |
437 | 44 options, args = parser.parse_args(args) |
433 | 45 |
467 | 46 # sanitization |
47 if options.default_push and options.default_push_ssh: | |
48 parser.error("Cannot set --push and --ssh") | |
49 | |
433 | 50 # if not specified, use repo from `hg root` |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 if not args: |
437 | 52 args = [subprocess.check_output(['hg', 'root']).strip()] |
433 | 53 |
54 # if not specified, set a default action | |
55 default_action = 'default_push_ssh' | |
466 | 56 available_actions = ('default_push', |
465 | 57 'default_push_ssh', |
58 'list_hgrc', | |
59 ) | |
60 actions = dict([(name, getattr(options, name)) | |
61 for name in available_actions | |
469 | 62 if getattr(options, name)]) |
465 | 63 if not actions: |
64 actions = {'default_push_ssh': True} | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 |
353 | 66 # find all hgrc files |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 hgrc = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 missing = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 not_hg = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 not_a_directory = [] |
350 | 71 errors = {'Missing path': missing, |
72 'Not a mercurial directory': not_hg, | |
73 'Not a directory': not_a_directory, | |
74 } | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 for path in args: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 if not os.path.exists(path): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 missing.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 path = os.path.abspath(os.path.normpath(path)) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 if os.path.isdir(path): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 basename = os.path.basename(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
82 if basename == '.hg': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
83 hgrcpath = os.path.join(path, 'hgrc') |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 elif os.path.exists(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 if not os.path.isdir(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
86 not_a_directory.append(subhgdir) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
87 continue |
437 | 88 hgrcpath = os.path.join(subhgdir, 'hgrc') |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 else: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
90 not_hg.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
91 continue |
437 | 92 hgrc.append(hgrcpath) |
350 | 93 else: |
94 assert os.path.isfile(path), "%s is not a file, exiting" % path | |
437 | 95 hgrc.append(path) |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
96 |
353 | 97 # raise errors if encountered |
437 | 98 if filter(None, errors.values()): |
353 | 99 for key, value in errors.items(): |
100 if value: | |
101 print '%s: %s' % (key, ', '.join(value)) | |
102 parser.exit(1) | |
103 | |
352 | 104 # construct ConfigParser objects and |
105 # ensure that all the files are parseable | |
106 config = {} | |
107 for path in hgrc: | |
353 | 108 config[path] = ConfigParser() |
352 | 109 if isinstance(path, basestring): |
353 | 110 if os.path.exists(path): |
111 config[path].read(path) | |
112 | |
433 | 113 # print the chosen hgrc paths |
470 | 114 if actions.pop('list_hgrc', None): |
351
971e7deca495
got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents:
350
diff
changeset
|
115 print '\n'.join(hgrc) |
433 | 116 |
470 | 117 # map of actions -> functions; |
118 # XXX this is pretty improv; to be improved | |
471 | 119 action_map = {'default_push_ssh': set_default_push_to_ssh, |
470 | 120 'default_push': set_default_push |
471 | 121 } |
470 | 122 |
465 | 123 # alter .hgrc files |
471 | 124 action_names = actions.keys() |
470 | 125 while actions: |
468 | 126 |
471 | 127 # XXX crappy |
472 | 128 action_name = action_names.pop() |
129 parameter = actions.pop(action_name) | |
473 | 130 method = action_map[action_name] |
471 | 131 |
132 # apply to all files | |
470 | 133 for path, ini in config.items(): |
473 | 134 if parameter is not None: |
135 method(ini, parameter) | |
136 else: | |
137 method(ini) | |
138 | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
139 |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
140 if __name__ == '__main__': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
141 main() |