annotate python/hgrc.py @ 396:f3472dd25d9e

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