From be1f96045b59058309c982ca63768aea02c67fbc Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sat, 15 Jul 2006 11:25:49 +0000 Subject: [PATCH] =?utf8?q?Fix=20bug=202637=20=C2=A0=20=C2=A0=20=C2=A0=20?= =?utf8?q?=C2=A0=20*=20src/graphics/GraphicsCacheItem.C=20=C2=A0=20=C2=A0?= =?utf8?q?=20=C2=A0=20=C2=A0=20(CacheItem::Impl::convertToDisplayFormat):?= =?utf8?q?=20Don't=20derive=20the=20temp=20=C2=A0=20=C2=A0=20=C2=A0=20?= =?utf8?q?=C2=A0=20file=20name=20from=20the=20original=20file=20name.=20Th?= =?utf8?q?is=20ensures=20that=20it=20is=20=C2=A0=20=C2=A0=20=C2=A0=20?= =?utf8?q?=C2=A0=20composed=20of=20valid=20characters=20only.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit         * src/graphics/GraphicsConverter.C: remove example script, since it         will get out of date.         (build_script): Copy the original file to a temp file before the         conversion chain starts. This avoids problems with converters that         can't handle ' in filenames. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14459 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/GraphicsCacheItem.C | 4 +- src/graphics/GraphicsConverter.C | 63 +++++++------------------------- 2 files changed, 14 insertions(+), 53 deletions(-) diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index 01b5fa4131..476b28521b 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -426,12 +426,10 @@ void CacheItem::Impl::convertToDisplayFormat() } lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl; - // Take only the filename part of the file, without path or extension. - string const temp = changeExtension(onlyFilename(filename), string()); // Add some stuff to create a uniquely named temporary file. // This file is deleted in loadImage after it is loaded into memory. - string const to_file_base = tempName(string(), temp); + string const to_file_base = tempName(string(), "CacheItem"); remove_loaded_file_ = true; // Remove the temp file, we only want the name... diff --git a/src/graphics/GraphicsConverter.C b/src/graphics/GraphicsConverter.C index a9e66b2a74..8f32d6bfa6 100644 --- a/src/graphics/GraphicsConverter.C +++ b/src/graphics/GraphicsConverter.C @@ -33,6 +33,7 @@ namespace support = lyx::support; using support::changeExtension; using support::Forkedcall; using support::ForkedCallQueue; +using support::getExtension; using support::libFileSearch; using support::libScriptSearch; using support::onlyPath; @@ -265,7 +266,6 @@ string const move_file(string const & from_file, string const & to_file) << "try:\n" << " os.rename(fromfile, tofile)\n" << "except:\n" - << " import shutil\n" << " try:\n" << " shutil.copy(fromfile, tofile)\n" << " except:\n" @@ -276,50 +276,6 @@ string const move_file(string const & from_file, string const & to_file) } -/* -A typical script looks like: - -#!/usr/bin/env python -tt -import os, sys - -def unlinkNoThrow(file): - ''' remove a file, do not throw if error occurs ''' - try: - os.unlink(file) - except: - pass - -infile = '/home/username/Figure3a.eps' -infile_base = '/home/username/Figure3a' -outfile = '/tmp/lyx_tmpdir12992hUwBqt/gconvert0129929eUBPm.pdf' - -if os.system(r'epstopdf ' + '"' + infile + '"' + ' --output ' + '"' + outfile + '"' + '') != 0: - unlinkNoThrow(outfile) - sys.exit(1) - -if not os.path.isfile(outfile): - if os.path.isfile(outfile + '.0'): - os.rename(outfile + '.0', outfile) - import glob - for file in glob.glob(outfile + '.?'): - unlinkNoThrow(file) - else: - sys.exit(1) - -fromfile = outfile -tofile = '/tmp/lyx_tmpdir12992hUwBqt/Figure3a129927ByaCl.ppm' - -try: - os.rename(fromfile, tofile) -except: - import shutil - try: - shutil.copy(fromfile, tofile) - except: - sys.exit(1) - unlinkNoThrow(fromfile) - -*/ bool build_script(string const & from_file, string const & to_file_base, string const & from_format, @@ -334,7 +290,7 @@ bool build_script(string const & from_file, return false; script << "#!/usr/bin/env python -tt\n" - "import os, sys\n\n" + "import os, shutil, sys\n\n" "def unlinkNoThrow(file):\n" " ''' remove a file, do not throw if an error occurs '''\n" " try:\n" @@ -361,7 +317,15 @@ bool build_script(string const & from_file, string const to_base = tempName(string(), tmp); unlink(to_base); - string outfile = from_file; + // 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. @@ -413,9 +377,8 @@ bool build_script(string const & from_file, " else:\n" " sys.exit(1)\n\n"; - // Delete the infile, if it isn't the original, from_file. - if (infile != from_file) - script << "unlinkNoThrow(infile)\n\n"; + // Delete the infile + script << "unlinkNoThrow(infile)\n\n"; } // Move the final outfile to to_file -- 2.39.2