From ee97b45de7fa521bfa5f58eb66f03a3c37e1252e Mon Sep 17 00:00:00 2001 From: Elias Karakoulakis Date: Wed, 18 Jan 2017 00:09:38 +0200 Subject: [PATCH] use simpler find for resource constrain Linuxes --- lib/ozwpaths.js | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/ozwpaths.js b/lib/ozwpaths.js index 87ccc267..c880e74b 100644 --- a/lib/ozwpaths.js +++ b/lib/ozwpaths.js @@ -22,30 +22,36 @@ var cp = require('child_process'); var util = require('util'); var path = require('path'); +function doScript(script, suppressException) { + //console.log('---> ' + script); + try { + return(cp.execSync(script).toString()); + } catch(e) { + //console.log('---> ' + e.toString()); + if (!suppressException) throw e; + } +} + var configs = { includedir: { findpattern: 'OZWException.h', - locations: ["/usr/include/", "/usr/local/include/"], - default: "/usr/local/include/openzwave/" + locations: ["/usr/local/include", "/usr/include"] }, libdir: { findpattern: 'libopenzwave.so', - locations: ["/usr/lib*", "/usr/local/lib*"], - default: "/usr/local/lib64/" + locations: ["/usr/local/lib64", "/usr/local/lib", "/usr/lib64", "/usr/lib"] }, sysconfdir: { findpattern: 'zwcfg.xsd', - locations: ["/usr/etc/", "/usr/local/etc/", "/etc/"], - default: "/usr/local/etc/openzwave/" + locations: ["/usr/local/etc", "/usr/etc", "/etc"] }, docdir: { - findpattern: 'openzwave-*', - locations: ["/usr/local/share/doc/"], - default: "/usr/local/share/doc/openzwave-1.4.2278" + findpattern: 'Doxyfile.in', + locations: ["/usr/local/share/doc", "/usr/doc"] } } -var usepkgconfig = true; +var usepkgconfig = false; try { cp.execSync("pkg-config --exists libopenzwave"); } catch (e) { @@ -53,37 +59,29 @@ try { } function getConfigItem(item) { - // 1. try using pkg-config + // 1. try the easy way first: use pkg-config and libopenzwave.pc if (usepkgconfig) { var cmdfmt = "pkg-config --variable=%s libopenzwave"; var cmd = util.format(cmdfmt, item); return (cp.execSync(cmd).toString().split('\n')[0]); } else { // 2. try using find - var cmdfmt = "find %s ! -readable -prune -o -name %s -print"; + var cmdfmt = "find %s -name %s"; var fp = configs[item].findpattern; for (var i in configs[item].locations) { var loc = configs[item].locations[i]; if (!fs.existsSync(loc)) continue; var cmd = util.format(cmdfmt, loc, fp); - try { - var dir = cp.execSync(cmd).toString().split('\n')[0]; - if (!dir) continue; - return path.dirname(dir) - } catch (e) { - // console.log('not found in %s', loc); - } + var dir = doScript(cmd, true); + if (!dir) continue; + return path.dirname(dir.split('\n')[0]); } } } var paths = {}; - -// try the easy way first: use pkg-config and libopenzwave.pc Object.keys(configs).forEach(function(item) { - var ret = getConfigItem(item); - // or fallback to the default value if automatic scan fails - paths[item] = ret || configs[item].default; + paths[item] = getConfigItem(item); }); //console.log('=====RESULTS=====');