From a328b3eedaa57f806f807b63ea3c013338960a1d Mon Sep 17 00:00:00 2001 From: Thomas Hobson Date: Sun, 25 Apr 2021 15:02:57 +1200 Subject: [PATCH] better support for multiple languages per package --- api/src/executor/job.js | 5 ++--- api/src/index.js | 5 +++-- api/src/ppman/package.js | 4 ++-- api/src/runtime.js | 46 +++++++++++++++++++++++++++++++++------- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/api/src/executor/job.js b/api/src/executor/job.js index f7adec2..63122d0 100644 --- a/api/src/executor/job.js +++ b/api/src/executor/job.js @@ -17,7 +17,7 @@ let gid = 0; class Job { - constructor({ runtime, files, args, stdin, timeouts, alias }) { + constructor({ runtime, files, args, stdin, timeouts }) { this.uuid = uuidv4(); this.runtime = runtime; this.files = files.map((file,i) => ({ @@ -28,7 +28,6 @@ class Job { this.args = args; this.stdin = stdin; this.timeouts = timeouts; - this.alias = alias; this.uid = config.runner_uid_min + uid; this.gid = config.runner_gid_min + gid; @@ -90,7 +89,7 @@ class Job { const proc = cp.spawn(proc_call[0], proc_call.splice(1) ,{ env: { ...this.runtime.env_vars, - PISTON_ALIAS: this.alias + PISTON_LANGUAGE: this.runtime.language }, stdio: 'pipe', cwd: this.dir, diff --git a/api/src/index.js b/api/src/index.js index 94840ad..fcf82d6 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -50,7 +50,7 @@ const app = express(); .flat() .filter(pkg => fss.exists_sync(path.join(pkg, globals.pkg_installed_file))); - installed_languages.forEach(pkg => new runtime.Runtime(pkg)); + installed_languages.forEach(pkg => runtime.load_package(pkg)); logger.info('Starting API Server'); logger.debug('Constructing Express App'); @@ -100,7 +100,8 @@ const app = express(); return { language: rt.language, version: rt.version.raw, - aliases: rt.aliases + aliases: rt.aliases, + runtime: rt.runtime }; }); diff --git a/api/src/ppman/package.js b/api/src/ppman/package.js index ee12bd9..0e3c994 100644 --- a/api/src/ppman/package.js +++ b/api/src/ppman/package.js @@ -85,7 +85,7 @@ class Package { }); logger.debug('Registering runtime'); - new runtime.Runtime(this.install_path); + runtime.load_package(this.install_path); logger.debug('Caching environment'); const get_env_command = `cd ${this.install_path}; source environment; env`; @@ -136,7 +136,7 @@ class Package { logger.info(`Uninstalling ${this.language}-${this.version.raw}`); logger.debug("Finding runtime") - const found_runtime = runtime.get_latest_runtime_matching_language_version(this.language, this.version.raw); + const found_runtime = runtime.get_runtime_by_name_and_version(this.language, this.version.raw); if(!found_runtime){ logger.error(`Uninstalling ${this.language}-${this.version.raw} failed: Not installed`) diff --git a/api/src/runtime.js b/api/src/runtime.js index c5239c3..673c0d8 100644 --- a/api/src/runtime.js +++ b/api/src/runtime.js @@ -9,17 +9,21 @@ const runtimes = []; class Runtime { - constructor(package_dir){ + constructor({language, version, aliases, pkgdir, runtime}){ + this.language = language; + this.version = version; + this.aliases = aliases; + this.pkgdir = pkgdir; + this.runtime = runtime; + } + + static load_package(package_dir){ let info = JSON.parse( fss.read_file_sync(path.join(package_dir, 'pkg-info.json')) ); - const { language, version, build_platform, aliases } = info; - - this.pkgdir = package_dir; - this.language = language; - this.version = semver.parse(version); - this.aliases = aliases; + let { language, version, build_platform, aliases, provides } = info; + version = semver.parse(version); if (build_platform !== globals.platform) { logger.warn( @@ -28,9 +32,29 @@ class Runtime { ); } + if(provides){ + // Multiple languages in 1 package + provides.forEach(lang => { + runtimes.push(new Runtime({ + language: lang.language, + aliases: lang.aliases, + version, + pkgdir: package_dir, + runtime: language + })); + }); + }else{ + runtimes.push(new Runtime({ + language, + version, + aliases, + pkgdir: package_dir + })) + } + logger.debug(`Package ${language}-${version} was loaded`); - runtimes.push(this); + } get compiled() { @@ -79,3 +103,9 @@ module.exports.get_latest_runtime_matching_language_version = function(lang, ver return module.exports.get_runtimes_matching_language_version(lang, ver) .sort((a,b) => semver.rcompare(a.version, b.version))[0]; }; + +module.exports.get_runtime_by_name_and_version = function(runtime, ver){ + return runtimes.find(rt => (rt.runtime == runtime || (rt.runtime === undefined && rt.language == lang)) && semver.satisfies(rt.version, ver)); +} + +module.exports.load_package = Runtime.load_package; \ No newline at end of file