Mercurial > hg
annotate hgext/largefiles/basestore.py @ 17424:e7cfe3587ea4
fix trivial spelling errors
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Wed, 15 Aug 2012 22:38:42 +0200 |
parents | 9e1616307c4c |
children | 5206af8894a3 |
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): | |
29 if self.url: | |
30 return ('%s: %s\n' | |
31 '(failed URL: %s)\n' | |
32 % (self.filename, self.detail, self.url)) | |
33 else: | |
34 return ('%s: %s\n' | |
35 '(no default or default-push path set in hgrc)\n' | |
36 % (self.filename, self.detail)) | |
37 | |
38 def __str__(self): | |
39 return "%s: %s" % (self.url, self.detail) | |
40 | |
41 class basestore(object): | |
42 def __init__(self, ui, repo, url): | |
43 self.ui = ui | |
44 self.repo = repo | |
45 self.url = url | |
46 | |
47 def put(self, source, hash): | |
48 '''Put source file into the store under <filename>/<hash>.''' | |
49 raise NotImplementedError('abstract method') | |
50 | |
17127
9e1616307c4c
largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents:
16247
diff
changeset
|
51 def exists(self, hashes): |
9e1616307c4c
largefiles: batch statlfile requests when pushing a largefiles repo (issue3386)
Na'Tosha Bard <natosha@unity3d.com>
parents:
16247
diff
changeset
|
52 '''Check to see if the store contains the given hashes.''' |
15168 | 53 raise NotImplementedError('abstract method') |
54 | |
55 def get(self, files): | |
56 '''Get the specified largefiles from the store and write to local | |
57 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
|
58 tuples. Return (success, missing), lists of files successfully |
15168 | 59 downloaded and those not found in the store. success is a list |
60 of (filename, hash) tuples; missing is a list of filenames that | |
61 we could not get. (The detailed error message will already have | |
62 been presented to the user, so missing is just supplied as a | |
63 summary.)''' | |
64 success = [] | |
65 missing = [] | |
66 ui = self.ui | |
67 | |
68 at = 0 | |
69 for filename, hash in files: | |
70 ui.progress(_('getting largefiles'), at, unit='lfile', | |
71 total=len(files)) | |
72 at += 1 | |
73 ui.note(_('getting %s:%s\n') % (filename, hash)) | |
74 | |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15302
diff
changeset
|
75 storefilename = lfutil.storepath(self.repo, hash) |
16154
9b072a5f8f92
largefiles: respect store.createmode in basestore.get
Martin Geisler <mg@aragost.com>
parents:
15943
diff
changeset
|
76 tmpfile = util.atomictempfile(storefilename, |
9b072a5f8f92
largefiles: respect store.createmode in basestore.get
Martin Geisler <mg@aragost.com>
parents:
15943
diff
changeset
|
77 createmode=self.repo.store.createmode) |
15168 | 78 |
79 try: | |
80 hhash = binascii.hexlify(self._getfile(tmpfile, filename, hash)) | |
81 except StoreError, err: | |
82 ui.warn(err.longmessage()) | |
83 hhash = "" | |
84 | |
85 if hhash != hash: | |
86 if hhash != "": | |
87 ui.warn(_('%s: data corruption (expected %s, got %s)\n') | |
88 % (filename, hash, hhash)) | |
16154
9b072a5f8f92
largefiles: respect store.createmode in basestore.get
Martin Geisler <mg@aragost.com>
parents:
15943
diff
changeset
|
89 tmpfile.discard() # no-op if it's already closed |
15168 | 90 missing.append(filename) |
91 continue | |
92 | |
16154
9b072a5f8f92
largefiles: respect store.createmode in basestore.get
Martin Geisler <mg@aragost.com>
parents:
15943
diff
changeset
|
93 tmpfile.close() |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15302
diff
changeset
|
94 lfutil.linktousercache(self.repo, hash) |
15168 | 95 success.append((filename, hhash)) |
96 | |
97 ui.progress(_('getting largefiles'), None) | |
98 return (success, missing) | |
99 | |
100 def verify(self, revs, contents=False): | |
101 '''Verify the existence (and, optionally, contents) of every big | |
102 file revision referenced by every changeset in revs. | |
103 Return 0 if all is well, non-zero on any errors.''' | |
104 write = self.ui.write | |
105 failed = False | |
106 | |
107 write(_('searching %d changesets for largefiles\n') % len(revs)) | |
108 verified = set() # set of (filename, filenode) tuples | |
109 | |
110 for rev in revs: | |
111 cctx = self.repo[rev] | |
112 cset = "%d:%s" % (cctx.rev(), node.short(cctx.node())) | |
113 | |
15319
9da7e96cd5c2
largefiles: remove redundant any_ function
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
114 failed = util.any(self._verifyfile( |
15168 | 115 cctx, cset, contents, standin, verified) for standin in cctx) |
116 | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
117 numrevs = len(verified) |
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
118 numlfiles = len(set([fname for (fname, fnode) in verified])) |
15168 | 119 if contents: |
120 write(_('verified contents of %d revisions of %d largefiles\n') | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
121 % (numrevs, numlfiles)) |
15168 | 122 else: |
123 write(_('verified existence of %d revisions of %d largefiles\n') | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
124 % (numrevs, numlfiles)) |
15168 | 125 |
126 return int(failed) | |
127 | |
128 def _getfile(self, tmpfile, filename, hash): | |
129 '''Fetch one revision of one file from the store and write it | |
130 to tmpfile. Compute the hash of the file on-the-fly as it | |
131 downloads and return the binary hash. Close tmpfile. Raise | |
132 StoreError if unable to download the file (e.g. it does not | |
133 exist in the store).''' | |
134 raise NotImplementedError('abstract method') | |
135 | |
136 def _verifyfile(self, cctx, cset, contents, standin, verified): | |
137 '''Perform the actual verification of a file in the store. | |
138 ''' | |
139 raise NotImplementedError('abstract method') | |
140 | |
141 import localstore, wirestore | |
142 | |
143 _storeprovider = { | |
144 'file': [localstore.localstore], | |
145 'http': [wirestore.wirestore], | |
146 'https': [wirestore.wirestore], | |
147 'ssh': [wirestore.wirestore], | |
148 } | |
149 | |
150 _scheme_re = re.compile(r'^([a-zA-Z0-9+-.]+)://') | |
151 | |
152 # During clone this function is passed the src's ui object | |
153 # but it needs the dest's ui object so it can read out of | |
154 # the config file. Use repo.ui instead. | |
155 def _openstore(repo, remote=None, put=False): | |
156 ui = repo.ui | |
157 | |
158 if not remote: | |
15943
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
159 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
|
160 if lfpullsource: |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
161 path = ui.expandpath(lfpullsource) |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
162 else: |
f9efb325ea32
largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents:
15319
diff
changeset
|
163 path = ui.expandpath('default-push', 'default') |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
164 |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
165 # ui.expandpath() leaves 'default-push' and 'default' alone if |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
166 # they cannot be expanded: fallback to the empty string, |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15169
diff
changeset
|
167 # meaning the current directory. |
15168 | 168 if path == 'default-push' or path == 'default': |
169 path = '' | |
170 remote = repo | |
171 else: | |
172 remote = hg.peer(repo, {}, path) | |
173 | |
174 # The path could be a scheme so use Mercurial's normal functionality | |
175 # 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
|
176 path = util.safehasattr(remote, 'url') and remote.url() or remote.path |
15168 | 177 |
178 match = _scheme_re.match(path) | |
179 if not match: # regular filesystem path | |
180 scheme = 'file' | |
181 else: | |
182 scheme = match.group(1) | |
183 | |
184 try: | |
185 storeproviders = _storeprovider[scheme] | |
186 except KeyError: | |
187 raise util.Abort(_('unsupported URL scheme %r') % scheme) | |
188 | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
189 for classobj in storeproviders: |
15168 | 190 try: |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16154
diff
changeset
|
191 return classobj(ui, repo, remote) |
15168 | 192 except lfutil.storeprotonotcapable: |
193 pass | |
194 | |
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
|
195 raise util.Abort(_('%s does not appear to be a largefile store') % path) |