Don't use import to read version number during setup

This commit is contained in:
Veronica K. B. Olsen
2021-04-17 17:18:47 +02:00
parent 421f2dd688
commit 6763ebdd66
+47 -12
View File
@@ -35,6 +35,30 @@ OS_LINUX = 1
OS_WIN = 2 OS_WIN = 2
OS_DARWIN = 3 OS_DARWIN = 3
# =============================================================================================== #
# Utilities
# =============================================================================================== #
def extractVersion():
"""Extract the novelWriter version number without having to import
anything else from the main package.
"""
def getValue(theString):
theBits = theString.partition("=")
return theBits[2].strip().strip('"')
numVers = "Unknown"
hexVers = "Unknown"
initFile = os.path.join("nw", "__init__.py")
with open(initFile, mode="r") as inFile:
for aLine in inFile:
if aLine.startswith("__version__"):
numVers = getValue((aLine))
if aLine.startswith("__hexversion__"):
hexVers = getValue((aLine))
return numVers, hexVers
# =============================================================================================== # # =============================================================================================== #
# General # General
# =============================================================================================== # # =============================================================================================== #
@@ -237,9 +261,11 @@ def buildSampleZip():
def makeMinimalPackage(targetOS): def makeMinimalPackage(targetOS):
"""Pack the core source file in a single zip file. """Pack the core source file in a single zip file.
""" """
from nw import __version__
from zipfile import ZipFile, ZIP_DEFLATED from zipfile import ZipFile, ZIP_DEFLATED
# Get the version
numVers, _ = extractVersion()
# Make sample.zip first # Make sample.zip first
try: try:
buildSampleZip() buildSampleZip()
@@ -268,7 +294,7 @@ def makeMinimalPackage(targetOS):
targName = "" targName = ""
print("") print("")
zipFile = f"novelWriter-{__version__}-minimal{targName}.zip" zipFile = f"novelWriter-{numVers}-minimal{targName}.zip"
outFile = os.path.join("dist", zipFile) outFile = os.path.join("dist", zipFile)
if os.path.isfile(outFile): if os.path.isfile(outFile):
os.unlink(outFile) os.unlink(outFile)
@@ -300,6 +326,8 @@ def makeMinimalPackage(targetOS):
print("Adding File: novelWriter.pyw") print("Adding File: novelWriter.pyw")
zipObj.write(os.path.join("setup", "setup_windows.bat"), "setup_windows.bat") zipObj.write(os.path.join("setup", "setup_windows.bat"), "setup_windows.bat")
print("Adding File: setup_windows.bat") print("Adding File: setup_windows.bat")
zipObj.write(os.path.join("setup", "uninstall_windows.bat"), "uninstall_windows.bat")
print("Adding File: setup_windows.bat")
else: else:
zipObj.write("novelWriter.py") zipObj.write("novelWriter.py")
print("Adding File: novelWriter.py") print("Adding File: novelWriter.py")
@@ -714,7 +742,6 @@ def winInstall():
"""Will attempt to install icons and make a launcher for Windows. """Will attempt to install icons and make a launcher for Windows.
""" """
import winreg import winreg
from nw import __version__, __hexversion__
try: try:
import win32com.client import win32com.client
except ImportError: except ImportError:
@@ -730,13 +757,14 @@ def winInstall():
print("===============") print("===============")
print("") print("")
nwTesting = not __hexversion__[-2] == "f" numVers, hexVers = extractVersion()
nwTesting = not hexVers[-2] == "f"
wShell = win32com.client.Dispatch("WScript.Shell") wShell = win32com.client.Dispatch("WScript.Shell")
if nwTesting: if nwTesting:
linkName = "novelWriter Testing %s.lnk" % __version__ linkName = "novelWriter Testing %s.lnk" % numVers
else: else:
linkName = "novelWriter %s.lnk" % __version__ linkName = "novelWriter %s.lnk" % numVers
desktopDir = wShell.SpecialFolders("Desktop") desktopDir = wShell.SpecialFolders("Desktop")
desktopIcon = os.path.join(desktopDir, linkName) desktopIcon = os.path.join(desktopDir, linkName)
@@ -838,7 +866,6 @@ def winUninstall():
"""Will attempt to uninstall icons previously installed. """Will attempt to uninstall icons previously installed.
""" """
import winreg import winreg
from nw import __version__, __hexversion__
try: try:
import win32com.client import win32com.client
except ImportError: except ImportError:
@@ -853,13 +880,14 @@ def winUninstall():
print("=================") print("=================")
print("") print("")
nwTesting = not __hexversion__[-2] == "f" numVers, hexVers = extractVersion()
nwTesting = not hexVers[-2] == "f"
wShell = win32com.client.Dispatch("WScript.Shell") wShell = win32com.client.Dispatch("WScript.Shell")
if nwTesting: if nwTesting:
linkName = "novelWriter Testing %s.lnk" % __version__ linkName = "novelWriter Testing %s.lnk" % numVers
else: else:
linkName = "novelWriter %s.lnk" % __version__ linkName = "novelWriter %s.lnk" % numVers
desktopDir = wShell.SpecialFolders("Desktop") desktopDir = wShell.SpecialFolders("Desktop")
desktopIcon = os.path.join(desktopDir, linkName) desktopIcon = os.path.join(desktopDir, linkName)
@@ -937,8 +965,8 @@ def innoSetup():
with open(os.path.join("setup", "win_setup_pyz.iss"), mode="r") as inFile: with open(os.path.join("setup", "win_setup_pyz.iss"), mode="r") as inFile:
issData = inFile.read() issData = inFile.read()
import nw # noqa: E402 numVers, _ = extractVersion()
issData = issData.replace(r"%%version%%", nw.__version__) issData = issData.replace(r"%%version%%", numVers)
issData = issData.replace(r"%%dir%%", os.getcwd()) issData = issData.replace(r"%%dir%%", os.getcwd())
with open("setup.iss", mode="w+") as outFile: with open("setup.iss", mode="w+") as outFile:
@@ -1042,6 +1070,13 @@ if __name__ == "__main__":
print(helpMsg) print(helpMsg)
sys.exit(0) sys.exit(0)
if "version" in sys.argv:
sys.argv.remove("version")
numVers, hexVers = extractVersion()
print("Semantic Version: %s" % numVers)
print("Hexadecimal Version: %s" % hexVers)
sys.exit(0)
if "pip" in sys.argv: if "pip" in sys.argv:
sys.argv.remove("pip") sys.argv.remove("pip")
installPackages(hostOS) installPackages(hostOS)