@stlite/desktop 0.18.2 → 0.19.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.
@@ -1,186 +1,13 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const yargs_1 = __importDefault(require("yargs"));
8
- const helpers_1 = require("yargs/helpers");
9
- const path_1 = __importDefault(require("path"));
10
- const promises_1 = __importDefault(require("fs/promises"));
11
- const fs_extra_1 = __importDefault(require("fs-extra"));
12
- const node_fetch_1 = __importDefault(require("node-fetch"));
13
- const pyodide_1 = require("pyodide");
14
- // @ts-ignore
15
- global.fetch = node_fetch_1.default; // The global `fetch()` is necessary for micropip.install() to load the remote packages.
16
- async function copyBuildDirectory(options) {
17
- console.info("Copy the build directory (the bare built app files) to this directory...");
18
- const sourceDir = path_1.default.resolve(__dirname, "../build");
19
- const sourceDirStat = await promises_1.default.stat(sourceDir);
20
- if (!sourceDirStat.isDirectory()) {
21
- throw new Error(`The source ${sourceDir} does not exist.`);
22
- }
23
- if (sourceDir === options.copyTo) {
24
- console.warn(`sourceDir == destDir (${sourceDir}). Are you in the development environment? Skip copying the directory.`);
25
- return;
26
- }
27
- if (options.keepOld) {
28
- try {
29
- await promises_1.default.access(options.copyTo);
30
- console.info(`${options.copyTo} already exists. Use it and skip copying.`);
31
- return;
32
- }
33
- catch {
34
- // If the destination directory does not exist
35
- throw new Error(`${options.copyTo} does not exist even though the \`keepOld\` option is specified`);
36
- }
37
- }
38
- console.log(`Copy ${sourceDir} to ${options.copyTo}`);
39
- await promises_1.default.rm(options.copyTo, { recursive: true, force: true });
40
- await fs_extra_1.default.copy(sourceDir, options.copyTo);
41
- }
42
- async function installLocalWheel(pyodide, localPath) {
43
- console.log(`Install the local wheel ${localPath}`);
44
- const data = await promises_1.default.readFile(localPath);
45
- const emfsPath = "/tmp/" + path_1.default.basename(localPath);
46
- pyodide.FS.writeFile(emfsPath, data);
47
- const micropip = pyodide.pyimport("micropip");
48
- const requirement = `emfs:${emfsPath}`;
49
- console.log(`Install ${requirement}`);
50
- await micropip.install.callKwargs(requirement, { keep_going: true });
51
- }
52
- async function createSitePackagesSnapshot(options) {
53
- console.info("Create the site-packages snapshot file...");
54
- const pyodide = await (0, pyodide_1.loadPyodide)();
55
- await pyodide.loadPackage(["micropip"]);
56
- if (options.useLocalKernelWheels) {
57
- const stliteKernelDir = path_1.default.dirname(require.resolve("@stlite/kernel")); // -> /path/to/kernel/dist
58
- const stliteKernelPyDir = path_1.default.resolve(stliteKernelDir, "../py"); // -> /path/to/kernel/py
59
- await installLocalWheel(pyodide, path_1.default.join(stliteKernelPyDir, "tornado/dist/tornado-6.2-py3-none-any.whl"));
60
- await installLocalWheel(pyodide, path_1.default.join(stliteKernelPyDir, "stlite-pyarrow/dist/stlite_pyarrow-0.1.0-py3-none-any.whl"));
61
- await installLocalWheel(pyodide, path_1.default.join(stliteKernelPyDir, "streamlit/lib/dist/streamlit-1.13.0-py2.py3-none-any.whl"));
62
- }
63
- else {
64
- const micropip = pyodide.pyimport("micropip");
65
- const wheelUrls = [
66
- "https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/tornado/dist/tornado-6.2-py3-none-any.whl",
67
- "https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/stlite-pyarrow/dist/stlite_pyarrow-0.1.0-py3-none-any.whl",
68
- "https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/streamlit/lib/dist/streamlit-1.13.0-py2.py3-none-any.whl",
69
- ];
70
- console.log("Install", wheelUrls);
71
- await micropip.install.callKwargs(wheelUrls, { keep_going: true });
72
- }
73
- console.log(`Install the requirements ${JSON.stringify(options.requirements)}`);
74
- const micropip = pyodide.pyimport("micropip");
75
- await micropip.install.callKwargs(options.requirements, { keep_going: true });
76
- console.log("Archive the site-packages director(y|ies)");
77
- const archiveFilePath = "/tmp/site-packages-snapshot.tar.gz";
78
- await pyodide.runPythonAsync(`
2
+ var D=Object.create;var m=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var f=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var P=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of S(t))!q.call(e,o)&&o!==r&&m(e,o,{get:()=>t[o],enumerable:!(i=T(t,o))||i.enumerable});return e};var s=(e,t,r)=>(r=e!=null?D(O(e)):{},P(t||!e||!e.__esModule?m(r,"default",{value:e,enumerable:!0}):r,e));var h=f(d=>{"use strict";Object.defineProperty(d,"__esModule",{value:!0});d.parseRequirementsTxt=void 0;function $(e){return e.split(`
3
+ `).filter(t=>!t.startsWith("#")).map(t=>t.trim()).filter(t=>t!=="")}d.parseRequirementsTxt=$});var g=f(c=>{"use strict";var j=c&&c.__createBinding||(Object.create?function(e,t,r,i){i===void 0&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);(!o||("get"in o?!t.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){i===void 0&&(i=r),e[i]=t[r]}),x=c&&c.__exportStar||function(e,t){for(var r in e)r!=="default"&&!Object.prototype.hasOwnProperty.call(t,r)&&j(t,e,r)};Object.defineProperty(c,"__esModule",{value:!0});x(h(),c)});var w=s(require("yargs")),v=require("yargs/helpers"),a=s(require("path")),n=s(require("fs/promises")),y=s(require("fs-extra")),b=s(require("node-fetch")),_=require("pyodide"),k=s(g());global.fetch=b.default;async function C(e){console.info("Copy the build directory (the bare built app files) to this directory...");let t=a.default.resolve(__dirname,"../build");if(!(await n.default.stat(t)).isDirectory())throw new Error(`The source ${t} does not exist.`);if(t===e.copyTo){console.warn(`sourceDir == destDir (${t}). Are you in the development environment? Skip copying the directory.`);return}if(e.keepOld)try{await n.default.access(e.copyTo),console.info(`${e.copyTo} already exists. Use it and skip copying.`);return}catch{throw new Error(`${e.copyTo} does not exist even though the \`keepOld\` option is specified`)}console.log(`Copy ${t} to ${e.copyTo}`),await n.default.rm(e.copyTo,{recursive:!0,force:!0}),await y.default.copy(t,e.copyTo)}async function u(e,t){console.log(`Install the local wheel ${t}`);let r=await n.default.readFile(t),i="/tmp/"+a.default.basename(t);e.FS.writeFile(i,r);let o=e.pyimport("micropip"),l=`emfs:${i}`;console.log(`Install ${l}`),await o.install.callKwargs(l,{keep_going:!0})}async function K(e){console.info("Create the site-packages snapshot file...");let t=await(0,_.loadPyodide)();if(await t.loadPackage(["micropip"]),e.useLocalKernelWheels){let l=a.default.dirname(require.resolve("@stlite/kernel")),p=a.default.resolve(l,"../py");await u(t,a.default.join(p,"tornado/dist/tornado-6.2-py3-none-any.whl")),await u(t,a.default.join(p,"stlite-pyarrow/dist/stlite_pyarrow-0.1.0-py3-none-any.whl")),await u(t,a.default.join(p,"streamlit/lib/dist/streamlit-1.13.0-py2.py3-none-any.whl"))}else{let l=t.pyimport("micropip"),p=["https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/tornado/dist/tornado-6.2-py3-none-any.whl","https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/stlite-pyarrow/dist/stlite_pyarrow-0.1.0-py3-none-any.whl","https://cdn.jsdelivr.net/npm/@stlite/kernel@0.10.1/py/streamlit/lib/dist/streamlit-1.13.0-py2.py3-none-any.whl"];console.log("Install",p),await l.install.callKwargs(p,{keep_going:!0})}console.log(`Install the requirements ${JSON.stringify(e.requirements)}`),await t.pyimport("micropip").install.callKwargs(e.requirements,{keep_going:!0}),console.log("Archive the site-packages director(y|ies)");let i="/tmp/site-packages-snapshot.tar.gz";await t.runPythonAsync(`
79
4
  import tarfile
80
5
  import site
81
6
 
82
7
  site_packages_dirs = site.getsitepackages()
83
8
 
84
- tar_file_name = '${archiveFilePath}'
9
+ tar_file_name = '${i}'
85
10
  with tarfile.open(tar_file_name, mode='w:gz') as gzf:
86
11
  for site_packages in site_packages_dirs:
87
12
  gzf.add(site_packages)
88
- `);
89
- console.log("Extract the archive file from EMFS");
90
- const archiveBin = pyodide.FS.readFile(archiveFilePath);
91
- console.log(`Save the archive file (${options.saveTo})`);
92
- await promises_1.default.writeFile(options.saveTo, archiveBin);
93
- }
94
- async function copyStreamlitAppDirectory(options) {
95
- console.info("Copy the Streamlit app directory...");
96
- console.log(`Copy ${options.sourceDir} to ${options.copyTo}`);
97
- await promises_1.default.rm(options.copyTo, { recursive: true, force: true });
98
- await fs_extra_1.default.copy(options.sourceDir, options.copyTo);
99
- }
100
- // Original: packages/sharing-editor/bin/gen-sample-app-manifests-json.ts
101
- // TODO: Be DRY
102
- async function readRequirements(requirementsTxtPath) {
103
- const requirementsTxtData = await promises_1.default.readFile(requirementsTxtPath, {
104
- encoding: "utf-8",
105
- });
106
- return requirementsTxtData
107
- .split("\n")
108
- .map((r) => r.trim())
109
- .filter((r) => r !== "");
110
- }
111
- // Original: kernel/src/requirements.ts
112
- // TODO: Be DRY
113
- function verifyRequirements(requirements) {
114
- requirements.forEach((req) => {
115
- let url;
116
- try {
117
- url = new URL(req);
118
- }
119
- catch {
120
- // `req` is not a URL -> OK
121
- return;
122
- }
123
- // Ref: The scheme checker in the micropip implementation is https://github.com/pyodide/micropip/blob/v0.1.0/micropip/_compat_in_pyodide.py#L23-L26
124
- if (url.protocol === "emfs:" || url.protocol === "file:") {
125
- throw new Error(`"emfs:" and "file:" protocols are not allowed for the requirement (${req})`);
126
- }
127
- });
128
- }
129
- (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
130
- .command("* <appHomeDirSource> [packages..]", "Put the user code and data and the snapshot of the required packages into the build artifact.", () => { }, (argv) => {
131
- console.info(argv);
132
- })
133
- .positional("appHomeDirSource", {
134
- describe: "The source directory of the user code and data that will be mounted in the Pyodide file system at app runtime",
135
- type: "string",
136
- demandOption: true,
137
- })
138
- .positional("packages", {
139
- describe: "Package names to install.",
140
- type: "string",
141
- array: true,
142
- })
143
- .options("requirement", {
144
- describe: "Install from the given requirements file. This option can be used multiple times.",
145
- array: true,
146
- type: "string",
147
- alias: "r",
148
- default: [],
149
- })
150
- .options("localKernelWheels", {
151
- describe: "Use the locally installed kernel wheels",
152
- type: "boolean",
153
- alias: "l",
154
- default: false,
155
- })
156
- .options("keepOldBuild", {
157
- type: "boolean",
158
- default: false,
159
- alias: "k",
160
- describe: "Keep the existing build directory contents except appHomeDir.",
161
- })
162
- .parseAsync()
163
- .then(async (args) => {
164
- const destDir = path_1.default.resolve(process.cwd(), "./build");
165
- try {
166
- await promises_1.default.access(args.appHomeDirSource);
167
- }
168
- catch {
169
- throw new Error(`${args.appHomeDirSource} does not exist.`);
170
- }
171
- let requirements = args.packages;
172
- for (const requirementTxtFilePath of args.requirement) {
173
- requirements = requirements.concat(await readRequirements(requirementTxtFilePath));
174
- }
175
- verifyRequirements(requirements);
176
- await copyBuildDirectory({ copyTo: destDir, keepOld: args.keepOldBuild });
177
- await createSitePackagesSnapshot({
178
- useLocalKernelWheels: args.localKernelWheels,
179
- requirements: requirements,
180
- saveTo: path_1.default.resolve(destDir, "./site-packages-snapshot.tar.gz"), // This path will be loaded in the `readSitePackagesSnapshot` handler in electron/main.ts.
181
- });
182
- await copyStreamlitAppDirectory({
183
- sourceDir: args.appHomeDirSource,
184
- copyTo: path_1.default.resolve(destDir, "./streamlit_app"), // This path will be loaded in the `readStreamlitAppDirectory` handler in electron/main.ts.
185
- });
186
- });
13
+ `),console.log("Extract the archive file from EMFS");let o=t.FS.readFile(i);console.log(`Save the archive file (${e.saveTo})`),await n.default.writeFile(e.saveTo,o)}async function F(e){console.info("Copy the Streamlit app directory..."),console.log(`Copy ${e.sourceDir} to ${e.copyTo}`),await n.default.rm(e.copyTo,{recursive:!0,force:!0}),await y.default.copy(e.sourceDir,e.copyTo)}async function B(e){let t=await n.default.readFile(e,{encoding:"utf-8"});return(0,k.parseRequirementsTxt)(t)}function E(e){e.forEach(t=>{let r;try{r=new URL(t)}catch{return}if(r.protocol==="emfs:"||r.protocol==="file:")throw new Error(`"emfs:" and "file:" protocols are not allowed for the requirement (${t})`)})}(0,w.default)((0,v.hideBin)(process.argv)).command("* <appHomeDirSource> [packages..]","Put the user code and data and the snapshot of the required packages into the build artifact.",()=>{},e=>{console.info(e)}).positional("appHomeDirSource",{describe:"The source directory of the user code and data that will be mounted in the Pyodide file system at app runtime",type:"string",demandOption:!0}).positional("packages",{describe:"Package names to install.",type:"string",array:!0}).options("requirement",{describe:"Install from the given requirements file. This option can be used multiple times.",array:!0,type:"string",alias:"r",default:[]}).options("localKernelWheels",{describe:"Use the locally installed kernel wheels",type:"boolean",alias:"l",default:!1}).options("keepOldBuild",{type:"boolean",default:!1,alias:"k",describe:"Keep the existing build directory contents except appHomeDir."}).parseAsync().then(async e=>{let t=a.default.resolve(process.cwd(),"./build");try{await n.default.access(e.appHomeDirSource)}catch{throw new Error(`${e.appHomeDirSource} does not exist.`)}let r=e.packages;for(let i of e.requirement)r=r.concat(await B(i));E(r),await C({copyTo:t,keepOld:e.keepOldBuild}),await K({useLocalKernelWheels:e.localKernelWheels,requirements:r,saveTo:a.default.resolve(t,"./site-packages-snapshot.tar.gz")}),await F({sourceDir:e.appHomeDirSource,copyTo:a.default.resolve(t,"./streamlit_app")})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stlite/desktop",
3
- "version": "0.18.2",
3
+ "version": "0.19.0",
4
4
  "license": "Apache-2.0",
5
5
  "homepage": "/",
6
6
  "main": "./build/electron/main.js",
@@ -20,11 +20,12 @@
20
20
  "start:electron": "tsc -p electron -w",
21
21
  "build:electron": "tsc -p electron",
22
22
  "build:pyodide": "curl -L https://github.com/pyodide/pyodide/releases/download/0.21.3/pyodide-build-0.21.3.tar.bz2 | tar xj -C ./build --files-from=./pyodide-files.txt",
23
- "build:bin": "tsc -p bin && sed -i'' -e '1 s/^#!.*$/#!\\/usr\\/bin\\/env node/' ./bin/*.js",
23
+ "build:bin": "esbuild ./bin/dump_artifacts.ts --bundle --minify --platform=node --external:@stlite/kernel --external:node-fetch --external:pyodide --external:yargs --external:fs-extra --outfile=./bin/dump_artifacts.js && sed -i'' -e '1 s/^#!.*$/#!\\/usr\\/bin\\/env node/' ./bin/*.js",
24
24
  "typecheck": "yarn tsc --noEmit -p electron",
25
25
  "start": "concurrently \"cross-env BROWSER=none yarn start:web\" \"wait-on http://localhost:3000 && yarn start:electron\" \"wait-on http://localhost:3000 && tsc -p electron && cross-env NODE_ENV=\"development\" electron .\"",
26
26
  "build:app": "yarn build:web && yarn build:electron && yarn build:pyodide",
27
27
  "build": "yarn build:app && yarn build:bin",
28
+ "dump": "dump-stlite-desktop-artifacts",
28
29
  "serve": "cross-env NODE_ENV=production electron .",
29
30
  "pack": "electron-builder --dir",
30
31
  "dist": "electron-builder",
@@ -49,7 +50,8 @@
49
50
  },
50
51
  "devDependencies": {
51
52
  "@craco/craco": "^6.1.2",
52
- "@stlite/kernel": "^0.18.2",
53
+ "@stlite/common": "^0.19.0",
54
+ "@stlite/kernel": "^0.19.0",
53
55
  "@testing-library/react": "^11.2.7",
54
56
  "@testing-library/user-event": "^13.1.9",
55
57
  "@types/jest": "^26.0.19",
@@ -60,6 +62,7 @@
60
62
  "electron": "20.2.0",
61
63
  "electron-builder": "^23.3.3",
62
64
  "electron-reload": "^2.0.0-alpha.1",
65
+ "esbuild": "^0.16.6",
63
66
  "fs-extra": "^10.1.0",
64
67
  "node-fetch": "2",
65
68
  "pyodide": "^0.21.3",