From 8a8bee33c1331366fdb4914822fe4b1fd0d5785c Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 13 Aug 2024 22:45:51 -0700 Subject: [PATCH] server: normalize fs path --- server/.vscode/launch.json | 4 ++-- server/package-lock.json | 8 ++++---- server/package.json | 2 +- server/python/plugin_remote.py | 2 +- server/src/plugin/plugin-remote-worker.ts | 8 ++++---- server/src/plugin/runtime/node-worker-common.ts | 4 ++++ server/src/plugin/runtime/python-worker.ts | 3 +++ 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/server/.vscode/launch.json b/server/.vscode/launch.json index cc6507147..12cb7c7ef 100644 --- a/server/.vscode/launch.json +++ b/server/.vscode/launch.json @@ -31,8 +31,8 @@ // "SCRYPTED_DEFAULT_AUTHENTICATION": "demo", // force usage of system python because brew python is 3.11 // which has no wheels for coreml tools or tflite-runtime - "SCRYPTED_PYTHON_PATH": "/opt/homebrew/bin/python3.11", - "SCRYPTED_PYTHON310_PATH": "/opt/homebrew/bin/python3.10", + // "SCRYPTED_PYTHON_PATH": "/opt/homebrew/bin/python3.11", + // "SCRYPTED_PYTHON310_PATH": "/opt/homebrew/bin/python3.10", // "SCRYPTED_PORTABLE_PYTHON": "true", // "SCRYPTED_SHARED_WORKER": "true", // "SCRYPTED_DISABLE_AUTHENTICATION": "true", diff --git a/server/package-lock.json b/server/package-lock.json index 726030bac..87d8548b1 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -29,7 +29,7 @@ "node-dijkstra": "^2.5.0", "node-forge": "^1.3.1", "node-gyp": "^10.2.0", - "py": "npm:@bjia56/portable-python@^0.1.54", + "py": "npm:@bjia56/portable-python@^0.1.74", "router": "^1.3.8", "semver": "^7.6.3", "sharp": "^0.33.4", @@ -2969,9 +2969,9 @@ }, "node_modules/py": { "name": "@bjia56/portable-python", - "version": "0.1.54", - "resolved": "https://registry.npmjs.org/@bjia56/portable-python/-/portable-python-0.1.54.tgz", - "integrity": "sha512-cvE+K+NEsFEa0H5iMn5RWEE5FdAxxmhNqFRafRcqd1kbGdB5kb5MZs2LC1amurB3BD/2pb9ZS/1iABwaNjgT5A==", + "version": "0.1.74", + "resolved": "https://registry.npmjs.org/@bjia56/portable-python/-/portable-python-0.1.74.tgz", + "integrity": "sha512-gg8t3/5d5c/anPb9NTvf68CSZXMXKJ/9ITycpCquMRP7A0YfHn4l493ifQY+d8/XCooSs9qWSisGufZaAb21UA==", "dependencies": { "adm-zip": "^0.5.10" } diff --git a/server/package.json b/server/package.json index 7b10eeb1c..229806445 100644 --- a/server/package.json +++ b/server/package.json @@ -22,7 +22,7 @@ "node-dijkstra": "^2.5.0", "node-forge": "^1.3.1", "node-gyp": "^10.2.0", - "py": "npm:@bjia56/portable-python@^0.1.54", + "py": "npm:@bjia56/portable-python@^0.1.74", "router": "^1.3.8", "semver": "^7.6.3", "sharp": "^0.33.4", diff --git a/server/python/plugin_remote.py b/server/python/plugin_remote.py index d1da3acf7..30244d10e 100644 --- a/server/python/plugin_remote.py +++ b/server/python/plugin_remote.py @@ -798,7 +798,7 @@ class PluginRemote: print("requirements.txt (up to date)") print(str_requirements) - sys.path.insert(0, zipPath) + sys.path.insert(0, plugin_zip_paths.get("unzipped_path")) sys.path.insert(0, pip_target) self.systemManager = SystemManager(self.api, self.systemState) diff --git a/server/src/plugin/plugin-remote-worker.ts b/server/src/plugin/plugin-remote-worker.ts index 03ec26247..57796d21c 100644 --- a/server/src/plugin/plugin-remote-worker.ts +++ b/server/src/plugin/plugin-remote-worker.ts @@ -252,10 +252,10 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe } if (worker_threads.isMainThread) { const fsDir = path.join(unzippedPath, 'fs') - if (fs.existsSync(fsDir)) - process.chdir(fsDir); - else - process.chdir(unzippedPath); + await fs.promises.mkdir(fsDir, { + recursive: true, + }); + process.chdir(fsDir); } const pluginReader = (name: string) => { diff --git a/server/src/plugin/runtime/node-worker-common.ts b/server/src/plugin/runtime/node-worker-common.ts index c65c1d1ab..b15f54477 100644 --- a/server/src/plugin/runtime/node-worker-common.ts +++ b/server/src/plugin/runtime/node-worker-common.ts @@ -93,6 +93,10 @@ export function extractZip(pluginVolume: string, h: string, zipBuffer: Buffer) { fs.writeFileSync(path.join(zipDirTmp, zipFilename), zipBuffer); const admZip = new AdmZip(zipBuffer); admZip.extractAllTo(path.join(zipDirTmp, 'unzipped'), true); + const fsPath = path.join(zipDirTmp, 'unzipped', 'fs'); + fs.mkdirSync(fsPath, { + recursive: true, + }); fs.renameSync(zipDirTmp, zipDir); return { diff --git a/server/src/plugin/runtime/python-worker.ts b/server/src/plugin/runtime/python-worker.ts index d74d30974..917bc1869 100644 --- a/server/src/plugin/runtime/python-worker.ts +++ b/server/src/plugin/runtime/python-worker.ts @@ -96,7 +96,10 @@ export class PythonRuntimeWorker extends ChildProcessWorker { const setup = () => { const types = require.resolve('@scrypted/types'); const PYTHONPATH = types.substring(0, types.indexOf('types') + 'types'.length); + const fsDir = path.join(options.unzippedPath, 'fs'); + fs.mkdirSync(fsDir, { recursive: true }); this.worker = child_process.spawn(pythonPath, args, { + cwd: fsDir, // stdin, stdout, stderr, peer in, peer out stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'], env: Object.assign({