annotate python/hgrc.py @ 484:50379f607e03

python/hgrc.py
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 10 Aug 2013 21:05:21 -0700
parents 4bd2932d21d4
children e192c235d5d6
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
477
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
14 import urlparse
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
15 from collections import OrderedDict
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
16 from ConfigParser import RawConfigParser as ConfigParser
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
18 ### global methods
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
19
480
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
20 class section(object):
482
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
21 def __init__(self, section_name, *section_names):
480
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
22 self.sections = [section_name]
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
23 self.sections.extend(section_names)
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
24 def __call__(self, parser):
482
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
25 def wrapped(parser, *args, **kwargs):
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
26 for section in self.sections:
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
27 if section not in parser.sections():
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
28 parser.add_section(section)
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
29 f(*args, **kwargs)
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
30 return wrapped
480
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
31
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
32 #@parser # decorator makes this x-form path -> ConfigParser automagically
480
529fd4e1087e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 479
diff changeset
33 @section('paths')
479
0a9e1e0d077c python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 478
diff changeset
34 def set_default(parser, default):
0a9e1e0d077c python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 478
diff changeset
35 """set [paths]:default"""
0a9e1e0d077c python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 478
diff changeset
36
482
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
37 @section('paths')
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
38 def set_default_push(parser, default_push):
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
39 """
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
40 set [paths]:default-push to `default_push`
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
41 """
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
42 if 'paths' not in parser.sections():
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
43 parser.add_section('paths')
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
44 parser.set('paths', 'default-push', default_push)
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
45
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
46
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
47 def set_default_push_to_ssh(parser):
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
48 """
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
49 set `[paths]:default-push` to that given by `[paths]:default` but
474
3326aea03a3b python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 473
diff changeset
50 turn the protocol to 'ssh'
477
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
51 If `[paths]:default` is not there, do nothing.
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
52 Returns True if written, otherwise False
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
53 """
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
54
477
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
55 # get [paths]:default value
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
56 if 'paths' not in parser.sections():
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
57 return False
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
58 if not parser.has_option('paths', 'default'):
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
59 return False
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
60 default = parser.get('paths', 'default')
475
efd50220556e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 474
diff changeset
61
477
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
62 # parse URL
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
63 scheme, netloc, path, query, anchor = urlparse.urlsplit(default)
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
64 ssh_url = urlparse.urlunsplit(('ssh', netloc, path, query, anchor))
6274107e477e python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 476
diff changeset
65
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
66 # set
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
67 set_default_push(parser, ssh_url)
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
68 return True # XXX could instead be url to set to or old value
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
69
473
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
70
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 def main(args=sys.argv[1:]):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
73 # parse command line arguments
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 usage = '%prog [options] repository <repository> <...>'
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 parser = optparse.OptionParser(usage=usage, description=__doc__)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
76 parser.add_option('-l', '--list', dest='list_hgrc',
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
77 action='store_true', default=False,
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
78 help="list full path to hgrc files")
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 parser.add_option('--ssh', dest='default_push_ssh',
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 action='store_true', default=False,
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
81 help="use `default` entries for `default-push`")
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
82 parser.add_option('--push', '--default-push', dest='default_push',
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
83 help="set [paths] default-push location")
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
84 parser.add_option('-d', '--default', dest='default',
479
0a9e1e0d077c python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 478
diff changeset
85 help="set [paths] default entry")
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
86 parser.add_option('-p', '--print', dest='print_ini',
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
87 action='store_true', default=False,
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
88 help="print .ini contents")
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
89 options, args = parser.parse_args(args)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
90
467
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
91 # sanitization
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
92 if options.default_push and options.default_push_ssh:
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
93 parser.error("Cannot set --push and --ssh")
980584d34134 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 466
diff changeset
94
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
95 # if not specified, use repo from `hg root`
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 if not args:
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
97 args = [subprocess.check_output(['hg', 'root']).strip()]
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
98
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
99 # if not specified, set a default action
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
100 default_action = 'default_push_ssh'
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
101 available_actions = ('default',
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
102 'default_push',
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
103 'default_push_ssh',
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
104 'print_ini',
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
105 'list_hgrc',
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
106 )
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
107 actions = [(name, getattr(options, name))
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
108 for name in available_actions
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
109 if getattr(options, name)])
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
110 if not actions:
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
111 actions = [('default_push_ssh', True)]
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
112 actions = OrderedDict(actions)
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
114 # find all hgrc files
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 hgrc = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 missing = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 not_hg = []
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 not_a_directory = []
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
119 errors = {'Missing path': missing,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
120 'Not a mercurial directory': not_hg,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
121 'Not a directory': not_a_directory,
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
122 }
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 for path in args:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
124 if not os.path.exists(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
125 missing.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
126 path = os.path.abspath(os.path.normpath(path))
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
127 if os.path.isdir(path):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
128 basename = os.path.basename(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
129 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
130 if basename == '.hg':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
131 hgrcpath = os.path.join(path, 'hgrc')
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
132 elif os.path.exists(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
133 if not os.path.isdir(subhgdir):
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
134 not_a_directory.append(subhgdir)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
135 continue
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
136 hgrcpath = os.path.join(subhgdir, 'hgrc')
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
137 else:
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
138 not_hg.append(path)
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
139 continue
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
140 hgrc.append(hgrcpath)
350
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
141 else:
52e718567731 minor improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 348
diff changeset
142 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
143 hgrc.append(path)
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
144
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
145 # raise errors if encountered
437
2588ca4ae849 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 435
diff changeset
146 if filter(None, errors.values()):
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
147 for key, value in errors.items():
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
148 if value:
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
149 print '%s: %s' % (key, ', '.join(value))
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
150 parser.exit(1)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
151
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
152 # construct ConfigParser objects and
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
153 # ensure that all the files are parseable
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
154 config = {}
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
155 for path in hgrc:
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
156 config[path] = ConfigParser()
352
eeff298cc922 more crap
Jeff Hammel <jhammel@mozilla.com>
parents: 351
diff changeset
157 if isinstance(path, basestring):
353
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
158 if os.path.exists(path):
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
159 config[path].read(path)
ee8ab3de9c7f f-ing print
Jeff Hammel <jhammel@mozilla.com>
parents: 352
diff changeset
160
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
161 # print the chosen hgrc paths
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
162 if 'list_hgrc' in actions:
351
971e7deca495 got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents: 350
diff changeset
163 print '\n'.join(hgrc)
433
6797477f6a8f adding conky
Jeff Hammel <jhammel@mozilla.com>
parents: 353
diff changeset
164
482
6c0aac8799da python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 481
diff changeset
165 # remove from actions list
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
166 actions.pop('list_hgrc', None)
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
167
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
168 # map of actions -> functions;
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
169 # XXX this is pretty improv; to be improved
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
170 action_map = {'default_push_ssh': set_default_push_to_ssh,
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
171 'default_push': set_default_push,
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
172 'default': set_default
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
173 }
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
174
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
175 # cache for later (XXX)
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
176 print_ini = actions.pop('print_ini', None)
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
177
465
ae2db9e613f1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 437
diff changeset
178 # alter .hgrc files
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
179 for action_name, parameter in actions:
468
4853551536d6 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 467
diff changeset
180
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
181 # XXX crappy
473
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
182 method = action_map[action_name]
476
9d81ec713f1b python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 475
diff changeset
183 if action_name == 'default_push_ssh':
9d81ec713f1b python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 475
diff changeset
184 parameter = None
471
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
185
dc179368b7d0 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 470
diff changeset
186 # apply to all files
470
949ca4b2f2d1 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 469
diff changeset
187 for path, ini in config.items():
481
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
188
65e38d33acae python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 480
diff changeset
189 # call method with parser
473
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
190 if parameter is not None:
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
191 method(ini, parameter)
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
192 else:
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
193 method(ini)
da087c5724d4 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 472
diff changeset
194
478
df60292c29b2 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 477
diff changeset
195 # print .hgrc files, if specified
484
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
196 if print_ini:
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
197 for path, ini in config.items():
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
198 print '+++ %s' % (path)
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
199 ini.write(sys.stdout)
50379f607e03 python/hgrc.py
Jeff Hammel <jhammel@mozilla.com>
parents: 483
diff changeset
200 print
348
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
201
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
202 if __name__ == '__main__':
6004e00b602d new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
203 main()