Mercurial > hg
comparison tests/wireprotosimplecache.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 2c4f656c8e9f |
children | 9d2b2df2c2ba |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
17 ) | 17 ) |
18 from mercurial.interfaces import ( | 18 from mercurial.interfaces import ( |
19 repository, | 19 repository, |
20 util as interfaceutil, | 20 util as interfaceutil, |
21 ) | 21 ) |
22 from mercurial.utils import ( | 22 from mercurial.utils import stringutil |
23 stringutil, | |
24 ) | |
25 | 23 |
26 CACHE = None | 24 CACHE = None |
27 | 25 |
28 configtable = {} | 26 configtable = {} |
29 configitem = registrar.configitem(configtable) | 27 configitem = registrar.configitem(configtable) |
30 | 28 |
31 configitem(b'simplecache', b'cacheapi', | 29 configitem(b'simplecache', b'cacheapi', default=False) |
32 default=False) | 30 configitem(b'simplecache', b'cacheobjects', default=False) |
33 configitem(b'simplecache', b'cacheobjects', | 31 configitem(b'simplecache', b'redirectsfile', default=None) |
34 default=False) | |
35 configitem(b'simplecache', b'redirectsfile', | |
36 default=None) | |
37 | 32 |
38 # API handler that makes cached keys available. | 33 # API handler that makes cached keys available. |
39 def handlecacherequest(rctx, req, res, checkperm, urlparts): | 34 def handlecacherequest(rctx, req, res, checkperm, urlparts): |
40 if rctx.repo.ui.configbool(b'simplecache', b'cacheobjects'): | 35 if rctx.repo.ui.configbool(b'simplecache', b'cacheobjects'): |
41 res.status = b'500 Internal Server Error' | 36 res.status = b'500 Internal Server Error' |
58 | 53 |
59 res.status = b'200 OK' | 54 res.status = b'200 OK' |
60 res.headers[b'Content-Type'] = b'application/mercurial-cbor' | 55 res.headers[b'Content-Type'] = b'application/mercurial-cbor' |
61 res.setbodybytes(CACHE[key]) | 56 res.setbodybytes(CACHE[key]) |
62 | 57 |
58 | |
63 def cachedescriptor(req, repo): | 59 def cachedescriptor(req, repo): |
64 return {} | 60 return {} |
61 | |
65 | 62 |
66 wireprotoserver.API_HANDLERS[b'simplecache'] = { | 63 wireprotoserver.API_HANDLERS[b'simplecache'] = { |
67 b'config': (b'simplecache', b'cacheapi'), | 64 b'config': (b'simplecache', b'cacheapi'), |
68 b'handler': handlecacherequest, | 65 b'handler': handlecacherequest, |
69 b'apidescriptor': cachedescriptor, | 66 b'apidescriptor': cachedescriptor, |
70 } | 67 } |
71 | 68 |
69 | |
72 @interfaceutil.implementer(repository.iwireprotocolcommandcacher) | 70 @interfaceutil.implementer(repository.iwireprotocolcommandcacher) |
73 class memorycacher(object): | 71 class memorycacher(object): |
74 def __init__(self, ui, command, encodefn, redirecttargets, redirecthashes, | 72 def __init__( |
75 req): | 73 self, ui, command, encodefn, redirecttargets, redirecthashes, req |
74 ): | |
76 self.ui = ui | 75 self.ui = ui |
77 self.encodefn = encodefn | 76 self.encodefn = encodefn |
78 self.redirecttargets = redirecttargets | 77 self.redirecttargets = redirecttargets |
79 self.redirecthashes = redirecthashes | 78 self.redirecthashes = redirecthashes |
80 self.req = req | 79 self.req = req |
129 paths.append(b'simplecache') | 128 paths.append(b'simplecache') |
130 paths.append(self.key) | 129 paths.append(self.key) |
131 | 130 |
132 url = b'%s/%s' % (self.req.baseurl, b'/'.join(paths)) | 131 url = b'%s/%s' % (self.req.baseurl, b'/'.join(paths)) |
133 | 132 |
134 #url = b'http://example.com/%s' % self.key | 133 # url = b'http://example.com/%s' % self.key |
135 self.ui.log(b'simplecache', b'sending content redirect for %s to ' | 134 self.ui.log( |
136 b'%s\n', self.key, url) | 135 b'simplecache', |
136 b'sending content redirect for %s to ' b'%s\n', | |
137 self.key, | |
138 url, | |
139 ) | |
137 response = wireprototypes.alternatelocationresponse( | 140 response = wireprototypes.alternatelocationresponse( |
138 url=url, | 141 url=url, mediatype=b'application/mercurial-cbor' |
139 mediatype=b'application/mercurial-cbor') | 142 ) |
140 | 143 |
141 return {b'objs': [response]} | 144 return {b'objs': [response]} |
142 | 145 |
143 if self.cacheobjects: | 146 if self.cacheobjects: |
144 return { | 147 return { |
164 else: | 167 else: |
165 CACHE[self.key] = b''.join(self.buffered) | 168 CACHE[self.key] = b''.join(self.buffered) |
166 | 169 |
167 return [] | 170 return [] |
168 | 171 |
169 def makeresponsecacher(orig, repo, proto, command, args, objencoderfn, | 172 |
170 redirecttargets, redirecthashes): | 173 def makeresponsecacher( |
171 return memorycacher(repo.ui, command, objencoderfn, redirecttargets, | 174 orig, |
172 redirecthashes, proto._req) | 175 repo, |
176 proto, | |
177 command, | |
178 args, | |
179 objencoderfn, | |
180 redirecttargets, | |
181 redirecthashes, | |
182 ): | |
183 return memorycacher( | |
184 repo.ui, | |
185 command, | |
186 objencoderfn, | |
187 redirecttargets, | |
188 redirecthashes, | |
189 proto._req, | |
190 ) | |
191 | |
173 | 192 |
174 def loadredirecttargets(ui): | 193 def loadredirecttargets(ui): |
175 path = ui.config(b'simplecache', b'redirectsfile') | 194 path = ui.config(b'simplecache', b'redirectsfile') |
176 if not path: | 195 if not path: |
177 return [] | 196 return [] |
179 with open(path, 'rb') as fh: | 198 with open(path, 'rb') as fh: |
180 s = fh.read() | 199 s = fh.read() |
181 | 200 |
182 return stringutil.evalpythonliteral(s) | 201 return stringutil.evalpythonliteral(s) |
183 | 202 |
203 | |
184 def getadvertisedredirecttargets(orig, repo, proto): | 204 def getadvertisedredirecttargets(orig, repo, proto): |
185 return loadredirecttargets(repo.ui) | 205 return loadredirecttargets(repo.ui) |
186 | 206 |
207 | |
187 def extsetup(ui): | 208 def extsetup(ui): |
188 global CACHE | 209 global CACHE |
189 | 210 |
190 CACHE = util.lrucachedict(10000) | 211 CACHE = util.lrucachedict(10000) |
191 | 212 |
192 extensions.wrapfunction(wireprotov2server, b'makeresponsecacher', | 213 extensions.wrapfunction( |
193 makeresponsecacher) | 214 wireprotov2server, b'makeresponsecacher', makeresponsecacher |
194 extensions.wrapfunction(wireprotov2server, b'getadvertisedredirecttargets', | 215 ) |
195 getadvertisedredirecttargets) | 216 extensions.wrapfunction( |
217 wireprotov2server, | |
218 b'getadvertisedredirecttargets', | |
219 getadvertisedredirecttargets, | |
220 ) |