hgext/largefiles/proto.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 28 Mar 2018 14:05:29 -0700
changeset 37293 d5d665f6615a
parent 36656 5c4c9eb1feb6
child 37414 2d965bfeb8f6
permissions -rw-r--r--
wireproto: stop aliasing wire protocol types (API) We generally shy away from aliasing module symbols. I think I was keeping this around for API compatibility. We've already made tons of other API breaks in the wire protocol code this release. What's one more? .. api:: ``wireproto`` module no longer re-exports various types used to define responses to wire protocol commands. Access these types from the ``wireprototypes`` module. Differential Revision: https://phab.mercurial-scm.org/D2979
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,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    14
    httppeer,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    15
    util,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    16
    wireproto,
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
    17
    wireprototypes,
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    18
)
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    19
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    20
from . import (
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    21
    lfutil,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    22
)
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
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15252
diff changeset
    27
LARGEFILES_REQUIRED_MSG = ('\nThis repository uses the largefiles extension.'
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15252
diff changeset
    28
                           '\n\nPlease enable it in your Mercurial config '
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15252
diff changeset
    29
                           'file.\n')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    30
18922
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    31
# 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
    32
ssholdcallstream = None
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    33
httpoldcallstream = None
d2c4d37f7db5 largefiles: quiet (and document) undefined name errors (issue3886)
Bryan O'Sullivan <bryano@fb.com>
parents: 18488
diff changeset
    34
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    35
def putlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    36
    '''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
    37
    and into the user cache.'''
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    38
    with proto.mayberedirectstdio() as output:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    39
        path = lfutil.storepath(repo, sha)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    40
        util.makedirs(os.path.dirname(path))
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    41
        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
    42
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    43
        try:
