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