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