@dominevo/types 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.3] - 2026-11-29
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Portable Python on macOS keeps working after the data folder is moved (the interpreter stub and pip scripts no longer embed an absolute path).
|
|
15
|
+
|
|
10
16
|
## [0.1.2] - 2026-11-29
|
|
11
17
|
|
|
12
18
|
### Changed
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { IScopedLogger } from '../interfaces/logging.js';
|
|
2
|
+
/** The `arch` flag that locks the slice of a universal2 binary to the host. */
|
|
3
|
+
export type ArchFlag = '-arm64' | '-x86_64';
|
|
4
|
+
/** The `arch` flag for a Node `process.arch` value. */
|
|
5
|
+
export declare function archFlagFor(arch: string): ArchFlag;
|
|
6
|
+
/** Name of the renamed interpreter the stubs delegate to. */
|
|
7
|
+
export declare const REAL_PYTHON_NAME = "python3.real";
|
|
8
|
+
/**
|
|
9
|
+
* The text of the arch stub. It resolves `python3.real` relative to its own
|
|
10
|
+
* location (following a symlinked invocation), so it survives a move.
|
|
11
|
+
*/
|
|
12
|
+
export declare function renderArchStub(archFlag: ArchFlag): string;
|
|
13
|
+
/**
|
|
14
|
+
* Rewrites the absolute interpreter path of the shebang (line 1) and of pip's
|
|
15
|
+
* `'''exec'` line (line 2) when it lies under a directory other than `binDir`.
|
|
16
|
+
* Any other path in those lines (a comment) and any interpreter that still exists (a system shebang) is left alone.
|
|
17
|
+
* Null when nothing needs to change.
|
|
18
|
+
*/
|
|
19
|
+
export declare function relocateInterpreterHeader(text: string, binDir: string): string | null;
|
|
20
|
+
/**
|
|
21
|
+
* Brings the install in `pythonDir` up to date: the arch stubs are the
|
|
22
|
+
* relative form, and no regular script in `bin/` names an interpreter under
|
|
23
|
+
* another directory. Bounded to `bin/`, to the first two lines, to text files.
|
|
24
|
+
* Never throws; returns how many files it rewrote.
|
|
25
|
+
*/
|
|
26
|
+
export declare function healPortablePython(pythonDir: string, archFlag: ArchFlag, logger: IScopedLogger): number;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { errMsg } from "../utils/err-msg.mjs";
|
|
2
2
|
import { extractArchive } from "./archive-extract.mjs";
|
|
3
3
|
import { verifySha256 } from "./binary-downloader.mjs";
|
|
4
|
+
import { REAL_PYTHON_NAME, archFlagFor, healPortablePython, renderArchStub } from "./python-portable-relocate.mjs";
|
|
4
5
|
import { chmodSync, createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
6
|
import { basename, join } from "node:path";
|
|
6
7
|
import { pipeline } from "node:stream/promises";
|
|
@@ -87,6 +88,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
87
88
|
const pythonDir = join(dataDir, "deps", "python");
|
|
88
89
|
const pythonBin = join(pythonDir, ...pythonInterpreterSegments(process.platform));
|
|
89
90
|
if (existsSync(pythonBin)) {
|
|
91
|
+
if (process.platform === "darwin") healPortablePython(pythonDir, archFlagFor(process.arch), logger);
|
|
90
92
|
logger.debug("Portable Python found", { meta: { pythonBin } });
|
|
91
93
|
return pythonBin;
|
|
92
94
|
}
|
|
@@ -125,18 +127,18 @@ async function ensurePython(dataDir, logger) {
|
|
|
125
127
|
}
|
|
126
128
|
chmodSync(pythonBin, 493);
|
|
127
129
|
if (process.platform === "darwin") {
|
|
128
|
-
const realBin = join(pythonDir, "bin",
|
|
130
|
+
const realBin = join(pythonDir, "bin", REAL_PYTHON_NAME);
|
|
131
|
+
const archFlag = archFlagFor(process.arch);
|
|
129
132
|
if (!existsSync(realBin)) {
|
|
130
133
|
renameSync(pythonBin, realBin);
|
|
131
|
-
|
|
132
|
-
writeFileSync(pythonBin, `#!/bin/sh\nexec /usr/bin/arch ${archFlag} "${realBin}" "$@"\n`);
|
|
134
|
+
writeFileSync(pythonBin, renderArchStub(archFlag));
|
|
133
135
|
chmodSync(pythonBin, 493);
|
|
134
136
|
const pyAlt = join(pythonDir, "bin", "python");
|
|
135
137
|
if (existsSync(pyAlt)) {
|
|
136
138
|
try {
|
|
137
139
|
rmSync(pyAlt, { force: true });
|
|
138
140
|
} catch {}
|
|
139
|
-
writeFileSync(pyAlt,
|
|
141
|
+
writeFileSync(pyAlt, renderArchStub(archFlag));
|
|
140
142
|
chmodSync(pyAlt, 493);
|
|
141
143
|
}
|
|
142
144
|
logger.info("Locked python3 to host arch via shell stub", { meta: {
|
|
@@ -144,6 +146,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
144
146
|
realBin
|
|
145
147
|
} });
|
|
146
148
|
}
|
|
149
|
+
healPortablePython(pythonDir, archFlag, logger);
|
|
147
150
|
}
|
|
148
151
|
logger.info("Portable Python installed", { meta: {
|
|
149
152
|
version: PYTHON_VERSION,
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { errMsg } from "../utils/err-msg.mjs";
|
|
2
|
+
import { chmodSync, closeSync, existsSync, lstatSync, openSync, readFileSync, readSync, readdirSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
//#region src/deps/python-portable-relocate.ts
|
|
5
|
+
/**
|
|
6
|
+
* Keeps the macOS portable Python runnable after its folder is moved.
|
|
7
|
+
*
|
|
8
|
+
* The arch-locking stub that replaces `bin/python3` used to embed the absolute
|
|
9
|
+
* path of `python3.real`, and pip's console scripts embed the absolute path of
|
|
10
|
+
* the interpreter that installed them. Move the data directory and every one of
|
|
11
|
+
* them points at a path that no longer exists. The stub now finds the real
|
|
12
|
+
* binary beside itself at run time, and {@link healPortablePython} rewrites the
|
|
13
|
+
* leftovers of an install made before that.
|
|
14
|
+
*/
|
|
15
|
+
/** The `arch` flag for a Node `process.arch` value. */
|
|
16
|
+
function archFlagFor(arch) {
|
|
17
|
+
return arch === "arm64" ? "-arm64" : "-x86_64";
|
|
18
|
+
}
|
|
19
|
+
/** Name of the renamed interpreter the stubs delegate to. */
|
|
20
|
+
var REAL_PYTHON_NAME = "python3.real";
|
|
21
|
+
/** Scripts bigger than this are never rewritten (a binary or a data file). */
|
|
22
|
+
var MAX_SCRIPT_BYTES = 1048576;
|
|
23
|
+
/** How much of a file is read to tell a script from a binary. */
|
|
24
|
+
var PROBE_BYTES = 512;
|
|
25
|
+
/**
|
|
26
|
+
* The text of the arch stub. It resolves `python3.real` relative to its own
|
|
27
|
+
* location (following a symlinked invocation), so it survives a move.
|
|
28
|
+
*/
|
|
29
|
+
function renderArchStub(archFlag) {
|
|
30
|
+
return [
|
|
31
|
+
"#!/bin/sh",
|
|
32
|
+
"p=$0",
|
|
33
|
+
"while [ -L \"$p\" ]; do",
|
|
34
|
+
" l=$(readlink \"$p\")",
|
|
35
|
+
" case $l in /*) p=$l ;; *) p=$(dirname \"$p\")/$l ;; esac",
|
|
36
|
+
"done",
|
|
37
|
+
`exec /usr/bin/arch ${archFlag} "$(cd "$(dirname "$p")" && pwd)/${REAL_PYTHON_NAME}" "$@"`,
|
|
38
|
+
""
|
|
39
|
+
].join("\n");
|
|
40
|
+
}
|
|
41
|
+
/** A shebang naming an absolute portable interpreter: `#!<dir>/bin/python3`. */
|
|
42
|
+
var SHEBANG_PATH = /^(#!)(\/[^"'\n]+?)\/bin\/(python3\.real|python3|python)(?![\w.])/;
|
|
43
|
+
/** The pip console-script re-exec line: `'''exec' "<dir>/bin/python3.real" "$0" "$@"`. */
|
|
44
|
+
var EXEC_LINE_PATH = /^('''exec' ")(\/[^"\n]+?)\/bin\/(python3\.real)(?![\w.])/;
|
|
45
|
+
function relocateLine(line, pattern, binDir) {
|
|
46
|
+
return line.replace(pattern, (whole, lead, dir, name) => `${dir}/bin` === binDir || existsSync(`${dir}/bin/${name}`) ? whole : `${lead}${binDir}/${name}`);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Rewrites the absolute interpreter path of the shebang (line 1) and of pip's
|
|
50
|
+
* `'''exec'` line (line 2) when it lies under a directory other than `binDir`.
|
|
51
|
+
* Any other path in those lines (a comment) and any interpreter that still exists (a system shebang) is left alone.
|
|
52
|
+
* Null when nothing needs to change.
|
|
53
|
+
*/
|
|
54
|
+
function relocateInterpreterHeader(text, binDir) {
|
|
55
|
+
const lines = text.split("\n");
|
|
56
|
+
const first = relocateLine(lines[0] ?? "", SHEBANG_PATH, binDir);
|
|
57
|
+
const second = relocateLine(lines[1] ?? "", EXEC_LINE_PATH, binDir);
|
|
58
|
+
if (first === lines[0] && second === (lines[1] ?? "")) return null;
|
|
59
|
+
return [
|
|
60
|
+
first,
|
|
61
|
+
...lines.length > 1 ? [second] : [],
|
|
62
|
+
...lines.slice(2)
|
|
63
|
+
].join("\n");
|
|
64
|
+
}
|
|
65
|
+
function isRegularFile(path) {
|
|
66
|
+
try {
|
|
67
|
+
return lstatSync(path).isFile();
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Replaces `path` atomically (temp file beside it, same mode, rename): several
|
|
74
|
+
* runners call `ensurePython` at once and none may exec a half-written file.
|
|
75
|
+
*/
|
|
76
|
+
function writeFileAtomic(path, content, mode) {
|
|
77
|
+
const tmp = `${path}.heal-${process.pid}-${Date.now()}`;
|
|
78
|
+
writeFileSync(tmp, content);
|
|
79
|
+
chmodSync(tmp, mode);
|
|
80
|
+
renameSync(tmp, path);
|
|
81
|
+
}
|
|
82
|
+
/** The first `PROBE_BYTES` bytes of a file, without reading the rest. */
|
|
83
|
+
function readProbe(path) {
|
|
84
|
+
const fd = openSync(path, "r");
|
|
85
|
+
try {
|
|
86
|
+
const buf = Buffer.alloc(PROBE_BYTES);
|
|
87
|
+
const n = readSync(fd, buf, 0, PROBE_BYTES, 0);
|
|
88
|
+
return buf.subarray(0, n);
|
|
89
|
+
} finally {
|
|
90
|
+
closeSync(fd);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Brings the install in `pythonDir` up to date: the arch stubs are the
|
|
95
|
+
* relative form, and no regular script in `bin/` names an interpreter under
|
|
96
|
+
* another directory. Bounded to `bin/`, to the first two lines, to text files.
|
|
97
|
+
* Never throws; returns how many files it rewrote.
|
|
98
|
+
*/
|
|
99
|
+
function healPortablePython(pythonDir, archFlag, logger) {
|
|
100
|
+
const binDir = join(pythonDir, "bin");
|
|
101
|
+
let healed = 0;
|
|
102
|
+
try {
|
|
103
|
+
if (!existsSync(join(binDir, "python3.real"))) return 0;
|
|
104
|
+
const stub = renderArchStub(archFlag);
|
|
105
|
+
for (const name of ["python3", "python"]) {
|
|
106
|
+
const path = join(binDir, name);
|
|
107
|
+
if (!isRegularFile(path)) continue;
|
|
108
|
+
const text = readFileSync(path, "utf8");
|
|
109
|
+
if (text === stub || !text.startsWith("#!/bin/sh\n")) continue;
|
|
110
|
+
if (!text.includes("/usr/bin/arch")) continue;
|
|
111
|
+
writeFileAtomic(path, stub, 493);
|
|
112
|
+
healed += 1;
|
|
113
|
+
}
|
|
114
|
+
for (const name of readdirSync(binDir)) {
|
|
115
|
+
const path = join(binDir, name);
|
|
116
|
+
try {
|
|
117
|
+
if (!isRegularFile(path)) continue;
|
|
118
|
+
const stat = lstatSync(path);
|
|
119
|
+
if (stat.size > MAX_SCRIPT_BYTES) continue;
|
|
120
|
+
const probe = readProbe(path);
|
|
121
|
+
if (probe.subarray(0, 2).toString("latin1") !== "#!" || probe.includes(0)) continue;
|
|
122
|
+
const next = relocateInterpreterHeader(readFileSync(path, "utf8"), binDir);
|
|
123
|
+
if (next === null) continue;
|
|
124
|
+
writeFileAtomic(path, next, stat.mode & 511);
|
|
125
|
+
healed += 1;
|
|
126
|
+
} catch (err) {
|
|
127
|
+
logger.warn("Could not repair a portable Python script", { meta: {
|
|
128
|
+
path,
|
|
129
|
+
error: errMsg(err)
|
|
130
|
+
} });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (healed > 0) logger.info("Repaired portable Python after its folder moved", { meta: {
|
|
134
|
+
healed,
|
|
135
|
+
binDir
|
|
136
|
+
} });
|
|
137
|
+
} catch (err) {
|
|
138
|
+
logger.warn("Portable Python repair failed; continuing", { meta: {
|
|
139
|
+
binDir,
|
|
140
|
+
error: errMsg(err)
|
|
141
|
+
} });
|
|
142
|
+
}
|
|
143
|
+
return healed;
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
export { REAL_PYTHON_NAME, archFlagFor, healPortablePython, renderArchStub };
|
package/dist/node.js
CHANGED
|
@@ -650,6 +650,148 @@ function writeOwnerOnlyFileSync(target, data, overrides = {}) {
|
|
|
650
650
|
}
|
|
651
651
|
}
|
|
652
652
|
//#endregion
|
|
653
|
+
//#region src/deps/python-portable-relocate.ts
|
|
654
|
+
/**
|
|
655
|
+
* Keeps the macOS portable Python runnable after its folder is moved.
|
|
656
|
+
*
|
|
657
|
+
* The arch-locking stub that replaces `bin/python3` used to embed the absolute
|
|
658
|
+
* path of `python3.real`, and pip's console scripts embed the absolute path of
|
|
659
|
+
* the interpreter that installed them. Move the data directory and every one of
|
|
660
|
+
* them points at a path that no longer exists. The stub now finds the real
|
|
661
|
+
* binary beside itself at run time, and {@link healPortablePython} rewrites the
|
|
662
|
+
* leftovers of an install made before that.
|
|
663
|
+
*/
|
|
664
|
+
/** The `arch` flag for a Node `process.arch` value. */
|
|
665
|
+
function archFlagFor(arch) {
|
|
666
|
+
return arch === "arm64" ? "-arm64" : "-x86_64";
|
|
667
|
+
}
|
|
668
|
+
/** Name of the renamed interpreter the stubs delegate to. */
|
|
669
|
+
var REAL_PYTHON_NAME = "python3.real";
|
|
670
|
+
/** Scripts bigger than this are never rewritten (a binary or a data file). */
|
|
671
|
+
var MAX_SCRIPT_BYTES = 1048576;
|
|
672
|
+
/** How much of a file is read to tell a script from a binary. */
|
|
673
|
+
var PROBE_BYTES = 512;
|
|
674
|
+
/**
|
|
675
|
+
* The text of the arch stub. It resolves `python3.real` relative to its own
|
|
676
|
+
* location (following a symlinked invocation), so it survives a move.
|
|
677
|
+
*/
|
|
678
|
+
function renderArchStub(archFlag) {
|
|
679
|
+
return [
|
|
680
|
+
"#!/bin/sh",
|
|
681
|
+
"p=$0",
|
|
682
|
+
"while [ -L \"$p\" ]; do",
|
|
683
|
+
" l=$(readlink \"$p\")",
|
|
684
|
+
" case $l in /*) p=$l ;; *) p=$(dirname \"$p\")/$l ;; esac",
|
|
685
|
+
"done",
|
|
686
|
+
`exec /usr/bin/arch ${archFlag} "$(cd "$(dirname "$p")" && pwd)/${REAL_PYTHON_NAME}" "$@"`,
|
|
687
|
+
""
|
|
688
|
+
].join("\n");
|
|
689
|
+
}
|
|
690
|
+
/** A shebang naming an absolute portable interpreter: `#!<dir>/bin/python3`. */
|
|
691
|
+
var SHEBANG_PATH = /^(#!)(\/[^"'\n]+?)\/bin\/(python3\.real|python3|python)(?![\w.])/;
|
|
692
|
+
/** The pip console-script re-exec line: `'''exec' "<dir>/bin/python3.real" "$0" "$@"`. */
|
|
693
|
+
var EXEC_LINE_PATH = /^('''exec' ")(\/[^"\n]+?)\/bin\/(python3\.real)(?![\w.])/;
|
|
694
|
+
function relocateLine(line, pattern, binDir) {
|
|
695
|
+
return line.replace(pattern, (whole, lead, dir, name) => `${dir}/bin` === binDir || (0, node_fs.existsSync)(`${dir}/bin/${name}`) ? whole : `${lead}${binDir}/${name}`);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Rewrites the absolute interpreter path of the shebang (line 1) and of pip's
|
|
699
|
+
* `'''exec'` line (line 2) when it lies under a directory other than `binDir`.
|
|
700
|
+
* Any other path in those lines (a comment) and any interpreter that still exists (a system shebang) is left alone.
|
|
701
|
+
* Null when nothing needs to change.
|
|
702
|
+
*/
|
|
703
|
+
function relocateInterpreterHeader(text, binDir) {
|
|
704
|
+
const lines = text.split("\n");
|
|
705
|
+
const first = relocateLine(lines[0] ?? "", SHEBANG_PATH, binDir);
|
|
706
|
+
const second = relocateLine(lines[1] ?? "", EXEC_LINE_PATH, binDir);
|
|
707
|
+
if (first === lines[0] && second === (lines[1] ?? "")) return null;
|
|
708
|
+
return [
|
|
709
|
+
first,
|
|
710
|
+
...lines.length > 1 ? [second] : [],
|
|
711
|
+
...lines.slice(2)
|
|
712
|
+
].join("\n");
|
|
713
|
+
}
|
|
714
|
+
function isRegularFile(path) {
|
|
715
|
+
try {
|
|
716
|
+
return (0, node_fs.lstatSync)(path).isFile();
|
|
717
|
+
} catch {
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Replaces `path` atomically (temp file beside it, same mode, rename): several
|
|
723
|
+
* runners call `ensurePython` at once and none may exec a half-written file.
|
|
724
|
+
*/
|
|
725
|
+
function writeFileAtomic$1(path, content, mode) {
|
|
726
|
+
const tmp = `${path}.heal-${process.pid}-${Date.now()}`;
|
|
727
|
+
(0, node_fs.writeFileSync)(tmp, content);
|
|
728
|
+
(0, node_fs.chmodSync)(tmp, mode);
|
|
729
|
+
(0, node_fs.renameSync)(tmp, path);
|
|
730
|
+
}
|
|
731
|
+
/** The first `PROBE_BYTES` bytes of a file, without reading the rest. */
|
|
732
|
+
function readProbe(path) {
|
|
733
|
+
const fd = (0, node_fs.openSync)(path, "r");
|
|
734
|
+
try {
|
|
735
|
+
const buf = Buffer.alloc(PROBE_BYTES);
|
|
736
|
+
const n = (0, node_fs.readSync)(fd, buf, 0, PROBE_BYTES, 0);
|
|
737
|
+
return buf.subarray(0, n);
|
|
738
|
+
} finally {
|
|
739
|
+
(0, node_fs.closeSync)(fd);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Brings the install in `pythonDir` up to date: the arch stubs are the
|
|
744
|
+
* relative form, and no regular script in `bin/` names an interpreter under
|
|
745
|
+
* another directory. Bounded to `bin/`, to the first two lines, to text files.
|
|
746
|
+
* Never throws; returns how many files it rewrote.
|
|
747
|
+
*/
|
|
748
|
+
function healPortablePython(pythonDir, archFlag, logger) {
|
|
749
|
+
const binDir = (0, node_path.join)(pythonDir, "bin");
|
|
750
|
+
let healed = 0;
|
|
751
|
+
try {
|
|
752
|
+
if (!(0, node_fs.existsSync)((0, node_path.join)(binDir, "python3.real"))) return 0;
|
|
753
|
+
const stub = renderArchStub(archFlag);
|
|
754
|
+
for (const name of ["python3", "python"]) {
|
|
755
|
+
const path = (0, node_path.join)(binDir, name);
|
|
756
|
+
if (!isRegularFile(path)) continue;
|
|
757
|
+
const text = (0, node_fs.readFileSync)(path, "utf8");
|
|
758
|
+
if (text === stub || !text.startsWith("#!/bin/sh\n")) continue;
|
|
759
|
+
if (!text.includes("/usr/bin/arch")) continue;
|
|
760
|
+
writeFileAtomic$1(path, stub, 493);
|
|
761
|
+
healed += 1;
|
|
762
|
+
}
|
|
763
|
+
for (const name of (0, node_fs.readdirSync)(binDir)) {
|
|
764
|
+
const path = (0, node_path.join)(binDir, name);
|
|
765
|
+
try {
|
|
766
|
+
if (!isRegularFile(path)) continue;
|
|
767
|
+
const stat = (0, node_fs.lstatSync)(path);
|
|
768
|
+
if (stat.size > MAX_SCRIPT_BYTES) continue;
|
|
769
|
+
const probe = readProbe(path);
|
|
770
|
+
if (probe.subarray(0, 2).toString("latin1") !== "#!" || probe.includes(0)) continue;
|
|
771
|
+
const next = relocateInterpreterHeader((0, node_fs.readFileSync)(path, "utf8"), binDir);
|
|
772
|
+
if (next === null) continue;
|
|
773
|
+
writeFileAtomic$1(path, next, stat.mode & 511);
|
|
774
|
+
healed += 1;
|
|
775
|
+
} catch (err) {
|
|
776
|
+
logger.warn("Could not repair a portable Python script", { meta: {
|
|
777
|
+
path,
|
|
778
|
+
error: require_composed_stable_id.errMsg(err)
|
|
779
|
+
} });
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
if (healed > 0) logger.info("Repaired portable Python after its folder moved", { meta: {
|
|
783
|
+
healed,
|
|
784
|
+
binDir
|
|
785
|
+
} });
|
|
786
|
+
} catch (err) {
|
|
787
|
+
logger.warn("Portable Python repair failed; continuing", { meta: {
|
|
788
|
+
binDir,
|
|
789
|
+
error: require_composed_stable_id.errMsg(err)
|
|
790
|
+
} });
|
|
791
|
+
}
|
|
792
|
+
return healed;
|
|
793
|
+
}
|
|
794
|
+
//#endregion
|
|
653
795
|
//#region src/deps/python-downloader.ts
|
|
654
796
|
/**
|
|
655
797
|
* Download portable Python (headless) from bjia56/portable-python.
|
|
@@ -730,6 +872,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
730
872
|
const pythonDir = (0, node_path.join)(dataDir, "deps", "python");
|
|
731
873
|
const pythonBin = (0, node_path.join)(pythonDir, ...pythonInterpreterSegments(process.platform));
|
|
732
874
|
if ((0, node_fs.existsSync)(pythonBin)) {
|
|
875
|
+
if (process.platform === "darwin") healPortablePython(pythonDir, archFlagFor(process.arch), logger);
|
|
733
876
|
logger.debug("Portable Python found", { meta: { pythonBin } });
|
|
734
877
|
return pythonBin;
|
|
735
878
|
}
|
|
@@ -768,18 +911,18 @@ async function ensurePython(dataDir, logger) {
|
|
|
768
911
|
}
|
|
769
912
|
(0, node_fs.chmodSync)(pythonBin, 493);
|
|
770
913
|
if (process.platform === "darwin") {
|
|
771
|
-
const realBin = (0, node_path.join)(pythonDir, "bin",
|
|
914
|
+
const realBin = (0, node_path.join)(pythonDir, "bin", REAL_PYTHON_NAME);
|
|
915
|
+
const archFlag = archFlagFor(process.arch);
|
|
772
916
|
if (!(0, node_fs.existsSync)(realBin)) {
|
|
773
917
|
(0, node_fs.renameSync)(pythonBin, realBin);
|
|
774
|
-
|
|
775
|
-
(0, node_fs.writeFileSync)(pythonBin, `#!/bin/sh\nexec /usr/bin/arch ${archFlag} "${realBin}" "$@"\n`);
|
|
918
|
+
(0, node_fs.writeFileSync)(pythonBin, renderArchStub(archFlag));
|
|
776
919
|
(0, node_fs.chmodSync)(pythonBin, 493);
|
|
777
920
|
const pyAlt = (0, node_path.join)(pythonDir, "bin", "python");
|
|
778
921
|
if ((0, node_fs.existsSync)(pyAlt)) {
|
|
779
922
|
try {
|
|
780
923
|
(0, node_fs.rmSync)(pyAlt, { force: true });
|
|
781
924
|
} catch {}
|
|
782
|
-
(0, node_fs.writeFileSync)(pyAlt,
|
|
925
|
+
(0, node_fs.writeFileSync)(pyAlt, renderArchStub(archFlag));
|
|
783
926
|
(0, node_fs.chmodSync)(pyAlt, 493);
|
|
784
927
|
}
|
|
785
928
|
logger.info("Locked python3 to host arch via shell stub", { meta: {
|
|
@@ -787,6 +930,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
787
930
|
realBin
|
|
788
931
|
} });
|
|
789
932
|
}
|
|
933
|
+
healPortablePython(pythonDir, archFlag, logger);
|
|
790
934
|
}
|
|
791
935
|
logger.info("Portable Python installed", { meta: {
|
|
792
936
|
version: PYTHON_VERSION,
|
package/dist/node.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { s as errMsg } from "./composed-stable-id-C7b2bPOX.mjs";
|
|
2
2
|
import { Cr as egressTransportFromRequest, Nr as isSoftwareDecode, Sr as canonicalEgressPlan, X as compositionSourceKey, _r as chooseFfmpegBinary, a as directedBroadcastOf, br as getFfmpegArtifact, c as implementedMethodsOf, gr as FFMPEG_PATH_ENV, i as buildMagicPacket, kr as buildFfmpegArgs, o as ipv4ToUint32, p as CUSTOMIZATION_BLOCK_NAME_PREFIX, s as parseMacAddress, vr as ffmpegBinaryName, wr as Fmp4BoxSplitter, xr as getFfmpegDownloadUrl, yr as FFMPEG_RELEASE } from "./wake-on-lan-DqbzeHhm.mjs";
|
|
3
3
|
import * as fs from "node:fs";
|
|
4
|
-
import { chmodSync, copyFileSync, createWriteStream, existsSync, mkdirSync, promises, readFileSync, readdirSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { chmodSync, closeSync, copyFileSync, createWriteStream, existsSync, lstatSync, mkdirSync, openSync, promises, readFileSync, readSync, readdirSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
5
5
|
import * as path$1 from "node:path";
|
|
6
6
|
import path, { basename, dirname, join, resolve, sep } from "node:path";
|
|
7
7
|
import { pipeline } from "node:stream/promises";
|
|
@@ -627,6 +627,148 @@ function writeOwnerOnlyFileSync(target, data, overrides = {}) {
|
|
|
627
627
|
}
|
|
628
628
|
}
|
|
629
629
|
//#endregion
|
|
630
|
+
//#region src/deps/python-portable-relocate.ts
|
|
631
|
+
/**
|
|
632
|
+
* Keeps the macOS portable Python runnable after its folder is moved.
|
|
633
|
+
*
|
|
634
|
+
* The arch-locking stub that replaces `bin/python3` used to embed the absolute
|
|
635
|
+
* path of `python3.real`, and pip's console scripts embed the absolute path of
|
|
636
|
+
* the interpreter that installed them. Move the data directory and every one of
|
|
637
|
+
* them points at a path that no longer exists. The stub now finds the real
|
|
638
|
+
* binary beside itself at run time, and {@link healPortablePython} rewrites the
|
|
639
|
+
* leftovers of an install made before that.
|
|
640
|
+
*/
|
|
641
|
+
/** The `arch` flag for a Node `process.arch` value. */
|
|
642
|
+
function archFlagFor(arch) {
|
|
643
|
+
return arch === "arm64" ? "-arm64" : "-x86_64";
|
|
644
|
+
}
|
|
645
|
+
/** Name of the renamed interpreter the stubs delegate to. */
|
|
646
|
+
var REAL_PYTHON_NAME = "python3.real";
|
|
647
|
+
/** Scripts bigger than this are never rewritten (a binary or a data file). */
|
|
648
|
+
var MAX_SCRIPT_BYTES = 1048576;
|
|
649
|
+
/** How much of a file is read to tell a script from a binary. */
|
|
650
|
+
var PROBE_BYTES = 512;
|
|
651
|
+
/**
|
|
652
|
+
* The text of the arch stub. It resolves `python3.real` relative to its own
|
|
653
|
+
* location (following a symlinked invocation), so it survives a move.
|
|
654
|
+
*/
|
|
655
|
+
function renderArchStub(archFlag) {
|
|
656
|
+
return [
|
|
657
|
+
"#!/bin/sh",
|
|
658
|
+
"p=$0",
|
|
659
|
+
"while [ -L \"$p\" ]; do",
|
|
660
|
+
" l=$(readlink \"$p\")",
|
|
661
|
+
" case $l in /*) p=$l ;; *) p=$(dirname \"$p\")/$l ;; esac",
|
|
662
|
+
"done",
|
|
663
|
+
`exec /usr/bin/arch ${archFlag} "$(cd "$(dirname "$p")" && pwd)/${REAL_PYTHON_NAME}" "$@"`,
|
|
664
|
+
""
|
|
665
|
+
].join("\n");
|
|
666
|
+
}
|
|
667
|
+
/** A shebang naming an absolute portable interpreter: `#!<dir>/bin/python3`. */
|
|
668
|
+
var SHEBANG_PATH = /^(#!)(\/[^"'\n]+?)\/bin\/(python3\.real|python3|python)(?![\w.])/;
|
|
669
|
+
/** The pip console-script re-exec line: `'''exec' "<dir>/bin/python3.real" "$0" "$@"`. */
|
|
670
|
+
var EXEC_LINE_PATH = /^('''exec' ")(\/[^"\n]+?)\/bin\/(python3\.real)(?![\w.])/;
|
|
671
|
+
function relocateLine(line, pattern, binDir) {
|
|
672
|
+
return line.replace(pattern, (whole, lead, dir, name) => `${dir}/bin` === binDir || existsSync(`${dir}/bin/${name}`) ? whole : `${lead}${binDir}/${name}`);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Rewrites the absolute interpreter path of the shebang (line 1) and of pip's
|
|
676
|
+
* `'''exec'` line (line 2) when it lies under a directory other than `binDir`.
|
|
677
|
+
* Any other path in those lines (a comment) and any interpreter that still exists (a system shebang) is left alone.
|
|
678
|
+
* Null when nothing needs to change.
|
|
679
|
+
*/
|
|
680
|
+
function relocateInterpreterHeader(text, binDir) {
|
|
681
|
+
const lines = text.split("\n");
|
|
682
|
+
const first = relocateLine(lines[0] ?? "", SHEBANG_PATH, binDir);
|
|
683
|
+
const second = relocateLine(lines[1] ?? "", EXEC_LINE_PATH, binDir);
|
|
684
|
+
if (first === lines[0] && second === (lines[1] ?? "")) return null;
|
|
685
|
+
return [
|
|
686
|
+
first,
|
|
687
|
+
...lines.length > 1 ? [second] : [],
|
|
688
|
+
...lines.slice(2)
|
|
689
|
+
].join("\n");
|
|
690
|
+
}
|
|
691
|
+
function isRegularFile(path) {
|
|
692
|
+
try {
|
|
693
|
+
return lstatSync(path).isFile();
|
|
694
|
+
} catch {
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Replaces `path` atomically (temp file beside it, same mode, rename): several
|
|
700
|
+
* runners call `ensurePython` at once and none may exec a half-written file.
|
|
701
|
+
*/
|
|
702
|
+
function writeFileAtomic$1(path, content, mode) {
|
|
703
|
+
const tmp = `${path}.heal-${process.pid}-${Date.now()}`;
|
|
704
|
+
writeFileSync(tmp, content);
|
|
705
|
+
chmodSync(tmp, mode);
|
|
706
|
+
renameSync(tmp, path);
|
|
707
|
+
}
|
|
708
|
+
/** The first `PROBE_BYTES` bytes of a file, without reading the rest. */
|
|
709
|
+
function readProbe(path) {
|
|
710
|
+
const fd = openSync(path, "r");
|
|
711
|
+
try {
|
|
712
|
+
const buf = Buffer.alloc(PROBE_BYTES);
|
|
713
|
+
const n = readSync(fd, buf, 0, PROBE_BYTES, 0);
|
|
714
|
+
return buf.subarray(0, n);
|
|
715
|
+
} finally {
|
|
716
|
+
closeSync(fd);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Brings the install in `pythonDir` up to date: the arch stubs are the
|
|
721
|
+
* relative form, and no regular script in `bin/` names an interpreter under
|
|
722
|
+
* another directory. Bounded to `bin/`, to the first two lines, to text files.
|
|
723
|
+
* Never throws; returns how many files it rewrote.
|
|
724
|
+
*/
|
|
725
|
+
function healPortablePython(pythonDir, archFlag, logger) {
|
|
726
|
+
const binDir = join(pythonDir, "bin");
|
|
727
|
+
let healed = 0;
|
|
728
|
+
try {
|
|
729
|
+
if (!existsSync(join(binDir, "python3.real"))) return 0;
|
|
730
|
+
const stub = renderArchStub(archFlag);
|
|
731
|
+
for (const name of ["python3", "python"]) {
|
|
732
|
+
const path = join(binDir, name);
|
|
733
|
+
if (!isRegularFile(path)) continue;
|
|
734
|
+
const text = readFileSync(path, "utf8");
|
|
735
|
+
if (text === stub || !text.startsWith("#!/bin/sh\n")) continue;
|
|
736
|
+
if (!text.includes("/usr/bin/arch")) continue;
|
|
737
|
+
writeFileAtomic$1(path, stub, 493);
|
|
738
|
+
healed += 1;
|
|
739
|
+
}
|
|
740
|
+
for (const name of readdirSync(binDir)) {
|
|
741
|
+
const path = join(binDir, name);
|
|
742
|
+
try {
|
|
743
|
+
if (!isRegularFile(path)) continue;
|
|
744
|
+
const stat = lstatSync(path);
|
|
745
|
+
if (stat.size > MAX_SCRIPT_BYTES) continue;
|
|
746
|
+
const probe = readProbe(path);
|
|
747
|
+
if (probe.subarray(0, 2).toString("latin1") !== "#!" || probe.includes(0)) continue;
|
|
748
|
+
const next = relocateInterpreterHeader(readFileSync(path, "utf8"), binDir);
|
|
749
|
+
if (next === null) continue;
|
|
750
|
+
writeFileAtomic$1(path, next, stat.mode & 511);
|
|
751
|
+
healed += 1;
|
|
752
|
+
} catch (err) {
|
|
753
|
+
logger.warn("Could not repair a portable Python script", { meta: {
|
|
754
|
+
path,
|
|
755
|
+
error: errMsg(err)
|
|
756
|
+
} });
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (healed > 0) logger.info("Repaired portable Python after its folder moved", { meta: {
|
|
760
|
+
healed,
|
|
761
|
+
binDir
|
|
762
|
+
} });
|
|
763
|
+
} catch (err) {
|
|
764
|
+
logger.warn("Portable Python repair failed; continuing", { meta: {
|
|
765
|
+
binDir,
|
|
766
|
+
error: errMsg(err)
|
|
767
|
+
} });
|
|
768
|
+
}
|
|
769
|
+
return healed;
|
|
770
|
+
}
|
|
771
|
+
//#endregion
|
|
630
772
|
//#region src/deps/python-downloader.ts
|
|
631
773
|
/**
|
|
632
774
|
* Download portable Python (headless) from bjia56/portable-python.
|
|
@@ -707,6 +849,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
707
849
|
const pythonDir = join(dataDir, "deps", "python");
|
|
708
850
|
const pythonBin = join(pythonDir, ...pythonInterpreterSegments(process.platform));
|
|
709
851
|
if (existsSync(pythonBin)) {
|
|
852
|
+
if (process.platform === "darwin") healPortablePython(pythonDir, archFlagFor(process.arch), logger);
|
|
710
853
|
logger.debug("Portable Python found", { meta: { pythonBin } });
|
|
711
854
|
return pythonBin;
|
|
712
855
|
}
|
|
@@ -745,18 +888,18 @@ async function ensurePython(dataDir, logger) {
|
|
|
745
888
|
}
|
|
746
889
|
chmodSync(pythonBin, 493);
|
|
747
890
|
if (process.platform === "darwin") {
|
|
748
|
-
const realBin = join(pythonDir, "bin",
|
|
891
|
+
const realBin = join(pythonDir, "bin", REAL_PYTHON_NAME);
|
|
892
|
+
const archFlag = archFlagFor(process.arch);
|
|
749
893
|
if (!existsSync(realBin)) {
|
|
750
894
|
renameSync(pythonBin, realBin);
|
|
751
|
-
|
|
752
|
-
writeFileSync(pythonBin, `#!/bin/sh\nexec /usr/bin/arch ${archFlag} "${realBin}" "$@"\n`);
|
|
895
|
+
writeFileSync(pythonBin, renderArchStub(archFlag));
|
|
753
896
|
chmodSync(pythonBin, 493);
|
|
754
897
|
const pyAlt = join(pythonDir, "bin", "python");
|
|
755
898
|
if (existsSync(pyAlt)) {
|
|
756
899
|
try {
|
|
757
900
|
rmSync(pyAlt, { force: true });
|
|
758
901
|
} catch {}
|
|
759
|
-
writeFileSync(pyAlt,
|
|
902
|
+
writeFileSync(pyAlt, renderArchStub(archFlag));
|
|
760
903
|
chmodSync(pyAlt, 493);
|
|
761
904
|
}
|
|
762
905
|
logger.info("Locked python3 to host arch via shell stub", { meta: {
|
|
@@ -764,6 +907,7 @@ async function ensurePython(dataDir, logger) {
|
|
|
764
907
|
realBin
|
|
765
908
|
} });
|
|
766
909
|
}
|
|
910
|
+
healPortablePython(pythonDir, archFlag, logger);
|
|
767
911
|
}
|
|
768
912
|
logger.info("Portable Python installed", { meta: {
|
|
769
913
|
version: PYTHON_VERSION,
|