comparison python/hgrc.py @ 506:2a5eee9418ba

hgrc
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 31 Aug 2013 21:17:16 -0700
parents ab405b339112
children bb6e21338c5c
comparison
equal deleted inserted replaced
505:ab405b339112 506:2a5eee9418ba
17 from collections import OrderedDict 17 from collections import OrderedDict
18 from ConfigParser import RawConfigParser as ConfigParser 18 from ConfigParser import RawConfigParser as ConfigParser
19 from StringIO import StringIO 19 from StringIO import StringIO
20 20
21 ### global methods 21 ### global methods
22
23 def isHTTP(path):
24 """is path an {http,https}:// URL?"""
25 return urlparse.urlsplit(path)[0] in ('http', 'https')
22 26
23 class section(object): 27 class section(object):
24 def __init__(self, section_name, *section_names): 28 def __init__(self, section_name, *section_names):
25 self.sections = [section_name] 29 self.sections = [section_name]
26 self.sections.extend(section_names) 30 self.sections.extend(section_names)
30 if section not in parser.sections(): 34 if section not in parser.sections():
31 parser.add_section(section) 35 parser.add_section(section)
32 function(parser, *args, **kwargs) 36 function(parser, *args, **kwargs)
33 return wrapped 37 return wrapped
34 38
35 #@parser # decorator makes this x-form path -> ConfigParser automagically 39
36 @section('paths') 40 @section('paths')
37 def set_default(parser, default): 41 def set_default(parser, default):
38 """set [paths]:default""" 42 """set [paths]:default"""
39 parser.set('paths', 'default', default) 43 parser.set('paths', 'default', default)
40 44
41
42 @section('paths') 45 @section('paths')
43 def set_default_push(parser, default_push): 46 def set_default_push(parser, default_push):
44 """ 47 """
45 set [paths]:default-push to `default_push` 48 set [paths]:default-push to `default_push`
46 """ 49 """
47 parser.set('paths', 'default-push', default_push) 50 parser.set('paths', 'default-push', default_push)
48
49 51
50 def set_default_push_to_ssh(parser): 52 def set_default_push_to_ssh(parser):
51 """ 53 """
52 set `[paths]:default-push` to that given by `[paths]:default` but 54 set `[paths]:default-push` to that given by `[paths]:default` but
53 turn the protocol to 'ssh' 55 turn the protocol to 'ssh'
118 actions = [('default_push_ssh', True)] 120 actions = [('default_push_ssh', True)]
119 actions = OrderedDict(actions) 121 actions = OrderedDict(actions)
120 if not actions: 122 if not actions:
121 parser.error("Please specify an action") 123 parser.error("Please specify an action")
122 124
123 # find all hgrc files 125 # find all hgrc files and URLs
124 hgrc = [] 126 hgrc = []
125 missing = [] 127 missing = []
126 not_hg = [] 128 not_hg = []
127 not_a_directory = [] 129 not_a_directory = []
130 urls = []
128 errors = {'Missing path': missing, 131 errors = {'Missing path': missing,
129 'Not a mercurial directory': not_hg, 132 'Not a mercurial directory': not_hg,
130 'Not a directory': not_a_directory, 133 'Not a directory': not_a_directory,
131 } 134 }
132 for path in args: 135 for path in args:
133 if not os.path.exists(path): 136 if not os.path.exists(path):
137 if isHTTP(path):
138 hgrc.append(path)
139 urls.append(path)
140 continue
134 missing.append(path) 141 missing.append(path)
135 path = os.path.abspath(os.path.normpath(path)) 142 path = os.path.abspath(os.path.normpath(path))
136 if os.path.isdir(path): 143 if os.path.isdir(path):
137 basename = os.path.basename(path) 144 basename = os.path.basename(path)
138 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory 145 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory
164 for path in hgrc: 171 for path in hgrc:
165 config[path] = ConfigParser() 172 config[path] = ConfigParser()
166 if isinstance(path, basestring): 173 if isinstance(path, basestring):
167 if os.path.exists(path): 174 if os.path.exists(path):
168 config[path].read(path) 175 config[path].read(path)
176 elif path in urls:
177 if 'default' not in actions:
178 set_default(config[path], path)
169 179
170 # print the chosen hgrc paths 180 # print the chosen hgrc paths
171 if 'list_hgrc' in actions: 181 if 'list_hgrc' in actions:
172 print '\n'.join(hgrc) 182 print '\n'.join(hgrc)
173 183
180 'default_push': set_default_push, 190 'default_push': set_default_push,
181 'default': set_default 191 'default': set_default
182 } 192 }
183 193
184 # cache for later (XXX) 194 # cache for later (XXX)
185 print_ini = actions.pop('print_ini', None) 195 print_ini = actions.pop('print_ini', bool(urls))
186 196
187 # alter .hgrc files 197 # alter .hgrc files
188 for action_name, parameter in actions.items(): 198 for action_name, parameter in actions.items():
189 199
190 # XXX crappy 200 # XXX crappy
205 if print_ini: 215 if print_ini:
206 values = [] 216 values = []
207 for path, ini in config.items(): 217 for path, ini in config.items():
208 _buffer = StringIO() 218 _buffer = StringIO()
209 ini.write(_buffer) 219 ini.write(_buffer)
210 values.append('+++ %s\n%s' % (path, _buffer.getvalue())) 220 value = _buffer.getvalue().strip()
221 if len(config) == 1:
222 values = [value]
223 else:
224 values.append('+++ %s\n%s' % (path, value))
211 print '\n'.join(values) 225 print '\n'.join(values)
212 226
213 # write .ini files 227 # write .ini files
214 for path, ini in config.items(): 228 for path, ini in config.items():
229 if path in urls:
230 continue
215 with file(path, 'w') as f: 231 with file(path, 'w') as f:
216 ini.write(f) 232 ini.write(f)
217 233
218 if __name__ == '__main__': 234 if __name__ == '__main__':
219 main() 235 main()