annotate python/hgrc.py @ 471:dc179368b7d0

python/hgrc.py
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 10 Aug 2013 19:05:32 -0700
parents 949ca4b2f2d1
children 57dfffa7e44a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
3 """
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
4 Script for modifying hgrc files.
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
6 If no arguments specified, the repository given by `hg root` is used.
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
7 """
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
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
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
12 import subprocess
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import sys
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
14 from ConfigParser import RawConfigParser as ConfigParser
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
16 #@parser # decorator makes this x-form path -> ConfigParser automagically
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
17 def set_default_push(parser, default_push):
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
18 """
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
19 set [paths]:default_push to `default_push`
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
20 """
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
21 pass
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
22
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
23 def set_default_push_to_ssh(parser):
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
24 """
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
25 pass
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
26 """
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
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
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
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
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
33 parser.add_option('-l', '--list', dest='list_hgrc',
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
34 action='store_true', default=False,
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
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
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
38 help="use `default` entries for `default-push`")
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
39 parser.add_option('--push', '--default-push', dest='default_push',
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
40 help="set [paths] default-push location")
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
41 options, args = parser.parse_args(args)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
42
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
43 # sanitization
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
44 if options.default_push and options.default_push_ssh:
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
45 parser.error("Cannot set --push and --ssh")
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
46
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
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
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
49 args = [subprocess.check_output(['hg', 'root']).strip()]
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
50
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
51 # if not specified, set a default action
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
52 default_action = 'default_push_ssh'
466
3ffcbffb6fb4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 465
diff changeset
53 available_actions = ('default_push',
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
54 'default_push_ssh',
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
55 'list_hgrc',
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
56 )
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
57 actions = dict([(name, getattr(options, name))
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
58 for name in available_actions
469
764535e1f991 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 468
diff changeset
59 if getattr(options, name)])
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
60 if not actions:
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
61 actions = {'default_push_ssh': True}
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
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
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
68 errors = {'Missing path': missing,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
69 'Not a mercurial directory': not_hg,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
70 'Not a directory': not_a_directory,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
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
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
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
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
89 hgrc.append(hgrcpath)
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
90 else:
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
91 assert os.path.isfile(path), "%s is not a file, exiting" % path
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
92 hgrc.append(path)
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
94 # raise errors if encountered
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
95 if filter(None, errors.values()):
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
96 for key, value in errors.items():
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
97 if value:
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
98 print '%s: %s' % (key, ', '.join(value))
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
99 parser.exit(1)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
100
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
101 # construct ConfigParser objects and
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
102 # ensure that all the files are parseable
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
103 config = {}
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
104 for path in hgrc:
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
105 config[path] = ConfigParser()
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
106 if isinstance(path, basestring):
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
107 if os.path.exists(path):
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
108 config[path].read(path)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
109
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
110 # print the chosen hgrc paths
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
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
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
113
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
114 # map of actions -> functions;
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
115 # XXX this is pretty improv; to be improved
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
116 action_map = {'default_push_ssh': set_default_push_to_ssh,
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
117 'default_push': set_default_push
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
118 }
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
119
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
120 # alter .hgrc files
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
121 action_names = actions.keys()
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
122 while actions:
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
123
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
124 # XXX crappy
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
125 action, parameter = actions.pop(action_names.pop())
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
126
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
127 # apply to all files
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
128 for path, ini in config.items():
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
129 import pdb; pdb.set_trace()
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
130
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
131 if __name__ == '__main__':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
132 main()