mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-02 14:01:10 +00:00
Fix bug 2637
* src/graphics/GraphicsConverter.C (Converter::Impl::Impl): Don't call the default converter directly, but create a temporary script with build_script() as for the configured converters. This makes sure that the file name does not need to be passed on the command line anymore. (build_script): This cannot fail anymore, so change the return type to void (build_script): Use build_conversion_command also for the default converter. This has the advantage that the special code for moving ${outfile}.0, ${outfile}.1 is actually used for ImageMagick's convert. (build_conversion_command): factored out from build_script git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14657 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
a392ba1c17
commit
f0648e2320
@ -133,10 +133,10 @@ string const & Converter::convertedFile() const
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/** Build the conversion script, returning true if able to build it.
|
/** Build the conversion script.
|
||||||
* The script is output to the ostringstream 'script'.
|
* The script is output to the stream \p script.
|
||||||
*/
|
*/
|
||||||
bool build_script(string const & from_file, string const & to_file_base,
|
void build_script(string const & from_file, string const & to_file_base,
|
||||||
string const & from_format, string const & to_format,
|
string const & from_format, string const & to_format,
|
||||||
ostream & script);
|
ostream & script);
|
||||||
|
|
||||||
@ -163,24 +163,7 @@ Converter::Impl::Impl(string const & from_file, string const & to_file_base,
|
|||||||
|
|
||||||
// The conversion commands are stored in a stringstream
|
// The conversion commands are stored in a stringstream
|
||||||
ostringstream script;
|
ostringstream script;
|
||||||
bool const success = build_script(from_file, to_file_base,
|
build_script(from_file, to_file_base, from_format, to_format, script);
|
||||||
from_format, to_format, script);
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
script_command_ =
|
|
||||||
support::os::python() + ' ' +
|
|
||||||
quoteName(libFileSearch("scripts", "convertDefault.py")) +
|
|
||||||
' ' +
|
|
||||||
quoteName((from_format.empty() ? "" : from_format + ':') + from_file) +
|
|
||||||
' ' +
|
|
||||||
quoteName(to_format + ':' + to_file_);
|
|
||||||
|
|
||||||
lyxerr[Debug::GRAPHICS]
|
|
||||||
<< "\tNo converter defined! I use convertDefault.py\n\t"
|
|
||||||
<< script_command_ << endl;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
lyxerr[Debug::GRAPHICS] << "\tConversion script:"
|
lyxerr[Debug::GRAPHICS] << "\tConversion script:"
|
||||||
<< "\n--------------------------------------\n"
|
<< "\n--------------------------------------\n"
|
||||||
<< script.str()
|
<< script.str()
|
||||||
@ -206,12 +189,11 @@ Converter::Impl::Impl(string const & from_file, string const & to_file_base,
|
|||||||
// The command needed to run the conversion process
|
// The command needed to run the conversion process
|
||||||
// We create a dummy command for ease of understanding of the
|
// We create a dummy command for ease of understanding of the
|
||||||
// list of forked processes.
|
// list of forked processes.
|
||||||
// Note: 'sh ' is absolutely essential, or execvp will fail.
|
// Note: 'python ' is absolutely essential, or execvp will fail.
|
||||||
script_command_ = support::os::python() + ' ' +
|
script_command_ = support::os::python() + ' ' +
|
||||||
quoteName(script_file_) + ' ' +
|
quoteName(script_file_) + ' ' +
|
||||||
quoteName(onlyFilename(from_file)) + ' ' +
|
quoteName(onlyFilename(from_file)) + ' ' +
|
||||||
quoteName(to_format);
|
quoteName(to_format);
|
||||||
}
|
|
||||||
// All is ready to go
|
// All is ready to go
|
||||||
valid_process_ = true;
|
valid_process_ = true;
|
||||||
}
|
}
|
||||||
@ -276,90 +258,13 @@ string const move_file(string const & from_file, string const & to_file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool build_script(string const & from_file,
|
void build_conversion_command(string const & command, ostream & script)
|
||||||
string const & to_file_base,
|
|
||||||
string const & from_format,
|
|
||||||
string const & to_format,
|
|
||||||
ostream & script)
|
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(from_format != to_format);
|
// Store in the python script
|
||||||
lyxerr[Debug::GRAPHICS] << "build_script ... ";
|
|
||||||
typedef Converters::EdgePath EdgePath;
|
|
||||||
|
|
||||||
if (from_format.empty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
script << "#!/usr/bin/env python -tt\n"
|
|
||||||
"import os, shutil, sys\n\n"
|
|
||||||
"def unlinkNoThrow(file):\n"
|
|
||||||
" ''' remove a file, do not throw if an error occurs '''\n"
|
|
||||||
" try:\n"
|
|
||||||
" os.unlink(file)\n"
|
|
||||||
" except:\n"
|
|
||||||
" pass\n\n";
|
|
||||||
|
|
||||||
// we do not use ChangeExtension because this is a basename
|
|
||||||
// which may nevertheless contain a '.'
|
|
||||||
string const to_file = to_file_base + '.'
|
|
||||||
+ formats.extension(to_format);
|
|
||||||
|
|
||||||
EdgePath edgepath = converters.getPath(from_format, to_format);
|
|
||||||
|
|
||||||
if (edgepath.empty()) {
|
|
||||||
lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a temporary base file-name for all intermediate steps.
|
|
||||||
// Remember to remove the temp file because we only want the name...
|
|
||||||
static int counter = 0;
|
|
||||||
string const tmp = "gconvert" + convert<string>(counter++);
|
|
||||||
string const to_base = tempName(string(), tmp);
|
|
||||||
unlink(to_base);
|
|
||||||
|
|
||||||
// Create a copy of the file in case the original name contains
|
|
||||||
// problematic characters like ' or ". We can work around that problem
|
|
||||||
// in python, but the converters might be shell scripts and have more
|
|
||||||
// troubles with it.
|
|
||||||
string outfile = changeExtension(to_base, getExtension(from_file));
|
|
||||||
script << "infile = '"
|
|
||||||
<< subst(subst(from_file, "\\", "\\\\"), "'", "\\'") << "'\n"
|
|
||||||
"outfile = " << quoteName(outfile) << "\n"
|
|
||||||
"shutil.copy(infile, outfile)\n";
|
|
||||||
|
|
||||||
// The conversion commands may contain these tokens that need to be
|
|
||||||
// changed to infile, infile_base, outfile respectively.
|
|
||||||
string const token_from("$$i");
|
|
||||||
string const token_base("$$b");
|
|
||||||
string const token_to("$$o");
|
|
||||||
|
|
||||||
EdgePath::const_iterator it = edgepath.begin();
|
|
||||||
EdgePath::const_iterator end = edgepath.end();
|
|
||||||
|
|
||||||
for (; it != end; ++it) {
|
|
||||||
::Converter const & conv = converters.get(*it);
|
|
||||||
|
|
||||||
// Build the conversion command
|
|
||||||
string const infile = outfile;
|
|
||||||
string const infile_base = changeExtension(infile, string());
|
|
||||||
outfile = changeExtension(to_base, conv.To->extension());
|
|
||||||
|
|
||||||
// Store these names in the shell script
|
|
||||||
script << "infile = " << quoteName(infile) << '\n'
|
|
||||||
<< "infile_base = " << quoteName(infile_base) << '\n'
|
|
||||||
<< "outfile = " << quoteName(outfile) << '\n';
|
|
||||||
|
|
||||||
string command = conv.command;
|
|
||||||
command = subst(command, token_from, "' + '\"' + infile + '\"' + '");
|
|
||||||
command = subst(command, token_base, "' + '\"' + infile_base + '\"' + '");
|
|
||||||
command = subst(command, token_to, "' + '\"' + outfile + '\"' + '");
|
|
||||||
command = libScriptSearch(command);
|
|
||||||
|
|
||||||
// Store in the shell script
|
|
||||||
script << "\nif os.system(r'" << command << "') != 0:\n";
|
script << "\nif os.system(r'" << command << "') != 0:\n";
|
||||||
|
|
||||||
// Test that this was successful. If not, remove
|
// Test that this was successful. If not, remove
|
||||||
// ${outfile} and exit the shell script
|
// ${outfile} and exit the python script
|
||||||
script << " unlinkNoThrow(outfile)\n"
|
script << " unlinkNoThrow(outfile)\n"
|
||||||
<< " sys.exit(1)\n\n";
|
<< " sys.exit(1)\n\n";
|
||||||
|
|
||||||
@ -381,11 +286,109 @@ bool build_script(string const & from_file,
|
|||||||
script << "unlinkNoThrow(infile)\n\n";
|
script << "unlinkNoThrow(infile)\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void build_script(string const & from_file,
|
||||||
|
string const & to_file_base,
|
||||||
|
string const & from_format,
|
||||||
|
string const & to_format,
|
||||||
|
ostream & script)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(from_format != to_format);
|
||||||
|
lyxerr[Debug::GRAPHICS] << "build_script ... ";
|
||||||
|
typedef Converters::EdgePath EdgePath;
|
||||||
|
|
||||||
|
script << "#!/usr/bin/env python\n"
|
||||||
|
"import os, shutil, sys\n\n"
|
||||||
|
"def unlinkNoThrow(file):\n"
|
||||||
|
" ''' remove a file, do not throw if an error occurs '''\n"
|
||||||
|
" try:\n"
|
||||||
|
" os.unlink(file)\n"
|
||||||
|
" except:\n"
|
||||||
|
" pass\n\n";
|
||||||
|
|
||||||
|
// we do not use ChangeExtension because this is a basename
|
||||||
|
// which may nevertheless contain a '.'
|
||||||
|
string const to_file = to_file_base + '.'
|
||||||
|
+ formats.extension(to_format);
|
||||||
|
|
||||||
|
EdgePath const edgepath = from_format.empty() ?
|
||||||
|
EdgePath() :
|
||||||
|
converters.getPath(from_format, to_format);
|
||||||
|
|
||||||
|
// Create a temporary base file-name for all intermediate steps.
|
||||||
|
// Remember to remove the temp file because we only want the name...
|
||||||
|
static int counter = 0;
|
||||||
|
string const tmp = "gconvert" + convert<string>(counter++);
|
||||||
|
string const to_base = tempName(string(), tmp);
|
||||||
|
unlink(to_base);
|
||||||
|
|
||||||
|
// Create a copy of the file in case the original name contains
|
||||||
|
// problematic characters like ' or ". We can work around that problem
|
||||||
|
// in python, but the converters might be shell scripts and have more
|
||||||
|
// troubles with it.
|
||||||
|
string outfile = changeExtension(to_base, getExtension(from_file));
|
||||||
|
script << "infile = '"
|
||||||
|
<< subst(subst(from_file, "\\", "\\\\"), "'", "\\'") << "'\n"
|
||||||
|
"outfile = " << quoteName(outfile) << "\n"
|
||||||
|
"shutil.copy(infile, outfile)\n";
|
||||||
|
|
||||||
|
if (edgepath.empty()) {
|
||||||
|
// Either from_format is unknown or we don't have a
|
||||||
|
// converter path from from_format to to_format, so we use
|
||||||
|
// the default converter.
|
||||||
|
script << "infile = outfile\n"
|
||||||
|
<< "outfile = " << quoteName(to_file) << '\n';
|
||||||
|
|
||||||
|
ostringstream os;
|
||||||
|
os << support::os::python() << " \""
|
||||||
|
<< libFileSearch("scripts", "convertDefault.py") << "\" ";
|
||||||
|
if (!from_format.empty())
|
||||||
|
os << from_format << ':';
|
||||||
|
os << "' + '\"' + infile + '\"' + ' "
|
||||||
|
<< to_format << ":' + '\"' + outfile + '\"' + '";
|
||||||
|
string const command = os.str();
|
||||||
|
|
||||||
|
lyxerr[Debug::GRAPHICS]
|
||||||
|
<< "\tNo converter defined! I use convertDefault.py\n\t"
|
||||||
|
<< command << endl;
|
||||||
|
|
||||||
|
build_conversion_command(command, script);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The conversion commands may contain these tokens that need to be
|
||||||
|
// changed to infile, infile_base, outfile respectively.
|
||||||
|
string const token_from("$$i");
|
||||||
|
string const token_base("$$b");
|
||||||
|
string const token_to("$$o");
|
||||||
|
|
||||||
|
EdgePath::const_iterator it = edgepath.begin();
|
||||||
|
EdgePath::const_iterator end = edgepath.end();
|
||||||
|
|
||||||
|
for (; it != end; ++it) {
|
||||||
|
::Converter const & conv = converters.get(*it);
|
||||||
|
|
||||||
|
// Build the conversion command
|
||||||
|
string const infile = outfile;
|
||||||
|
string const infile_base = changeExtension(infile, string());
|
||||||
|
outfile = changeExtension(to_base, conv.To->extension());
|
||||||
|
|
||||||
|
// Store these names in the python script
|
||||||
|
script << "infile = " << quoteName(infile) << '\n'
|
||||||
|
<< "infile_base = " << quoteName(infile_base) << '\n'
|
||||||
|
<< "outfile = " << quoteName(outfile) << '\n';
|
||||||
|
|
||||||
|
string command = conv.command;
|
||||||
|
command = subst(command, token_from, "' + '\"' + infile + '\"' + '");
|
||||||
|
command = subst(command, token_base, "' + '\"' + infile_base + '\"' + '");
|
||||||
|
command = subst(command, token_to, "' + '\"' + outfile + '\"' + '");
|
||||||
|
command = libScriptSearch(command);
|
||||||
|
|
||||||
|
build_conversion_command(command, script);
|
||||||
|
}
|
||||||
|
|
||||||
// Move the final outfile to to_file
|
// Move the final outfile to to_file
|
||||||
script << move_file("outfile", quoteName(to_file));
|
script << move_file("outfile", quoteName(to_file));
|
||||||
lyxerr[Debug::GRAPHICS] << "ready!" << endl;
|
lyxerr[Debug::GRAPHICS] << "ready!" << endl;
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace anon
|
} // namespace anon
|
||||||
|
Loading…
Reference in New Issue
Block a user