UNPKG

ai-planning-val

Version:

Javascript/typescript wrapper for VAL (AI Planning plan validation and evaluation tools from KCL Planning department and the planning community around the ICAPS conference).

286 lines 14 kB
"use strict"; /* eslint-disable @typescript-eslint/no-use-before-define */ /* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeValManifest = exports.readValManifest = exports.ValDownloader = exports.X32 = exports.X64 = exports.DARWIN = exports.LINUX = exports.WIN32 = exports.UnsupportedOperatingSystem = void 0; const url_1 = require("url"); const path = __importStar(require("path")); const pddl_workspace_1 = require("pddl-workspace"); const fs = __importStar(require("fs")); const os = __importStar(require("os")); const adm_zip_1 = __importDefault(require("adm-zip")); const httpUtils_1 = require("./httpUtils"); class UnsupportedOperatingSystem { constructor(supportedOperatingSystems, yourOperatingSystem, yourCpuArchitecture) { this.supportedOperatingSystems = supportedOperatingSystems; this.yourOperatingSystem = yourOperatingSystem; this.yourCpuArchitecture = yourCpuArchitecture; } get name() { return "UnsupportedOperatingSystem"; } get message() { return `Binaries for operating system ${this.yourOperatingSystem} ${this.yourCpuArchitecture} are not available. Supported: ${this.supportedOperatingSystems.join(', ')}`; } } exports.UnsupportedOperatingSystem = UnsupportedOperatingSystem; exports.WIN32 = "win32"; exports.LINUX = "linux"; exports.DARWIN = "darwin"; exports.X64 = "x64"; exports.X32 = "x32"; class ValDownloader { downloadDelegate(url, zipPath, message) { return __awaiter(this, void 0, void 0, function* () { console.log(message); return (0, httpUtils_1.getFile)(new url_1.URL(url), zipPath); }); } /** * Downloads given version of VAL. * @param buildId VAL build ID to download artifacts from * @param destinationDirectory Directory where VAL binaries are to be downloaded locally. * @param platform optionally specify the platform * @param architecture optionally specify the architecture */ download(buildId, destinationDirectory, platform, architecture) { return __awaiter(this, void 0, void 0, function* () { const artifactName = ValDownloader.getBuildArtifactName(platform, architecture); if (!artifactName) { throw this.unsupportedOperatingSystem(); } yield pddl_workspace_1.utils.afs.mkdirIfDoesNotExist(destinationDirectory, { mode: 0o755, recursive: true }); const zipPath = path.join(destinationDirectory, "drop.zip"); yield pddl_workspace_1.utils.afs.mkdirIfDoesNotExist(path.dirname(zipPath), 0o755); const url = `https://dev.azure.com/schlumberger/4e6bcb11-cd68-40fe-98a2-e3777bfec0a6/_apis/build/builds/${buildId}/artifacts?artifactName=${artifactName}&api-version=5.2-preview.5&%24format=zip`; yield this.downloadDelegate(url, zipPath, 'Downloading VAL tools...'); console.log("Done downloading."); const dropEntries = yield this.unzip(zipPath, destinationDirectory); console.log("Done unzipping."); const zipEntries = dropEntries .filter(entry => entry.endsWith('.zip')); if (zipEntries.length !== 1) { throw new Error(`Binary archive contains unexpected number of zip entries: ${zipEntries.length}. Content: ${dropEntries}`); } const valZipFileName = zipEntries[0]; console.log(`Zip found ${valZipFileName}`); const versionMatch = /^Val-(\d{8}\.\d+(\.DRAFT)?(-Linux)?)/.exec(path.basename(valZipFileName)); if (!versionMatch) { throw new Error("Binary archive version does not conform to the expected pattern: " + valZipFileName); } const version = versionMatch[1]; const valToolFileNames = yield this.decompress(path.join(destinationDirectory, valZipFileName), destinationDirectory); console.log(`Val binaries unzipped to directory: ${path.join(process.cwd(), destinationDirectory)}`); // clean-up and delete the drop content yield ValDownloader.deleteAll(dropEntries.map(file => path.join(destinationDirectory, file))); // delete the drop zip yield fs.promises.unlink(zipPath); const manifest = { buildId: buildId, version: version, files: valToolFileNames, parserPath: findValToolPath(valToolFileNames, PARSER_FILE_NAME), validatePath: findValToolPath(valToolFileNames, VALIDATE_FILE_NAME), valueSeqPath: findValToolPath(valToolFileNames, VALUE_SEQ_FILE_NAME), valStepPath: findValToolPath(valToolFileNames, VAL_STEP_FILE_NAME) }; this.allowExecution(destinationDirectory, manifest); return manifest; }); } decompress(compressedFilePath, destinationDirectory) { return __awaiter(this, void 0, void 0, function* () { if (compressedFilePath.endsWith(".zip")) { return this.unzip(compressedFilePath, destinationDirectory); } else { throw new Error(`VAL tools were downloaded to ${compressedFilePath}, and must be de-compressed and configured manually.`); } }); } unzip(zipPath, destinationDirectory) { return __awaiter(this, void 0, void 0, function* () { console.log(`Unzipping ${zipPath} to ${destinationDirectory}`); const zip = new adm_zip_1.default(zipPath); const entryNames = zip.getEntries() .filter(entry => !entry.isDirectory) .map(entry => entry.entryName); console.log(`Unzipping ${entryNames.join(', ')}`); zip.extractAllTo(destinationDirectory, true); console.log(`Unzipped ${entryNames.join(', ')}`); return entryNames; // return await new Promise<string[]>((resolve, reject) => { // zip.extractAllToAsync(destinationDirectory, true, err => { // console.log(`Done unzipping to ${destinationDirectory}, err: ${err}`) // if (err) { // reject(err); // return; // } // else { // resolve(entryNames); // } // }); // }); }); } static deleteAll(files) { return __awaiter(this, void 0, void 0, function* () { // 1. delete downloaded files const deletionPromises = files .filter(file => fs.existsSync(file)) .map((file) => __awaiter(this, void 0, void 0, function* () { return yield fs.promises.unlink(file); })); yield Promise.all(deletionPromises); // 2. delete empty directories const directories = [...new Set(files.map(file => path.dirname(file)))]; const emptyDirectories = directories // sorted from longest to shortest to delete sub-directories first .sort((a, b) => b.length - a.length); for (const directory of emptyDirectories) { if ((yield pddl_workspace_1.utils.afs.exists(directory)) && (yield pddl_workspace_1.utils.afs.isEmpty(directory))) { yield fs.promises.rmdir(directory); } } }); } /** * Calculates the artifact name for this computer, or given the specified `platform` and `architecture`. * @param platform optionally specify the platform * @param architecture optionally specify the architecture */ static getBuildArtifactName(platform, architecture) { switch (platform !== null && platform !== void 0 ? platform : os.platform()) { case exports.WIN32: switch (architecture !== null && architecture !== void 0 ? architecture : os.arch()) { case exports.X64: return "win64"; case exports.X32: case "ia32": return exports.WIN32; default: return null; } case exports.LINUX: switch (architecture !== null && architecture !== void 0 ? architecture : os.arch()) { case exports.X64: return "linux64"; default: return null; } case exports.DARWIN: switch (architecture !== null && architecture !== void 0 ? architecture : os.arch()) { case exports.X64: return "macos64"; default: return null; } default: return null; } } allowExecution(targetDirectory, manifest) { const executePermission = fs.constants.S_IXUSR | fs.constants.S_IRGRP; [manifest.parserPath, manifest.valStepPath, manifest.validatePath, manifest.valueSeqPath] .filter(tool => !!tool) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion .map(tool => tool) .forEach(tool => { fs.chmodSync(path.join(targetDirectory, tool), executePermission); }); } unsupportedOperatingSystem() { return new UnsupportedOperatingSystem(["win32 (arch: x64, x32, ia32)", "linux (arch: x64)", "darwin (x64)"], os.platform(), os.arch()); } } exports.ValDownloader = ValDownloader; ValDownloader.VAL_BINARY_PROJECT = "https://dev.azure.com/schlumberger/ai-planning-validation"; ValDownloader.VAL_REPO = `https://github.com/KCL-Planning/VAL`; const PARSER_FILE_NAME = "Parser"; const VALIDATE_FILE_NAME = "Validate"; const VALUE_SEQ_FILE_NAME = "ValueSeq"; const VAL_STEP_FILE_NAME = "ValStep"; /** * Finds the path of given VAL tool in the given version. * @param allFiles all downloaded VAL files (relative paths) * @param toolName tool name for which we are looking for its path * @returns corresponding path, or _undefined_ if the _valVersion_ argument is null or undefined */ function findValToolPath(allFiles, toolName) { if (!allFiles) { return undefined; } const pattern = new RegExp("\\b" + toolName + "(?:\\.exe)?$"); return allFiles.find(filePath => pattern.test(filePath)); } function readValManifest(manifestPath) { return __awaiter(this, void 0, void 0, function* () { try { const versionAsString = yield fs.promises.readFile(manifestPath, { encoding: 'utf8' }); return JSON.parse(versionAsString); } catch (err) { if (err instanceof Error) { throw new Error(`Error reading VAL manifest ${err.name}: ${err.message}`); } else { throw err; } } }); } exports.readValManifest = readValManifest; function writeValManifest(manifestPath, valVersion) { return __awaiter(this, void 0, void 0, function* () { const json = JSON.stringify(valVersion, null, 2); try { console.log(`Saving Manifest to ${manifestPath}`); yield fs.promises.writeFile(manifestPath, json, { encoding: 'utf8' }); console.log(`Manifest saved to ${manifestPath}`); } catch (err) { if (err instanceof Error) { throw new Error(`Error saving VAL manifest ${err.name}: ${err.message}`); } else { throw err; } } }); } exports.writeValManifest = writeValManifest; //# sourceMappingURL=ValDownloader.js.map