hgext/largefiles/proto.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 41062 0a7f582f6f1f
child 43077 687b865b95ad
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     1
# Copyright 2011 Fog Creek Software
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     2
#
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
     5
from __future__ import absolute_import
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     7
import os
19917
cff331cbb5ee largefiles: make the protocol hack for replacing heads with lheads more precise
Mads Kiilerich <madski@unity3d.com>
parents: 19009
diff changeset
     8
import re
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    10
from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    11
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    12
from mercurial import (
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    13
    error,
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    14
    exthelper,
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    15
    httppeer,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    16
    util,
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
    17
    wireprototypes,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37484
diff changeset
    18
    wireprotov1peer,
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    19
    wireprotov1server,
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    20
)
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    22
from . import lfutil
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    23
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    24
urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    25
urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    27
LARGEFILES_REQUIRED_MSG = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    28
    '\nThis repository uses the largefiles extension.'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    29
    '\n\nPlease enable it in your Mercurial config '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    30
    'file.\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    31
)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    32
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    33
eh = exthelper.exthelper()
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    34
18922
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    35
# these will all be replaced by largefiles.uisetup
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    36
ssholdcallstream = None
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    37
httpoldcallstream = None
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    39
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    40
def putlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    41
    '''Server command for putting a largefile into a repository's local store
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    42
    and into the user cache.'''
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    43
    with proto.mayberedirectstdio() as output:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    44
        path = lfutil.storepath(repo, sha)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    45
        util.makedirs(os.path.dirname(path))
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    46
        tmpfp = util.atomictempfile(path, createmode=repo.store.createmode)
16594
5516fdf3fe24 largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents: 16247
diff changeset
    47
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    48
        try:
37414
2d965bfeb8f6 wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents: 37293
diff changeset
    49
            for p in proto.getpayload():
2d965bfeb8f6 wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents: 37293
diff changeset
    50
                tmpfp.write(p)
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    51
            tmpfp._fp.seek(0)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    52
            if sha != lfutil.hexsha1(tmpfp._fp):
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    53
                raise IOError(0, _('largefile contents do not match hash'))
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    54
            tmpfp.close()
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    55
            lfutil.linktousercache(repo, sha)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    56
        except IOError as e:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    57
            repo.ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    58
                _('largefiles: failed to put %s into store: %s\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    59
                % (sha, e.strerror)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    60
            )
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    61
            return wireprototypes.pushres(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    62
                1, output.getvalue() if output else ''
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    63
            )
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    64
        finally:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    65
            tmpfp.discard()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    66
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    67
    return wireprototypes.pushres(0, output.getvalue() if output else '')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    68
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    69
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    70
def getlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    71
    '''Server command for retrieving a largefile from the repository-local
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    72
    cache or user cache.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    73
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    74
    if not filename:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    75
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    76
            _('requested largefile %s not present in cache') % sha
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    77
        )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    78
    f = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    79
    length = os.fstat(f.fileno())[6]
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    80
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    81
    # Since we can't set an HTTP content-length header here, and
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    82
    # Mercurial core provides no way to give the length of a streamres
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    83
    # (and reading the entire file into RAM would be ill-advised), we
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    84
    # just send the length on the first line of the response, like the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    85
    # ssh proto does for string responses.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    86
    def generator():
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    87
        yield '%d\n' % length
19009
07e40d589b64 largefiles: use filechunkiter for iterating largefile when serving getlfile
Mads Kiilerich <madski@unity3d.com>
parents: 19006
diff changeset
    88
        for chunk in util.filechunkiter(f):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    89
            yield chunk
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    90
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    91
    return wireprototypes.streamreslegacy(gen=generator())
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    92
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    93
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    94
def statlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    95
    '''Server command for checking if a largefile is present - returns '2\n' if
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    96
    the largefile is missing, '0\n' if it seems to be in good condition.
