unify processing for executing a python script

This commit is contained in:
Jean-Francois Dockes 2020-09-28 14:04:09 +02:00
parent 90f39ffacc
commit 3716ea3dac
5 changed files with 68 additions and 74 deletions

View File

@ -1677,6 +1677,43 @@ string RclConfig::findFilter(const string &icmd) const
}
}
bool RclConfig::processFilterCmd(std::vector<std::string>& cmd) const
{
LOGDEB0("processFilterCmd: in: " << stringsToString(cmd) << "\n");
auto it = cmd.begin();
// Special-case python and perl on windows: we need to also locate the
// first argument which is the script name "python somescript.py".
// On Unix, thanks to #!, we usually just run "somescript.py", but need
// the same change if we ever want to use the same cmd line as windows
bool hasinterp = !stringlowercmp("python", *it) ||
!stringlowercmp("perl", *it);
*it++ = findFilter(*it);
if (hasinterp) {
if (cmd.size() < 2) {
LOGERR("processFilterCmd: python/perl cmd: no script?. [" <<
stringsToString(cmd) << "]\n");
return false;
} else {
*it = findFilter(*it);
}
}
LOGDEB0("processFilterCmd: out: " << stringsToString(cmd) << "\n");
return true;
}
bool RclConfig::pythonCmd(const std::string& scriptname,
std::vector<std::string>& cmd) const
{
#ifdef _WIN32
cmd = {"python", scriptname};
#else
cmd = {scriptname};
#endif
return processFilterCmd(cmd);
}
/**
* Return decompression command line for given mime type
*/
@ -1693,32 +1730,14 @@ bool RclConfig::getUncompressor(const string &mtype, vector<string>& cmd) const
LOGERR("getUncompressor: empty spec for mtype " << mtype << "\n");
return false;
}
vector<string>::iterator it = tokens.begin();
auto it = tokens.begin();
if (tokens.size() < 2)
return false;
if (stringlowercmp("uncompress", *it++))
return false;
cmd.clear();
cmd.push_back(findFilter(*it));
// Special-case python and perl on windows: we need to also locate the
// first argument which is the script name "python somescript.py".
// On Unix, thanks to #!, we usually just run "somescript.py", but need
// the same change if we ever want to use the same cmdling as windows
if (!stringlowercmp("python", *it) || !stringlowercmp("perl", *it)) {
it++;
if (tokens.size() < 3) {
LOGERR("getUncpressor: python/perl cmd: no script?. [" <<
mtype << "]\n");
} else {
*it = findFilter(*it);
}
} else {
it++;
}
cmd.insert(cmd.end(), it, tokens.end());
return true;
return processFilterCmd(cmd);
}
static const char blurb0[] =

View File

