forked from platformio/platformio-core
Verify downloaded package checksum with native Python's hashlib and support all OS
This commit is contained in:
@ -12,6 +12,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import hashlib
|
||||||
from email.utils import parsedate_tz
|
from email.utils import parsedate_tz
|
||||||
from math import ceil
|
from math import ceil
|
||||||
from os.path import getsize, join
|
from os.path import getsize, join
|
||||||
@ -27,7 +28,6 @@ from platformio.exception import (
|
|||||||
FDSizeMismatch,
|
FDSizeMismatch,
|
||||||
FDUnrecognizedStatusCode,
|
FDUnrecognizedStatusCode,
|
||||||
)
|
)
|
||||||
from platformio.proc import exec_command
|
|
||||||
|
|
||||||
|
|
||||||
class FileDownloader(object):
|
class FileDownloader(object):
|
||||||
@ -103,25 +103,19 @@ class FileDownloader(object):
|
|||||||
_dlsize = getsize(self._destination)
|
_dlsize = getsize(self._destination)
|
||||||
if self.get_size() != -1 and _dlsize != self.get_size():
|
if self.get_size() != -1 and _dlsize != self.get_size():
|
||||||
raise FDSizeMismatch(_dlsize, self._fname, self.get_size())
|
raise FDSizeMismatch(_dlsize, self._fname, self.get_size())
|
||||||
|
|
||||||
if not sha1:
|
if not sha1:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
dlsha1 = None
|
checksum = hashlib.sha1()
|
||||||
try:
|
with open(self._destination, "rb") as fp:
|
||||||
result = exec_command(["sha1sum", self._destination])
|
while True:
|
||||||
dlsha1 = result["out"]
|
chunk = fp.read(1024 * 64)
|
||||||
except (OSError, ValueError):
|
if not chunk:
|
||||||
try:
|
break
|
||||||
result = exec_command(["shasum", "-a", "1", self._destination])
|
checksum.update(chunk)
|
||||||
dlsha1 = result["out"]
|
|
||||||
except (OSError, ValueError):
|
if sha1.lower() != checksum.hexdigest().lower():
|
||||||
pass
|
raise FDSHASumMismatch(checksum.hexdigest(), self._fname, sha1)
|
||||||
if not dlsha1:
|
|
||||||
return None
|
|
||||||
dlsha1 = dlsha1[1:41] if dlsha1.startswith("\\") else dlsha1[:40]
|
|
||||||
if sha1.lower() != dlsha1.lower():
|
|
||||||
raise FDSHASumMismatch(dlsha1, self._fname, sha1)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _preserve_filemtime(self, lmdate):
|
def _preserve_filemtime(self, lmdate):
|
||||||
|
Reference in New Issue
Block a user