@pylair/cli 0.1.0-rc.15 → 0.1.0-rc.17
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/README.md +1 -1
- package/dist/build/bytecode.d.ts +29 -0
- package/dist/build/bytecode.d.ts.map +1 -0
- package/dist/build/bytecode.js +64 -0
- package/dist/build/bytecode.js.map +1 -0
- package/dist/build/download.d.ts +53 -0
- package/dist/build/download.d.ts.map +1 -0
- package/dist/build/download.js +284 -0
- package/dist/build/download.js.map +1 -0
- package/dist/build/notices.d.ts +10 -0
- package/dist/build/notices.d.ts.map +1 -0
- package/dist/build/notices.js +108 -0
- package/dist/build/notices.js.map +1 -0
- package/dist/build/pbs.d.ts +12 -0
- package/dist/build/pbs.d.ts.map +1 -0
- package/dist/build/pbs.js +36 -0
- package/dist/build/pbs.js.map +1 -0
- package/dist/build/python.d.ts +19 -0
- package/dist/build/python.d.ts.map +1 -0
- package/dist/build/python.js +109 -0
- package/dist/build/python.js.map +1 -0
- package/dist/build/sign.d.ts +4 -0
- package/dist/build/sign.d.ts.map +1 -0
- package/dist/build/sign.js +108 -0
- package/dist/build/sign.js.map +1 -0
- package/dist/build/source.d.ts +16 -0
- package/dist/build/source.d.ts.map +1 -0
- package/dist/build/source.js +85 -0
- package/dist/build/source.js.map +1 -0
- package/dist/build/util.d.ts +4 -0
- package/dist/build/util.d.ts.map +1 -0
- package/dist/build/util.js +12 -0
- package/dist/build/util.js.map +1 -0
- package/dist/build.d.ts +6 -64
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +36 -582
- package/dist/build.js.map +1 -1
- package/dist/cli.d.ts +5 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +25 -5
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -0
- package/dist/config.js.map +1 -1
- package/package.json +1 -1
package/dist/build.js
CHANGED
|
@@ -3,590 +3,44 @@
|
|
|
3
3
|
* interpreter (native wheels only — no cross-compile) → stage app
|
|
4
4
|
* source → prune → tauri build → unsigned warning.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
/*
|
|
7
|
+
* Where each step lives. build() below calls into them; this file also
|
|
8
|
+
* re-exports the helpers other modules and the tests import from here.
|
|
9
|
+
*
|
|
10
|
+
* build/pbs.ts python-build-standalone release, versions, host
|
|
11
|
+
* triples, committed SHA-256 table, hostTriple()
|
|
12
|
+
* build/download.ts the interpreter download: retry, resume, progress,
|
|
13
|
+
* timeout, cache and SHA-256 check (ensureTarball)
|
|
14
|
+
* build/python.ts fetchPython (unpack CPython), uv export and
|
|
15
|
+
* `uv pip install` into site-packages (stageEnv)
|
|
16
|
+
* build/source.ts stageSource: copy the app source, refuse secrets
|
|
17
|
+
* build/notices.ts stageNotices: THIRD-PARTY-NOTICES for the bundle
|
|
18
|
+
* build/bytecode.ts compileBytecode: checked-hash pycs for the bundle
|
|
19
|
+
* build/sign.ts macOS ad-hoc signing of the built .app (adHocSign)
|
|
20
|
+
* build/util.ts isWin and run(), shared by build() and the modules
|
|
21
|
+
* ./prune.ts pruning the staged tree (DEFAULT_PRUNE, prune)
|
|
22
|
+
* ./config.ts [tool.pylair] and tauri.conf.json loading
|
|
23
|
+
*/
|
|
24
|
+
import { spawnSync } from "node:child_process";
|
|
25
|
+
import { existsSync, rmSync } from "node:fs";
|
|
10
26
|
import { arch, platform, tmpdir } from "node:os";
|
|
11
|
-
import {
|
|
27
|
+
import { join } from "node:path";
|
|
12
28
|
import { fileURLToPath } from "node:url";
|
|
13
|
-
import { importRoot, loadConfig, loadTauriConfig, tauriCliEntry } from "./config.js";
|
|
14
|
-
import { matchGlob } from "./glob.js";
|
|
29
|
+
import { importRoot, loadConfig, loadTauriConfig, tauriCliEntry, windowsSigningConfigured, } from "./config.js";
|
|
15
30
|
import { DEFAULT_PRUNE, prune } from "./prune.js";
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
* release. Bump together with PBS_TAG/PBS_VERSIONS; verify each value
|
|
30
|
-
* against upstream SHA256SUMS before committing.
|
|
31
|
-
*/
|
|
32
|
-
const PBS_SHA256 = {
|
|
33
|
-
"3.12.12-aarch64-apple-darwin": "9955ac87eff0d561e64920dc46fd8625912d2c2be4a53c66ad80125d6d9fbdb7",
|
|
34
|
-
"3.12.12-aarch64-unknown-linux-gnu": "4c4594cb38ddf6769f11f86818a4c6617652f4994e5a962eb4f8cf0ec977a1be",
|
|
35
|
-
"3.12.12-x86_64-apple-darwin": "1b4d8eec597ff69a3b67946f30622a6c25c6612e6d15aa431b864ebff66ac8a1",
|
|
36
|
-
"3.12.12-x86_64-pc-windows-msvc": "70ecfaf0bfc58298db11d6389be92c29ec4ef8cd93ebb6a7e54b41d9fc056b10",
|
|
37
|
-
"3.12.12-x86_64-unknown-linux-gnu": "8d2cd1393c1aec9d701cebeccb83d1519951f71d3ecb42d5e70c70af4db9be34",
|
|
38
|
-
"3.13.9-aarch64-apple-darwin": "29d38bd5fbe267b37cecac990015c662796edcf51c2f5e321d59ec06a1850816",
|
|
39
|
-
"3.13.9-aarch64-unknown-linux-gnu": "fdc5d80488b64fcef3b99f297618f68e5374c13d89e888a66abeff4967afd308",
|
|
40
|
-
"3.13.9-x86_64-apple-darwin": "9da51afb00480a8ccd4909bd302c80d9c104d633c5e865cf37dd5e587185e125",
|
|
41
|
-
"3.13.9-x86_64-pc-windows-msvc": "f4c22b31ddbf8d7824cbcba2d8707621c2c8fab1fb6d2c1810c2bb0304d8e9a8",
|
|
42
|
-
"3.13.9-x86_64-unknown-linux-gnu": "1bc9a94ab2425fb6ae58e45f69cab61647759b57895649701390822d976fa923",
|
|
43
|
-
};
|
|
44
|
-
export function hostTriple() {
|
|
45
|
-
const t = HOST_TRIPLES[`${platform()}:${arch()}`];
|
|
46
|
-
if (!t)
|
|
47
|
-
throw new Error(`no standalone CPython build for ${platform()}/${arch()}`);
|
|
48
|
-
return t;
|
|
49
|
-
}
|
|
50
|
-
const DOWNLOAD_IDLE_TIMEOUT_MS = 30_000;
|
|
51
|
-
const MAX_REDIRECTS = 5;
|
|
52
|
-
/** Download `url` to `to`, following up to `maxRedirects` redirects and
|
|
53
|
-
* failing after `timeoutMs` with no bytes. The body lands in `to.part`
|
|
54
|
-
* and is renamed into place only once it is complete — a dropped
|
|
55
|
-
* connection must never leave a partial file at the cache path (the
|
|
56
|
-
* SHA check would then refuse every later build until it is deleted by
|
|
57
|
-
* hand). `get` is injectable so tests need no network. */
|
|
58
|
-
export function download(url, to, opts) {
|
|
59
|
-
const getImpl = opts?.get ?? get;
|
|
60
|
-
const timeoutMs = opts?.timeoutMs ?? DOWNLOAD_IDLE_TIMEOUT_MS;
|
|
61
|
-
const maxRedirects = opts?.maxRedirects ?? MAX_REDIRECTS;
|
|
62
|
-
const part = `${to}.part`;
|
|
63
|
-
return new Promise((res, rej) => {
|
|
64
|
-
let settled = false;
|
|
65
|
-
let out;
|
|
66
|
-
const fail = (e) => {
|
|
67
|
-
if (settled)
|
|
68
|
-
return;
|
|
69
|
-
settled = true;
|
|
70
|
-
if (out === undefined) {
|
|
71
|
-
rmSync(part, { force: true });
|
|
72
|
-
rej(e);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
// The stream owns the fd. destroy() waits for in-flight writes
|
|
76
|
-
// before closing it — closing early (closeSync) or twice lets a
|
|
77
|
-
// reused fd number absorb the stale op: the write lands in some
|
|
78
|
-
// other file, or the close kills it. .part is removed only once
|
|
79
|
-
// 'close' says the fd is really gone, keeping the rejection
|
|
80
|
-
// ordered after cleanup.
|
|
81
|
-
const s = out;
|
|
82
|
-
out = undefined;
|
|
83
|
-
s.once("close", () => {
|
|
84
|
-
rmSync(part, { force: true });
|
|
85
|
-
rej(e);
|
|
86
|
-
});
|
|
87
|
-
s.destroy();
|
|
88
|
-
};
|
|
89
|
-
const onErr = (e) => fail(e instanceof Error ? e : new Error(String(e)));
|
|
90
|
-
const follow = (u, redirects) => {
|
|
91
|
-
try {
|
|
92
|
-
if (redirects > maxRedirects)
|
|
93
|
-
return fail(new Error(`GET ${url}: too many redirects (cap ${maxRedirects})`));
|
|
94
|
-
const req = getImpl(u, (r) => {
|
|
95
|
-
try {
|
|
96
|
-
if (r.statusCode >= 300 && r.statusCode < 400 && r.headers.location) {
|
|
97
|
-
// Location may be relative, and only https targets are
|
|
98
|
-
// fetchable — resolve first, refuse anything else.
|
|
99
|
-
const target = new URL(r.headers.location, u);
|
|
100
|
-
if (target.protocol !== "https:")
|
|
101
|
-
return fail(new Error(`GET ${u}: refusing non-https redirect to ${target.origin}`));
|
|
102
|
-
return follow(target.href, redirects + 1);
|
|
103
|
-
}
|
|
104
|
-
if (r.statusCode !== 200)
|
|
105
|
-
return fail(new Error(`GET ${u} -> ${r.statusCode}`));
|
|
106
|
-
const want = Number(r.headers["content-length"]);
|
|
107
|
-
let got = 0;
|
|
108
|
-
let ended = false;
|
|
109
|
-
r.on("data", (c) => {
|
|
110
|
-
got += c.length;
|
|
111
|
-
});
|
|
112
|
-
r.on("end", () => {
|
|
113
|
-
ended = true;
|
|
114
|
-
});
|
|
115
|
-
r.on("error", fail);
|
|
116
|
-
r.on("close", () => {
|
|
117
|
-
if (!ended)
|
|
118
|
-
fail(new Error(`GET ${u}: connection dropped mid-body`));
|
|
119
|
-
});
|
|
120
|
-
// Open eagerly — <part>'s existence is synchronous, so no
|
|
121
|
-
// deferred fs.open can land after fail()'s rmSync and recreate
|
|
122
|
-
// the file. autoClose: false keeps fd closing on our explicit
|
|
123
|
-
// destroy() calls only (see fail()).
|
|
124
|
-
const stream = createWriteStream(part, {
|
|
125
|
-
fd: openSync(part, "w"),
|
|
126
|
-
autoClose: false,
|
|
127
|
-
});
|
|
128
|
-
out = stream;
|
|
129
|
-
r.pipe(stream);
|
|
130
|
-
stream.on("finish", () => {
|
|
131
|
-
if (settled)
|
|
132
|
-
return;
|
|
133
|
-
out = undefined;
|
|
134
|
-
stream.once("close", () => {
|
|
135
|
-
if (!Number.isNaN(want) && got !== want)
|
|
136
|
-
return fail(new Error(`GET ${u}: incomplete body — ${got} of ${want} bytes`));
|
|
137
|
-
settled = true;
|
|
138
|
-
renameSync(part, to);
|
|
139
|
-
res();
|
|
140
|
-
});
|
|
141
|
-
stream.destroy();
|
|
142
|
-
});
|
|
143
|
-
stream.on("error", fail);
|
|
144
|
-
}
|
|
145
|
-
catch (e) {
|
|
146
|
-
onErr(e);
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
req.setTimeout(timeoutMs, () => req.destroy(new Error(`GET ${u}: timed out — no data for ${timeoutMs / 1000}s`)));
|
|
150
|
-
req.on("error", fail);
|
|
151
|
-
}
|
|
152
|
-
catch (e) {
|
|
153
|
-
onErr(e);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
follow(url, 0);
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
const sha256File = (p) => createHash("sha256").update(readFileSync(p)).digest("hex");
|
|
160
|
-
/** Cache + integrity for a downloaded asset: a cached file that fails
|
|
161
|
-
* its SHA is deleted and re-downloaded ONCE — a second mismatch still
|
|
162
|
-
* refuses, naming the cache so it is fixable by hand. `dl` injectable
|
|
163
|
-
* for tests. */
|
|
164
|
-
export async function ensureTarball(url, tarball, want, dl = download) {
|
|
165
|
-
const cached = existsSync(tarball);
|
|
166
|
-
if (!cached) {
|
|
167
|
-
console.log(`fetching ${basename(tarball)}`);
|
|
168
|
-
await dl(url, tarball);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
console.log(`using cached ${basename(tarball)}`);
|
|
172
|
-
}
|
|
173
|
-
if (sha256File(tarball) === want)
|
|
174
|
-
return;
|
|
175
|
-
if (!cached)
|
|
176
|
-
throw new Error(`SHA256 mismatch for ${basename(tarball)}: got ${sha256File(tarball)}, ` +
|
|
177
|
-
`want ${want} — refusing to bundle`);
|
|
178
|
-
// A cached-but-corrupt file is the dropped-partial failure mode: one
|
|
179
|
-
// retry, then refuse.
|
|
180
|
-
console.warn(`cached ${basename(tarball)} failed its SHA256 — ` +
|
|
181
|
-
`deleting ${tarball} and re-downloading once`);
|
|
182
|
-
rmSync(tarball, { force: true });
|
|
183
|
-
await dl(url, tarball);
|
|
184
|
-
const got = sha256File(tarball);
|
|
185
|
-
if (got !== want)
|
|
186
|
-
throw new Error(`SHA256 mismatch for ${basename(tarball)} after re-download: got ${got}, ` +
|
|
187
|
-
`want ${want} — refusing to bundle (cache file: ${tarball})`);
|
|
188
|
-
}
|
|
189
|
-
async function fetchPython(resources, cacheDir, versionClass) {
|
|
190
|
-
const ver = PBS_VERSIONS[versionClass];
|
|
191
|
-
if (!ver)
|
|
192
|
-
throw new Error(`python-version "${versionClass}" not available — supported: ${Object.keys(PBS_VERSIONS).join(", ")}`);
|
|
193
|
-
const triple = hostTriple();
|
|
194
|
-
const asset = `cpython-${ver}+${PBS_TAG}-${triple}-install_only_stripped.tar.gz`;
|
|
195
|
-
const base = `https://github.com/astral-sh/python-build-standalone/releases/download/${PBS_TAG}`;
|
|
196
|
-
mkdirSync(resources, { recursive: true });
|
|
197
|
-
mkdirSync(cacheDir, { recursive: true });
|
|
198
|
-
const tarball = join(cacheDir, asset);
|
|
199
|
-
// The interpreter ships to end users — verify against hashes committed
|
|
200
|
-
// in-repo (see PBS_SHA256), not a checksum file from the same release.
|
|
201
|
-
const want = PBS_SHA256[`${ver}-${triple}`];
|
|
202
|
-
if (!want)
|
|
203
|
-
throw new Error(`no committed SHA256 for ${ver}/${triple}`);
|
|
204
|
-
await ensureTarball(`${base}/${asset}`, tarball, want);
|
|
205
|
-
const dest = join(resources, "python");
|
|
206
|
-
rmSync(dest, { recursive: true, force: true });
|
|
207
|
-
mkdirSync(dest, { recursive: true });
|
|
208
|
-
// Extract in-process — same lesson as the tomllib probe: whichever
|
|
209
|
-
// `tar` PATH resolves to can differ (System32 bsdtar vs Git Bash GNU
|
|
210
|
-
// tar, which dies on drive-letter paths). The `tar` package handles
|
|
211
|
-
// pax long names (>100 chars) and the bin/python3 -> python3.12
|
|
212
|
-
// symlinks the POSIX builds carry.
|
|
213
|
-
await untar({ file: tarball, cwd: dest, strip: 1 });
|
|
214
|
-
const py = join(dest, isWin ? "python.exe" : "bin/python3");
|
|
215
|
-
if (!existsSync(py))
|
|
216
|
-
throw new Error(`expected interpreter at ${py}`);
|
|
217
|
-
return py;
|
|
218
|
-
}
|
|
219
|
-
function stageEnv(resources, python, appDir, reqs, cacheDir) {
|
|
220
|
-
const target = join(resources, "site-packages");
|
|
221
|
-
rmSync(target, { recursive: true, force: true });
|
|
222
|
-
// dev uses `uv sync` (uv.lock) — build must install the SAME locked set,
|
|
223
|
-
// hashed, or dev/prod parity is a fiction. requirements.txt is the
|
|
224
|
-
// fallback for projects that don't lock.
|
|
225
|
-
const lock = join(appDir, "uv.lock");
|
|
226
|
-
let reqsFile = reqs;
|
|
227
|
-
let exported;
|
|
228
|
-
if (existsSync(lock)) {
|
|
229
|
-
exported = join(cacheDir, "requirements.exported.txt");
|
|
230
|
-
execFileSync("uv", ["export", "--frozen", "--format", "requirements-txt", "-o", exported], { cwd: appDir, stdio: "inherit" });
|
|
231
|
-
reqsFile = exported;
|
|
232
|
-
}
|
|
233
|
-
else if (!existsSync(reqs)) {
|
|
234
|
-
console.log(`no uv.lock and no ${reqs} — bundling stdlib only`);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
console.warn("warning: no uv.lock — building from requirements.txt; " +
|
|
239
|
-
"the bundled env may drift from `pylair dev`'s env");
|
|
240
|
-
}
|
|
241
|
-
execFileSync("uv", ["pip", "install", "--python", python, "--target", target, "-r", reqsFile], { cwd: appDir, stdio: "inherit" });
|
|
242
|
-
}
|
|
243
|
-
function run(cmd, args, cwd) {
|
|
244
|
-
const r = spawnSync(cmd, args, { cwd, stdio: "inherit" });
|
|
245
|
-
if (r.error)
|
|
246
|
-
throw new Error(`${cmd} ${args.join(" ")} failed to start: ${r.error.message}`);
|
|
247
|
-
if (r.status !== 0)
|
|
248
|
-
throw new Error(`${cmd} ${args.join(" ")} exited ${r.status}`);
|
|
249
|
-
}
|
|
250
|
-
/** pyc invalidation mode shipped in installers — see docs/build.md.
|
|
251
|
-
* checked-hash: installers rewrite mtimes, so timestamp pycs silently
|
|
252
|
-
* invalidate; hash-based headers survive repackaging. checked (not
|
|
253
|
-
* unchecked) re-validates the source hash per import — ~10ms slower on
|
|
254
|
-
* a numpy-sized tree but a post-install source edit recompiles instead
|
|
255
|
-
* of running stale bytecode. Measured (Windows, numpy tree):
|
|
256
|
-
* ~665ms no-pyc / ~267ms checked / ~256ms unchecked per cold import.
|
|
257
|
-
*/
|
|
258
|
-
export const PYC_INVALIDATION_MODE = "checked-hash";
|
|
259
|
-
/** Precompile the staged .py tree with the BUNDLED interpreter — same
|
|
260
|
-
* CPython and magic number as the runtime that will load the pycs.
|
|
261
|
-
* An installed app (possibly read-only) must not compile its Python on
|
|
262
|
-
* every launch. Runs post-prune so denied dirs stay uncompiled, and
|
|
263
|
-
* relative args + cwd keep co_filename bundle-relative: identical pycs
|
|
264
|
-
* regardless of build path, tracebacks show `site-packages/…`.
|
|
265
|
-
* `-s`/`-d` would also work; relative args are the smallest version of
|
|
266
|
-
* the same thing.
|
|
267
|
-
*
|
|
268
|
-
* Exit 1 from compileall means "some files failed to compile" — a
|
|
269
|
-
* dependency may ship a py2-only file or invalid fixture that nothing
|
|
270
|
-
* imports. That warns (paths returned) instead of failing the build;
|
|
271
|
-
* the files are left uncompiled and will compile — or fail to import —
|
|
272
|
-
* as normal at runtime. Real failures still throw: the interpreter
|
|
273
|
-
* not starting, a signal, or any other exit code. Returns the failed
|
|
274
|
-
* paths (empty on full success). */
|
|
275
|
-
export function compileBytecode(resources, python, dirs) {
|
|
276
|
-
const existing = dirs.filter((d) => existsSync(join(resources, d)));
|
|
277
|
-
if (!existing.length)
|
|
278
|
-
return [];
|
|
279
|
-
const r = spawnSync(python, [
|
|
280
|
-
"-m",
|
|
281
|
-
"compileall",
|
|
282
|
-
"-q",
|
|
283
|
-
"-f",
|
|
284
|
-
"-j",
|
|
285
|
-
"0",
|
|
286
|
-
"--invalidation-mode",
|
|
287
|
-
PYC_INVALIDATION_MODE,
|
|
288
|
-
...existing,
|
|
289
|
-
], { cwd: resources, encoding: "utf-8" });
|
|
290
|
-
if (r.error)
|
|
291
|
-
throw new Error(`${python} -m compileall failed to start: ${r.error.message}`);
|
|
292
|
-
if (r.signal || (r.status !== 0 && r.status !== 1)) {
|
|
293
|
-
throw new Error(`${python} -m compileall ${r.signal ? `died on ${r.signal}` : `exited ${r.status}`}`);
|
|
294
|
-
}
|
|
295
|
-
if (r.status === 0)
|
|
296
|
-
return [];
|
|
297
|
-
// Quiet mode still prints `*** Error compiling '<path>'...` per file.
|
|
298
|
-
const failed = [
|
|
299
|
-
...String(r.stderr).matchAll(/^\*\*\* Error compiling '([^']+)'/gm),
|
|
300
|
-
...String(r.stdout).matchAll(/^\*\*\* Error compiling '([^']+)'/gm),
|
|
301
|
-
].map((m) => m[1].replace(/\\\\/g, "\\")); // repr-escaped on Windows
|
|
302
|
-
const shown = failed.slice(0, 10).join("\n ");
|
|
303
|
-
console.warn(`warning: ${failed.length} file(s) failed to byte-compile and are left` +
|
|
304
|
-
` uncompiled — they will compile (or fail to import) as normal at` +
|
|
305
|
-
` runtime:\n ${shown}${failed.length > 10 ? `\n …and ${failed.length - 10} more` : ""}`);
|
|
306
|
-
return failed;
|
|
307
|
-
}
|
|
308
|
-
// ── macOS ad-hoc signing ──────────────────────────────────────────────
|
|
309
|
-
// Mach-O magic numbers, read as the first BE u32 — includes the
|
|
310
|
-
// byte-swapped forms and both fat headers.
|
|
311
|
-
const MACHO_MAGIC = new Set([
|
|
312
|
-
0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe,
|
|
313
|
-
0xcafebabe, 0xbebafeca, 0xcafebabf, 0xbfbafeca,
|
|
314
|
-
]);
|
|
315
|
-
function isMachO(p) {
|
|
316
|
-
const fd = openSync(p, "r");
|
|
317
|
-
try {
|
|
318
|
-
const b = Buffer.alloc(4);
|
|
319
|
-
return readSync(fd, b, 0, 4, 0) === 4 && MACHO_MAGIC.has(b.readUInt32BE(0));
|
|
320
|
-
}
|
|
321
|
-
finally {
|
|
322
|
-
closeSync(fd);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
/** Re-sign a built .app ad-hoc and REQUIRE it to verify. With no signing
|
|
326
|
-
* identity Tauri ships only the linker's ad-hoc stub on the main binary —
|
|
327
|
-
* a signature that doesn't cover the bundle is the worst state for
|
|
328
|
-
* Gatekeeper ("is damaged"), worse than none at all. Signs every Mach-O
|
|
329
|
-
* nested-first rather than `codesign --deep`, which is documented to miss
|
|
330
|
-
* binaries in layouts it doesn't model — a vendored CPython tree is
|
|
331
|
-
* exactly that. Verify with --deep --strict; if it fails, the build
|
|
332
|
-
* fails — a broken signature must not ship silently.
|
|
333
|
-
*/
|
|
334
|
-
function codesignVerify(path) {
|
|
335
|
-
const v = spawnSync("codesign", ["--verify", "--deep", "--strict", path], {
|
|
336
|
-
encoding: "utf-8",
|
|
337
|
-
});
|
|
338
|
-
return v.status === 0 ? null : (v.stderr ?? "").trim();
|
|
339
|
-
}
|
|
340
|
-
function adHocSign(tauriDir, productName) {
|
|
341
|
-
const bundleDir = join(tauriDir, "target", "release", "bundle", "macos");
|
|
342
|
-
if (!existsSync(bundleDir))
|
|
343
|
-
return; // --bundles skipped app — nothing to sign
|
|
344
|
-
const app = readdirSync(bundleDir).find((n) => n.endsWith(".app"));
|
|
345
|
-
if (!app)
|
|
346
|
-
return;
|
|
347
|
-
const appPath = join(bundleDir, app);
|
|
348
|
-
const machos = [];
|
|
349
|
-
const walk = (dir) => {
|
|
350
|
-
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
351
|
-
const p = join(dir, e.name);
|
|
352
|
-
// Dirent types describe the link, not the target — symlinked files
|
|
353
|
-
// and dirs are skipped; the real binary is signed in place.
|
|
354
|
-
if (e.isDirectory())
|
|
355
|
-
walk(p);
|
|
356
|
-
else if (e.isFile() && isMachO(p))
|
|
357
|
-
machos.push(p);
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
walk(appPath);
|
|
361
|
-
// Deepest first — a parent's signature seals its children, so children
|
|
362
|
-
// must be final when the parent is written.
|
|
363
|
-
machos.sort((a, b) => b.split("/").length - a.split("/").length);
|
|
364
|
-
for (const p of machos)
|
|
365
|
-
run("codesign", ["--force", "--sign", "-", p], appPath);
|
|
366
|
-
run("codesign", ["--force", "--sign", "-", appPath], bundleDir);
|
|
367
|
-
const err = codesignVerify(appPath);
|
|
368
|
-
if (err)
|
|
369
|
-
throw new Error(`ad-hoc signing produced an invalid bundle:\n${err}`);
|
|
370
|
-
console.log(`ad-hoc signed ${app} — ${machos.length} nested binaries, codesign --verify clean`);
|
|
371
|
-
// A dmg built in the same `tauri build` already captured the
|
|
372
|
-
// PRE-signing .app — signing after bundling can't reach inside a
|
|
373
|
-
// compressed image. Repackage each dmg from the signed app and verify
|
|
374
|
-
// the shipped artifact itself, not just the staged copy. The rebuilt
|
|
375
|
-
// image is plain (app + /Applications link) — Tauri's cosmetic layout
|
|
376
|
-
// returns once real signing is configured and repackaging stops.
|
|
377
|
-
const dmgDir = join(tauriDir, "target", "release", "bundle", "dmg");
|
|
378
|
-
if (!existsSync(dmgDir))
|
|
379
|
-
return;
|
|
380
|
-
for (const dmg of readdirSync(dmgDir).filter((n) => n.endsWith(".dmg"))) {
|
|
381
|
-
const dmgPath = join(dmgDir, dmg);
|
|
382
|
-
const staging = mkdtempSync(join(tmpdir(), "pylair-dmg-"));
|
|
383
|
-
try {
|
|
384
|
-
run("ditto", [appPath, join(staging, app)], tauriDir);
|
|
385
|
-
run("ln", ["-s", "/Applications", join(staging, "Applications")], tauriDir);
|
|
386
|
-
run("hdiutil", ["create", "-volname", productName, "-srcfolder", staging, "-ov", "-format", "UDZO", dmgPath], tauriDir);
|
|
387
|
-
}
|
|
388
|
-
finally {
|
|
389
|
-
rmSync(staging, { recursive: true, force: true });
|
|
390
|
-
}
|
|
391
|
-
// Verify the app INSIDE the shipped dmg — mount read-only at a fixed
|
|
392
|
-
// point, verify, always detach, then judge.
|
|
393
|
-
const mount = mkdtempSync(join(tmpdir(), "pylair-mnt-"));
|
|
394
|
-
run("hdiutil", ["attach", "-nobrowse", "-readonly", "-mountpoint", mount, dmgPath], tauriDir);
|
|
395
|
-
let inDmg = null;
|
|
396
|
-
try {
|
|
397
|
-
inDmg = codesignVerify(join(mount, app));
|
|
398
|
-
}
|
|
399
|
-
finally {
|
|
400
|
-
spawnSync("hdiutil", ["detach", mount, "-quiet"], { stdio: "inherit" });
|
|
401
|
-
rmSync(mount, { recursive: true, force: true });
|
|
402
|
-
}
|
|
403
|
-
if (inDmg)
|
|
404
|
-
throw new Error(`${dmg} contains an invalidly-signed app:\n${inDmg}`);
|
|
405
|
-
console.log(`repackaged ${dmg} with the signed app — verified inside the image`);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
/** Shipped bundles must carry the licenses of what they embed. Collects:
|
|
409
|
-
* pylair's own dual license, the interpreter's license tree (PSF license
|
|
410
|
-
* requires LICENSE text with redistributed CPython), and each wheel's
|
|
411
|
-
* dist-info license files. Runs before prune so nothing collected is
|
|
412
|
-
* deleted afterwards. Returns the notice dir.
|
|
413
|
-
*/
|
|
414
|
-
export function stageNotices(resources, pylairDir) {
|
|
415
|
-
const out = join(resources, "THIRD-PARTY-NOTICES");
|
|
416
|
-
rmSync(out, { recursive: true, force: true });
|
|
417
|
-
mkdirSync(out, { recursive: true });
|
|
418
|
-
const index = [];
|
|
419
|
-
const copyInto = (subdir, src, dest) => {
|
|
420
|
-
mkdirSync(join(out, subdir), { recursive: true });
|
|
421
|
-
cpSync(src, join(out, subdir, dest));
|
|
422
|
-
};
|
|
423
|
-
const LICENSEISH = /^(licen[cs]e|copying|notice|authors|copyright)/i;
|
|
424
|
-
for (const f of ["LICENSE-MIT", "LICENSE-APACHE"]) {
|
|
425
|
-
const src = join(pylairDir, f);
|
|
426
|
-
if (existsSync(src))
|
|
427
|
-
copyInto("pylair", src, f);
|
|
428
|
-
}
|
|
429
|
-
index.push("pylair/ — pylair itself (MIT OR Apache-2.0)");
|
|
430
|
-
// The interpreter: python-build-standalone ships a licenses/ dir holding
|
|
431
|
-
// CPython's license plus every statically-linked dep (OpenSSL, libffi…).
|
|
432
|
-
const pyRoot = join(resources, "python");
|
|
433
|
-
let found = 0;
|
|
434
|
-
const walkPy = (dir) => {
|
|
435
|
-
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
436
|
-
const p = join(dir, e.name);
|
|
437
|
-
if (e.isDirectory()) {
|
|
438
|
-
if (e.name.toLowerCase() === "licenses") {
|
|
439
|
-
cpSync(p, join(out, "python", "licenses"), { recursive: true });
|
|
440
|
-
found++;
|
|
441
|
-
}
|
|
442
|
-
else
|
|
443
|
-
walkPy(p);
|
|
444
|
-
}
|
|
445
|
-
else if (LICENSEISH.test(e.name)) {
|
|
446
|
-
copyInto("python", p, e.name);
|
|
447
|
-
found++;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
if (existsSync(pyRoot))
|
|
452
|
-
walkPy(pyRoot);
|
|
453
|
-
if (!found) {
|
|
454
|
-
mkdirSync(join(out, "python"), { recursive: true });
|
|
455
|
-
writeFileSync(join(out, "python", "NOTICE.txt"), "This app bundles CPython (PSF-2.0 license) via python-build-standalone.\n" +
|
|
456
|
-
"License texts were not located in the interpreter tree at build time;\n" +
|
|
457
|
-
"they ship upstream in the python/licenses directory of each\n" +
|
|
458
|
-
"python-build-standalone release.\n");
|
|
459
|
-
console.warn("warning: no license files found under resources/python — wrote placeholder NOTICE");
|
|
460
|
-
}
|
|
461
|
-
index.push("python/ — bundled CPython interpreter (PSF-2.0) and its vendored deps");
|
|
462
|
-
// Wheels: every installed dist-info/egg-info carries license files or
|
|
463
|
-
// license metadata. Copy whichever exists.
|
|
464
|
-
const sp = join(resources, "site-packages");
|
|
465
|
-
let wheels = 0;
|
|
466
|
-
if (existsSync(sp)) {
|
|
467
|
-
for (const e of readdirSync(sp, { withFileTypes: true })) {
|
|
468
|
-
if (!e.isDirectory())
|
|
469
|
-
continue;
|
|
470
|
-
const m = /^(.*?)-[\d.]+.*\.(dist-info|egg-info)$/.exec(e.name);
|
|
471
|
-
if (!m)
|
|
472
|
-
continue;
|
|
473
|
-
const pkg = m[1].replace(/[^\w.-]/g, "_");
|
|
474
|
-
const d = join(sp, e.name);
|
|
475
|
-
let kept = false;
|
|
476
|
-
for (const f of readdirSync(d, { withFileTypes: true })) {
|
|
477
|
-
if (f.isDirectory() && f.name.toLowerCase() === "licenses") {
|
|
478
|
-
cpSync(join(d, f.name), join(out, pkg, "licenses"), { recursive: true });
|
|
479
|
-
kept = true;
|
|
480
|
-
}
|
|
481
|
-
else if (f.isFile() && LICENSEISH.test(f.name)) {
|
|
482
|
-
copyInto(pkg, join(d, f.name), f.name);
|
|
483
|
-
kept = true;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
const meta = join(d, "METADATA");
|
|
487
|
-
if (existsSync(meta)) {
|
|
488
|
-
const lines = readFileSync(meta, "utf-8")
|
|
489
|
-
.split("\n")
|
|
490
|
-
.filter((l) => /^(License|Classifier: License)/i.test(l));
|
|
491
|
-
// Copyleft is legal to ship but app authors deserve to know they
|
|
492
|
-
// bundled it — warn, never block.
|
|
493
|
-
if (/\b(a?lgpl|gpl)[ -]?v?\d/i.test(lines.join(" ")))
|
|
494
|
-
console.warn(`warning: bundled wheel ${e.name} declares a copyleft license`);
|
|
495
|
-
if (lines.length) {
|
|
496
|
-
mkdirSync(join(out, pkg), { recursive: true });
|
|
497
|
-
writeFileSync(join(out, pkg, "METADATA-LICENSE.txt"), lines.join("\n") + "\n");
|
|
498
|
-
kept = true;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
if (kept) {
|
|
502
|
-
wheels++;
|
|
503
|
-
index.push(`${pkg}/ — from ${e.name}`);
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
writeFileSync(join(out, "INDEX.txt"), index.join("\n") + "\n");
|
|
508
|
-
console.log(`third-party notices: pylair, python interpreter, ${wheels} wheel(s)`);
|
|
509
|
-
return out;
|
|
510
|
-
}
|
|
511
|
-
/** Basename globs for files that look like secrets or credentials —
|
|
512
|
-
* staged source must never carry them into an installer. */
|
|
513
|
-
const SECRET_PATTERNS = [
|
|
514
|
-
"*.pem",
|
|
515
|
-
"*.key",
|
|
516
|
-
"*.p12",
|
|
517
|
-
"*.pfx",
|
|
518
|
-
"id_rsa*",
|
|
519
|
-
"id_ed25519*",
|
|
520
|
-
"credentials*.json",
|
|
521
|
-
"service-account*.json",
|
|
522
|
-
];
|
|
523
|
-
/** True when a `/`-separated relative path looks like it holds secrets:
|
|
524
|
-
* any dot-segment (.env, .git, .venv, .DS_Store) or a basename matching
|
|
525
|
-
* the credential patterns. */
|
|
526
|
-
function secretish(rel) {
|
|
527
|
-
const segs = rel.split("/");
|
|
528
|
-
if (segs.some((s) => s.startsWith(".")))
|
|
529
|
-
return true;
|
|
530
|
-
return SECRET_PATTERNS.some((g) => matchGlob(g, segs[segs.length - 1]));
|
|
531
|
-
}
|
|
532
|
-
/** Could `pattern` match something beneath `dir`? Refusing a denied dir
|
|
533
|
-
* would otherwise hide an included descendant from cpSync's walk. */
|
|
534
|
-
function couldMatchUnder(pattern, dir) {
|
|
535
|
-
const d = dir.split("/");
|
|
536
|
-
const p = pattern.split("/");
|
|
537
|
-
for (let i = 0; i < d.length; i++) {
|
|
538
|
-
if (i >= p.length)
|
|
539
|
-
return false; // pattern ends above the dir
|
|
540
|
-
if (p[i] === "**")
|
|
541
|
-
return true; // ** crosses any depth
|
|
542
|
-
if (/[*?]/.test(p[i])) {
|
|
543
|
-
if (!matchGlob(p[i], d[i]))
|
|
544
|
-
return false;
|
|
545
|
-
}
|
|
546
|
-
else if (p[i] !== d[i])
|
|
547
|
-
return false;
|
|
548
|
-
}
|
|
549
|
-
return true;
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Copy the app source tree into the bundle, minus anything that looks
|
|
553
|
-
* like a secret. A path is skipped when any segment starts with "." or
|
|
554
|
-
* its basename matches SECRET_PATTERNS, UNLESS an `include` glob
|
|
555
|
-
* (relative to srcDir, same shape as `prune` entries) names it — an
|
|
556
|
-
* include wins even inside a denied directory, which stays walkable so
|
|
557
|
-
* the descent can reach it. Skips are reported, not fatal: `warn` gets
|
|
558
|
-
* the count, the first 10 names, and how to opt a path back in; nothing
|
|
559
|
-
* is printed when nothing was skipped.
|
|
560
|
-
*
|
|
561
|
-
* Returns the `/`-separated relative paths that were refused.
|
|
562
|
-
*/
|
|
563
|
-
export function stageSource(srcDir, dest, include = [], warn = console.warn) {
|
|
564
|
-
const pats = include.map((g) => g.replace(/\\/g, "/").replace(/^\.\//, ""));
|
|
565
|
-
const skipped = [];
|
|
566
|
-
cpSync(srcDir, dest, {
|
|
567
|
-
recursive: true,
|
|
568
|
-
filter: (src) => {
|
|
569
|
-
const rel = relative(srcDir, src).replace(/\\/g, "/");
|
|
570
|
-
if (!rel)
|
|
571
|
-
return true; // the root itself
|
|
572
|
-
if (pats.some((g) => matchGlob(g, rel)))
|
|
573
|
-
return true;
|
|
574
|
-
if (!secretish(rel))
|
|
575
|
-
return true;
|
|
576
|
-
if (pats.some((g) => couldMatchUnder(g, rel)))
|
|
577
|
-
return true; // descend only
|
|
578
|
-
skipped.push(rel);
|
|
579
|
-
return false;
|
|
580
|
-
},
|
|
581
|
-
});
|
|
582
|
-
if (skipped.length) {
|
|
583
|
-
warn(`warning: skipped ${skipped.length} secret-looking path(s) under ${srcDir}: ` +
|
|
584
|
-
skipped.slice(0, 10).join(", ") +
|
|
585
|
-
(skipped.length > 10 ? ", …" : ""));
|
|
586
|
-
warn(` to ship one deliberately, list it in [tool.pylair] include = ["path/under/source"]`);
|
|
587
|
-
}
|
|
588
|
-
return skipped;
|
|
589
|
-
}
|
|
31
|
+
import { compileBytecode } from "./build/bytecode.js";
|
|
32
|
+
import { stageNotices } from "./build/notices.js";
|
|
33
|
+
import { hostTriple } from "./build/pbs.js";
|
|
34
|
+
import { fetchPython, stageEnv } from "./build/python.js";
|
|
35
|
+
import { adHocSign } from "./build/sign.js";
|
|
36
|
+
import { stageSource } from "./build/source.js";
|
|
37
|
+
import { isWin, run } from "./build/util.js";
|
|
38
|
+
export { compileBytecode, PYC_INVALIDATION_MODE } from "./build/bytecode.js";
|
|
39
|
+
export { DownloadError, backoffMs, download, downloadTimeoutMs, downloadWithRetry, ensureTarball, } from "./build/download.js";
|
|
40
|
+
export { stageNotices } from "./build/notices.js";
|
|
41
|
+
export { hostTriple } from "./build/pbs.js";
|
|
42
|
+
export { stageSource } from "./build/source.js";
|
|
43
|
+
export { requirementsHaveHashes, uvExportArgs, uvPipInstallArgs } from "./build/python.js";
|
|
590
44
|
export async function build(opts) {
|
|
591
45
|
const { appDir } = opts;
|
|
592
46
|
const tauriDir = join(appDir, "src-tauri");
|
|
@@ -648,7 +102,7 @@ export async function build(opts) {
|
|
|
648
102
|
const hasSigning = platform() === "darwin"
|
|
649
103
|
? appleSigned
|
|
650
104
|
: platform() === "win32"
|
|
651
|
-
?
|
|
105
|
+
? windowsSigningConfigured(appDir)
|
|
652
106
|
: true; // no signing concept on Linux — don't warn
|
|
653
107
|
run(process.execPath, [tauriCliEntry(appDir), "build", ...(opts.tauriArgs ?? [])], appDir);
|
|
654
108
|
// Unsigned macOS builds get a full-bundle ad-hoc signature — better
|