From 9c156fe38182dbc06c8b78979573c64ceef20a07 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 7 Apr 2026 15:21:21 -0500 Subject: [PATCH] feat(build-backend): add function to remove Python bytecode artifacts and set PYTHONDONTWRITEBYTECODE for builds on macOS --- scripts/build-backend.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/scripts/build-backend.js b/scripts/build-backend.js index 3570e9e..e121953 100755 --- a/scripts/build-backend.js +++ b/scripts/build-backend.js @@ -18,6 +18,27 @@ function getFiles(dir, fileList = []) { return fileList; } +function stripPythonBytecodeArtifacts(dir) { + if (!fs.existsSync(dir)) return; + const entries = fs.readdirSync(dir, { withFileTypes: true }); + for (const ent of entries) { + const full = path.join(dir, ent.name); + if (ent.isDirectory()) { + if (ent.name === "__pycache__") { + fs.rmSync(full, { recursive: true, force: true }); + } else { + stripPythonBytecodeArtifacts(full); + } + } else if (ent.name.endsWith(".pyc") || ent.name.endsWith(".pyo")) { + const stem = ent.name.replace(/\.pyc$/i, "").replace(/\.pyo$/i, ""); + const siblingPy = path.join(dir, `${stem}.py`); + if (fs.existsSync(siblingPy)) { + fs.unlinkSync(full); + } + } + } +} + function generateManifest(buildDir, manifestPath) { console.log("Generating backend integrity manifest..."); const files = getFiles(buildDir); @@ -69,6 +90,7 @@ try { ...process.env, CX_FREEZE_TARGET_NAME: targetName, CX_FREEZE_BUILD_EXE: buildDirRelative, + PYTHONDONTWRITEBYTECODE: "1", }; const cmdParts = pythonCmd.trim().split(/\s+/).filter(Boolean); @@ -100,6 +122,9 @@ try { } if (fs.existsSync(buildDir)) { + if (isDarwin) { + stripPythonBytecodeArtifacts(buildDir); + } const manifestPath = path.join(buildDir, "backend-manifest.json"); generateManifest(buildDir, manifestPath); } else {