OpenDroneMap-NodeODM/libs/processRunner.js

121 wiersze
4.3 KiB
JavaScript

2017-01-18 14:13:17 +00:00
/*
2023-06-08 09:58:50 +00:00
NodeODM App and REST API to access ODM.
Copyright (C) 2016 NodeODM Contributors
2017-01-18 14:13:17 +00:00
This program is free software: you can redistribute it and/or modify
2023-06-08 09:58:50 +00:00
it under the terms of the GNU Affero General Public License as published by
2017-01-18 14:13:17 +00:00
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2023-06-08 09:58:50 +00:00
You should have received a copy of the GNU Affero General Public License
2017-01-18 14:13:17 +00:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let fs = require('fs');
2021-05-24 15:19:16 +00:00
let apps = require('./apps');
2017-01-18 14:13:17 +00:00
let path = require('path');
let assert = require('assert');
let spawn = require('child_process').spawn;
let config = require('../config.js');
let logger = require('./logger');
2019-07-15 19:02:59 +00:00
let utils = require('./utils');
2017-01-18 14:13:17 +00:00
2020-01-19 20:21:54 +00:00
function makeRunner(command, args, requiredOptions = [], outputTestFile = null, skipOnTest = true){
return function(options, done, outputReceived){
for (let requiredOption of requiredOptions){
assert(options[requiredOption] !== undefined, `${requiredOption} must be defined`);
}
2017-01-18 14:13:17 +00:00
let commandArgs = args;
if (typeof commandArgs === 'function') commandArgs = commandArgs(options);
2017-01-18 14:13:17 +00:00
logger.info(`About to run: ${command} ${commandArgs.join(" ")}`);
2017-01-18 14:13:17 +00:00
2020-01-19 20:21:54 +00:00
if (config.test && skipOnTest){
logger.info("Test mode is on, command will not execute");
2017-01-18 14:13:17 +00:00
if (outputTestFile){
fs.readFile(path.resolve(__dirname, outputTestFile), 'utf8', (err, text) => {
if (!err){
2020-01-21 22:07:13 +00:00
if (outputReceived !== undefined){
let lines = text.split("\n");
lines.forEach(line => outputReceived(line));
}
done(null, 0, null);
}else{
logger.warn(`Error: ${err.message}`);
done(err);
}
});
}else{
done(null, 0, null);
}
2017-01-18 14:13:17 +00:00
return;// Skip rest
}
2017-01-18 14:13:17 +00:00
// Launch
2019-07-15 19:02:59 +00:00
const env = utils.clone(process.env);
env.LD_LIBRARY_PATH = path.join(config.odm_path, "SuperBuild", "install", "lib");
2020-01-19 20:21:54 +00:00
let cwd = undefined;
if (options.cwd) cwd = options.cwd;
let childProcess = spawn(command, commandArgs, { env, cwd });
2017-01-18 14:13:17 +00:00
childProcess
.on('exit', (code, signal) => done(null, code, signal))
.on('error', done);
2017-01-18 14:13:17 +00:00
2020-01-21 22:07:13 +00:00
if (outputReceived !== undefined){
childProcess.stdout.on('data', chunk => outputReceived(chunk.toString()));
childProcess.stderr.on('data', chunk => outputReceived(chunk.toString()));
2020-02-05 16:23:08 +00:00
}else{
childProcess.stdout.on('data', () => {});
childProcess.stderr.on('data', () => {});
2020-01-21 22:07:13 +00:00
}
2017-01-18 14:13:17 +00:00
return childProcess;
};
2017-01-18 14:13:17 +00:00
}
module.exports = {
runPostProcessingScript: makeRunner(path.join(__dirname, "..", "scripts", "postprocess.sh"),
function(options){
return [options.projectFolderPath];
},
2020-01-19 20:21:54 +00:00
["projectFolderPath"]),
2021-05-24 15:19:16 +00:00
sevenZip: makeRunner(apps.sevenZ, function(options){
2020-09-10 16:24:08 +00:00
return ["a", "-mx=0", "-y", "-r", "-bd", options.destination].concat(options.pathsToArchive);
2020-01-19 20:21:54 +00:00
},
["destination", "pathsToArchive", "cwd"],
null,
2020-09-10 16:24:08 +00:00
false),
2021-05-24 15:19:16 +00:00
sevenUnzip: makeRunner(apps.sevenZ, function(options){
2020-09-10 16:24:08 +00:00
let cmd = "x"; // eXtract files with full paths
if (options.noDirectories) cmd = "e"; //Extract files from archive (without using directory names)
return [cmd, "-aoa", "-bd", "-y", `-o${options.destination}`, options.file];
},
["destination", "file"],
null,
2020-09-10 16:55:22 +00:00
false),
2021-05-24 15:19:16 +00:00
unzip: makeRunner(apps.unzip, function(options){
2020-09-10 16:55:22 +00:00
const opts = options.noDirectories ? ["-j"] : [];
return opts.concat(["-qq", "-o", options.file, "-d", options.destination]);
},
["destination", "file"],
null,
2020-01-19 20:21:54 +00:00
false)
2017-01-18 14:13:17 +00:00
};