@ -332,6 +332,16 @@ class RclConfig {
bool getMissingHelperDesc(string&) const;
void storeMissingHelperDesc(const string &s);
/** Replace simple command name(s) inside vector with full
* paths. May have to replace two if the first entry is an
* interpreter name */
bool processFilterCmd(std::vector<std::string>& cmd) const;
/** Build command vector for python script, possibly prepending
interpreter on Windows */
bool pythonCmd(
const std::string& script, std::vector<std::string>& cmd) const;
/** Find exec file for external filter.
*
* If the input is an absolute path, we just return it. Else We

View File

@ -59,13 +59,13 @@ static const string magicpage{"NEWPPPAGE"};
void TextSplit::koStaticConfInit(RclConfig *config, const string& tagger)
{
#ifdef _WIN32
o_cmdpath = config->findFilter("python");
o_cmdargs.clear();
o_cmdargs.push_back(config->findFilter("kosplitter.py"));
#else
o_cmdpath = config->findFilter("kosplitter.py");
#endif
std::vector<std::string> cmdvec;
if (config->pythonCmd("kosplitter.py", cmdvec)) {
auto it = cmdvec.begin();
o_cmdpath = *it++;
o_cmdargs.clear();
o_cmdargs.insert(o_cmdargs.end(), it, cmdvec.end());
}
if (tagger == "Okt" || tagger == "Mecab" || tagger == "Komoran") {
o_taggername = tagger;
if (tagger == "Komoran")

View File

@ -66,22 +66,9 @@ bool runWebFilesMoverScript(RclConfig *config)
downloadsdir = path_tildexpand("~/Downloads");
}
}
static string cmdpath;
vector<string> args;
#ifdef _WIN32
const static string cmdnm{"python"};
args.push_back(config->findFilter("recoll-we-move-files.py"));
#else
const static string cmdnm{"recoll-we-move-files.py"};
#endif
if (cmdpath.empty()) {
cmdpath = config->findFilter(cmdnm);
if (cmdpath.empty()) {
LOGERR("runWFMoverScript: recoll-we-move-files.py not found\n");
return false;
}
}
vector<string> cmdvec;
config->pythonCmd("recoll-we-move-files.py", cmdvec);
/* Arrange to not actually run the script if the directory did not change */
static time_t dirmtime;
time_t ndirmtime = 0;
@ -100,7 +87,7 @@ bool runWebFilesMoverScript(RclConfig *config)
are created during the run. */
dirmtime = ndirmtime;
ExecCmd cmd;
int status = cmd.doexec(cmdpath, args);
int status = cmd.doexec1(cmdvec);
return status == 0;
}
return true;

View File

@ -231,27 +231,12 @@ MimeHandlerExec *mhExecFactory(RclConfig *cfg, const string& mtype, string& hs,
"]: [" << hs << "]\n");
return 0;
}
MimeHandlerExec *h = multiple ?
new MimeHandlerExecMultiple(cfg, id) :
new MimeHandlerExec(cfg, id);
vector<string>::iterator it = cmdtoks.begin();
// Special-case python and perl on windows: we need to also locate the
// first argument which is the script name "python somescript.py".
// On Unix, thanks to #!, we usually just run "somescript.py", but need
// the same change if we ever want to use the same cmdling as windows
if (!stringlowercmp("python", *it) || !stringlowercmp("perl", *it)) {
if (cmdtoks.size() < 2) {
LOGERR("mhExecFactory: python/perl cmd: no script?. [" <<
mtype << "]: [" << hs << "]\n");
}
vector<string>::iterator it1(it);
it1++;
*it1 = cfg->findFilter(*it1);
if (!cfg->processFilterCmd(cmdtoks)) {
return nullptr;
}
h->params.push_back(cfg->findFilter(*it++));
h->params.insert(h->params.end(), it, cmdtoks.end());
MimeHandlerExec *h = multiple ? new MimeHandlerExecMultiple(cfg, id) :
new MimeHandlerExec(cfg, id);
h->params = cmdtoks;
// Handle additional attributes. We substitute the semi-colons
// with newlines and use a ConfSimple
@ -261,16 +246,9 @@ MimeHandlerExec *mhExecFactory(RclConfig *cfg, const string& mtype, string& hs,
if (attrs.get(cstr_dj_keymt, value))
h->cfgFilterOutputMtype = stringtolower((const string&)value);
#if 0
string scmd;
for (it = h->params.begin(); it != h->params.end(); it++) {
scmd += string("[") + *it + "] ";
}
LOGDEB("mhExecFactory:mt [" << mtype << "] cfgmt [" <<
h->cfgFilterOutputMtype << "] cfgcs [" <<
h->cfgFilterOutputCharset << "] cmd: [" << scmd << "]\n");
#endif
LOGDEB2("mhExecFactory:mt [" << mtype << "] cfgmt [" <<
h->cfgFilterOutputMtype << "] cfgcs ["<<h->cfgFilterOutputCharset <<
"] cmd: [" << stringsToString(h->params) << "]\n");
return h;
}