hgext/largefiles/proto.py
author Matt Mackall <mpm@selenic.com>
Fri, 30 Sep 2011 15:11:19 -0500
changeset 15182 de496752d936
parent 15170 c1a4a3220711
child 15224 7c604d8c7e83
permissions -rw-r--r--
merge with stable
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.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     5
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     6
import os
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     7
import tempfile
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     8
import urllib2
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    10
from mercurial import error, httprepo, util, wireproto
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    11
from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    12
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    13
import lfutil
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    14
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    15
LARGEFILES_REQUIRED_MSG = '\nThis repository uses the largefiles extension.' \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    16
                          '\n\nPlease enable it in your Mercurial config ' \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    17
                          'file.\n'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    18
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    19
def putlfile(repo, proto, sha):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    20
    """putlfile puts a largefile into a repository's local cache and into the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    21
    system cache."""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    22
    f = None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    23
    proto.redirect()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    24
    try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    25
        try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    26
            f = tempfile.NamedTemporaryFile(mode='wb+', prefix='hg-putlfile-')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    27
            proto.getfile(f)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    28
            f.seek(0)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    29
            if sha != lfutil.hexsha1(f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    30
                return wireproto.pushres(1)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    31
            lfutil.copytocacheabsolute(repo, f.name, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    32
        except IOError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    33
            repo.ui.warn(
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    34
                _('error: could not put received data into largefile store'))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    35
            return wireproto.pushres(1)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    36
    finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    37
        if f:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    38
            f.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    39
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    40
    return wireproto.pushres(0)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    41
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    42
def getlfile(repo, proto, sha):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    43
    """getlfile retrieves a largefile from the repository-local cache or system
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    44
    cache."""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    45
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    46
    if not filename:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    47
        raise util.Abort(_('requested largefile %s not present in cache') % sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    48
    f = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    49
    length = os.fstat(f.fileno())[6]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    50
    # since we can't set an HTTP content-length header here, and mercurial core
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    51
    # provides no way to give the length of a streamres (and reading the entire
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    52
    # file into RAM would be ill-advised), we just send the length on the first
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    53
    # line of the response, like the ssh proto does for string responses.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    54
    def generator():
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    55
        yield '%d\n' % length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    56
        for chunk in f:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    57
            yield chunk
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    58
    return wireproto.streamres(generator())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    59
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    60
def statlfile(repo, proto, sha):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    61
    """statlfile sends '2\n' if the largefile is missing, '1\n' if it has a
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    62
    mismatched checksum, or '0\n' if it is in good condition"""
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:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    65
        return '2\n'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    66
    fd = None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    67
    try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    68
        fd = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    69
        return lfutil.hexsha1(fd) == sha and '0\n' or '1\n'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    70
    finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    71
        if fd:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    72
            fd.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    73
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    74
def wirereposetup(ui, repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    75
    class lfileswirerepository(repo.__class__):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    76
        def putlfile(self, sha, fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    77
            # unfortunately, httprepository._callpush tries to convert its
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    78
            # input file-like into a bundle before sending it, so we can't use
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    79
            # it ...
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    80
            if issubclass(self.__class__, httprepo.httprepository):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    81
                try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    82
                    return int(self._call('putlfile', data=fd, sha=sha,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    83
                        headers={'content-type':'application/mercurial-0.1'}))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    84
                except (ValueError, urllib2.HTTPError):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    85
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    86
            # ... but we can't use sshrepository._call because the data=
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    87
            # argument won't get sent, and _callpush does exactly what we want
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    88
            # in this case: send the data straight through
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    89
            else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    90
                try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    91
                    ret, output = self._callpush("putlfile", fd, sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    92
                    if ret == "":
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    93
                        raise error.ResponseError(_('putlfile failed:'),
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    94
                                output)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    95
                    return int(ret)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    96
                except IOError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    97
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    98
                except ValueError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    99
                    raise error.ResponseError(
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   100
                        _('putlfile failed (unexpected response):'), ret)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   101
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   102
        def getlfile(self, sha):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   103
            stream = self._callstream("getlfile", sha=sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   104
            length = stream.readline()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   105
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   106
                length = int(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   107
            except ValueError:
15170
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15168
diff changeset
   108
                self._abort(error.ResponseError(_("unexpected response:"),
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15168
diff changeset
   109
                                                length))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   110
            return (length, stream)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   111
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   112
        def statlfile(self, sha):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   113
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   114
                return int(self._call("statlfile", sha=sha))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   115
            except (ValueError, urllib2.HTTPError):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   116
                # if the server returns anything but an integer followed by a
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   117
                # newline, newline, it's not speaking our language; if we get
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   118
                # an HTTP error, we can't be sure the largefile is present;
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   119
                # either way, consider it missing
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   120
                return 2
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   121
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   122
    repo.__class__ = lfileswirerepository
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   123
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   124
# advertise the largefiles=serve capability
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   125
def capabilities(repo, proto):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   126
    return capabilities_orig(repo, proto) + ' largefiles=serve'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   127
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   128
# duplicate what Mercurial's new out-of-band errors mechanism does, because
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   129
# clients old and new alike both handle it well
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   130
def webproto_refuseclient(self, message):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   131
    self.req.header([('Content-Type', 'application/hg-error')])
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   132
    return message
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   133
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   134
def sshproto_refuseclient(self, message):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   135
    self.ui.write_err('%s\n-\n' % message)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   136
    self.fout.write('\n')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   137
    self.fout.flush()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   138
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   139
    return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   140
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   141
def heads(repo, proto):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   142
    if lfutil.islfilesrepo(repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   143
        try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   144
            # Mercurial >= f4522df38c65
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   145
            return wireproto.ooberror(LARGEFILES_REQUIRED_MSG)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   146
        except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   147
            return proto.refuseclient(LARGEFILES_REQUIRED_MSG)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   148
    return wireproto.heads(repo, proto)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   149
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   150
def sshrepo_callstream(self, cmd, **args):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   151
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   152
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   153
    if cmd == 'batch' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   154
        args['cmds'] = args['cmds'].replace('heads ', 'lheads ')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   155
    return ssh_oldcallstream(self, cmd, **args)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   156
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   157
def httprepo_callstream(self, cmd, **args):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   158
    if cmd == 'heads' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   159
        cmd = 'lheads'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   160
    if cmd == 'batch' and self.capable('largefiles'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   161
        args['cmds'] = args['cmds'].replace('heads ', 'lheads ')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   162
    return http_oldcallstream(self, cmd, **args)