Added functions to read and write project lockfile

This commit is contained in:
Veronica K. B. Olsen
2020-02-26 22:09:37 +01:00
parent 8d1e4a2778
commit af410e0b15
3 changed files with 96 additions and 2 deletions
+5 -1
View File
@@ -20,7 +20,7 @@ from os import path, mkdir, unlink, rename
from datetime import datetime from datetime import datetime
from PyQt5.Qt import PYQT_VERSION_STR from PyQt5.Qt import PYQT_VERSION_STR
from PyQt5.QtCore import QT_VERSION_STR, QStandardPaths from PyQt5.QtCore import QT_VERSION_STR, QStandardPaths, QSysInfo
from nw.constants import nwFiles, nwUnicode from nw.constants import nwFiles, nwUnicode
from nw.common import splitVersionNumber from nw.common import splitVersionNumber
@@ -156,6 +156,10 @@ class Config:
else: else:
self.osUnknown = True self.osUnknown = True
# Other System Info
self.hostName = QSysInfo.machineHostName()
self.kernelVer = QSysInfo.kernelVersion()
# Packages # Packages
self.hasEnchant = False self.hasEnchant = False
self.hasSymSpell = False self.hasSymSpell = False
+1
View File
@@ -23,6 +23,7 @@ class nwFiles():
APP_ICON = "novelWriter.svg" APP_ICON = "novelWriter.svg"
PROJ_FILE = "nwProject.nwx" PROJ_FILE = "nwProject.nwx"
PROJ_DICT = "wordlist.txt" PROJ_DICT = "wordlist.txt"
PROJ_LOCK = "nwProject.lock"
SESS_INFO = "sessionInfo.log" SESS_INFO = "sessionInfo.log"
INDEX_FILE = "tagsIndex.json" INDEX_FILE = "tagsIndex.json"
OPTS_FILE = "guiOptions.json" OPTS_FILE = "guiOptions.json"
+90 -1
View File
@@ -40,6 +40,7 @@ class NWProject():
self.projOpened = None # The time stamp of when the project file was opened self.projOpened = None # The time stamp of when the project file was opened
self.projChanged = None # The project has unsaved changes self.projChanged = None # The project has unsaved changes
self.projAltered = None # The project has been altered this session self.projAltered = None # The project has been altered this session
self.lockedBy = None # Data on which computer has the project open
# Debug # Debug
self.handleSeed = None self.handleSeed = None
@@ -178,7 +179,7 @@ class NWProject():
return return
def openProject(self, fileName): def openProject(self, fileName, overrideLock=False):
"""Open the project file provided, or if doesn't exist, assume """Open the project file provided, or if doesn't exist, assume
it is a folder, and look for the file within it. If successful, it is a folder, and look for the file within it. If successful,
parse the XML of the file and populate the project variables and parse the XML of the file and populate the project variables and
@@ -201,6 +202,21 @@ class NWProject():
if not self._checkFolder(self.projMeta): if not self._checkFolder(self.projMeta):
return return
if overrideLock:
self._clearLockFile()
lockStatus = self._readLockFile()
if len(lockStatus) > 0:
if lockStatus[0] == "ERROR":
logger.warning("Failed to check lock file")
else:
logger.error("Project is locked, so not opening")
self.lockedBy = lockStatus
self.clearProject()
return False
else:
logger.verbose("Project is not locked")
try: try:
projectMaintenance(self) projectMaintenance(self)
except Exception as E: except Exception as E:
@@ -308,6 +324,7 @@ class NWProject():
self.setProjectChanged(False) self.setProjectChanged(False)
self.projOpened = time() self.projOpened = time()
self.projAltered = False self.projAltered = False
self._writeLockFile()
return True return True
@@ -398,14 +415,19 @@ class NWProject():
self.mainConf.updateRecentCache(self.projPath, self.projName, self.currWCount, saveTime) self.mainConf.updateRecentCache(self.projPath, self.projName, self.currWCount, saveTime)
self.mainConf.saveRecentCache() self.mainConf.saveRecentCache()
self._writeLockFile()
self.theParent.setStatus("Saved Project: %s" % self.projName) self.theParent.setStatus("Saved Project: %s" % self.projName)
self.setProjectChanged(False) self.setProjectChanged(False)
return True return True
def closeProject(self): def closeProject(self):
"""Close the current project and clear all meta data.
"""
self._appendSessionStats() self._appendSessionStats()
self._clearLockFile()
self.clearProject() self.clearProject()
self.lockedBy = None
return True return True
## ##
@@ -645,6 +667,73 @@ class NWProject():
# Internal Functions # Internal Functions
## ##
def _readLockFile(self):
"""Reads the lock file in the project folder.
"""
if self.projPath is None:
return ["ERROR"]
lockFile = path.join(self.projPath, nwFiles.PROJ_LOCK)
if not path.isfile(lockFile):
return []
try:
with open(lockFile, mode="r", encoding="utf8") as inFile:
theData = inFile.read()
theLines = theData.splitlines()
if len(theLines) == 4:
return theLines
else:
return ["ERROR"]
except Exception as e:
logger.error("Failed to read project lockfile")
logger.error(str(e))
return ["ERROR"]
return ["ERROR"]
def _writeLockFile(self):
"""Writes a lock file to the project folder.
"""
if self.projPath is None:
return False
lockFile = path.join(self.projPath, nwFiles.PROJ_LOCK)
try:
with open(lockFile, mode="w+", encoding="utf8") as outFile:
outFile.write("%s\n" % self.mainConf.hostName)
outFile.write("%s\n" % self.mainConf.osType)
outFile.write("%s\n" % self.mainConf.kernelVer)
outFile.write("%d\n" % time())
except Exception as e:
logger.error("Failed to write project lockfile")
logger.error(str(e))
return False
return True
def _clearLockFile(self):
"""Remove the lock file, if it exists.
"""
if self.projPath is None:
return False
lockFile = path.join(self.projPath, nwFiles.PROJ_LOCK)
if path.isfile(lockFile):
try:
unlink(lockFile)
return True
except Exception as e:
logger.error("Failed to remove project lockfile")
logger.error(str(e))
return False
return None
def _checkFolder(self, thePath): def _checkFolder(self, thePath):
if not path.isdir(thePath): if not path.isdir(thePath):
try: try: