comparison hgext/chgserver.py @ 28262:53dc4aada2d9

chgserver: add utilities to calculate confighash confighash is the hash of sensitive config items like [extensions], and sensitive environment variables like HG*, LD_*, etc. The config items can come from global, user, repo config, and command line flags. For chgserver, it is designed that once confighash changes, the server is not qualified to serve its client and should redirect the client to a new server. The server does not need to exit in this case, since it can still be valid (have a matched confighash) to serve other chg clients.
author Jun Wu <quark@fb.com>
date Fri, 26 Feb 2016 14:50:04 +0000
parents 2ab59ac06b76
children 3682e201cce6
comparison
equal deleted inserted replaced
28261:2ab59ac06b76 28262:53dc4aada2d9
55 # be specifying the version(s) of Mercurial they are tested with, or 55 # be specifying the version(s) of Mercurial they are tested with, or
56 # leave the attribute unspecified. 56 # leave the attribute unspecified.
57 testedwith = 'internal' 57 testedwith = 'internal'
58 58
59 _log = commandserver.log 59 _log = commandserver.log
60
61 def _hashlist(items):
62 """return sha1 hexdigest for a list"""
63 return util.sha1(str(items)).hexdigest()
64
65 # sensitive config sections affecting confighash
66 _configsections = ['extensions']
67
68 # sensitive environment variables affecting confighash
69 _envre = re.compile(r'''\A(?:
70 CHGHG
71 |HG.*
72 |LANG(?:UAGE)?
73 |LC_.*
74 |LD_.*
75 |PATH
76 |PYTHON.*
77 |TERM(?:INFO)?
78 |TZ
79 )\Z''', re.X)
80
81 def _confighash(ui):
82 """return a quick hash for detecting config/env changes
83
84 confighash is the hash of sensitive config items and environment variables.
85
86 for chgserver, it is designed that once confighash changes, the server is
87 not qualified to serve its client and should redirect the client to a new
88 server. different from mtimehash, confighash change will not mark the
89 server outdated and exit since the user can have different configs at the
90 same time.
91 """
92 sectionitems = []
93 for section in _configsections:
94 sectionitems.append(ui.configitems(section))
95 sectionhash = _hashlist(sectionitems)
96 envitems = [(k, v) for k, v in os.environ.iteritems() if _envre.match(k)]
97 envhash = _hashlist(sorted(envitems))
98 return sectionhash[:6] + envhash[:6]
60 99
61 # copied from hgext/pager.py:uisetup() 100 # copied from hgext/pager.py:uisetup()
62 def _setuppagercmd(ui, options, cmd): 101 def _setuppagercmd(ui, options, cmd):
63 if not ui.formatted(): 102 if not ui.formatted():
64 return 103 return