annotate python/hgrc.py @ 435:ac7973ec485e

python/hgrc.py
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 08 Aug 2013 10:36:19 -0700
parents 6797477f6a8f
children 2588ca4ae849
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
351
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
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
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def main(args=sys.argv[1:]):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
18 # parse command line arguments
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 usage = '%prog [options] repository <repository> <...>'
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 parser = optparse.OptionParser(usage=usage, description=__doc__)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
21 parser.add_option('-l', '--list', dest='list_hgrc',
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
22 action='store_true', default=False,
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
23 help="list full path to hgrc files")
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 parser.add_option('--ssh', dest='default_push_ssh',
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 action='store_true', default=False,
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
26 help="use `default` entries for `default-push`")
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
27 parser.add_option('--push', '--default-push', dest='default_push',
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
28 help="set [paths] default-push location")
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 options, args = options.parse_args(args)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
30
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
31 # if not specified, use repo from `hg root`
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if not args:
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
33 args = [subprocess.check_output(['hg', 'root'])]
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
34
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
35 # if not specified, set a default action
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
36 default_action = 'default_push_ssh'
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
37 actions = ('default_push',
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
38 'default_push_ssh',
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
39 )
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
41 # find all hgrc files
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 hgrc = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 missing = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 not_hg = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 not_a_directory = []
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
46 errors = {'Missing path': missing,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
47 'Not a mercurial directory': not_hg,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
48 'Not a directory': not_a_directory,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
49 }
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 for path in args:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 if not os.path.exists(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 missing.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 path = os.path.abspath(os.path.normpath(path))
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 if os.path.isdir(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 basename = os.path.basename(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 if basename == '.hg':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 hgrcpath = os.path.join(path, 'hgrc')
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 elif os.path.exists(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if not os.path.isdir(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 not_a_directory.append(subhgdir)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 continue
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 else:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 not_hg.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 continue
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
66 else:
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
67 assert os.path.isfile(path), "%s is not a file, exiting" % path
351
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
68 hgrc.append(path)
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
70 # raise errors if encountered
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
71 if sum(errors.values()):
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
72 for key, value in errors.items():
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
73 if value:
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
74 print '%s: %s' % (key, ', '.join(value))
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
75 parser.exit(1)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
76
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
77 # construct ConfigParser objects and
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
78 # ensure that all the files are parseable
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
79 config = {}
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
80 for path in hgrc:
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
81 config[path] = ConfigParser()
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
82 if isinstance(path, basestring):
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
83 if os.path.exists(path):
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
84 config[path].read(path)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
85
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
86 # print the chosen hgrc paths
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
87 if options.list_hgrc:
351
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
88 print '\n'.join(hgrc)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
89
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 if __name__ == '__main__':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 main()