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