Debian Codegraph @

hgext/largefiles/remotestore.py

raw
1 # Copyright 2010-2011 Fog Creek Software
2 # Copyright 2010-2011 Unity Technologies
3 #
4 # This software may be used and distributed according to the terms of the
5 # GNU General Public License version 2 or any later version.
6
7 '''remote largefile store; the base class for wirestore'''
8
9 from __future__ import annotations
10
11 from mercurial.i18n import _
12
13 from mercurial import (
14 error,
15 util,
16 )
17
18 from mercurial.utils import (
19 stringutil,
20 urlutil,
21 )
22
23 from . import (
24 basestore,
25 lfutil,
26 localstore,
27 )
28
29 urlerr = util.urlerr
30 urlreq = util.urlreq
31
32
33 class remotestore(basestore.basestore):
34 '''a largefile store accessed over a network'''
35
36 def __init__(self, ui, repo, url):
37 super().__init__(ui, repo, url)
38 self._lstore = None
39 if repo is not None:
40 self._lstore = localstore.localstore(self.ui, self.repo, self.repo)
41
42 def put(self, source, hash):
43 if self.sendfile(source, hash):
44 raise error.Abort(
45 _(b'remotestore: could not put %s to remote store %s')
46 % (source, urlutil.hidepassword(self.url))
47 )
48 self.ui.debug(
49 _(b'remotestore: put %s to remote store %s\n')
50 % (source, urlutil.hidepassword(self.url))
51 )
52
53 def exists(self, hashes):
54 return {
55 h: s == 0
56 for (h, s) in self._stat(hashes).items()
57 # dict-from-generator
58 }
59
60 def sendfile(self, filename, hash):
61 self.ui.debug(b'remotestore: sendfile(%s, %s)\n' % (filename, hash))
62 try:
63 with lfutil.httpsendfile(self.ui, filename) as fd:
64 return self._put(hash, fd)
65 except OSError as e:
66 raise error.Abort(
67 _(b'remotestore: could not open file %s: %s')
68 % (filename, stringutil.forcebytestr(e))
69 )
70
71 def _getfile(self, tmpfile, filename, hash):
72 try:
73 chunks = self._get(hash)
74 except urlerr.httperror as e:
75 # 401s get converted to error.Aborts; everything else is fine being
76 # turned into a StoreError
77 raise basestore.StoreError(
78 filename, hash, self.url, stringutil.forcebytestr(e)
79 )
80 except urlerr.urlerror as e:
81 # This usually indicates a connection problem, so don't
82 # keep trying with the other files... they will probably
83 # all fail too.
84 raise error.Abort(
85 b'%s: %s' % (urlutil.hidepassword(self.url), e.reason)
86 )
87 except OSError as e:
88 raise basestore.StoreError(
89 filename, hash, self.url, stringutil.forcebytestr(e)
90 )
91
92 return lfutil.copyandhash(chunks, tmpfile)
93
94 def _hashesavailablelocally(self, hashes):
95 existslocallymap = self._lstore.exists(hashes)
96 localhashes = [hash for hash in hashes if existslocallymap[hash]]
97 return localhashes
98
99 def _verifyfiles(self, contents, filestocheck):
100 failed = False
101 expectedhashes = [
102 expectedhash for cset, filename, expectedhash in filestocheck
103 ]
104 localhashes = self._hashesavailablelocally(expectedhashes)
105 stats = self._stat(
106 [
107 expectedhash
108 for expectedhash in expectedhashes
109 if expectedhash not in localhashes
110 ]
111 )
112
113 for cset, filename, expectedhash in filestocheck:
114 if expectedhash in localhashes:
115 filetocheck = (cset, filename, expectedhash)
116 verifyresult = self._lstore._verifyfiles(
117 contents, [filetocheck]
118 )
119 if verifyresult:
120 failed = True
121 else:
122 stat = stats[expectedhash]
123 if stat:
124 if stat == 1:
125 self.ui.warn(
126 _(b'changeset %s: %s: contents differ\n')
127 % (cset, filename)
128 )
129 failed = True
130 elif stat == 2:
131 self.ui.warn(
132 _(b'changeset %s: %s missing\n') % (cset, filename)
133 )
134 failed = True
135 else:
136 raise RuntimeError(
137 b'verify failed: unexpected response '
138 b'from statlfile (%r)' % stat
139 )
140 return failed
141
142 def _put(self, hash, fd):
143 '''Put file with the given hash in the remote store.'''
144 raise NotImplementedError(b'abstract method')
145
146 def _get(self, hash):
147 '''Get a iterator for content with the given hash.'''
148 raise NotImplementedError(b'abstract method')
149
150 def _stat(self, hashes):
151 """Get information about availability of files specified by
152 hashes in the remote store. Return dictionary mapping hashes
153 to return code where 0 means that file is available, other
154 values if not."""
155 raise NotImplementedError(b'abstract method')