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,43 +297,30 @@ 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 iterate through the top level directory moving everything
# out of the temp directory and into it's final home.
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.
if os.path.isdir(os.path.join(temp_dir, item)):
try:
FileHelpers.move_dir_exist(
os.path.join(temp_dir, item),
os.path.join(new_dir, item),
)
except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
else:
# we'll check if the single item parameter was passed try:
# if it was the developer only wants one file back FileHelpers.move_file(
# probably for a server executable update. os.path.join(temp_dir, item),
if not single_item: os.path.join(new_dir, item),
# we'll iterate through the top level directory moving everything )
# out of the temp directory and into it's final home. except Exception as ex:
for item in os.listdir(temp_dir): logger.error(f"ERROR IN ZIP IMPORT: {ex}")
# we handle files and dirs differently or we'll crash out.
if os.path.isdir(os.path.join(temp_dir, item)):
try:
FileHelpers.move_dir_exist(
os.path.join(temp_dir, item),
os.path.join(new_dir, item),
)
except Exception 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 Exception as 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")