Rework logic

This commit is contained in:
amcmanu3 2023-02-25 16:15:50 -05:00
parent b766ae10e9
commit 1dc731c9f7
2 changed files with 27 additions and 41 deletions

View File

@ -283,7 +283,8 @@ class FileHelpers:
return True return True
@staticmethod @staticmethod
def unzip_file(zip_path, single_item=False): def unzip_file(zip_path, server_update=False):
ignored_names = ["server.properties", "permissions.json", "allowlist.json"]
# Get directory without zipfile name # Get directory without zipfile name
new_dir = pathlib.Path(zip_path).parents[0] new_dir = pathlib.Path(zip_path).parents[0]
# make sure we're able to access the zip file # make sure we're able to access the zip file
@ -296,14 +297,12 @@ class FileHelpers:
with zipfile.ZipFile(zip_path, "r") as zip_ref: with zipfile.ZipFile(zip_path, "r") as zip_ref:
# 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)
# we'll check if the single item parameter was passed
# if it was the developer only wants one file back
# probably for a server executable update.
if not single_item:
# 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.
for item in os.listdir(temp_dir): for item in os.listdir(temp_dir):
# if the file is one of our ignored names we'll skip it
if item in ignored_names and server_update:
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)): if os.path.isdir(os.path.join(temp_dir, item)):
try: try:
@ -314,6 +313,7 @@ class FileHelpers:
except Exception as ex: except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}") logger.error(f"ERROR IN ZIP IMPORT: {ex}")
else: else:
try: try:
FileHelpers.move_file( FileHelpers.move_file(
os.path.join(temp_dir, item), os.path.join(temp_dir, item),
@ -321,18 +321,6 @@ class FileHelpers:
) )
except Exception as ex: except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}") logger.error(f"ERROR IN ZIP IMPORT: {ex}")
else:
# if there is a single item the correct file name should be passed
# we'll just try to move that one file based on the name provided
# then we'll move along.
try:
FileHelpers.move_file(
os.path.join(temp_dir, single_item),
os.path.join(new_dir, single_item),
)
except FileNotFoundError:
logger.error("Could not unpack single file in bedrock update.")
return "false"
except Exception as ex: except Exception as ex:
Console.error(ex) Console.error(ex)
else: else:

View File

@ -1320,10 +1320,7 @@ class ServerInstance:
unzip_path = os.path.join(self.settings["path"], "bedrock_server.zip") unzip_path = os.path.join(self.settings["path"], "bedrock_server.zip")
unzip_path = self.helper.wtol_path(unzip_path) unzip_path = self.helper.wtol_path(unzip_path)
# unzips archive that was downloaded. # unzips archive that was downloaded.
if self.helper.is_os_windows(): FileHelpers.unzip_file(unzip_path, server_update=True)
FileHelpers.unzip_file(unzip_path, "bedrock_server.exe")
else:
FileHelpers.unzip_file(unzip_path, "bedrock_server")
# adjusts permissions for execution if os is not windows # adjusts permissions for execution if os is not windows
if not self.helper.is_os_windows(): if not self.helper.is_os_windows():
os.chmod( os.chmod(
@ -1337,6 +1334,7 @@ class ServerInstance:
logger.critical( logger.critical(
f"Failed to download bedrock executable for update \n{e}" f"Failed to download bedrock executable for update \n{e}"
) )
downloaded = False
if downloaded: if downloaded:
logger.info("Executable updated successfully. Starting Server") logger.info("Executable updated successfully. Starting Server")