annotate python/hgrc.py @ 829:2bfd237c4e6c

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