Mercurial > hg
comparison hgext/largefiles/remotestore.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | f0b6fbea00cf |
children | 687b865b95ad |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
12 from mercurial import ( | 12 from mercurial import ( |
13 error, | 13 error, |
14 util, | 14 util, |
15 ) | 15 ) |
16 | 16 |
17 from mercurial.utils import ( | 17 from mercurial.utils import stringutil |
18 stringutil, | |
19 ) | |
20 | 18 |
21 from . import ( | 19 from . import ( |
22 basestore, | 20 basestore, |
23 lfutil, | 21 lfutil, |
24 localstore, | 22 localstore, |
25 ) | 23 ) |
26 | 24 |
27 urlerr = util.urlerr | 25 urlerr = util.urlerr |
28 urlreq = util.urlreq | 26 urlreq = util.urlreq |
29 | 27 |
28 | |
30 class remotestore(basestore.basestore): | 29 class remotestore(basestore.basestore): |
31 '''a largefile store accessed over a network''' | 30 '''a largefile store accessed over a network''' |
31 | |
32 def __init__(self, ui, repo, url): | 32 def __init__(self, ui, repo, url): |
33 super(remotestore, self).__init__(ui, repo, url) | 33 super(remotestore, self).__init__(ui, repo, url) |
34 self._lstore = None | 34 self._lstore = None |
35 if repo is not None: | 35 if repo is not None: |
36 self._lstore = localstore.localstore(self.ui, self.repo, self.repo) | 36 self._lstore = localstore.localstore(self.ui, self.repo, self.repo) |
37 | 37 |
38 def put(self, source, hash): | 38 def put(self, source, hash): |
39 if self.sendfile(source, hash): | 39 if self.sendfile(source, hash): |
40 raise error.Abort( | 40 raise error.Abort( |
41 _('remotestore: could not put %s to remote store %s') | 41 _('remotestore: could not put %s to remote store %s') |
42 % (source, util.hidepassword(self.url))) | 42 % (source, util.hidepassword(self.url)) |
43 ) | |
43 self.ui.debug( | 44 self.ui.debug( |
44 _('remotestore: put %s to remote store %s\n') | 45 _('remotestore: put %s to remote store %s\n') |
45 % (source, util.hidepassword(self.url))) | 46 % (source, util.hidepassword(self.url)) |
47 ) | |
46 | 48 |
47 def exists(self, hashes): | 49 def exists(self, hashes): |
48 return dict((h, s == 0) for (h, s) in # dict-from-generator | 50 return dict( |
49 self._stat(hashes).iteritems()) | 51 (h, s == 0) |
52 for (h, s) in self._stat(hashes).iteritems() # dict-from-generator | |
53 ) | |
50 | 54 |
51 def sendfile(self, filename, hash): | 55 def sendfile(self, filename, hash): |
52 self.ui.debug('remotestore: sendfile(%s, %s)\n' % (filename, hash)) | 56 self.ui.debug('remotestore: sendfile(%s, %s)\n' % (filename, hash)) |
53 try: | 57 try: |
54 with lfutil.httpsendfile(self.ui, filename) as fd: | 58 with lfutil.httpsendfile(self.ui, filename) as fd: |
55 return self._put(hash, fd) | 59 return self._put(hash, fd) |
56 except IOError as e: | 60 except IOError as e: |
57 raise error.Abort( | 61 raise error.Abort( |
58 _('remotestore: could not open file %s: %s') | 62 _('remotestore: could not open file %s: %s') |
59 % (filename, stringutil.forcebytestr(e))) | 63 % (filename, stringutil.forcebytestr(e)) |
64 ) | |
60 | 65 |
61 def _getfile(self, tmpfile, filename, hash): | 66 def _getfile(self, tmpfile, filename, hash): |
62 try: | 67 try: |
63 chunks = self._get(hash) | 68 chunks = self._get(hash) |
64 except urlerr.httperror as e: | 69 except urlerr.httperror as e: |
65 # 401s get converted to error.Aborts; everything else is fine being | 70 # 401s get converted to error.Aborts; everything else is fine being |
66 # turned into a StoreError | 71 # turned into a StoreError |
67 raise basestore.StoreError(filename, hash, self.url, | 72 raise basestore.StoreError( |
68 stringutil.forcebytestr(e)) | 73 filename, hash, self.url, stringutil.forcebytestr(e) |
74 ) | |
69 except urlerr.urlerror as e: | 75 except urlerr.urlerror as e: |
70 # This usually indicates a connection problem, so don't | 76 # This usually indicates a connection problem, so don't |
71 # keep trying with the other files... they will probably | 77 # keep trying with the other files... they will probably |
72 # all fail too. | 78 # all fail too. |
73 raise error.Abort('%s: %s' % | 79 raise error.Abort( |
74 (util.hidepassword(self.url), e.reason)) | 80 '%s: %s' % (util.hidepassword(self.url), e.reason) |
81 ) | |
75 except IOError as e: | 82 except IOError as e: |
76 raise basestore.StoreError(filename, hash, self.url, | 83 raise basestore.StoreError( |
77 stringutil.forcebytestr(e)) | 84 filename, hash, self.url, stringutil.forcebytestr(e) |
85 ) | |
78 | 86 |
79 return lfutil.copyandhash(chunks, tmpfile) | 87 return lfutil.copyandhash(chunks, tmpfile) |
80 | 88 |
81 def _hashesavailablelocally(self, hashes): | 89 def _hashesavailablelocally(self, hashes): |
82 existslocallymap = self._lstore.exists(hashes) | 90 existslocallymap = self._lstore.exists(hashes) |
83 localhashes = [hash for hash in hashes if existslocallymap[hash]] | 91 localhashes = [hash for hash in hashes if existslocallymap[hash]] |
84 return localhashes | 92 return localhashes |
85 | 93 |
86 def _verifyfiles(self, contents, filestocheck): | 94 def _verifyfiles(self, contents, filestocheck): |
87 failed = False | 95 failed = False |
88 expectedhashes = [expectedhash | 96 expectedhashes = [ |
89 for cset, filename, expectedhash in filestocheck] | 97 expectedhash for cset, filename, expectedhash in filestocheck |
98 ] | |
90 localhashes = self._hashesavailablelocally(expectedhashes) | 99 localhashes = self._hashesavailablelocally(expectedhashes) |
91 stats = self._stat([expectedhash for expectedhash in expectedhashes | 100 stats = self._stat( |
92 if expectedhash not in localhashes]) | 101 [ |
102 expectedhash | |
103 for expectedhash in expectedhashes | |
104 if expectedhash not in localhashes | |
105 ] | |
106 ) | |
93 | 107 |
94 for cset, filename, expectedhash in filestocheck: | 108 for cset, filename, expectedhash in filestocheck: |
95 if expectedhash in localhashes: | 109 if expectedhash in localhashes: |
96 filetocheck = (cset, filename, expectedhash) | 110 filetocheck = (cset, filename, expectedhash) |
97 verifyresult = self._lstore._verifyfiles(contents, | 111 verifyresult = self._lstore._verifyfiles( |
98 [filetocheck]) | 112 contents, [filetocheck] |
113 ) | |
99 if verifyresult: | 114 if verifyresult: |
100 failed = True | 115 failed = True |
101 else: | 116 else: |
102 stat = stats[expectedhash] | 117 stat = stats[expectedhash] |
103 if stat: | 118 if stat: |
104 if stat == 1: | 119 if stat == 1: |
105 self.ui.warn( | 120 self.ui.warn( |
106 _('changeset %s: %s: contents differ\n') | 121 _('changeset %s: %s: contents differ\n') |
107 % (cset, filename)) | 122 % (cset, filename) |
123 ) | |
108 failed = True | 124 failed = True |
109 elif stat == 2: | 125 elif stat == 2: |
110 self.ui.warn( | 126 self.ui.warn( |
111 _('changeset %s: %s missing\n') | 127 _('changeset %s: %s missing\n') % (cset, filename) |
112 % (cset, filename)) | 128 ) |
113 failed = True | 129 failed = True |
114 else: | 130 else: |
115 raise RuntimeError('verify failed: unexpected response ' | 131 raise RuntimeError( |
116 'from statlfile (%r)' % stat) | 132 'verify failed: unexpected response ' |
133 'from statlfile (%r)' % stat | |
134 ) | |
117 return failed | 135 return failed |
118 | 136 |
119 def _put(self, hash, fd): | 137 def _put(self, hash, fd): |
120 '''Put file with the given hash in the remote store.''' | 138 '''Put file with the given hash in the remote store.''' |
121 raise NotImplementedError('abstract method') | 139 raise NotImplementedError('abstract method') |