annotate python/hgrc.py @ 352:eeff298cc922

more crap
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 27 Jun 2013 02:13:40 -0700
parents 971e7deca495
children ee8ab3de9c7f
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__)
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
20 parser.add_option('-p', '--print', dest='print',
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`")
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 options, args = options.parse_args(args)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if not args:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 parser.print_usage()
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 parser.exit()
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 # find all .hgrc files
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 hgrc = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 missing = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 not_hg = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 not_a_directory = []
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
36 errors = {'Missing path': missing,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
37 'Not a mercurial directory': not_hg,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
38 'Not a directory': not_a_directory,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
39 }
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 for path in args:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 if not os.path.exists(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 missing.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 path = os.path.abspath(os.path.normpath(path))
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if os.path.isdir(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 basename = os.path.basename(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 if basename == '.hg':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 hgrcpath = os.path.join(path, 'hgrc')
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 elif os.path.exists(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 if not os.path.isdir(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 not_a_directory.append(subhgdir)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 continue
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 else:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 not_hg.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 continue
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
56 else:
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
57 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
58 hgrc.append(path)
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
60 # construct ConfigParser objects and
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
61 # ensure that all the files are parseable
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
62 config = {}
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
63 for path in hgrc:
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
64 config['path'] = ConfigParser()
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
65 if isinstance(path, basestring):
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
66 config['path'].read(path)
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
67
351
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
68 if options.print:
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
69 # print the chosen hgrc paths and you're done
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
70 print '\n'.join(hgrc)
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
71 parser.exit()
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 if __name__ == '__main__':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 main()