version repacker command

This commit is contained in:
Ondrej Samohel 2021-11-16 18:34:14 +01:00 committed by Ondřej Samohel
parent 0a3588e761
commit cc580b07f3
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
5 changed files with 174 additions and 67 deletions

View file

@ -10,6 +10,7 @@ import tempfile
from pathlib import Path
from typing import Union, Callable, List, Tuple
import hashlib
import platform
from zipfile import ZipFile, BadZipFile
@ -196,21 +197,21 @@ class OpenPypeVersion(semver.VersionInfo):
return str(self.finalize_version())
@staticmethod
def version_in_str(string: str) -> Tuple:
def version_in_str(string: str) -> Union[None, OpenPypeVersion]:
"""Find OpenPype version in given string.
Args:
string (str): string to search.
Returns:
tuple: True/False and OpenPypeVersion if found.
OpenPypeVersion: of detected or None.
"""
m = re.search(OpenPypeVersion._VERSION_REGEX, string)
if not m:
return False, None
return None
version = OpenPypeVersion.parse(string[m.start():m.end()])
return True, version
return version
@classmethod
def parse(cls, version):
@ -531,6 +532,7 @@ class BootstrapRepos:
processed_path = file
self._print(f"- processing {processed_path}")
checksums.append(
(
sha256sum(file.as_posix()),
@ -542,7 +544,10 @@ class BootstrapRepos:
checksums_str = ""
for c in checksums:
checksums_str += "{}:{}\n".format(c[0], c[1])
file_str = c[1]
if platform.system().lower() == "windows":
file_str = c[1].as_posix().replace("\\", "/")
checksums_str += "{}:{}\n".format(c[0], file_str)
zip_file.writestr("checksums", checksums_str)
# test if zip is ok
zip_file.testzip()
@ -589,13 +594,16 @@ class BootstrapRepos:
# calculate and compare checksums in the zip file
for file in checksums:
file_name = file[1]
if platform.system().lower() == "windows":
file_name = file_name.replace("/", "\\")
h = hashlib.sha256()
try:
h.update(zip_file.read(file[1]))
h.update(zip_file.read(file_name))
except FileNotFoundError:
return False, f"Missing file [ {file[1]} ]"
return False, f"Missing file [ {file_name} ]"
if h.hexdigest() != file[0]:
return False, f"Invalid checksum on {file[1]}"
return False, f"Invalid checksum on {file_name}"
# get list of files in zip minus `checksums` file itself
# and turn in to set to compare against list of files
@ -631,13 +639,16 @@ class BootstrapRepos:
files_in_checksum = set([file[1] for file in checksums])
for file in checksums:
file_name = file[1]
if platform.system().lower() == "windows":
file_name = file_name.replace("/", "\\")
try:
current = sha256sum((path / file[1]).as_posix())
current = sha256sum((path / file_name).as_posix())
except FileNotFoundError:
return False, f"Missing file [ {file[1]} ]"
return False, f"Missing file [ {file_name} ]"
if file[0] != current:
return False, f"Invalid checksum on {file[1]}"
return False, f"Invalid checksum on {file_name}"
diff = files_in_dir.difference(files_in_checksum)
if diff:
return False, f"Missing files {diff}"
@ -1163,7 +1174,7 @@ class BootstrapRepos:
if result[0]:
detected_version: OpenPypeVersion
detected_version = result[1]
detected_version = result
if item.is_dir() and not self._is_openpype_in_dir(
item, detected_version