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