Mercurial > hg
annotate hgext/largefiles/basestore.py @ 18574:4db9e31ae605
largefiles: docstrings for verify methods
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Fri, 08 Feb 2013 13:08:25 +0100 |
parents | 003730ca254d |
children | c2d079387b2c |
rev | line source |
---|---|
15168 | 1 # Copyright 2009-2010 Gregory P. Ward |
2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated | |
3 # Copyright 2010-2011 Fog Creek Software | |
4 # Copyright 2010-2011 Unity Technologies | |
5 # | |
6 # This software may be used and distributed according to the terms of the | |
7 # GNU General Public License version 2 or any later version. | |
8 | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
9 '''base class for store implementations and store-related utility code''' |
15168 | 10 |
11 import binascii | |
12 import re | |
13 | |
14 from mercurial import util, node, hg | |
15 from mercurial.i18n import _ | |
16 | |
17 import lfutil | |
18 | |
19 class StoreError(Exception): | |
20 '''Raised when there is a problem getting files from or putting | |
21 files to a central store.''' | |
22 def __init__(self, filename, hash, url, detail): | |
23 self.filename = filename | |
24 self.hash = hash | |
25 self.url = url | |
26 self.detail = detail | |
27 | |
28 def longmessage(self): | |
18461
abfbb04fab8e
largefiles: enhance error message to make it more i18n-friendly
Wagner Bruna <wbruna@softwareexpress.com.br>
parents:
18155
diff
changeset
|
29 return (_("error getting id %s from url %s for file %s: %s\n") % |
18155
5206af8894a3
largefiles: cleanup of warnings on errors getting largefiles
Mads Kiilerich <madski@unity3d.com>
parents:
17424
diff
changeset
|
30 (self.hash, self.url, self.filename, self.detail)) |
15168 | 31 |
32 def __str__(self): | |
33 return "%s: %s" % (self.url, self.detail) | |
34 | |
35 class basestore(object): | |
36 def __init__(self, ui, repo, url): | |
37 self.ui = ui | |
38 self.repo = repo | |
39 self.url = url | |
40 | |
41 def put(self, source, hash): | |
42 '''Put source file into the store under <filename>/<hash>.''' | |
43 raise NotImplementedError('abstract method') | |
44 | |
17127
9e1616307c4c
largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents:
16247
diff
changeset
|
45 def exists(self, hashes): |
18573
003730ca254d
largefiles: fold oddly named _verify into remotestore.exists
Mads Kiilerich <mads@kiilerich.com>
parents:
18546
diff
changeset
|
46 '''Check to see if the store contains the given hashes. Given an |
003730ca254d
largefiles: fold oddly named _verify into remotestore.exists
Mads Kiilerich <mads@kiilerich.com>
parents:
18546
diff
changeset
|
47 iterable of hashes it returns a mapping from hash to bool.''' |
15168 | 48 raise NotImplementedError('abstract method') |
49 | |
50 def get(self, files): | |
51 '''Get the specified largefiles from the store and write to local | |
52 files under repo.root. files is a list of (filename, hash) | |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
17127
diff
changeset
|
53 tuples. Return (success, missing), lists of files successfully |
15168 | 54 downloaded and those not found in the store. success is a list |
55 of (filename, hash) tuples; missing is a list of filenames that | |
56 we could not get. (The detailed error message will already have | |
57 been presented to the user, so missing is just supplied as a | |
58 summary.)''' | |
59 success = [] | |
60 missing = [] | |
61 ui = self.ui | |
62 | |
63 at = 0 | |
64 for filename, hash in files: | |
65 ui.progress(_('getting largefiles'), at, unit='lfile', | |
66 total=len(files)) | |
67 at += 1 | |
68 ui.note(_('getting %s:%s\n') % (filename, hash)) | |
69 | |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15302
diff
changeset
|
70 storefilename = lfutil.storepath(self.repo, hash) |
18483
ce5f529deb36
largefiles: don't allow corruption to propagate after detection
Mads Kiilerich <madski@unity3d.com>
parents:
18461
diff
changeset
|
71 tmpfile = util.atomictempfile(storefilename + '.tmp', |
16154
9b072a5f8f92
largefiles: respect store.createmode in basestore.get
Martin Geisler <mg@aragost.com>
parents:
15943
diff
changeset
|
72 createmode=self.repo.store.createmode) |
15168 | 73 |
74 try: | |
75 hhash = binascii.hexlify(self._getfile(tmpfile, filename, hash)) | |
76 except StoreError, err: | |
77 ui.warn(err.longmessage()) | |
78 hhash = "" | |
18483
ce5f529deb36
largefiles: don't allow corruption to propagate after detection
Mads Kiilerich <madski@unity3d.com>
parents:
18461
diff
changeset
|
79 tmpfile.close() # has probably already been closed! |
15168 | 80 |
81 if hhash != hash: | |
82 if hhash != "": | |
83 ui.warn(_('%s: data corruption (expected %s, got %s)\n') | |
84 % (filename, hash, hhash)) | |
18483
ce5f529deb36
largefiles: don't allow corruption to propagate after detection
Mads Kiilerich <madski@unity3d.com>
parents:
18461
diff
changeset
|
85 util.unlink(storefilename + '.tmp') |
15168 | 86 missing.append(filename) |
87 continue | |
88 | |
18483
ce5f529deb36
largefiles: don't allow corruption to propagate after detection
Mads Kiilerich <madski@unity3d.com>
parents:
18461
diff
changeset
|
89 util.rename(storefilename + '.tmp', storefilename) |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15302
diff
changeset
|
90 lfutil.linktousercache(self.repo, hash) |
15168 | 91 success.append((filename, hhash)) |
92 | |
93 ui.progress(_('getting largefiles'), None) | |
94 return (success, missing) | |
95 | |
96 def verify(self, revs, contents=False): | |
97 '''Verify the existence (and, optionally, contents) of every big | |
98 file revision referenced by every changeset in revs. | |
99 Return 0 if all is well, non-zero on any errors.''' | |
100 failed = False | |
101 | |
18546
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
102 self.ui.status(_('searching %d changesets for largefiles\n') % |
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
103 len(revs)) |
15168 | 104 verified = set() # set of (filename, filenode) tuples |
105 | |
106 for rev in revs: | |
107 cctx = self.repo[rev] | |
108 cset = "%d:%s" % (cctx.rev(), node.short(cctx.node())) | |
109 | |
18486
1067a6240f86
largefiles: verify all files in each revision and report errors in any revision
Mads Kiilerich <madski@unity3d.com>
parents:
18483
diff
changeset
|
110 for standin in cctx: |
1067a6240f86
largefiles: verify all files in each revision and report errors in any revision
Mads Kiilerich <madski@unity3d.com>
parents:
18483
diff
changeset
|
111 if self._verifyfile(cctx, cset, contents, standin, verified): |
1067a6240f86
largefiles: verify all files in each revision and report errors in any revision
Mads Kiilerich <madski@unity3d.com>
parents:
18483
diff
changeset
|
112 failed = True |
15168 | 113 |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
114 numrevs = len(verified) |
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
115 numlfiles = len(set([fname for (fname, fnode) in verified])) |
15168 | 116 if contents: |
18546
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
117 self.ui.status( |
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
118 _('verified contents of %d revisions of %d largefiles\n') |
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
119 % (numrevs, numlfiles)) |
15168 | 120 else: |
18546
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
121 self.ui.status( |
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
122 _('verified existence of %d revisions of %d largefiles\n') |
fb0e8966a4be
largefiles: verify status should be written as status, not as write
Mads Kiilerich <madski@unity3d.com>
parents:
18489
diff
changeset
|
123 % (numrevs, numlfiles)) |
15168 | 124 return int(failed) |
125 | |
126 def _getfile(self, tmpfile, filename, hash): | |
127 '''Fetch one revision of one file from the store and write it | |
128 to tmpfile. Compute the hash of the file on-the-fly as it | |
129 downloads and return the binary hash. Close tmpfile. Raise | |
130 StoreError if unable to download the file (e.g. it does not | |
131 exist in the store).''' | |
132 raise NotImplementedError('abstract method') | |
133 | |
134 def _verifyfile(self, cctx, cset, contents, standin, verified): | |
135 '''Perform the actual verification of a file in the store. | |
18574
4db9e31ae605
largefiles: docstrings for verify methods
Mads Kiilerich <mads@kiilerich.com>
parents:
18573
diff
changeset
|
136 'cset' is only used in warnings. |
4db9e31ae605
largefiles: docstrings for verify methods
Mads Kiilerich <mads@kiilerich.com>
parents:
18573
diff
changeset
|
137 'contents' controls verification of content hash. |
4db9e31ae605
largefiles: docstrings for verify methods
Mads Kiilerich <mads@kiilerich.com>
parents:
18573
diff
changeset
|
138 'standin' is the standin path of the largefile to verify. |
4db9e31ae605
largefiles: docstrings for verify methods
Mads Kiilerich <mads@kiilerich.com>
parents:
18573
diff
changeset
|
139 'verified' is maintained as a set of already verified files. |
4db9e31ae605
largefiles: docstrings for verify methods
Mads Kiilerich <mads@kiilerich.com>
parents:
18573
diff
changeset
|
140 Returns _true_ if it is a standin and any problems are found! |
15168 | 141 ''' |
142 raise NotImplementedError('abstract method') | |
143 | |
144 import localstore, wirestore | |
145 | |
146 _storeprovider = { | |
147 'file': [localstore.localstore], | |
148 'http': [wirestore.wirestore], | |
149 'https': [wirestore.wirestore], | |
150 'ssh': [wirestore.wirestore], | |
151 } | |
152 | |
153 _scheme_re = re.compile(r'^([a-zA-Z0-9+-.]+)://') | |
154 | |
155 # During clone this function is passed the src's ui object | |
156 # but it needs the dest's ui object so it can read out of | |
157 # the config file. Use repo.ui instead. | |
158 def _openstore(repo, remote=None, put=False): | |
159 ui = repo.ui | |
160 | |
161 if not remote: | |
15943
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
162 lfpullsource = getattr(repo, 'lfpullsource', None) |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
163 if lfpullsource: |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
164 path = ui.expandpath(lfpullsource) |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
165 else: |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
166 path = ui.expandpath('default-push', 'default') |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
167 |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
168 # ui.expandpath() leaves 'default-push' and 'default' alone if |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
169 # they cannot be expanded: fallback to the empty string, |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
170 # meaning the current directory. |
15168 | 171 if path == 'default-push' or path == 'default': |
172 path = '' | |
173 remote = repo | |
174 else: | |
18489
f1700480bef7
largefiles: allow use of urls with #revision
Mads Kiilerich <madski@unity3d.com>
parents:
18486
diff
changeset
|
175 path, _branches = hg.parseurl(path) |
15168 | 176 remote = hg.peer(repo, {}, path) |
177 | |
178 # The path could be a scheme so use Mercurial's normal functionality | |
179 # to resolve the scheme to a repository and use its path | |
15169
aa262fff87ac
largefile: fix up hasattr usage
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
180 path = util.safehasattr(remote, 'url') and remote.url() or remote.path |
15168 | 181 |
182 match = _scheme_re.match(path) | |
183 if not match: # regular filesystem path | |
184 scheme = 'file' | |
185 else: | |
186 scheme = match.group(1) | |
187 | |
188 try: | |
189 storeproviders = _storeprovider[scheme] | |
190 except KeyError: | |
191 raise util.Abort(_('unsupported URL scheme %r') % scheme) | |
192 | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
193 for classobj in storeproviders: |
15168 | 194 try: |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
195 return classobj(ui, repo, remote) |
15168 | 196 except lfutil.storeprotonotcapable: |
197 pass | |
198 | |
15302
225d30bacabd
largefiles: string formatting typo in basestore._openstore where comma is used instead of modulo
Hao Lian <hao@fogcreek.com>
parents:
15255
diff
changeset
|
199 raise util.Abort(_('%s does not appear to be a largefile store') % path) |