author | Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
Thu, 26 Apr 2012 03:47:17 +0200 | |
branch | stable |
changeset 16532 | 9eba72cdde34 |
parent 16247 | d87d9d8a8e03 |
child 16594 | 5516fdf3fe24 |
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 |
|
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
23 |
tmpfp = util.atomictempfile(lfutil.storepath(repo, sha), |
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
24 |
createmode=repo.store.createmode) |
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) |
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
28 |
tmpfp._fp.seek(0) |
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
29 |
if sha != lfutil.hexsha1(tmpfp._fp): |
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() |
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
32 |
lfutil.linktousercache(repo, sha) |
15391
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: |
|
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
38 |
tmpfp.discard() |
15168 | 39 |
|
40 |
return wireproto.pushres(0) |
|
41 |
||
42 |
def getlfile(repo, proto, sha): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
43 |
'''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
|
44 |
cache.''' |
15168 | 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] |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
50 |
|
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
51 |
# 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
|
52 |
# 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
|
53 |
# (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
|
54 |
# 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
|
55 |
# ssh proto does for string responses. |
15168 | 56 |
def generator(): |
57 |
yield '%d\n' % length |
|
58 |
for chunk in f: |
|
59 |
yield chunk |
|
60 |
return wireproto.streamres(generator()) |
|
61 |
||
62 |
def statlfile(repo, proto, sha): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
63 |
'''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
|
64 |
mismatched checksum, or '0\n' if it is in good condition''' |
15168 | 65 |
filename = lfutil.findfile(repo, sha) |
66 |
if not filename: |
|
67 |
return '2\n' |
|
68 |
fd = None |
|
69 |
try: |
|
70 |
fd = open(filename, 'rb') |
|
71 |
return lfutil.hexsha1(fd) == sha and '0\n' or '1\n' |
|
72 |
finally: |
|
73 |
if fd: |
|
74 |
fd.close() |
|
75 |
||
76 |
def wirereposetup(ui, repo): |
|
77 |
class lfileswirerepository(repo.__class__): |
|
78 |
def putlfile(self, sha, fd): |
|
79 |
# unfortunately, httprepository._callpush tries to convert its |
|
80 |
# input file-like into a bundle before sending it, so we can't use |
|
81 |
# it ... |
|
82 |
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
|
83 |
res = None |
15168 | 84 |
try: |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
d, output = res.split('\n', 1) |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
88 |
for l in output.splitlines(True): |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
89 |
self.ui.warn(_('remote: '), l, '\n') |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
90 |
return int(d) |
15168 | 91 |
except (ValueError, urllib2.HTTPError): |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
92 |
self.ui.warn(_('unexpected putlfile response: %s') % res) |
15168 | 93 |
return 1 |
94 |
# ... but we can't use sshrepository._call because the data= |
|
95 |
# argument won't get sent, and _callpush does exactly what we want |
|
96 |
# in this case: send the data straight through |
|
97 |
else: |
|
98 |
try: |
|
99 |
ret, output = self._callpush("putlfile", fd, sha=sha) |
|
100 |
if ret == "": |
|
101 |
raise error.ResponseError(_('putlfile failed:'), |
|
102 |
output) |
|
103 |
return int(ret) |
|
104 |
except IOError: |
|
105 |
return 1 |
|
106 |
except ValueError: |
|
107 |
raise error.ResponseError( |
|
108 |
_('putlfile failed (unexpected response):'), ret) |
|
109 |
||
110 |
def getlfile(self, sha): |
|
111 |
stream = self._callstream("getlfile", sha=sha) |
|
112 |
length = stream.readline() |
|
113 |
try: |
|
114 |
length = int(length) |
|
115 |
except ValueError: |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
116 |
self._abort(error.ResponseError(_("unexpected response:"), |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
117 |
length)) |
15168 | 118 |
return (length, stream) |
119 |
||
120 |
def statlfile(self, sha): |
|
121 |
try: |
|
122 |
return int(self._call("statlfile", sha=sha)) |
|
123 |
except (ValueError, urllib2.HTTPError): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
124 |
# If the server returns anything but an integer followed by a |
15168 | 125 |
# newline, newline, it's not speaking our language; if we get |
126 |
# 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
|
127 |
# either way, consider it missing. |
15168 | 128 |
return 2 |
129 |
||
130 |
repo.__class__ = lfileswirerepository |
|
131 |
||
132 |
# advertise the largefiles=serve capability |
|
133 |
def capabilities(repo, proto): |
|
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
134 |
return capabilitiesorig(repo, proto) + ' largefiles=serve' |
15168 | 135 |
|
136 |
# duplicate what Mercurial's new out-of-band errors mechanism does, because |
|
137 |
# clients old and new alike both handle it well |
|
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
138 |
def webprotorefuseclient(self, message): |
15168 | 139 |
self.req.header([('Content-Type', 'application/hg-error')]) |
140 |
return message |
|
141 |
||
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
142 |
def sshprotorefuseclient(self, message): |
15168 | 143 |
self.ui.write_err('%s\n-\n' % message) |
144 |
self.fout.write('\n') |
|
145 |
self.fout.flush() |
|
146 |
||
147 |
return '' |
|
148 |
||
149 |
def heads(repo, proto): |
|
150 |
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
|
151 |
return wireproto.ooberror(LARGEFILES_REQUIRED_MSG) |
15168 | 152 |
return wireproto.heads(repo, proto) |
153 |
||
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
154 |
def sshrepocallstream(self, cmd, **args): |
15168 | 155 |
if cmd == 'heads' and self.capable('largefiles'): |
156 |
cmd = 'lheads' |
|
157 |
if cmd == 'batch' and self.capable('largefiles'): |
|
158 |
args['cmds'] = args['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
|
159 |
return ssholdcallstream(self, cmd, **args) |
15168 | 160 |
|
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
161 |
def httprepocallstream(self, cmd, **args): |
15168 | 162 |
if cmd == 'heads' and self.capable('largefiles'): |
163 |
cmd = 'lheads' |
|
164 |
if cmd == 'batch' and self.capable('largefiles'): |
|
165 |
args['cmds'] = args['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
|
166 |
return httpoldcallstream(self, cmd, **args) |