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