Mercurial > hg > config
annotate python/hgrc.py @ 860:af998083be84
add ssh keys
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 14 Apr 2018 16:01:05 -0700 |
parents | b9be1237fffa |
children | 6da7d26e257b |
rev | line source |
---|---|
832
b9be1237fffa
revert to python2 for time being
Jeff Hammel <k0scist@gmail.com>
parents:
831
diff
changeset
|
1 #!/usr/bin/env python2 |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
350 | 3 """ |
4 Script for modifying hgrc files. | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
433 | 6 If no arguments specified, the repository given by `hg root` is used. |
505 | 7 |
568 | 8 If http(s):// arguments are given, create hgrc file from such a thing |
350 | 9 """ |
433 | 10 |
568 | 11 ## TODO: |
12 # - functionality to populate [web]-> description in hgrc file from | |
13 # setup.py, e.g. | |
14 # http://stackoverflow.com/questions/1541778/mercurial-how-do-i-populate-repository-descriptions-for-hgwebdir-cgi | |
15 # Could also loop over a directory; e.g. | |
16 # `hgrc --setup-web . # loop over all .hg repos in this directory` | |
17 | |
351
971e7deca495
got --print working, maybe
Jeff Hammel <jhammel@mozilla.com>
parents:
350
diff
changeset
|
18 # imports |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 import optparse |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 import os |
433 | 21 import subprocess |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 import sys |
484 | 23 from collections import OrderedDict |
831 | 24 |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 |
828 | 26 try: |
27 # python 2 | |
28 import urlparse | |
829 | 29 from ConfigParser import RawConfigParser as ConfigParser |
831 | 30 from StringIO import StringIO |
828 | 31 except ImportError: |
829 | 32 # python 3 |
828 | 33 import urllib.parse as urlparse |
829 | 34 from configparser import RawConfigParser as ConfigParser |
831 | 35 from io import StringIO |
828 | 36 |
484 | 37 ### global methods |
38 | |
506 | 39 def isHTTP(path): |
40 """is path an {http,https}:// URL?""" | |
41 return urlparse.urlsplit(path)[0] in ('http', 'https') | |
42 | |
480 | 43 class section(object): |
482 | 44 def __init__(self, section_name, *section_names): |
480 | 45 self.sections = [section_name] |
46 self.sections.extend(section_names) | |
486 | 47 def __call__(self, function): |
482 | 48 def wrapped(parser, *args, **kwargs): |
49 for section in self.sections: | |
50 if section not in parser.sections(): | |
51 parser.add_section(section) | |
486 | 52 function(parser, *args, **kwargs) |
482 | 53 return wrapped |
480 | 54 |
506 | 55 |
480 | 56 @section('paths') |
479 | 57 def set_default(parser, default): |
58 """set [paths]:default""" | |
488 | 59 parser.set('paths', 'default', default) |
487 | 60 |
482 | 61 @section('paths') |
467 | 62 def set_default_push(parser, default_push): |
63 """ | |
478 | 64 set [paths]:default-push to `default_push` |
467 | 65 """ |
478 | 66 parser.set('paths', 'default-push', default_push) |
67 | |
468 | 68 def set_default_push_to_ssh(parser): |
69 """ | |
478 | 70 set `[paths]:default-push` to that given by `[paths]:default` but |
474 | 71 turn the protocol to 'ssh' |
477 | 72 If `[paths]:default` is not there, do nothing. |
73 Returns True if written, otherwise False | |
468 | 74 """ |
467 | 75 |
477 | 76 # get [paths]:default value |
77 if 'paths' not in parser.sections(): | |
78 return False | |
79 if not parser.has_option('paths', 'default'): | |
80 return False | |
81 default = parser.get('paths', 'default') | |
475 | 82 |
477 | 83 # parse URL |
84 scheme, netloc, path, query, anchor = urlparse.urlsplit(default) | |
85 ssh_url = urlparse.urlunsplit(('ssh', netloc, path, query, anchor)) | |
86 | |
478 | 87 # set |
88 set_default_push(parser, ssh_url) | |
89 return True # XXX could instead be url to set to or old value | |
90 | |
473 | 91 |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
92 def main(args=sys.argv[1:]): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
93 |
433 | 94 # parse command line arguments |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
95 usage = '%prog [options] repository <repository> <...>' |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
96 parser = optparse.OptionParser(usage=usage, description=__doc__) |
433 | 97 parser.add_option('-l', '--list', dest='list_hgrc', |
350 | 98 action='store_true', default=False, |
433 | 99 help="list full path to hgrc files") |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
100 parser.add_option('--ssh', dest='default_push_ssh', |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
101 action='store_true', default=False, |
350 | 102 help="use `default` entries for `default-push`") |
353 | 103 parser.add_option('--push', '--default-push', dest='default_push', |
104 help="set [paths] default-push location") | |
484 | 105 parser.add_option('-d', '--default', dest='default', |
479 | 106 help="set [paths] default entry") |
478 | 107 parser.add_option('-p', '--print', dest='print_ini', |
108 action='store_true', default=False, | |
109 help="print .ini contents") | |
489 | 110 parser.add_option('--dry-run', dest='dry_run', |
111 action='store_true', default=False, | |
112 help="don't write to disk") | |
437 | 113 options, args = parser.parse_args(args) |
433 | 114 |
467 | 115 # sanitization |
116 if options.default_push and options.default_push_ssh: | |
117 parser.error("Cannot set --push and --ssh") | |
118 | |
433 | 119 # if not specified, use repo from `hg root` |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
120 if not args: |
831 | 121 args = [subprocess.check_output(['hg', 'root']).strip().decode('utf-8')] |
433 | 122 |
123 # if not specified, set a default action | |
124 default_action = 'default_push_ssh' | |
481 | 125 available_actions = ('default', |
126 'default_push', | |
127 'default_push_ssh', | |
484 | 128 'print_ini', |
481 | 129 'list_hgrc', |
465 | 130 ) |
481 | 131 actions = [(name, getattr(options, name)) |
132 for name in available_actions | |
485 | 133 if getattr(options, name)] |
465 | 134 if not actions: |
490 | 135 # add a default action for our convenience |
481 | 136 actions = [('default_push_ssh', True)] |
484 | 137 actions = OrderedDict(actions) |
490 | 138 if not actions: |
139 parser.error("Please specify an action") | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
140 |
506 | 141 # find all hgrc files and URLs |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
142 hgrc = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
143 missing = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
144 not_hg = [] |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
145 not_a_directory = [] |
506 | 146 urls = [] |
350 | 147 errors = {'Missing path': missing, |
148 'Not a mercurial directory': not_hg, | |
149 'Not a directory': not_a_directory, | |
150 } | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
151 for path in args: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
152 if not os.path.exists(path): |
506 | 153 if isHTTP(path): |
154 hgrc.append(path) | |
155 urls.append(path) | |
156 continue | |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
157 missing.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
158 path = os.path.abspath(os.path.normpath(path)) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
159 if os.path.isdir(path): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
160 basename = os.path.basename(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
161 subhgdir = os.path.join(path, '.hg') # hypothetical .hg subdirectory |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
162 if basename == '.hg': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
163 hgrcpath = os.path.join(path, 'hgrc') |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
164 elif os.path.exists(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
165 if not os.path.isdir(subhgdir): |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
166 not_a_directory.append(subhgdir) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
167 continue |
437 | 168 hgrcpath = os.path.join(subhgdir, 'hgrc') |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
169 else: |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
170 not_hg.append(path) |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
171 continue |
437 | 172 hgrc.append(hgrcpath) |
350 | 173 else: |
174 assert os.path.isfile(path), "%s is not a file, exiting" % path | |
437 | 175 hgrc.append(path) |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
176 |
353 | 177 # raise errors if encountered |
437 | 178 if filter(None, errors.values()): |
353 | 179 for key, value in errors.items(): |
180 if value: | |
827 | 181 print ('%s: %s' % (key, ', '.join(value))) |
353 | 182 parser.exit(1) |
183 | |
352 | 184 # construct ConfigParser objects and |
185 # ensure that all the files are parseable | |
186 config = {} | |
187 for path in hgrc: | |
353 | 188 config[path] = ConfigParser() |
352 | 189 if isinstance(path, basestring): |
353 | 190 if os.path.exists(path): |
191 config[path].read(path) | |
506 | 192 elif path in urls: |
193 if 'default' not in actions: | |
194 set_default(config[path], path) | |
353 | 195 |
433 | 196 # print the chosen hgrc paths |
484 | 197 if 'list_hgrc' in actions: |
827 | 198 print ('\n'.join(hgrc)) |
433 | 199 |
482 | 200 # remove from actions list |
484 | 201 actions.pop('list_hgrc', None) |
481 | 202 |
470 | 203 # map of actions -> functions; |
204 # XXX this is pretty improv; to be improved | |
471 | 205 action_map = {'default_push_ssh': set_default_push_to_ssh, |
484 | 206 'default_push': set_default_push, |
207 'default': set_default | |
471 | 208 } |
470 | 209 |
484 | 210 # cache for later (XXX) |
506 | 211 print_ini = actions.pop('print_ini', bool(urls)) |
484 | 212 |
465 | 213 # alter .hgrc files |
486 | 214 for action_name, parameter in actions.items(): |
468 | 215 |
471 | 216 # XXX crappy |
473 | 217 method = action_map[action_name] |
476 | 218 if action_name == 'default_push_ssh': |
219 parameter = None | |
471 | 220 |
221 # apply to all files | |
470 | 222 for path, ini in config.items(): |
481 | 223 |
224 # call method with parser | |
486 | 225 if parameter is None: |
226 method(ini) | |
227 else: | |
473 | 228 method(ini, parameter) |
229 | |
478 | 230 # print .hgrc files, if specified |
484 | 231 if print_ini: |
489 | 232 values = [] |
484 | 233 for path, ini in config.items(): |
489 | 234 _buffer = StringIO() |
235 ini.write(_buffer) | |
506 | 236 value = _buffer.getvalue().strip() |
237 if len(config) == 1: | |
238 values = [value] | |
239 else: | |
240 values.append('+++ %s\n%s' % (path, value)) | |
827 | 241 print ('\n'.join(values)) |
489 | 242 |
243 # write .ini files | |
244 for path, ini in config.items(): | |
506 | 245 if path in urls: |
246 continue | |
827 | 247 with open(path, 'w') as f: |
489 | 248 ini.write(f) |
348
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
249 |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
250 if __name__ == '__main__': |
6004e00b602d
new hg file; TODO: incorporate!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
251 main() |