Merge branch 'tweak/support-logs-zip-comment' into 'dev'

Add Zip comments to support archives

See merge request crafty-controller/crafty-4!438
This commit is contained in:
Iain Powrie 2022-08-27 19:15:49 +00:00
commit ee71e48035
3 changed files with 30 additions and 12 deletions

View File

@ -11,6 +11,7 @@
### Tweaks ### Tweaks
- Make imports threaded ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433)) - Make imports threaded ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433))
- Add 'Created By' Field to servers ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/434)) - Add 'Created By' Field to servers ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/434))
- Add Zip comments to support archives ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/438))
### Lang ### Lang
TBD TBD
<br><br> <br><br>

View File

@ -70,10 +70,13 @@ class FileHelpers:
FileHelpers.del_file(src_path) FileHelpers.del_file(src_path)
@staticmethod @staticmethod
def make_archive(path_to_destination, path_to_zip): def make_archive(path_to_destination, path_to_zip, comment=""):
# create a ZipFile object # create a ZipFile object
path_to_destination += ".zip" path_to_destination += ".zip"
with ZipFile(path_to_destination, "w") as zip_file: with ZipFile(path_to_destination, "w") as zip_file:
zip_file.comment = bytes(
comment, "utf-8"
) # comments over 65535 bytes will be truncated
for root, _dirs, files in os.walk(path_to_zip, topdown=True): for root, _dirs, files in os.walk(path_to_zip, topdown=True):
ziproot = path_to_zip ziproot = path_to_zip
for file in files: for file in files:
@ -98,10 +101,13 @@ class FileHelpers:
return True return True
@staticmethod @staticmethod
def make_compressed_archive(path_to_destination, path_to_zip): def make_compressed_archive(path_to_destination, path_to_zip, comment=""):
# create a ZipFile object # create a ZipFile object
path_to_destination += ".zip" path_to_destination += ".zip"
with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file: with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file:
zip_file.comment = bytes(
comment, "utf-8"
) # comments over 65535 bytes will be truncated
for root, _dirs, files in os.walk(path_to_zip, topdown=True): for root, _dirs, files in os.walk(path_to_zip, topdown=True):
ziproot = path_to_zip ziproot = path_to_zip
for file in files: for file in files:
@ -127,7 +133,7 @@ class FileHelpers:
return True return True
def make_compressed_backup( def make_compressed_backup(
self, path_to_destination, path_to_zip, excluded_dirs, server_id self, path_to_destination, path_to_zip, excluded_dirs, server_id, comment=""
): ):
# create a ZipFile object # create a ZipFile object
path_to_destination += ".zip" path_to_destination += ".zip"
@ -145,6 +151,9 @@ class FileHelpers:
results, results,
) )
with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file: with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file:
zip_file.comment = bytes(
comment, "utf-8"
) # comments over 65535 bytes will be truncated
for root, dirs, files in os.walk(path_to_zip, topdown=True): for root, dirs, files in os.walk(path_to_zip, topdown=True):
for l_dir in dirs: for l_dir in dirs:
if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace: if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace:
@ -189,7 +198,9 @@ class FileHelpers:
return True return True
def make_backup(self, path_to_destination, path_to_zip, excluded_dirs, server_id): def make_backup(
self, path_to_destination, path_to_zip, excluded_dirs, server_id, comment=""
):
# create a ZipFile object # create a ZipFile object
path_to_destination += ".zip" path_to_destination += ".zip"
ex_replace = [p.replace("\\", "/") for p in excluded_dirs] ex_replace = [p.replace("\\", "/") for p in excluded_dirs]
@ -206,6 +217,9 @@ class FileHelpers:
results, results,
) )
with ZipFile(path_to_destination, "w") as zip_file: with ZipFile(path_to_destination, "w") as zip_file:
zip_file.comment = bytes(
comment, "utf-8"
) # comments over 65535 bytes will be truncated
for root, dirs, files in os.walk(path_to_zip, topdown=True): for root, dirs, files in os.walk(path_to_zip, topdown=True):
for l_dir in dirs: for l_dir in dirs:
if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace: if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace:

View File

@ -1,6 +1,7 @@
import os import os
import pathlib import pathlib
from pathlib import Path from pathlib import Path
from datetime import datetime
import platform import platform
import shutil import shutil
import time import time
@ -180,18 +181,20 @@ class Controller:
) )
# Make version file .txt when we download it for support # Make version file .txt when we download it for support
# Most people have a default editor for .txt also more mobile friendly... # Most people have a default editor for .txt also more mobile friendly...
FileHelpers.copy_file( sys_info_string = (
os.path.join(self.project_root, "app", "config", "version.json"), f"Crafty v{self.helper.get_version_string()} Support Logs\n"
os.path.join(temp_dir, "crafty_sys_info.txt"), f"\n"
f"OS Info:- \n"
f"OS: {str(platform.system())}\n"
f"Version: {str(platform.release())}"
f"\n \n"
f"Log archive created on: {datetime.now()}"
) )
with open( with open(
os.path.join(temp_dir, "crafty_sys_info.txt"), "a", encoding="utf-8" os.path.join(temp_dir, "crafty_sys_info.txt"), "a", encoding="utf-8"
) as f: ) as f:
f.write("\n") f.write(sys_info_string)
f.write("OS Info:\n") FileHelpers.make_compressed_archive(temp_zip_storage, temp_dir, sys_info_string)
f.write("OS: " + str(platform.system()) + "\n")
f.write("Version: " + str(platform.release()))
FileHelpers.make_compressed_archive(temp_zip_storage, temp_dir)
if len(self.helper.websocket_helper.clients) > 0: if len(self.helper.websocket_helper.clients) > 0:
self.helper.websocket_helper.broadcast_user( self.helper.websocket_helper.broadcast_user(
exec_user["user_id"], exec_user["user_id"],