@bjia56/portable-python 0.1.0

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.
@@ -0,0 +1,41 @@
1
+ declare class PortablePython {
2
+ installDir: string;
3
+ _version: string;
4
+ constructor(version: string, installDir: string);
5
+ /**
6
+ * Contains the path to the Python executable.
7
+ */
8
+ get executablePath(): string;
9
+ /**
10
+ * Contains the selected Python version.
11
+ */
12
+ get version(): string;
13
+ /**
14
+ * Contains the release tag that will be downloaded.
15
+ */
16
+ get releaseTag(): string;
17
+ /**
18
+ * Contains the full name of the Python distribution.
19
+ */
20
+ get pythonDistributionName(): string;
21
+ /**
22
+ * Contains the path to the extracted Python distribution folder.
23
+ */
24
+ get extractPath(): string;
25
+ /**
26
+ * Checks if the selected Python version has been installed on the host platform.
27
+ * @returns True if the installation exists, false otherwise.
28
+ */
29
+ isInstalled(): boolean;
30
+ /**
31
+ * Will download the compressed Python installation and extract it to
32
+ * the installation directory.
33
+ */
34
+ install(): Promise<void>;
35
+ /**
36
+ * Uninstalls the Python distribution.
37
+ */
38
+ uninstall(): Promise<void>;
39
+ }
40
+
41
+ export { PortablePython };
@@ -0,0 +1,41 @@
1
+ declare class PortablePython {
2
+ installDir: string;
3
+ _version: string;
4
+ constructor(version: string, installDir: string);
5
+ /**
6
+ * Contains the path to the Python executable.
7
+ */
8
+ get executablePath(): string;
9
+ /**
10
+ * Contains the selected Python version.
11
+ */
12
+ get version(): string;
13
+ /**
14
+ * Contains the release tag that will be downloaded.
15
+ */
16
+ get releaseTag(): string;
17
+ /**
18
+ * Contains the full name of the Python distribution.
19
+ */
20
+ get pythonDistributionName(): string;
21
+ /**
22
+ * Contains the path to the extracted Python distribution folder.
23
+ */
24
+ get extractPath(): string;
25
+ /**
26
+ * Checks if the selected Python version has been installed on the host platform.
27
+ * @returns True if the installation exists, false otherwise.
28
+ */
29
+ isInstalled(): boolean;
30
+ /**
31
+ * Will download the compressed Python installation and extract it to
32
+ * the installation directory.
33
+ */
34
+ install(): Promise<void>;
35
+ /**
36
+ * Uninstalls the Python distribution.
37
+ */
38
+ uninstall(): Promise<void>;
39
+ }
40
+
41
+ export { PortablePython };
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var __async = (__this, __arguments, generator) => {
30
+ return new Promise((resolve, reject) => {
31
+ var fulfilled = (value) => {
32
+ try {
33
+ step(generator.next(value));
34
+ } catch (e) {
35
+ reject(e);
36
+ }
37
+ };
38
+ var rejected = (value) => {
39
+ try {
40
+ step(generator.throw(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
46
+ step((generator = generator.apply(__this, __arguments)).next());
47
+ });
48
+ };
49
+
50
+ // src/index.ts
51
+ var src_exports = {};
52
+ __export(src_exports, {
53
+ PortablePython: () => PortablePython
54
+ });
55
+ module.exports = __toCommonJS(src_exports);
56
+ var import_fs = require("fs");
57
+ var import_path = require("path");
58
+ var import_os = require("os");
59
+ var import_stream = require("stream");
60
+ var import_promises = require("stream/promises");
61
+ var import_adm_zip = __toESM(require("adm-zip"));
62
+ var DL_PLATFORM = (() => {
63
+ if ((0, import_os.platform)() == "win32") {
64
+ return "windows";
65
+ }
66
+ return (0, import_os.platform)();
67
+ })();
68
+ var DL_ARCH = (() => {
69
+ if (DL_PLATFORM == "darwin") {
70
+ return "universal2";
71
+ }
72
+ switch ((0, import_os.arch)()) {
73
+ case "ia32":
74
+ return "i386";
75
+ case "x64":
76
+ return "x86_64";
77
+ case "arm64":
78
+ return "aarch64";
79
+ }
80
+ return (0, import_os.arch)();
81
+ })();
82
+ var VERSIONS = [
83
+ "3.10.13",
84
+ "3.9.18",
85
+ "3.9.17",
86
+ "3.8.18",
87
+ "3.8.17"
88
+ ];
89
+ var VERSION_BUILDS = /* @__PURE__ */ new Map([
90
+ ["3.10.13", "v3.10.13-build.3"],
91
+ ["3.9.18", "v3.9.18-beta.4"],
92
+ ["3.9.17", "v3.9.17-build.4"],
93
+ ["3.8.18", "v3.8.18-build.0"],
94
+ ["3.8.17", "v3.8.17-build.3"]
95
+ ]);
96
+ function pickVersion(version) {
97
+ for (let i = 0; i < VERSIONS.length; ++i) {
98
+ if (VERSIONS[i].startsWith(version)) {
99
+ return VERSIONS[i];
100
+ }
101
+ }
102
+ return VERSIONS[0];
103
+ }
104
+ function download(url, dest) {
105
+ return __async(this, null, function* () {
106
+ const res = yield fetch(url);
107
+ const file = (0, import_fs.createWriteStream)(dest);
108
+ yield (0, import_promises.finished)(import_stream.Readable.fromWeb(res.body).pipe(file));
109
+ });
110
+ }
111
+ var PortablePython = class {
112
+ constructor(version, installDir) {
113
+ this.installDir = installDir;
114
+ if (!version) {
115
+ throw Error("version must not be empty");
116
+ }
117
+ this._version = pickVersion(version);
118
+ if (!this.releaseTag) {
119
+ throw Error("no releases available for this version");
120
+ }
121
+ }
122
+ /**
123
+ * Contains the path to the Python executable.
124
+ */
125
+ get executablePath() {
126
+ return (0, import_path.join)(this.installDir, this.pythonDistributionName, "bin", "python" + ((0, import_os.platform)() === "win32" ? ".exe" : ""));
127
+ }
128
+ /**
129
+ * Contains the selected Python version.
130
+ */
131
+ get version() {
132
+ return this._version;
133
+ }
134
+ /**
135
+ * Contains the release tag that will be downloaded.
136
+ */
137
+ get releaseTag() {
138
+ return VERSION_BUILDS.get(this.version);
139
+ }
140
+ /**
141
+ * Contains the full name of the Python distribution.
142
+ */
143
+ get pythonDistributionName() {
144
+ return `python-${this.version}-${DL_PLATFORM}-${DL_ARCH}`;
145
+ }
146
+ /**
147
+ * Contains the path to the extracted Python distribution folder.
148
+ */
149
+ get extractPath() {
150
+ return (0, import_path.join)(this.installDir, this.pythonDistributionName);
151
+ }
152
+ /**
153
+ * Checks if the selected Python version has been installed on the host platform.
154
+ * @returns True if the installation exists, false otherwise.
155
+ */
156
+ isInstalled() {
157
+ return (0, import_fs.existsSync)(this.extractPath);
158
+ }
159
+ /**
160
+ * Will download the compressed Python installation and extract it to
161
+ * the installation directory.
162
+ */
163
+ install() {
164
+ return __async(this, null, function* () {
165
+ if (this.isInstalled()) {
166
+ return;
167
+ }
168
+ const url = `https://github.com/bjia56/portable-python/releases/download/${this.releaseTag}/${this.pythonDistributionName}.zip`;
169
+ const downloadPath = (0, import_path.join)(this.installDir, `${this.pythonDistributionName}.zip`);
170
+ (0, import_fs.mkdirSync)(this.installDir, { recursive: true });
171
+ const installDir = this.installDir;
172
+ yield download(url, downloadPath);
173
+ const zip = new import_adm_zip.default(downloadPath);
174
+ zip.extractAllTo(installDir, true);
175
+ (0, import_fs.rmSync)(downloadPath);
176
+ if (!this.isInstalled()) {
177
+ throw Error("something went wrong and the installation failed");
178
+ }
179
+ (0, import_fs.chmodSync)(this.executablePath, 511);
180
+ });
181
+ }
182
+ /**
183
+ * Uninstalls the Python distribution.
184
+ */
185
+ uninstall() {
186
+ return __async(this, null, function* () {
187
+ if (!this.isInstalled()) {
188
+ return;
189
+ }
190
+ yield new Promise((resolve, reject) => (0, import_fs.rm)(this.extractPath, { force: true, recursive: true }, (e) => {
191
+ if (e) {
192
+ reject(e);
193
+ } else {
194
+ resolve();
195
+ }
196
+ }));
197
+ });
198
+ }
199
+ };
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ PortablePython
203
+ });
204
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createWriteStream, existsSync, rm, rmSync, mkdirSync, chmodSync } from \"fs\";\nimport { join } from \"path\";\nimport { platform, arch } from \"os\";\nimport { Readable } from 'stream';\nimport { finished } from 'stream/promises';\nimport { ReadableStream } from 'stream/web';\nimport AdmZip from \"adm-zip\";\n\nconst DL_PLATFORM = (() => {\n if (platform() == \"win32\") {\n return \"windows\";\n }\n return platform();\n})();\n\nconst DL_ARCH = (() => {\n if (DL_PLATFORM == \"darwin\") {\n return \"universal2\";\n }\n\n switch (arch()) {\n case \"ia32\":\n return \"i386\";\n case \"x64\":\n return \"x86_64\";\n case \"arm64\":\n return \"aarch64\";\n }\n\n return arch();\n})();\n\nconst VERSIONS = [\n \"3.10.13\",\n \"3.9.18\",\n \"3.9.17\",\n \"3.8.18\",\n \"3.8.17\",\n];\n\nconst VERSION_BUILDS = new Map<string, string>([\n [\"3.10.13\", \"v3.10.13-build.3\"],\n [\"3.9.18\", \"v3.9.18-beta.4\"],\n [\"3.9.17\", \"v3.9.17-build.4\"],\n [\"3.8.18\", \"v3.8.18-build.0\"],\n [\"3.8.17\", \"v3.8.17-build.3\"],\n]);\n\nfunction pickVersion(version: string) {\n for (let i = 0; i < VERSIONS.length; ++i) {\n // TODO: This doesn't handle semver correctly, e.g. 3.8.17 will match 3.8.1\n if (VERSIONS[i].startsWith(version)) {\n return VERSIONS[i];\n }\n }\n return VERSIONS[0]\n}\n\n// https://stackoverflow.com/a/32134846\nasync function download(url: string, dest: string) {\n const res = await fetch(url);\n const file = createWriteStream(dest);\n await finished(Readable.fromWeb(res.body as ReadableStream).pipe(file));\n}\n\nexport class PortablePython {\n _version: string\n\n constructor(version: string, public installDir: string) {\n if (!version) {\n throw Error(\"version must not be empty\");\n }\n\n this._version = pickVersion(version);\n if (!this.releaseTag) {\n throw Error(\"no releases available for this version\");\n }\n }\n\n /**\n * Contains the path to the Python executable.\n */\n get executablePath() {\n return join(this.installDir, this.pythonDistributionName, \"bin\", \"python\" + (platform() === \"win32\" ? \".exe\" : \"\"));\n }\n\n /**\n * Contains the selected Python version.\n */\n get version() {\n return this._version;\n }\n\n /**\n * Contains the release tag that will be downloaded.\n */\n get releaseTag() {\n return VERSION_BUILDS.get(this.version) as string;\n }\n\n /**\n * Contains the full name of the Python distribution.\n */\n get pythonDistributionName() {\n return `python-${this.version}-${DL_PLATFORM}-${DL_ARCH}`;\n }\n\n /**\n * Contains the path to the extracted Python distribution folder.\n */\n get extractPath() {\n return join(this.installDir, this.pythonDistributionName);\n }\n\n /**\n * Checks if the selected Python version has been installed on the host platform.\n * @returns True if the installation exists, false otherwise.\n */\n isInstalled() {\n return existsSync(this.extractPath);\n }\n\n /**\n * Will download the compressed Python installation and extract it to\n * the installation directory.\n */\n async install() {\n if (this.isInstalled()) {\n return;\n }\n\n const url = `https://github.com/bjia56/portable-python/releases/download/${this.releaseTag}/${this.pythonDistributionName}.zip`\n const downloadPath = join(this.installDir, `${this.pythonDistributionName}.zip`);\n\n mkdirSync(this.installDir, { recursive: true });\n\n const installDir = this.installDir;\n await download(url, downloadPath);\n\n const zip = new AdmZip(downloadPath);\n zip.extractAllTo(installDir, true)\n\n rmSync(downloadPath);\n if (!this.isInstalled()) {\n throw Error(\"something went wrong and the installation failed\");\n }\n\n chmodSync(this.executablePath, 0o777);\n }\n\n /**\n * Uninstalls the Python distribution.\n */\n async uninstall() {\n if (!this.isInstalled()) {\n return;\n }\n await new Promise<void>((resolve, reject) => rm(this.extractPath, { force: true, recursive: true }, (e) => {\n if (e) {\n reject(e);\n } else {\n resolve();\n }\n }));\n }\n\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAgF;AAChF,kBAAqB;AACrB,gBAA+B;AAC/B,oBAAyB;AACzB,sBAAyB;AAEzB,qBAAmB;AAEnB,IAAM,eAAe,MAAM;AACvB,UAAI,oBAAS,KAAK,SAAS;AACvB,WAAO;AAAA,EACX;AACA,aAAO,oBAAS;AACpB,GAAG;AAEH,IAAM,WAAW,MAAM;AACnB,MAAI,eAAe,UAAU;AACzB,WAAO;AAAA,EACX;AAEA,cAAQ,gBAAK,GAAG;AAAA,IAChB,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,EACX;AAEA,aAAO,gBAAK;AAChB,GAAG;AAEH,IAAM,WAAW;AAAA,EACb;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AAEA,IAAM,iBAAiB,oBAAI,IAAoB;AAAA,EAC3C,CAAC,WAAW,kBAAkB;AAAA,EAC9B,CAAC,UAAU,gBAAgB;AAAA,EAC3B,CAAC,UAAU,iBAAiB;AAAA,EAC5B,CAAC,UAAU,iBAAiB;AAAA,EAC5B,CAAC,UAAU,iBAAiB;AAChC,CAAC;AAED,SAAS,YAAY,SAAiB;AAClC,WAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,EAAE,GAAG;AAEtC,QAAI,SAAS,CAAC,EAAE,WAAW,OAAO,GAAG;AACjC,aAAO,SAAS,CAAC;AAAA,IACrB;AAA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;AACzH,YAAM,mBAAe,kBAAK,KAAK,YAAY,GAAG,KAAK,sBAAsB,MAAM;AAE/E,+BAAU,KAAK,YAAY,EAAE,WAAW,KAAK,CAAC;AAE9C,YAAM,aAAa,KAAK;AACxB,YAAM,SAAS,KAAK,YAAY;AAEhC,YAAM,MAAM,IAAI,eAAAA,QAAO,YAAY;AACnC,UAAI,aAAa,YAAY,IAAI;AAEjC,4BAAO,YAAY;AACnB,UAAI,CAAC,KAAK,YAAY,GAAG;AACrB,cAAM,MAAM,kDAAkD;AAAA,MAClE;AAEA,+BAAU,KAAK,gBAAgB,GAAK;AAAA,IACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAKM,YAAY;AAAA;AACd,UAAI,CAAC,KAAK,YAAY,GAAG;AACrB;AAAA,MACJ;AACA,YAAM,IAAI,QAAc,CAAC,SAAS,eAAW,cAAG,KAAK,aAAa,EAAE,OAAO,MAAM,WAAW,KAAK,GAAG,CAAC,MAAM;AACvG,YAAI,GAAG;AACH,iBAAO,CAAC;AAAA,QACZ,OAAO;AACH,kBAAQ;AAAA,QACZ;AAAA,MACJ,CAAC,CAAC;AAAA,IACN;AAAA;AAEJ;","names":["AdmZip"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,170 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/index.ts
23
+ import { createWriteStream, existsSync, rm, rmSync, mkdirSync, chmodSync } from "fs";
24
+ import { join } from "path";
25
+ import { platform, arch } from "os";
26
+ import { Readable } from "stream";
27
+ import { finished } from "stream/promises";
28
+ import AdmZip from "adm-zip";
29
+ var DL_PLATFORM = (() => {
30
+ if (platform() == "win32") {
31
+ return "windows";
32
+ }
33
+ return platform();
34
+ })();
35
+ var DL_ARCH = (() => {
36
+ if (DL_PLATFORM == "darwin") {
37
+ return "universal2";
38
+ }
39
+ switch (arch()) {
40
+ case "ia32":
41
+ return "i386";
42
+ case "x64":
43
+ return "x86_64";
44
+ case "arm64":
45
+ return "aarch64";
46
+ }
47
+ return arch();
48
+ })();
49
+ var VERSIONS = [
50
+ "3.10.13",
51
+ "3.9.18",
52
+ "3.9.17",
53
+ "3.8.18",
54
+ "3.8.17"
55
+ ];
56
+ var VERSION_BUILDS = /* @__PURE__ */ new Map([
57
+ ["3.10.13", "v3.10.13-build.3"],
58
+ ["3.9.18", "v3.9.18-beta.4"],
59
+ ["3.9.17", "v3.9.17-build.4"],
60
+ ["3.8.18", "v3.8.18-build.0"],
61
+ ["3.8.17", "v3.8.17-build.3"]
62
+ ]);
63
+ function pickVersion(version) {
64
+ for (let i = 0; i < VERSIONS.length; ++i) {
65
+ if (VERSIONS[i].startsWith(version)) {
66
+ return VERSIONS[i];
67
+ }
68
+ }
69
+ return VERSIONS[0];
70
+ }
71
+ function download(url, dest) {
72
+ return __async(this, null, function* () {
73
+ const res = yield fetch(url);
74
+ const file = createWriteStream(dest);
75
+ yield finished(Readable.fromWeb(res.body).pipe(file));
76
+ });
77
+ }
78
+ var PortablePython = class {
79
+ constructor(version, installDir) {
80
+ this.installDir = installDir;
81
+ if (!version) {
82
+ throw Error("version must not be empty");
83
+ }
84
+ this._version = pickVersion(version);
85
+ if (!this.releaseTag) {
86
+ throw Error("no releases available for this version");
87
+ }
88
+ }
89
+ /**
90
+ * Contains the path to the Python executable.
91
+ */
92
+ get executablePath() {
93
+ return join(this.installDir, this.pythonDistributionName, "bin", "python" + (platform() === "win32" ? ".exe" : ""));
94
+ }
95
+ /**
96
+ * Contains the selected Python version.
97
+ */
98
+ get version() {
99
+ return this._version;
100
+ }
101
+ /**
102
+ * Contains the release tag that will be downloaded.
103
+ */
104
+ get releaseTag() {
105
+ return VERSION_BUILDS.get(this.version);
106
+ }
107
+ /**
108
+ * Contains the full name of the Python distribution.
109
+ */
110
+ get pythonDistributionName() {
111
+ return `python-${this.version}-${DL_PLATFORM}-${DL_ARCH}`;
112
+ }
113
+ /**
114
+ * Contains the path to the extracted Python distribution folder.
115
+ */
116
+ get extractPath() {
117
+ return join(this.installDir, this.pythonDistributionName);
118
+ }
119
+ /**
120
+ * Checks if the selected Python version has been installed on the host platform.
121
+ * @returns True if the installation exists, false otherwise.
122
+ */
123
+ isInstalled() {
124
+ return existsSync(this.extractPath);
125
+ }
126
+ /**
127
+ * Will download the compressed Python installation and extract it to
128
+ * the installation directory.
129
+ */
130
+ install() {
131
+ return __async(this, null, function* () {
132
+ if (this.isInstalled()) {
133
+ return;
134
+ }
135
+ const url = `https://github.com/bjia56/portable-python/releases/download/${this.releaseTag}/${this.pythonDistributionName}.zip`;
136
+ const downloadPath = join(this.installDir, `${this.pythonDistributionName}.zip`);
137
+ mkdirSync(this.installDir, { recursive: true });
138
+ const installDir = this.installDir;
139
+ yield download(url, downloadPath);
140
+ const zip = new AdmZip(downloadPath);
141
+ zip.extractAllTo(installDir, true);
142
+ rmSync(downloadPath);
143
+ if (!this.isInstalled()) {
144
+ throw Error("something went wrong and the installation failed");
145
+ }
146
+ chmodSync(this.executablePath, 511);
147
+ });
148
+ }
149
+ /**
150
+ * Uninstalls the Python distribution.
151
+ */
152
+ uninstall() {
153
+ return __async(this, null, function* () {
154
+ if (!this.isInstalled()) {
155
+ return;
156
+ }
157
+ yield new Promise((resolve, reject) => rm(this.extractPath, { force: true, recursive: true }, (e) => {
158
+ if (e) {
159
+ reject(e);
160
+ } else {
161
+ resolve();
162
+ }
163
+ }));
164
+ });
165
+ }
166
+ };
167
+ export {
168
+ PortablePython
169
+ };
170
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createWriteStream, existsSync, rm, rmSync, mkdirSync, chmodSync } from \"fs\";\nimport { join } from \"path\";\nimport { platform, arch } from \"os\";\nimport { Readable } from 'stream';\nimport { finished } from 'stream/promises';\nimport { ReadableStream } from 'stream/web';\nimport AdmZip from \"adm-zip\";\n\nconst DL_PLATFORM = (() => {\n if (platform() == \"win32\") {\n return \"windows\";\n }\n return platform();\n})();\n\nconst DL_ARCH = (() => {\n if (DL_PLATFORM == \"darwin\") {\n return \"universal2\";\n }\n\n switch (arch()) {\n case \"ia32\":\n return \"i386\";\n case \"x64\":\n return \"x86_64\";\n case \"arm64\":\n return \"aarch64\";\n }\n\n return arch();\n})();\n\nconst VERSIONS = [\n \"3.10.13\",\n \"3.9.18\",\n \"3.9.17\",\n \"3.8.18\",\n \"3.8.17\",\n];\n\nconst VERSION_BUILDS = new Map<string, string>([\n [\"3.10.13\", \"v3.10.13-build.3\"],\n [\"3.9.18\", \"v3.9.18-beta.4\"],\n [\"3.9.17\", \"v3.9.17-build.4\"],\n [\"3.8.18\", \"v3.8.18-build.0\"],\n [\"3.8.17\", \"v3.8.17-build.3\"],\n]);\n\nfunction pickVersion(version: string) {\n for (let i = 0; i < VERSIONS.length; ++i) {\n // TODO: This doesn't handle semver correctly, e.g. 3.8.17 will match 3.8.1\n if (VERSIONS[i].startsWith(version)) {\n return VERSIONS[i];\n }\n }\n return VERSIONS[0]\n}\n\n// https://stackoverflow.com/a/32134846\nasync function download(url: string, dest: string) {\n const res = await fetch(url);\n const file = createWriteStream(dest);\n await finished(Readable.fromWeb(res.body as ReadableStream).pipe(file));\n}\n\nexport class PortablePython {\n _version: string\n\n constructor(version: string, public installDir: string) {\n if (!version) {\n throw Error(\"version must not be empty\");\n }\n\n this._version = pickVersion(version);\n if (!this.releaseTag) {\n throw Error(\"no releases available for this version\");\n }\n }\n\n /**\n * Contains the path to the Python executable.\n */\n get executablePath() {\n return join(this.installDir, this.pythonDistributionName, \"bin\", \"python\" + (platform() === \"win32\" ? \".exe\" : \"\"));\n }\n\n /**\n * Contains the selected Python version.\n */\n get version() {\n return this._version;\n }\n\n /**\n * Contains the release tag that will be downloaded.\n */\n get releaseTag() {\n return VERSION_BUILDS.get(this.version) as string;\n }\n\n /**\n * Contains the full name of the Python distribution.\n */\n get pythonDistributionName() {\n return `python-${this.version}-${DL_PLATFORM}-${DL_ARCH}`;\n }\n\n /**\n * Contains the path to the extracted Python distribution folder.\n */\n get extractPath() {\n return join(this.installDir, this.pythonDistributionName);\n }\n\n /**\n * Checks if the selected Python version has been installed on the host platform.\n * @returns True if the installation exists, false otherwise.\n */\n isInstalled() {\n return existsSync(this.extractPath);\n }\n\n /**\n * Will download the compressed Python installation and extract it to\n * the installation directory.\n */\n async install() {\n if (this.isInstalled()) {\n return;\n }\n\n const url = `https://github.com/bjia56/portable-python/releases/download/${this.releaseTag}/${this.pythonDistributionName}.zip`\n const downloadPath = join(this.installDir, `${this.pythonDistributionName}.zip`);\n\n mkdirSync(this.installDir, { recursive: true });\n\n const installDir = this.installDir;\n await download(url, downloadPath);\n\n const zip = new AdmZip(downloadPath);\n zip.extractAllTo(installDir, true)\n\n rmSync(downloadPath);\n if (!this.isInstalled()) {\n throw Error(\"something went wrong and the installation failed\");\n }\n\n chmodSync(this.executablePath, 0o777);\n }\n\n /**\n * Uninstalls the Python distribution.\n */\n async uninstall() {\n if (!this.isInstalled()) {\n return;\n }\n await new Promise<void>((resolve, reject) => rm(this.extractPath, { force: true, recursive: true }, (e) => {\n if (e) {\n reject(e);\n } else {\n resolve();\n }\n }));\n }\n\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,mBAAmB,YAAY,IAAI,QAAQ,WAAW,iBAAiB;AAChF,SAAS,YAAY;AACrB,SAAS,UAAU,YAAY;AAC/B,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AAEzB,OAAO,YAAY;AAEnB,IAAM,eAAe,MAAM;AACvB,MAAI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M,IAAI,QAAc,CAAC,SAAS,WAAW,GAAG,KAAK,aAAa,EAAE,OAAO,MAAM,WAAW,KAAK,GAAG,CAAC,MAAM;AACvG,YAAI,GAAG;AACH,iBAAO,CAAC;AAAA,QACZ,OAAO;AACH,kBAAQ;AAAA,QACZ;AAAA,MACJ,CAAC,CAAC;AAAA,IACN;AAAA;AAEJ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@bjia56/portable-python",
3
+ "version": "0.1.0",
4
+ "description": "Portable Python",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "prepublishOnly": "npm run build",
14
+ "test": "echo \"Error: no test specified\" && exit 1"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/bjia56/portable-python.git"
19
+ },
20
+ "keywords": [
21
+ "python"
22
+ ],
23
+ "author": "Brett Jia",
24
+ "license": "Apache-2.0",
25
+ "bugs": {
26
+ "url": "https://github.com/bjia56/portable-python/issues"
27
+ },
28
+ "homepage": "https://github.com/bjia56/portable-python",
29
+ "dependencies": {
30
+ "adm-zip": "^0.5.10"
31
+ },
32
+ "devDependencies": {
33
+ "@types/adm-zip": "^0.5.5",
34
+ "ts-node": "^10.9.1",
35
+ "tsup": "^8.0.1",
36
+ "typescript": "^5.3.2"
37
+ }
38
+ }