Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update installer and bundles for new implementations #86

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions installers/nodejs/src/cpython.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { platform, arch, release } from "os";
import { chmod, symlink, rename } from "fs/promises";
import { join } from "path";

import { IInstaller, IPortablePython, IPortablePythonOptions } from './types';
import { IInstaller, IPortablePython } from './types';

const DL_PLATFORM = (() => {
if (platform() == "win32") {
Expand Down Expand Up @@ -34,17 +34,17 @@ const DL_ARCH = (() => {
})();

export default class CPythonInstaller implements IInstaller {
constructor(private parent: IPortablePython, private options: IPortablePythonOptions) {}
constructor(private parent: IPortablePython) {}

get relativeExecutablePath(): string {
return join(this.pythonDistributionName, "bin", "python" + (
this.options.distribution === "cosmo" ? ".com" :
this.parent.distribution === "cosmo" ? ".com" :
(platform() === "win32" ? ".exe" : "")
));
}

get relativePipPath(): string {
if (this.options.distribution != "cosmo" && platform() === "win32") {
if (this.parent.distribution != "cosmo" && platform() === "win32") {
return join(this.pythonDistributionName, "Scripts", `pip${this.parent.major}.exe`);
}
return join(this.pythonDistributionName, "bin", `pip${this.parent.major}`);
Expand All @@ -59,11 +59,11 @@ export default class CPythonInstaller implements IInstaller {
}

validateOptions(): void {
if (this.options.implementation !== "cpython") {
if (this.parent.implementation !== "cpython") {
throw Error("expected cpython implementation");
}

if (!["auto", "cosmo", "headless", "full"].includes(this.options.distribution)) {
if (!["auto", "cosmo", "headless", "full"].includes(this.parent.distribution)) {
throw Error("invalid distribution");
}
}
Expand All @@ -74,7 +74,7 @@ export default class CPythonInstaller implements IInstaller {
}

await chmod(this.parent.executablePath, 0o777);
if (this.options.distribution != "cosmo" && platform() != "win32") {
if (this.parent.distribution != "cosmo" && platform() != "win32") {
// node can't create symlinks over existing files, so we create symlinks with temp names,
// then rename to overwrite existing files
await symlink("python", `${this.parent.executablePath}${this.parent.major}_`, "file");
Expand Down
11 changes: 7 additions & 4 deletions installers/nodejs/src/graalpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class GraalPyInstaller implements IInstaller {
private pythonMajor: number = 3;
private pythonMinor: number = 10;

constructor(private parent: IPortablePython, private options: IPortablePythonOptions) {}
constructor(private parent: IPortablePython) {}

get relativeExecutablePath(): string {
return join(this.pythonDistributionName, "bin", "python" + (platform() === "win32" ? ".exe" : ""));
Expand All @@ -39,15 +39,18 @@ export default class GraalPyInstaller implements IInstaller {
}

get pythonDistributionName(): string {
return `graalpy-community-jvm-${this.parent.version}-${DL_PLATFORM}-${DL_ARCH}`;
if (this.parent.distribution === "auto" || this.parent.distribution === "standard") {
return `graalpy-${this.parent.version}-${DL_PLATFORM}-${DL_ARCH}`;
}
return `graalpy-${this.parent.distribution}-${this.parent.version}-${DL_PLATFORM}-${DL_ARCH}`;
}

validateOptions(): void {
if (this.options.implementation !== "graalpy") {
if (this.parent.implementation !== "graalpy") {
throw Error("expected graalpy implementation");
}

if (this.options.distribution !== "auto") {
if (!["auto", "standard", "community", "jvm", "community-jvm"].includes(this.parent.distribution)) {
throw Error("invalid distribution");
}
}
Expand Down
9 changes: 4 additions & 5 deletions installers/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { IInstaller, IPortablePython, IPortablePythonOptions } from "./types";
import { getVersionBuilds, pickVersion } from "./versions";
import CPythonInstaller from "./cpython";
import GraalPyInstaller from "./graalpy";
import PyPyInstaller from "./pypy";

const INSTALLERS = new Map<string, new (parent: IPortablePython, options: IPortablePythonOptions) => IInstaller>([
const INSTALLERS = new Map<string, new (parent: IPortablePython) => IInstaller>([
["cpython", CPythonInstaller],
["graalpy", GraalPyInstaller],
["pypy", PyPyInstaller],
]);

async function download(url: string, dest: string) {
Expand Down Expand Up @@ -46,10 +48,7 @@ export class PortablePython implements IPortablePython {
}

const ctor = INSTALLERS.get(this.implementation)!;
this._installer = new ctor(this, {
implementation: this.implementation,
distribution: this.distribution,
});
this._installer = new ctor(this);
this._installer.validateOptions();

if (installDir) {
Expand Down
88 changes: 88 additions & 0 deletions installers/nodejs/src/pypy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { platform, arch } from "os";
import { chmod, symlink, rename } from "fs/promises";
import { join } from "path";

import { IInstaller, IPortablePython } from './types';

const DL_PLATFORM = (() => {
if (platform() == "win32") {
return "windows";
}
return platform();
})();

const DL_ARCH = (() => {
switch (arch()) {
case "x64":
return "x86_64";
case "arm64":
return "aarch64";
}
return arch();
})();

export default class PyPyInstaller implements IInstaller {
constructor(private parent: IPortablePython) {}

get pythonMajor(): string {
if (this.parent.distribution === "auto") {
return "3";
}
return this.parent.distribution.split(".")[0];
}

get pythonMinor(): string {
if (this.parent.distribution === "auto") {
return "10";
}
return this.parent.distribution.split(".")[1];
}

get relativeExecutablePath(): string {
if (platform() === "win32") {
return join(this.pythonDistributionName, "python.exe");
}
return join(this.pythonDistributionName, "bin", "python");
}

get relativePipPath(): string {
if (platform() === "win32") {
return join(this.pythonDistributionName, "Scripts", `pip${this.pythonMajor}.exe`);
}
return join(this.pythonDistributionName, "bin", `pip${this.pythonMajor}`);
}

get pythonDistributionName(): string {
return `pypy${this.pythonMajor}.${this.pythonMinor}-${this.parent.version}-${DL_PLATFORM}-${DL_ARCH}`;
}

validateOptions(): void {
if (this.parent.implementation !== "pypy") {
throw Error("expected pypy implementation");
}

if (!["auto", "3.9", "3.10"].includes(this.parent.distribution)) {
throw Error("invalid distribution");
}
}

async postInstall(): Promise<void> {
if (!this.parent.isInstalled()) {
throw Error("something went wrong and the installation failed");
}

await chmod(this.parent.executablePath, 0o777);
if (platform() != "win32") {
// node can't create symlinks over existing files, so we create symlinks with temp names,
// then rename to overwrite existing files
await symlink("python", `${this.parent.executablePath}${this.pythonMajor}_`, "file");
await rename(`${this.parent.executablePath}${this.pythonMajor}_`, `${this.parent.executablePath}${this.pythonMajor}`);
await symlink("python", `${this.parent.executablePath}${this.pythonMajor}.${this.pythonMinor}_`, "file");
await rename(`${this.parent.executablePath}${this.pythonMajor}.${this.pythonMinor}_`, `${this.parent.executablePath}${this.pythonMajor}.${this.pythonMinor}`);

// ensure the pip script is executable
await chmod(this.parent.pipPath, 0o777);
await chmod(`${this.parent.pipPath}.${this.pythonMinor}`, 0o777);
}
}
}
5 changes: 5 additions & 0 deletions scripts/repackage_graalpy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ function repackage_graal () {
fi
maybe_docker ./bin/python -m ensurepip

if [[ "${PLATFORM}" != "windows" ]]; then
python3 ${WORKDIR}/scripts/patch_pip_script.py ./bin/pip3
python3 ${WORKDIR}/scripts/patch_pip_script.py ./bin/pip3.10
fi

cd ${WORKDIR}
if [[ "${EXTRACTED_FILENAME}" != "${UPLOAD_FILENAME}" ]]; then
mv ${EXTRACTED_FILENAME} ${UPLOAD_FILENAME}
Expand Down
5 changes: 5 additions & 0 deletions scripts/repackage_pypy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ function repackage_pypy () {
./bin/python -m ensurepip
fi

if [[ "${PLATFORM}" != "windows" ]]; then
python3 ${WORKDIR}/scripts/patch_pip_script.py ./bin/pip3
python3 ${WORKDIR}/scripts/patch_pip_script.py ./bin/pip${DISTRIBUTION}
fi

cd ${WORKDIR}
mv ${DL_FILENAME} ${UPLOAD_FILENAME}

Expand Down
Loading