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 * 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"); LOGERR("getUncompressor: empty spec for mtype " << mtype << "\n");
return false; return false;
} }
vector<string>::iterator it = tokens.begin(); auto it = tokens.begin();
if (tokens.size() < 2) if (tokens.size() < 2)
return false; return false;
if (stringlowercmp("uncompress", *it++)) if (stringlowercmp("uncompress", *it++))
return false; return false;
cmd.clear(); 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()); cmd.insert(cmd.end(), it, tokens.end());
return true; return processFilterCmd(cmd);
} }
static const char blurb0[] = static const char blurb0[] =

View File

@ -332,6 +332,16 @@ class RclConfig {
bool getMissingHelperDesc(string&) const; bool getMissingHelperDesc(string&) const;
void storeMissingHelperDesc(const string &s); 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. /** Find exec file for external filter.
* *
* If the input is an absolute path, we just return it. Else We * 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) void TextSplit::koStaticConfInit(RclConfig *config, const string& tagger)
{ {
#ifdef _WIN32 std::vector<std::string> cmdvec;
o_cmdpath = config->findFilter("python"); if (config->pythonCmd("kosplitter.py", cmdvec)) {
o_cmdargs.clear(); auto it = cmdvec.begin();
o_cmdargs.push_back(config->findFilter("kosplitter.py")); o_cmdpath = *it++;
#else o_cmdargs.clear();
o_cmdpath = config->findFilter("kosplitter.py"); o_cmdargs.insert(o_cmdargs.end(), it, cmdvec.end());
#endif }
if (tagger == "Okt" || tagger == "Mecab" || tagger == "Komoran") { if (tagger == "Okt" || tagger == "Mecab" || tagger == "Komoran") {
o_taggername = tagger; o_taggername = tagger;
if (tagger == "Komoran") if (tagger == "Komoran")

View File

@ -66,22 +66,9 @@ bool runWebFilesMoverScript(RclConfig *config)
downloadsdir = path_tildexpand("~/Downloads"); downloadsdir = path_tildexpand("~/Downloads");
} }
} }
static string cmdpath; vector<string> cmdvec;
vector<string> args; config->pythonCmd("recoll-we-move-files.py", cmdvec);
#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;
}
}
/* Arrange to not actually run the script if the directory did not change */ /* Arrange to not actually run the script if the directory did not change */
static time_t dirmtime; static time_t dirmtime;
time_t ndirmtime = 0; time_t ndirmtime = 0;
@ -100,7 +87,7 @@ bool runWebFilesMoverScript(RclConfig *config)
are created during the run. */ are created during the run. */
dirmtime = ndirmtime; dirmtime = ndirmtime;
ExecCmd cmd; ExecCmd cmd;
int status = cmd.doexec(cmdpath, args); int status = cmd.doexec1(cmdvec);
return status == 0; return status == 0;
} }
return true; return true;

View File

@ -231,27 +231,12 @@ MimeHandlerExec *mhExecFactory(RclConfig *cfg, const string& mtype, string& hs,
"]: [" << hs << "]\n"); "]: [" << hs << "]\n");
return 0; return 0;
} }
MimeHandlerExec *h = multiple ? if (!cfg->processFilterCmd(cmdtoks)) {
new MimeHandlerExecMultiple(cfg, id) : return nullptr;
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);
} }
MimeHandlerExec *h = multiple ? new MimeHandlerExecMultiple(cfg, id) :
h->params.push_back(cfg->findFilter(*it++)); new MimeHandlerExec(cfg, id);
h->params.insert(h->params.end(), it, cmdtoks.end()); h->params = cmdtoks;
// Handle additional attributes. We substitute the semi-colons // Handle additional attributes. We substitute the semi-colons
// with newlines and use a ConfSimple // 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)) if (attrs.get(cstr_dj_keymt, value))
h->cfgFilterOutputMtype = stringtolower((const string&)value); h->cfgFilterOutputMtype = stringtolower((const string&)value);
#if 0 LOGDEB2("mhExecFactory:mt [" << mtype << "] cfgmt [" <<
string scmd; h->cfgFilterOutputMtype << "] cfgcs ["<<h->cfgFilterOutputCharset <<
for (it = h->params.begin(); it != h->params.end(); it++) { "] cmd: [" << stringsToString(h->params) << "]\n");
scmd += string("[") + *it + "] ";
}
LOGDEB("mhExecFactory:mt [" << mtype << "] cfgmt [" <<
h->cfgFilterOutputMtype << "] cfgcs [" <<
h->cfgFilterOutputCharset << "] cmd: [" << scmd << "]\n");
#endif
return h; return h;
} }