36070
90ca4986616c wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36067
diff changeset
    44
            proto.forwardpayload(tmpfp)
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    45
            tmpfp._fp.seek(0)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    46
            if sha != lfutil.hexsha1(tmpfp._fp):
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    47
                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
    48
            tmpfp.close()
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    49
            lfutil.linktousercache(repo, sha)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    50
        except IOError as e:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    51
            repo.ui.warn(_('largefiles: failed to put %s into store: %s\n') %
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    52
                         (sha, e.strerror))
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    53
            return wireprototypes.pushres(
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    54
                1, output.getvalue() if output else '')
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    55
        finally:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    56
            tmpfp.discard()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    57
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    58
    return wireprototypes.pushres(0, output.getvalue() if output else '')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    59
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    60
def getlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    61
    '''Server command for retrieving a largefile from the repository-local
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    62
    cache or user cache.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    63
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    64
    if not filename:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
    65
        raise error.Abort(_('requested largefile %s not present in cache')
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
    66
                          % sha)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    67
    f = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    68
    length = os.fstat(f.fileno())[6]
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    69
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    70
    # 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
    71
    # 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
    72
    # (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
    73
    # 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
    74
    # ssh proto does for string responses.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    75
    def generator():
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    76
        yield '%d\n' % length
19009
07e40d589b64 largefiles: use filechunkiter for iterating largefile when serving getlfile
Mads Kiilerich <madski@unity3d.com>
parents: 19006
diff changeset
    77
        for chunk in util.filechunkiter(f):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    78
            yield chunk
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    79
    return wireprototypes.streamreslegacy(gen=generator())
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    80
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    81
def statlfile(repo, proto, sha):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    82
    '''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
    83
    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
    84
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    85
    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
    86
    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
    87
    server side.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    88
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    89
    if not filename:
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
    90
        return wireprototypes.bytesresponse('2\n')
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
    91
    return wireprototypes.bytesresponse('0\n')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    92
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    93
def wirereposetup(ui, repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    94
    class lfileswirerepository(repo.__class__):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    95
        def putlfile(self, sha, fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    96
            # unfortunately, httprepository._callpush tries to convert its
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    97
            # input file-like into a bundle before sending it, so we can't use
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    98
            # it ...
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 17127
diff changeset
    99
            if issubclass(self.__class__, httppeer.httppeer):
26825
78539633acf3 largefiles: don't mute and obfuscate http errors when putlfile fails
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
   100
                res = self._call('putlfile', data=fd, sha=sha,
36656
5c4c9eb1feb6 largefiles: headers and values need to be sysstrs, add r prefixes
Augie Fackler <augie@google.com>
parents: 36312
diff changeset
   101
                    headers={r'content-type': r'application/mercurial-0.1'})
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   102
                try:
15778
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   103
                    d, output = res.split('\n', 1)
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   104
                    for l in output.splitlines(True):
19949
29f12a7a03ee largefiles: don't add extra \n when displaying remote messages in putlfile
Mads Kiilerich <madski@unity3d.com>
parents: 19948
diff changeset
   105
                        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
   106
                    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
   107
                except ValueError:
19947
2a03faf8b5fe largefiles: fix 'unexpected response' warning newlines
Mads Kiilerich <madski@unity3d.com>
parents: 19917
diff changeset
   108
                    self.ui.warn(_('unexpected putlfile response: %r\n') % res)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   109
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   110
            # ... but we can't use sshrepository._call because the data=
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   111
            # argument won't get sent, and _callpush does exactly what we want
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   112
            # in this case: send the data straight through
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   113
            else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   114
                try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   115
                    ret, output = self._callpush("putlfile", fd, sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   116
                    if ret == "":
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   117
                        raise error.ResponseError(_('putlfile failed:'),
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   118
                                output)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   119
                    return int(ret)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   120
                except IOError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   121
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   122
                except ValueError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   123
                    raise error.ResponseError(
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   124
                        _('putlfile failed (unexpected response):'), ret)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   125
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   126
        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
   127
            """returns an iterable with the chunks of the file with sha sha"""
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   128
            stream = self._callstream("getlfile", sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   129
            length = stream.readline()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   130
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   131
                length = int(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   132
            except ValueError:
15170
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15168
diff changeset
   133
                self._abort(error.ResponseError(_("unexpected response:"),
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15168
diff changeset
   134
                                                length))
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   135
19005
1b84047e7d16 largefiles: drop limitreader, use filechunkiter limit
Mads Kiilerich <madski@unity3d.com>
parents: 19004
diff changeset
   136
            # 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
   137
            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
   138
                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
   139
            # 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
   140
            # 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
   141
            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
   142
                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
   143
                if chunk:
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   144
                    self._abort(error.ResponseError(_("unexpected response:"),
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   145
                                                    chunk))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   146
21084
70252bdfd39c largefiles: import whole modules instead of importing parts of them
Mads Kiilerich <madski@unity3d.com>
parents: 19949
diff changeset
   147
        @wireproto.batchable
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   148
        def statlfile(self, sha):
21084
70252bdfd39c largefiles: import whole modules instead of importing parts of them
Mads Kiilerich <madski@unity3d.com>
parents: 19949
diff changeset
   149
            f = wireproto.future()
17127
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   150
            result = {'sha': sha}
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   151
            yield result, f
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   152
            try:
17127
9e1616307c4c largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents: 16594
diff changeset
   153
                yield int(f.value)
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
   154
            except (ValueError, urlerr.httperror):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
   155
                # If the server returns anything but an integer followed by a
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   156
                # newline, newline, it's not speaking our language; if we get
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   157
                # 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
   158
                # 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
   159
                yield 2
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   160
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   161
    repo.__class__ = lfileswirerepository
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   162
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   163
# advertise the largefiles=serve capability
35507
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   164
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
   165
    '''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
   166
    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
   167
    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
   168
    return caps
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   169
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   170
def heads(repo, proto):
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
   171
    '''Wrap server command - largefile capable clients will know to call
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
   172
    lheads instead'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   173
    if lfutil.islfilesrepo(repo):
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
   174
        return wireprototypes.ooberror(LARGEFILES_REQUIRED_MSG)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   175
    return wireproto.heads(repo, proto)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   176
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   177
def sshrepocallstream(self, cmd, **args):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   178
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   179
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   180
    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
   181
        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
   182
    return ssholdcallstream(self, cmd, **args)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   183
36312
3ac8b5c1c36c largefiles: mark headre as bytes regex
Augie Fackler <augie@google.com>
parents: 36074
diff changeset
   184
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
   185
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16155
diff changeset
   186
def httprepocallstream(self, cmd, **args):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   187
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   188
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   189
    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
   190
        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
   191
    return httpoldcallstream(self, cmd, **args)