mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Further improve unzip_file per per sonarcloud.
Pull out file/folder move logic into a new function. Remove the number of indenting and number of exceptions.
This commit is contained in:
parent
e23d7435c1
commit
85b930d67a
@ -394,7 +394,24 @@ class FileHelpers:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unzip_file(zip_path, server_update: bool = False) -> None:
|
def move_item_file_or_dir(old_dir, new_dir, item) -> None:
|
||||||
|
try:
|
||||||
|
if os.path.isdir(os.path.join(old_dir, item)):
|
||||||
|
FileHelpers.move_dir_exist(
|
||||||
|
os.path.join(old_dir, item),
|
||||||
|
os.path.join(new_dir, item),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
FileHelpers.move_file(
|
||||||
|
os.path.join(old_dir, item),
|
||||||
|
os.path.join(new_dir, item),
|
||||||
|
)
|
||||||
|
except shutil.Error as why:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Error moving {old_dir} to {new_dir} with information: {why}"
|
||||||
|
) from why
|
||||||
|
|
||||||
|
def unzip_file(self, zip_path, server_update: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
Unzips zip file at zip_path to location generated at new_dir based on zip
|
Unzips zip file at zip_path to location generated at new_dir based on zip
|
||||||
contents.
|
contents.
|
||||||
@ -423,15 +440,9 @@ class FileHelpers:
|
|||||||
# we'll extract this to the temp dir using zipfile module
|
# we'll extract this to the temp dir using zipfile module
|
||||||
zip_ref.extractall(temp_dir)
|
zip_ref.extractall(temp_dir)
|
||||||
# Catch zipfile extract all error or file open errors.
|
# Catch zipfile extract all error or file open errors.
|
||||||
except ValueError as why:
|
except (ValueError, FileNotFoundError, PermissionError) as why:
|
||||||
Console.error(f"Unzip failed with information: {why}")
|
Console.error(f"Unzip failed with information: {why}")
|
||||||
raise RuntimeError(f"Unzip failed for path: {zip_path}") from why
|
raise RuntimeError(f"Unzip failed for path: {zip_path}") from why
|
||||||
except FileNotFoundError as why:
|
|
||||||
Console.error(f"Unzip failed file not found: {zip_path}")
|
|
||||||
raise FileNotFoundError(f"Unable to find file at path: {zip_path}") from why
|
|
||||||
except PermissionError as why:
|
|
||||||
Console.error(f"Bad permissions for file at: {zip_path}")
|
|
||||||
raise PermissionError(f"Bad permissions for file at: {zip_path}") from why
|
|
||||||
|
|
||||||
# we'll iterate through the top level directory moving everything
|
# we'll iterate through the top level directory moving everything
|
||||||
# out of the temp directory and into it's final home.
|
# out of the temp directory and into it's final home.
|
||||||
@ -441,24 +452,13 @@ class FileHelpers:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# we handle files and dirs differently or we'll crash out.
|
# we handle files and dirs differently or we'll crash out.
|
||||||
if os.path.isdir(os.path.join(temp_dir, item)):
|
|
||||||
try:
|
try:
|
||||||
FileHelpers.move_dir_exist(
|
self.move_item_file_or_dir(temp_dir, new_dir, item)
|
||||||
os.path.join(temp_dir, item),
|
|
||||||
os.path.join(new_dir, item),
|
|
||||||
)
|
|
||||||
except shutil.Error as ex:
|
|
||||||
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
FileHelpers.move_file(
|
|
||||||
os.path.join(temp_dir, item),
|
|
||||||
os.path.join(new_dir, item),
|
|
||||||
)
|
|
||||||
except shutil.Error as ex:
|
except shutil.Error as ex:
|
||||||
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
||||||
|
|
||||||
def unzip_server(self, zip_path, user_id):
|
@staticmethod
|
||||||
|
def unzip_server(zip_path, user_id):
|
||||||
if Helpers.check_file_perms(zip_path):
|
if Helpers.check_file_perms(zip_path):
|
||||||
temp_dir = tempfile.mkdtemp()
|
temp_dir = tempfile.mkdtemp()
|
||||||
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
||||||
|
Loading…
Reference in New Issue
Block a user