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