author | Matt Mackall <mpm@selenic.com> |
Mon, 30 Jan 2012 11:23:17 -0600 | |
branch | stable |
changeset 16021 | 7f47873d7484 |
parent 15778 | f15c646bffc7 |
child 16155 | 1b2b42e866be |
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 urllib2 |
|
8 |
||
9 |
from mercurial import error, httprepo, util, wireproto |
|
10 |
from mercurial.i18n import _ |
|
11 |
||
12 |
import lfutil |
|
13 |
||
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
14 |
LARGEFILES_REQUIRED_MSG = ('\nThis repository uses the largefiles extension.' |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
15 |
'\n\nPlease enable it in your Mercurial config ' |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
16 |
'file.\n') |
15168 | 17 |
|
18 |
def putlfile(repo, proto, sha): |
|
15317
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
19 |
'''Put a largefile into a repository's local store and into the |
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
20 |
user cache.''' |
15168 | 21 |
proto.redirect() |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
22 |
|
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
23 |
fd, tmpname = lfutil.mkstemp(repo, prefix='hg-putlfile') |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
24 |
tmpfp = os.fdopen(fd, 'wb+') |
15168 | 25 |
try: |
26 |
try: |
|
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
27 |
proto.getfile(tmpfp) |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
28 |
tmpfp.seek(0) |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
29 |
if sha != lfutil.hexsha1(tmpfp): |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
30 |
raise IOError(0, _('largefile contents do not match hash')) |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
31 |
tmpfp.close() |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
32 |
lfutil.copytostoreabsolute(repo, tmpname, sha) |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
33 |
except IOError, e: |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
34 |
repo.ui.warn(_('largefiles: failed to put %s into store: %s') % |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
35 |
(sha, e.strerror)) |
15168 | 36 |
return wireproto.pushres(1) |
37 |
finally: |
|
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
38 |
tmpfp.close() |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
39 |
os.unlink(tmpname) |
15168 | 40 |
|
41 |
return wireproto.pushres(0) |
|
42 |
||
43 |
def getlfile(repo, proto, sha): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
44 |
'''Retrieve a largefile from the repository-local cache or system |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
45 |
cache.''' |
15168 | 46 |
filename = lfutil.findfile(repo, sha) |
47 |
if not filename: |
|
48 |
raise util.Abort(_('requested largefile %s not present in cache') % sha) |
|
49 |
f = open(filename, 'rb') |
|
50 |
length = os.fstat(f.fileno())[6] |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
51 |
|
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
52 |
# 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
|
53 |
# 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
|
54 |
# (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
|
55 |
# 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
|
56 |
# ssh proto does for string responses. |
15168 | 57 |
def generator(): |
58 |
yield '%d\n' % length |
|
59 |
for chunk in f: |
|
60 |
yield chunk |
|
61 |
return wireproto.streamres(generator()) |
|
62 |
||
63 |
def statlfile(repo, proto, sha): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
64 |
'''Return '2\n' if the largefile is missing, '1\n' if it has a |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
65 |
mismatched checksum, or '0\n' if it is in good condition''' |
15168 | 66 |
filename = lfutil.findfile(repo, sha) |
67 |
if not filename: |
|
68 |
return '2\n' |
|
69 |
fd = None |
|
70 |
try: |
|
71 |
fd = open(filename, 'rb') |
|
72 |
return lfutil.hexsha1(fd) == sha and '0\n' or '1\n' |
|
73 |
finally: |
|
74 |
if fd: |
|
75 |
fd.close() |
|
76 |
||
77 |
def wirereposetup(ui, repo): |
|
78 |
class lfileswirerepository(repo.__class__): |
|
79 |
def putlfile(self, sha, fd): |
|
80 |
# unfortunately, httprepository._callpush tries to convert its |
|
81 |
# input file-like into a bundle before sending it, so we can't use |
|
82 |
# it ... |
|
83 |
if issubclass(self.__class__, httprepo.httprepository): |
|
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
84 |
res = None |
15168 | 85 |
try: |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
86 |
res = self._call('putlfile', data=fd, sha=sha, |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
87 |
headers={'content-type':'application/mercurial-0.1'}) |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
88 |
d, output = res.split('\n', 1) |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
89 |
for l in output.splitlines(True): |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
90 |
self.ui.warn(_('remote: '), l, '\n') |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
91 |
return int(d) |
15168 | 92 |
except (ValueError, urllib2.HTTPError): |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
93 |
self.ui.warn(_('unexpected putlfile response: %s') % res) |
15168 | 94 |
return 1 |
95 |
# ... but we can't use sshrepository._call because the data= |
|
96 |
# argument won't get sent, and _callpush does exactly what we want |
|
97 |
# in this case: send the data straight through |
|
98 |
else: |
|
99 |
try: |
|
100 |
ret, output = self._callpush("putlfile", fd, sha=sha) |
|
101 |
if ret == "": |
|
102 |
raise error.ResponseError(_('putlfile failed:'), |
|
103 |
output) |
|
104 |
return int(ret) |
|
105 |
except IOError: |
|
106 |
return 1 |
|
107 |
except ValueError: |
|
108 |
raise error.ResponseError( |
|
109 |
_('putlfile failed (unexpected response):'), ret) |
|
110 |
||
111 |
def getlfile(self, sha): |
|
112 |
stream = self._callstream("getlfile", sha=sha) |
|
113 |
length = stream.readline() |
|
114 |
try: |
|
115 |
length = int(length) |
|
116 |
except ValueError: |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
117 |
self._abort(error.ResponseError(_("unexpected response:"), |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
118 |
length)) |
15168 | 119 |
return (length, stream) |
120 |
||
121 |
def statlfile(self, sha): |
|
122 |
try: |
|
123 |
return int(self._call("statlfile", sha=sha)) |
|
124 |
except (ValueError, urllib2.HTTPError): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
125 |
# If the server returns anything but an integer followed by a |
15168 | 126 |
# newline, newline, it's not speaking our language; if we get |
127 |
# 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
|
128 |
# either way, consider it missing. |
15168 | 129 |
return 2 |
130 |
||
131 |
repo.__class__ = lfileswirerepository |
|
132 |
||
133 |
# advertise the largefiles=serve capability |
|
134 |
def capabilities(repo, proto): |
|
135 |
return capabilities_orig(repo, proto) + ' largefiles=serve' |
|
136 |
||
137 |
# duplicate what Mercurial's new out-of-band errors mechanism does, because |
|
138 |
# clients old and new alike both handle it well |
|
139 |
def webproto_refuseclient(self, message): |
|
140 |
self.req.header([('Content-Type', 'application/hg-error')]) |
|
141 |
return message |
|
142 |
||
143 |
def sshproto_refuseclient(self, message): |
|
144 |
self.ui.write_err('%s\n-\n' % message) |
|
145 |
self.fout.write('\n') |
|
146 |
self.fout.flush() |
|
147 |
||
148 |
return '' |
|
149 |
||
150 |
def heads(repo, proto): |
|
151 |
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
|
152 |
return wireproto.ooberror(LARGEFILES_REQUIRED_MSG) |
15168 | 153 |
return wireproto.heads(repo, proto) |
154 |
||
155 |
def sshrepo_callstream(self, cmd, **args): |
|
156 |
if cmd == 'heads' and self.capable('largefiles'): |
|
157 |
cmd = 'lheads' |
|
158 |
if cmd == 'batch' and self.capable('largefiles'): |
|
159 |
args['cmds'] = args['cmds'].replace('heads ', 'lheads ') |
|
160 |
return ssh_oldcallstream(self, cmd, **args) |
|
161 |
||
162 |
def httprepo_callstream(self, cmd, **args): |
|
163 |
if cmd == 'heads' and self.capable('largefiles'): |
|
164 |
cmd = 'lheads' |
|
165 |
if cmd == 'batch' and self.capable('largefiles'): |
|
166 |
args['cmds'] = args['cmds'].replace('heads ', 'lheads ') |
|
167 |
return http_oldcallstream(self, cmd, **args) |