comparison 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
comparison
equal deleted inserted replaced
483:4bd2932d21d4 484:50379f607e03
10 import optparse 10 import optparse
11 import os 11 import os
12 import subprocess 12 import subprocess
13 import sys 13 import sys
14 import urlparse 14 import urlparse
15 from collections import OrderedDict
15 from ConfigParser import RawConfigParser as ConfigParser 16 from ConfigParser import RawConfigParser as ConfigParser
17
18 ### global methods
16 19
17 class section(object): 20 class section(object):
18 def __init__(self, section_name, *section_names): 21 def __init__(self, section_name, *section_names):
19 self.sections = [section_name] 22 self.sections = [section_name]
20 self.sections.extend(section_names) 23 self.sections.extend(section_names)
76 parser.add_option('--ssh', dest='default_push_ssh', 79 parser.add_option('--ssh', dest='default_push_ssh',
77 action='store_true', default=False, 80 action='store_true', default=False,
78 help="use `default` entries for `default-push`") 81 help="use `default` entries for `default-push`")
79 parser.add_option('--push', '--default-push', dest='default_push', 82 parser.add_option('--push', '--default-push', dest='default_push',
80 help="set [paths] default-push location") 83 help="set [paths] default-push location")
81 parser.add_option('--default', dest='default', 84 parser.add_option('-d', '--default', dest='default',
82 help="set [paths] default entry") 85 help="set [paths] default entry")
83 parser.add_option('-p', '--print', dest='print_ini', 86 parser.add_option('-p', '--print', dest='print_ini',
84 action='store_true', default=False, 87 action='store_true', default=False,
85 help="print .ini contents") 88 help="print .ini contents")
86 options, args = parser.parse_args(args) 89 options, args = parser.parse_args(args)
96 # if not specified, set a default action 99 # if not specified, set a default action
97 default_action = 'default_push_ssh' 100 default_action = 'default_push_ssh'
98 available_actions = ('default', 101 available_actions = ('default',
99 'default_push', 102 'default_push',
100 'default_push_ssh', 103 'default_push_ssh',
104 'print_ini',
101 'list_hgrc', 105 'list_hgrc',
102 ) 106 )
103 actions = [(name, getattr(options, name)) 107 actions = [(name, getattr(options, name))
104 for name in available_actions 108 for name in available_actions
105 if getattr(options, name)] 109 if getattr(options, name)])
106 if not actions: 110 if not actions:
107 actions = [('default_push_ssh', True)] 111 actions = [('default_push_ssh', True)]
112 actions = OrderedDict(actions)
108 113
109 # find all hgrc files 114 # find all hgrc files
110 hgrc = [] 115 hgrc = []
111 missing = [] 116 missing = []
112 not_hg = [] 117 not_hg = []
152 if isinstance(path, basestring): 157 if isinstance(path, basestring):
153 if os.path.exists(path): 158 if os.path.exists(path):
154 config[path].read(path) 159 config[path].read(path)
155 160
156 # print the chosen hgrc paths 161 # print the chosen hgrc paths
157 if options.list_hgrc: 162 if 'list_hgrc' in actions:
158 print '\n'.join(hgrc) 163 print '\n'.join(hgrc)
159 164
160 # remove from actions list 165 # remove from actions list
161 # TODO -> OrderedDict 166 actions.pop('list_hgrc', None)
162 # actions.pop('list_hgrc', None)
163 actions.pop()
164 167
165 # map of actions -> functions; 168 # map of actions -> functions;
166 # XXX this is pretty improv; to be improved 169 # XXX this is pretty improv; to be improved
167 action_map = {'default_push_ssh': set_default_push_to_ssh, 170 action_map = {'default_push_ssh': set_default_push_to_ssh,
168 'default_push': set_default_push 171 'default_push': set_default_push,
172 'default': set_default
169 } 173 }
174
175 # cache for later (XXX)
176 print_ini = actions.pop('print_ini', None)
170 177
171 # alter .hgrc files 178 # alter .hgrc files
172 for action_name, parameter in actions: 179 for action_name, parameter in actions:
173 180
174 # XXX crappy 181 # XXX crappy
184 method(ini, parameter) 191 method(ini, parameter)
185 else: 192 else:
186 method(ini) 193 method(ini)
187 194
188 # print .hgrc files, if specified 195 # print .hgrc files, if specified
189 for path, ini in config.items(): 196 if print_ini:
190 print '+++ %s' % (path) 197 for path, ini in config.items():
191 ini.write(sys.stdout) 198 print '+++ %s' % (path)
192 print 199 ini.write(sys.stdout)
200 print
193 201
194 if __name__ == '__main__': 202 if __name__ == '__main__':
195 main() 203 main()