--- a/hgext/lfs/wireprotolfsserver.py Sat Oct 05 10:29:34 2019 -0400
+++ b/hgext/lfs/wireprotolfsserver.py Sun Oct 06 09:45:02 2019 -0400
@@ -12,9 +12,7 @@
import json
import traceback
-from mercurial.hgweb import (
- common as hgwebcommon,
-)
+from mercurial.hgweb import common as hgwebcommon
from mercurial import (
exthelper,
@@ -35,6 +33,7 @@
eh = exthelper.exthelper()
+
@eh.wrapfunction(wireprotoserver, 'handlewsgirequest')
def handlewsgirequest(orig, rctx, req, res, checkperm):
"""Wrap wireprotoserver.handlewsgirequest() to possibly process an LFS
@@ -59,9 +58,9 @@
# TODO: reserve and use a path in the proposed http wireprotocol /api/
# namespace?
elif req.dispatchpath.startswith(b'.hg/lfs/objects'):
- return _processbasictransfer(rctx.repo, req, res,
- lambda perm:
- checkperm(rctx, req, perm))
+ return _processbasictransfer(
+ rctx.repo, req, res, lambda perm: checkperm(rctx, req, perm)
+ )
return False
except hgwebcommon.ErrorResponse as e:
# XXX: copied from the handler surrounding wireprotoserver._callhttp()
@@ -73,11 +72,13 @@
res.setbodybytes(b'0\n%s\n' % pycompat.bytestr(e))
return True
+
def _sethttperror(res, code, message=None):
res.status = hgwebcommon.statusmessage(code, message=message)
res.headers[b'Content-Type'] = b'text/plain; charset=utf-8'
res.setbodybytes(b'')
+
def _logexception(req):
"""Write information about the current exception to wsgi.errors."""
tb = pycompat.sysbytes(traceback.format_exc())
@@ -88,8 +89,10 @@
uri += req.apppath
uri += b'/' + req.dispatchpath
- errorlog.write(b"Exception happened while processing request '%s':\n%s" %
- (uri, tb))
+ errorlog.write(
+ b"Exception happened while processing request '%s':\n%s" % (uri, tb)
+ )
+
def _processbatchrequest(repo, req, res):
"""Handle a request for the Batch API, which is the gateway to granting file
@@ -134,22 +137,32 @@
# If no transfer handlers are explicitly requested, 'basic' is assumed.
if r'basic' not in lfsreq.get(r'transfers', [r'basic']):
- _sethttperror(res, HTTP_BAD_REQUEST,
- b'Only the basic LFS transfer handler is supported')
+ _sethttperror(
+ res,
+ HTTP_BAD_REQUEST,
+ b'Only the basic LFS transfer handler is supported',
+ )
return True
operation = lfsreq.get(r'operation')
operation = pycompat.bytestr(operation)
if operation not in (b'upload', b'download'):
- _sethttperror(res, HTTP_BAD_REQUEST,
- b'Unsupported LFS transfer operation: %s' % operation)
+ _sethttperror(
+ res,
+ HTTP_BAD_REQUEST,
+ b'Unsupported LFS transfer operation: %s' % operation,
+ )
return True
localstore = repo.svfs.lfslocalblobstore
- objects = [p for p in _batchresponseobjects(req, lfsreq.get(r'objects', []),
- operation, localstore)]
+ objects = [
+ p
+ for p in _batchresponseobjects(
+ req, lfsreq.get(r'objects', []), operation, localstore
+ )
+ ]
rsp = {
r'transfer': r'basic',
@@ -162,6 +175,7 @@
return True
+
def _batchresponseobjects(req, objects, action, store):
"""Yield one dictionary of attributes for the Batch API response for each
object in the list.
@@ -197,7 +211,7 @@
rsp = {
r'oid': soid,
r'size': obj.get(r'size'), # XXX: should this check the local size?
- #r'authenticated': True,
+ # r'authenticated': True,
}
exists = True
@@ -222,7 +236,7 @@
rsp[r'error'] = {
r'code': 500,
- r'message': inst.strerror or r'Internal Server Server'
+ r'message': inst.strerror or r'Internal Server Server',
}
yield rsp
continue
@@ -235,15 +249,15 @@
if not exists:
rsp[r'error'] = {
r'code': 404,
- r'message': r"The object does not exist"
+ r'message': r"The object does not exist",
}
yield rsp
continue
elif not verifies:
rsp[r'error'] = {
- r'code': 422, # XXX: is this the right code?
- r'message': r"The object is corrupt"
+ r'code': 422, # XXX: is this the right code?
+ r'message': r"The object is corrupt",
}
yield rsp
continue
@@ -258,9 +272,7 @@
# The spec doesn't mention the Accept header here, but avoid
# a gratuitous deviation from lfs-test-server in the test
# output.
- hdr = {
- r'Accept': r'application/vnd.git-lfs'
- }
+ hdr = {r'Accept': r'application/vnd.git-lfs'}
auth = req.headers.get(b'Authorization', b'')
if auth.startswith(b'Basic '):
@@ -269,9 +281,11 @@
return hdr
rsp[r'actions'] = {
- r'%s' % pycompat.strurl(action): {
- r'href': pycompat.strurl(b'%s%s/.hg/lfs/objects/%s'
- % (req.baseurl, req.apppath, oid)),
+ r'%s'
+ % pycompat.strurl(action): {
+ r'href': pycompat.strurl(
+ b'%s%s/.hg/lfs/objects/%s' % (req.baseurl, req.apppath, oid)
+ ),
# datetime.isoformat() doesn't include the 'Z' suffix
r"expires_at": expiresat.strftime(r'%Y-%m-%dT%H:%M:%SZ'),
r'header': _buildheader(),
@@ -280,6 +294,7 @@
yield rsp
+
def _processbasictransfer(repo, req, res, checkperm):
"""Handle a single file upload (PUT) or download (GET) action for the Basic
Transfer Adapter.
@@ -347,6 +362,9 @@
return True
else:
- _sethttperror(res, HTTP_METHOD_NOT_ALLOWED,
- message=b'Unsupported LFS transfer method: %s' % method)
+ _sethttperror(
+ res,
+ HTTP_METHOD_NOT_ALLOWED,
+ message=b'Unsupported LFS transfer method: %s' % method,
+ )
return True