From d61329a4f343bd8bb5c125846179b2475e4e36c0 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 10 Jun 2025 00:08:48 +0200 Subject: [PATCH] Improve build code --- .github/workflows/build_linux.yml | 2 ++ utils/build_debian.py | 7 +++--- utils/build_windows.py | 25 ++++-------------- utils/common.py | 42 +++++++++++++++---------------- utils/docs.py | 6 ++--- 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 9c38f29e..64f13d21 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -8,6 +8,8 @@ jobs: buildLinux-AppImage: needs: buildAssets + # Needs to stay on 22.04 as long as we're using manylinux_2_28 + # as libxcb-cursor0 in 22.04 supports glibc >= 2.17 runs-on: ubuntu-22.04 env: PYTHON_VERSION: "3.13" diff --git a/utils/build_debian.py b/utils/build_debian.py index 35b3603f..4835ec86 100644 --- a/utils/build_debian.py +++ b/utils/build_debian.py @@ -24,12 +24,11 @@ import argparse import datetime import email.utils import shutil -import subprocess import sys from utils.common import ( ROOT_DIR, SETUP_DIR, checkAssetsExist, copyPackageFiles, copySourceCode, - extractVersion, makeCheckSum, toUpload, writeFile + extractVersion, makeCheckSum, systemCall, toUpload, writeFile ) SIGN_KEY = "D6A9F6B8F227CF7C6F6D1EE84DBBE4B734B0BD08" @@ -134,10 +133,10 @@ def makeDebianPackage( signArgs = [f"-k{signKey}"] if sourceBuild: - subprocess.call(["debuild", "-S", *signArgs], cwd=outDir) + systemCall(["debuild", "-S", *signArgs], cwd=outDir) toUpload(bldDir / f"{bldPkg}.tar.xz") else: - subprocess.call(["dpkg-buildpackage", *signArgs], cwd=outDir) + systemCall(["dpkg-buildpackage", *signArgs], cwd=outDir) shutil.copyfile(bldDir / f"{bldPkg}.tar.xz", bldDir / f"{bldPkg}.debian.tar.xz") toUpload(bldDir / f"{bldPkg}.debian.tar.xz") toUpload(bldDir / f"{bldPkg}_all.deb") diff --git a/utils/build_windows.py b/utils/build_windows.py index 98d5e8c2..76fdc281 100644 --- a/utils/build_windows.py +++ b/utils/build_windows.py @@ -23,7 +23,6 @@ from __future__ import annotations import argparse import compileall import shutil -import subprocess import sys import urllib.request import zipfile @@ -32,7 +31,7 @@ from pathlib import Path from utils.common import ( ROOT_DIR, SETUP_DIR, copySourceCode, extractVersion, readFile, - removeRedundantQt, writeFile + removeRedundantQt, systemCall, writeFile ) @@ -89,20 +88,11 @@ def embedPython(bldDir: Path, outDir: Path) -> None: def installRequirements(libDir: Path) -> None: """Install dependencies.""" print("Install dependencies ...") - - try: - subprocess.call([ - sys.executable, "-m", - "pip", "install", "-r", "requirements.txt", "--target", str(libDir) - ]) - except Exception as exc: - print("Failed with error:") - print(str(exc)) - sys.exit(1) - + systemCall([ + sys.executable, "-m", "pip", "install", "-r", "requirements.txt", "--target", libDir + ]) print("Done") print("") - return @@ -162,12 +152,7 @@ def main(args: argparse.Namespace) -> None: writeFile(ROOT_DIR / "setup.iss", issData) print("") - try: - subprocess.call(["iscc", "setup.iss"]) - except Exception as exc: - print("Inno Setup failed with error:") - print(str(exc)) - sys.exit(1) + systemCall(["iscc", "setup.iss"]) print("") print("Done") diff --git a/utils/common.py b/utils/common.py index 4ba82e00..1784ed01 100644 --- a/utils/common.py +++ b/utils/common.py @@ -51,11 +51,11 @@ def extractVersion(beQuiet: bool = False) -> tuple[str, str, str]: if aLine.startswith("__date__"): relDate = getValue(aLine) except Exception as exc: - print(f"Could not read file: {initFile}") - print(str(exc)) + print(f"Could not read file: {initFile}", flush=True) + print(str(exc), flush=True) if not beQuiet: - print(f"novelWriter version: {numVers} ({hexVers}) at {relDate}") + print(f"novelWriter version: {numVers} ({hexVers}) at {relDate}", flush=True) return numVers, hexVers, relDate @@ -78,16 +78,16 @@ def copySourceCode(dst: Path) -> None: for item in src.glob("**/*"): relSrc = item.relative_to(ROOT_DIR) if item.suffix in (".pyc", ".pyo"): - print("Ignored:", relSrc) + print("Ignored:", relSrc, flush=True) continue if item.parent.is_dir() and item.parent.name != "__pycache__": dstDir = dst / relSrc.parent if not dstDir.exists(): dstDir.mkdir(parents=True) - print("Created:", dstDir.relative_to(ROOT_DIR)) + print("Created:", dstDir.relative_to(ROOT_DIR), flush=True) if item.is_file(): shutil.copyfile(item, dst / relSrc) - print("Copied:", relSrc) + print("Copied:", relSrc, flush=True) return @@ -96,7 +96,7 @@ def copyPackageFiles(dst: Path, setupPy: bool = False) -> None: copyFiles = ["LICENSE.md", "CREDITS.md", "pyproject.toml"] for copyFile in copyFiles: shutil.copyfile(copyFile, dst / copyFile) - print("Copied:", copyFile) + print("Copied:", copyFile, flush=True) writeFile(dst / "MANIFEST.in", ( "include LICENSE.md\n" @@ -137,10 +137,10 @@ def makeCheckSum(sumFile: str, cwd: Path | None = None) -> str: shaFile = cwd / f"{sumFile}.sha256" with open(shaFile, mode="w", encoding="utf-8") as fOut: subprocess.call(["shasum", "-a", "256", sumFile], stdout=fOut, cwd=cwd) - print(f"SHA256 Sum: {shaFile}") + print(f"SHA256 Sum: {shaFile}", flush=True) except Exception as exc: - print("Could not generate sha256 file") - print(str(exc)) + print("Could not generate sha256 file", flush=True) + print(str(exc), flush=True) return "" return str(shaFile) @@ -154,17 +154,17 @@ def checkAssetsExist() -> bool: sampleZip = ROOT_DIR / "novelwriter" / "assets" / "sample.zip" if sampleZip.is_file(): - print(f"Found: {sampleZip}") + print(f"Found: {sampleZip}", flush=True) hasSample = True pdfManual = ROOT_DIR / "novelwriter" / "assets" / "manual.pdf" if pdfManual.is_file(): - print(f"Found: {pdfManual}") + print(f"Found: {pdfManual}", flush=True) hasManual = True i18nAssets = ROOT_DIR / "novelwriter" / "assets" / "i18n" if len(list(i18nAssets.glob("*.qm"))) > 0: - print(f"Found: {i18nAssets}/*.qm") + print(f"Found: {i18nAssets}/*.qm", flush=True) hasQmData = True return hasSample and hasManual and hasQmData @@ -187,29 +187,29 @@ def readFile(file: Path) -> str: def writeFile(file: Path, text: str) -> int: """Write string to file.""" result = file.write_text(text, encoding="utf-8") - print("Wrote:", file.relative_to(ROOT_DIR)) + print("Wrote:", file.relative_to(ROOT_DIR), flush=True) return result def freshFolder(path: Path) -> None: """Make sure a folder exists and is empty.""" if path.exists(): - print("Removing:", str(path)) + print("Removing:", str(path), flush=True) shutil.rmtree(path) path.mkdir() return -def systemCall(cmd: list, cwd: Path | str | None = None, env: dict | None = None) -> None: +def systemCall(cmd: list, cwd: Path | str | None = None, env: dict | None = None) -> int: """Make a system call using subprocess.""" if isinstance(cwd, Path): cwd = str(cwd) try: - subprocess.call([str(c) for c in cmd], cwd=cwd, env=env) + code = subprocess.call([str(c) for c in cmd], cwd=cwd, env=env) except Exception as exc: - print("ERROR:", str(exc)) + print("ERROR:", str(exc), flush=True) sys.exit(1) - return + return code def removeRedundantQt(qtBase: Path) -> None: @@ -218,12 +218,12 @@ def removeRedundantQt(qtBase: Path) -> None: def unlinkIfFound(file: Path) -> None: if file.is_file(): file.unlink() - print("Deleted:", file.relative_to(ROOT_DIR)) + print("Deleted:", file.relative_to(ROOT_DIR), flush=True) def deleteFolder(folder: Path) -> None: if folder.is_dir(): shutil.rmtree(folder) - print("Deleted:", folder.relative_to(ROOT_DIR)) + print("Deleted:", folder.relative_to(ROOT_DIR), flush=True) def unlinkIfPrefix(folder: Path, prefix: tuple[str, ...]) -> None: if folder.is_dir(): diff --git a/utils/docs.py b/utils/docs.py index c43d3f37..7a4de94b 100644 --- a/utils/docs.py +++ b/utils/docs.py @@ -25,7 +25,7 @@ import os import shutil import subprocess -from utils.common import ROOT_DIR +from utils.common import ROOT_DIR, systemCall def updateDocsTranslationSources(args: argparse.Namespace) -> None: @@ -40,7 +40,7 @@ def updateDocsTranslationSources(args: argparse.Namespace) -> None: locsDir.mkdir(exist_ok=True) print("Generating POT Files") - subprocess.call(["make", "gettext"], cwd=docsDir) + systemCall(["make", "gettext"], cwd=docsDir) print("") lang = args.lang @@ -55,7 +55,7 @@ def updateDocsTranslationSources(args: argparse.Namespace) -> None: print("") for code in update: - subprocess.call(["sphinx-intl", "update", "-p", "build/gettext", "-l", code], cwd=docsDir) + systemCall(["sphinx-intl", "update", "-p", "build/gettext", "-l", code], cwd=docsDir) print("") print("Done")