Clean up and refactor package script

This commit is contained in:
Veronica Berglyd Olsen
2024-04-17 00:12:06 +02:00
parent 5acbfeffa5
commit 8150edbc43
+19 -38
View File
@@ -33,6 +33,8 @@ import datetime
import subprocess import subprocess
import email.utils import email.utils
from pathlib import Path
OS_NONE = 0 OS_NONE = 0
OS_LINUX = 1 OS_LINUX = 1
OS_WIN = 2 OS_WIN = 2
@@ -54,16 +56,15 @@ def extractVersion(beQuiet: bool = False) -> tuple[str, str, str]:
numVers = "0" numVers = "0"
hexVers = "0x0" hexVers = "0x0"
relDate = "Unknown" relDate = "Unknown"
initFile = os.path.join("novelwriter", "__init__.py") initFile = Path("novelwriter") / "__init__.py"
try: try:
with open(initFile, mode="r", encoding="utf-8") as inFile: for aLine in initFile.read_text(encoding="utf-8").splitlines():
for aLine in inFile: if aLine.startswith("__version__"):
if aLine.startswith("__version__"): numVers = getValue((aLine))
numVers = getValue((aLine)) if aLine.startswith("__hexversion__"):
if aLine.startswith("__hexversion__"): hexVers = getValue((aLine))
hexVers = getValue((aLine)) if aLine.startswith("__date__"):
if aLine.startswith("__date__"): relDate = getValue((aLine))
relDate = getValue((aLine))
except Exception as exc: except Exception as exc:
print("Could not read file: %s" % initFile) print("Could not read file: %s" % initFile)
print(str(exc)) print(str(exc))
@@ -88,26 +89,23 @@ def stripVersion(version: str) -> str:
def readFile(fileName: str) -> str: def readFile(fileName: str) -> str:
"""Read an entire file and return as a string.""" """Read an entire file and return as a string."""
with open(fileName, mode="r", encoding="utf-8") as inFile: return Path(fileName).read_text(encoding="utf-8")
return inFile.read()
def writeFile(fileName: str, writeText: str) -> None: def writeFile(fileName: str, text: str) -> None:
"""Write string to file.""" """Write string to file."""
with open(fileName, mode="w+", encoding="utf-8") as outFile: Path(fileName).write_text(text, encoding="utf-8")
outFile.write(writeText) return
def toUpload(srcPath: str, dstName: str | None = None) -> None: def toUpload(srcPath: str | Path, dstName: str | None = None) -> None:
"""Copy a file produced by one of the build functions to the upload """Copy a file produced by one of the build functions to the upload
directory. The file can optionally be given a new name. directory. The file can optionally be given a new name.
""" """
uplDir = "dist_upload" uplDir = Path("dist_upload")
if not os.path.isdir(uplDir): uplDir.mkdir(exist_ok=True)
os.mkdir(uplDir) srcPath = Path(srcPath)
if dstName is None: shutil.copyfile(srcPath, uplDir / (dstName or srcPath.name))
dstName = os.path.basename(srcPath)
shutil.copyfile(srcPath, os.path.join(uplDir, dstName))
return return
@@ -1551,19 +1549,6 @@ if __name__ == "__main__":
sysArgs = sys.argv.copy() sysArgs = sys.argv.copy()
# Set Target OS
if "--target-linux" in sysArgs:
sysArgs.remove("--target-linux")
targetOS = OS_LINUX
elif "--target-darwin" in sysArgs:
sysArgs.remove("--target-darwin")
targetOS = OS_DARWIN
elif "--target-win" in sysArgs:
sysArgs.remove("--target-win")
targetOS = OS_WIN
else:
targetOS = hostOS
# Sign package # Sign package
if "--sign" in sysArgs: if "--sign" in sysArgs:
sysArgs.remove("--sign") sysArgs.remove("--sign")
@@ -1587,10 +1572,6 @@ if __name__ == "__main__":
"novelWriter as a package on Linux, Mac and Windows. The available options", "novelWriter as a package on Linux, Mac and Windows. The available options",
"are as follows:", "are as follows:",
"", "",
"Some of the commands can be targeted towards a different OS than the host OS.",
"To target the command, add one of '--target-linux', '--target-darwin' or",
"'--target-win'.",
"",
"General:", "General:",
"", "",
" help Print the help message.", " help Print the help message.",