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