Moved the backup function to the project class since it was only one function anyway

This commit is contained in:
Veronica K. B. Olsen
2020-05-08 18:19:09 +02:00
parent a39b28332a
commit 3d910041cd
5 changed files with 75 additions and 111 deletions
+68
View File
@@ -38,6 +38,7 @@ from lxml import etree
from hashlib import sha256
from datetime import datetime
from time import time
from shutil import make_archive
from nw.tools import projectMaintenance, OptionState
from nw.common import checkString, checkBool, checkInt
@@ -478,6 +479,73 @@ class NWProject():
self.lockedBy = None
return True
##
# Backup Project
##
def zipIt(self, doNotify):
"""Create a zip file of the entire project.
"""
logger.info("Backing up project")
self.theParent.statusBar.setStatus("Backing up project ...")
if self.mainConf.backupPath is None or self.mainConf.backupPath == "":
self.theParent.makeAlert((
"Cannot backup project because no backup path is set. "
"Please set a valid backup location in Tools > Preferences."
), nwAlert.WARN)
return False
if self.projName is None or self.projName == "":
self.theParent.makeAlert((
"Cannot backup project because no project name is set. "
"Please set a Working Title in Project > Project Settings."
), nwAlert.WARN)
return False
if not path.isdir(self.mainConf.backupPath):
self.theParent.makeAlert((
"Cannot backup project because the backup path does not exist. "
"Please set a valid backup location in Tools > Preferences."
), nwAlert.WARN)
return False
cleanName = ""
for c in self.projName.strip():
if c.isalpha() or c.isdigit() or c == " ":
cleanName += c
baseDir = path.join(self.mainConf.backupPath, cleanName)
if not path.isdir(baseDir):
mkdir(baseDir)
logger.debug("Created folder %s" % baseDir)
archName = "Backup on %s" % datetime.now().strftime("%Y-%m-%d at %H.%M.%S")
baseName = path.join(baseDir, archName)
try:
self._clearLockFile()
make_archive(baseName, "zip", self.projPath, ".")
self._writeLockFile()
if doNotify:
self.theParent.makeAlert(
"Backup archive file written to: '%s.zip'" % path.join(cleanName, archName),
nwAlert.INFO
)
else:
logger.info("Backup written to: %s" % archName)
except Exception as e:
self.theParent.makeAlert(
["Could not write backup archive.",str(e)],
nwAlert.ERROR
)
return False
self.theParent.statusBar.setStatus("Project backed up to '%s.zip'" % baseName)
return True
##
# Setters
##