Skip to content

Commit

Permalink
- Print debug info during backup/restore process
Browse files Browse the repository at this point in the history
  • Loading branch information
DJSchaffner committed Sep 23, 2020
1 parent 9569405 commit 927f174
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(self):
self.patch_list = self.webhook.query_patch_list(self.app_id)
self.depot_list = self.__get_depot_list()
self.patch_change_list = self.webhook.query_patch_change_list()

self.process_queue = Queue()

def patch(self, username: str, password: str, patch: dict, language: Languages):
Expand Down Expand Up @@ -129,7 +128,7 @@ def restore(self):
# Remove added files from the path
try:
print("Removing patched files")
utils.remove_patched_files(self.game_dir, self.download_dir)
utils.remove_patched_files(self.game_dir, self.download_dir, True)
print("Finished removing patched files")

# Copy backed up files to game path again
Expand Down Expand Up @@ -223,7 +222,7 @@ def __backup(self):
shutil.rmtree(self.backup_dir.absolute())
self.backup_dir.mkdir()

utils.backup_files(self.game_dir, self.download_dir, self.backup_dir)
utils.backup_files(self.game_dir, self.download_dir, self.backup_dir, True)

return True
except BaseException:
Expand Down
22 changes: 18 additions & 4 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,54 @@ def remove_file_or_dir(dir, file):
else:
(dir / file).unlink(missing_ok=True)

def backup_files(original_dir: pathlib.Path, override_dir: pathlib.Path, backup_dir: pathlib.Path):
def backup_files(original_dir: pathlib.Path, override_dir: pathlib.Path, backup_dir: pathlib.Path, debug_info: bool):
"""Recursively performs backup of original_dir to backup_dir assuming all files/folder from override_dir will be patched."""
changed_file_list = list(set(os.listdir(original_dir.absolute())).intersection(set(os.listdir(override_dir.absolute()))))

for file in changed_file_list:
# Its a folder, backup its contents
if (original_dir / file).is_dir():
(backup_dir / file).mkdir()
backup_files(original_dir / file, override_dir / file, backup_dir / file)
backup_files(original_dir / file, override_dir / file, backup_dir / file, debug_info)
# Its a file, copy it
else:
if debug_info:
print(f"Copying {(original_dir / file).absolute()}")

copy_file_or_dir(original_dir, backup_dir, file)

def remove_patched_files(original_dir: pathlib.Path, override_dir: pathlib.Path):
def remove_patched_files(original_dir: pathlib.Path, override_dir: pathlib.Path, debug_info: bool):
"""Recursively removes all patched files assuming original_dir has been patched with all files from override_dir."""
changed_file_list = os.listdir(override_dir.absolute())

# Remove all overridden files
for file in changed_file_list:
# Its a folder, remove its contents
if (original_dir / file).is_dir():
remove_patched_files(original_dir / file, override_dir / file)
remove_patched_files(original_dir / file, override_dir / file, debug_info)

# Remove directory if its now empty
if len(os.listdir((original_dir / file).absolute())) == 0:
try:
if debug_info:
print(f"Removing {(original_dir / file).absolute()}")

remove_file_or_dir(original_dir, file)
except BaseException as e:
raise e
# Its a file, remove it
else:
try:
if debug_info:
print(f"Removing {(original_dir / file).absolute()}")

remove_file_or_dir(original_dir, file)
except BaseException as e:
raise e

def extract_date(date_string: str):
"""Extract a date in the format of 'd(d) Monthname yyyy'.
Returns a datetime object
"""
date_stripped = re.search(r"\d+ \w* \d+", date_string).group(0)
Expand Down

0 comments on commit 927f174

Please sign in to comment.