18488
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    97
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    98
    The value 1 is reserved for mismatched checksum, but that is too expensive
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    99
    to be verified on every stat and must be caught be running 'hg verify'
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
   100
    server side.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   101
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   102
    if not filename:
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
   103
        return wireprototypes.bytesresponse('2\n')
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
   104
    return wireprototypes.bytesresponse('0\n')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   105
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   106
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   107
def wirereposetup(ui, repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   108
    class lfileswirerepository(repo.__class__):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   109
        def putlfile(self, sha, fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   110
            # unfortunately, httprepository._callpush tries to convert its
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   111
            # input file-like into a bundle before sending it, so we can't use
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   112
            # it ...
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 17127
diff changeset
   113
            if issubclass(self.__class__, httppeer.httppeer):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   114
                res = self._call(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   115
                    'putlfile',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   116
                    data=fd,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   117
                    sha=sha,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   118
                    headers={r'content-type': r'application/mercurial-0.1'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   119
                )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   120
                try:
15778
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   121
                    d, output = res.split('\n', 1)
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   122
                    for l in output.splitlines(True):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   123
                        self.ui.warn(_('remote: '), l)  # assume l ends with \n
15778
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   124
                    return int(d)
26825
78539633acf3 largefiles: don't mute and obfuscate http errors when putlfile fails
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
   125
                except ValueError:
19947
2a03faf8b5fe largefiles: fix 'unexpected response' warning newlines
Mads Kiilerich <madski@unity3d.com>
parents: 19917
diff changeset
   126
                    self.ui.warn(_('unexpected putlfile response: %r\n') % res)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   127
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   128
            # ... but we can't use sshrepository._call because the data=
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   129
            # argument won't get sent, and _callpush does exactly what we want
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   130
            # in this case: send the data straight through
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   131
            else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   132
                try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   133
                    ret, output = self._callpush("putlfile", fd, sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   134
                    if ret == "":
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   135
                        raise error.ResponseError(_('putlfile failed:'), output)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   136
                    return int(ret)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   137
                except IOError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   138
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   139
                except ValueError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   140
                    raise error.ResponseError(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   141
                        _('putlfile failed (unexpected response):'), ret
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   142
                    )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   143
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   144
        def getlfile(self, sha):
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   145
            """returns an iterable with the chunks of the file with sha sha"""
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   146
            stream = self._callstream("getlfile", sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   147
            length = stream.readline()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   148
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   149
                length = int(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   150
            except ValueError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   151
                self._abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   152
                    error.ResponseError(_("unexpected response:"), length)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   153
                )
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   154
19005
1b84047e7d16 largefiles: drop limitreader, use filechunkiter limit
Mads Kiilerich <madski@unity3d.com>
parents: 19004
diff changeset
   155
            # SSH streams will block if reading more than length
30181
7356e6b1f5b8 util: increase filechunkiter size to 128k
Mads Kiilerich <madski@unity3d.com>
parents: 29312
diff changeset
   156
            for chunk in util.filechunkiter(stream, limit=length):
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   157
                yield chunk
19006
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   158
            # HTTP streams must hit the end to process the last empty
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   159
            # chunk of Chunked-Encoding so the connection can be reused.
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   160
            if issubclass(self.__class__, httppeer.httppeer):
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   161
                chunk = stream.read(1)
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   162
                if chunk:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   163
                    self._abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   164
                        error.ResponseError(_("unexpected response:"), chunk)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   165
                    )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   166
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37484
diff changeset
   167
        @wireprotov1peer.batchable
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   168
        def statlfile(self, sha):
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37484
diff changeset
   169
            f = wireprotov1peer.future()
17127
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   170
            result = {'sha': sha}
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   171
            yield result, f
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   172
            try:
17127
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   173
                yield int(f.value)
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
   174
            except (ValueError, urlerr.httperror):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
   175
                # If the server returns anything but an integer followed by a
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   176
                # newline, newline, it's not speaking our language; if we get
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   177
                # an HTTP error, we can't be sure the largefile is present;
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
   178
                # either way, consider it missing.
17127
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   179
                yield 2
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   180
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   181
    repo.__class__ = lfileswirerepository
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   182
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   183
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   184
# advertise the largefiles=serve capability
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
   185
@eh.wrapfunction(wireprotov1server, '_capabilities')
35507
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   186
def _capabilities(orig, repo, proto):
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   187
    '''announce largefile server capability'''
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   188
    caps = orig(repo, proto)
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   189
    caps.append('largefiles=serve')
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   190
    return caps
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   191
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   192
37484
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   193
def heads(orig, repo, proto):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
   194
    '''Wrap server command - largefile capable clients will know to call
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
   195
    lheads instead'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   196
    if lfutil.islfilesrepo(repo):
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
   197
        return wireprototypes.ooberror(LARGEFILES_REQUIRED_MSG)
37484
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   198
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   199
    return orig(repo, proto)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   201
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   202
def sshrepocallstream(self, cmd, **args):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   203
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   204
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   205
    if cmd == 'batch' and self.capable('largefiles'):
35348
576ba8194fa8 py3: handle keyword arguments correctly in hgext/largefiles/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30466
diff changeset
   206
        args[r'cmds'] = args[r'cmds'].replace('heads ', 'lheads ')
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   207
    return ssholdcallstream(self, cmd, **args)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   209
36312
3ac8b5c1c36c largefiles: mark headre as bytes regex
Augie Fackler <augie@google.com>
parents: 36074
diff changeset
   210
headsre = re.compile(br'(^|;)heads\b')
19917
cff331cbb5ee largefiles: make the protocol hack for replacing heads with lheads more precise
Mads Kiilerich <madski@unity3d.com>
parents: 19009
diff changeset
   211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   212
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   213
def httprepocallstream(self, cmd, **args):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   214
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   215
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   216
    if cmd == 'batch' and self.capable('largefiles'):
35348
576ba8194fa8 py3: handle keyword arguments correctly in hgext/largefiles/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30466
diff changeset
   217
        args[r'cmds'] = headsre.sub('lheads', args[r'cmds'])
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   218
    return httpoldcallstream(self, cmd, **args)