mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Implement fs.calculate_file_hashsum
This commit is contained in:
@ -12,7 +12,6 @@
|
|||||||
# 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
|
|
||||||
import io
|
import io
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
@ -23,7 +22,7 @@ from time import mktime
|
|||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from platformio import app, util
|
from platformio import app, fs, util
|
||||||
from platformio.exception import (
|
from platformio.exception import (
|
||||||
FDSHASumMismatch,
|
FDSHASumMismatch,
|
||||||
FDSizeMismatch,
|
FDSizeMismatch,
|
||||||
@ -103,17 +102,9 @@ class FileDownloader(object):
|
|||||||
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
|
||||||
|
checksum = fs.calculate_file_hashsum("sha1", self._destination)
|
||||||
checksum = hashlib.sha1()
|
if sha1.lower() != checksum.lower():
|
||||||
with io.open(self._destination, "rb", buffering=0) as fp:
|
raise FDSHASumMismatch(checksum, self._fname, sha1)
|
||||||
while True:
|
|
||||||
chunk = fp.read(io.DEFAULT_BUFFER_SIZE)
|
|
||||||
if not chunk:
|
|
||||||
break
|
|
||||||
checksum.update(chunk)
|
|
||||||
|
|
||||||
if sha1.lower() != checksum.hexdigest().lower():
|
|
||||||
raise FDSHASumMismatch(checksum.hexdigest(), self._fname, sha1)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _preserve_filemtime(self, lmdate):
|
def _preserve_filemtime(self, lmdate):
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# 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
|
||||||
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -72,6 +74,17 @@ def format_filesize(filesize):
|
|||||||
return "%d%sB" % ((base * filesize / unit), suffix)
|
return "%d%sB" % ((base * filesize / unit), suffix)
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_file_hashsum(algorithm, path):
|
||||||
|
h = hashlib.new(algorithm)
|
||||||
|
with io.open(path, "rb", buffering=0) as fp:
|
||||||
|
while True:
|
||||||
|
chunk = fp.read(io.DEFAULT_BUFFER_SIZE)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
h.update(chunk)
|
||||||
|
return h.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def ensure_udev_rules():
|
def ensure_udev_rules():
|
||||||
from platformio.util import get_systype # pylint: disable=import-outside-toplevel
|
from platformio.util import get_systype # pylint: disable=import-outside-toplevel
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user