From fa524d8225a306ab43d5486cc19b28a2ccf9a9c3 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 16 Dec 2021 20:17:17 -0800 Subject: [PATCH] server: arch specific dependency paths --- server/python/plugin-remote.py | 9 ++--- server/src/plugin/plugin-npm-dependencies.ts | 36 +++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/server/python/plugin-remote.py b/server/python/plugin-remote.py index 3c2b8020b..c331f7892 100644 --- a/server/python/plugin-remote.py +++ b/server/python/plugin-remote.py @@ -21,7 +21,8 @@ from asyncio.futures import Future from asyncio.streams import StreamReader, StreamWriter import os from os import sys -from sys import stderr, stdout +import platform +import shutil class SystemDeviceState(TypedDict): lastEventTime: int @@ -164,8 +165,8 @@ class PluginRemote: zip = zipfile.ZipFile(zipPath) - python_prefix = os.path.join(os.environ.get( - 'SCRYPTED_PLUGIN_VOLUME'), 'python') + plugin_volume = os.environ.get('SCRYPTED_PLUGIN_VOLUME') + python_prefix = os.path.join(plugin_volume, 'python-%s-%s' % (platform.system(), platform.machine())) if not os.path.exists(python_prefix): os.makedirs(python_prefix) @@ -179,7 +180,7 @@ class PluginRemote: requirementstxt = os.path.join(python_prefix, 'requirements.txt') installed_requirementstxt = os.path.join( - python_prefix, 'installed-requirements.txt') + python_prefix, 'requirements.installed.txt') need_pip = True try: diff --git a/server/src/plugin/plugin-npm-dependencies.ts b/server/src/plugin/plugin-npm-dependencies.ts index 260521e3f..29e5c6c85 100644 --- a/server/src/plugin/plugin-npm-dependencies.ts +++ b/server/src/plugin/plugin-npm-dependencies.ts @@ -3,14 +3,19 @@ import fs from 'fs'; import child_process from 'child_process'; import path from 'path'; import { once } from 'events'; +import process from 'process'; +import rimraf from "rimraf"; +import mkdirp from "mkdirp"; export async function installOptionalDependencies(console: Console, packageJson: any) { const pluginVolume = ensurePluginVolume(packageJson.name); - const optPj = path.join(pluginVolume, 'package.json'); + const nodePrefix = path.join(pluginVolume, `${process.platform}-${process.arch}`); + const packageJsonPath = path.join(nodePrefix, 'package.json'); + const currentInstalledPackageJsonPath = path.join(nodePrefix, 'package.installed.json'); let currentPackageJson: any; try { - currentPackageJson = JSON.parse(fs.readFileSync(optPj).toString()); + currentPackageJson = JSON.parse(fs.readFileSync(currentInstalledPackageJsonPath).toString()); } catch (e) { } @@ -34,21 +39,18 @@ export async function installOptionalDependencies(console: Console, packageJson: delete reduced.optionalDependencies; delete reduced.devDependencies; - try { - fs.writeFileSync(optPj, JSON.stringify(reduced)); + mkdirp.sync(nodePrefix); + fs.writeFileSync(packageJsonPath, JSON.stringify(reduced)); - const cp = child_process.spawn('npm', ['--prefix', pluginVolume, 'install'], { - cwd: pluginVolume, - stdio: 'inherit', - }); - - await once(cp, 'exit'); - if (cp.exitCode !== 0) - throw new Error('npm installation failed with exit code ' + cp.exitCode); - } - catch (e) { - fs.rmSync(optPj); - throw e; - } + const cp = child_process.spawn('npm', ['--prefix', nodePrefix, 'install'], { + cwd: nodePrefix, + stdio: 'inherit', + }); + + await once(cp, 'exit'); + if (cp.exitCode !== 0) + throw new Error('npm installation failed with exit code ' + cp.exitCode); + + fs.writeFileSync(currentInstalledPackageJsonPath, JSON.stringify(reduced)); console.log('native dependencies installed.'); }