Fix ignored files for Windows

This commit is contained in:
ChrisTerBeke 2018-05-17 15:07:28 +02:00
parent aa07de45ed
commit 7b3f334678

View File

@ -1,8 +1,9 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import fnmatch
import io import io
import os import os
import re
import shutil import shutil
from typing import Optional from typing import Optional
@ -23,7 +24,7 @@ class Backup:
""" """
# These files should be ignored when making a backup. # These files should be ignored when making a backup.
IGNORED_FILES = {"cura.log", "cache", "(?s).qmlc"} IGNORED_FILES = [r"cura\.log", r"plugins\.json", r"cache", r"__pycache__", r"\.qmlc", r"\.pyc"]
# Re-use translation catalog. # Re-use translation catalog.
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
@ -80,25 +81,15 @@ class Backup:
:param root_path: The root directory to archive recursively. :param root_path: The root directory to archive recursively.
:return: The archive as bytes. :return: The archive as bytes.
""" """
contents = os.walk(root_path) ignore_string = re.compile("|".join(self.IGNORED_FILES))
try: try:
archive = ZipFile(buffer, "w", ZIP_DEFLATED) archive = ZipFile(buffer, "w", ZIP_DEFLATED)
for root, folders, files in contents: for root, folders, files in os.walk(root_path):
for folder_name in folders: for item_name in folders + files:
for ignore_rule in self.IGNORED_FILES: absolute_path = os.path.join(root, item_name)
if fnmatch.fnmatch(ignore_rule, folder_name): if ignore_string.search(absolute_path):
continue continue
absolute_path = os.path.join(root, folder_name) archive.write(absolute_path, absolute_path[len(root_path) + len(os.sep):])
relative_path = absolute_path[len(root_path) + len(os.sep):]
archive.write(absolute_path, relative_path)
for file_name in files:
for ignore_rule in self.IGNORED_FILES:
if fnmatch.fnmatch(ignore_rule, file_name):
print("FNMATCH=====", ignore_rule, file_name)
continue
absolute_path = os.path.join(root, file_name)
relative_path = absolute_path[len(root_path) + len(os.sep):]
archive.write(absolute_path, relative_path)
archive.close() archive.close()
return archive return archive
except (IOError, OSError, BadZipfile) as error: except (IOError, OSError, BadZipfile) as error:
@ -141,7 +132,8 @@ class Backup:
return extracted return extracted
def _extractArchive(self, archive: "ZipFile", target_path: str) -> bool: @staticmethod
def _extractArchive(archive: "ZipFile", target_path: str) -> bool:
""" """
Extract the whole archive to the given target path. Extract the whole archive to the given target path.
:param archive: The archive as ZipFile. :param archive: The archive as ZipFile.