]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.C
* src/graphics/GraphicsConverter.C
[lyx.git] / src / graphics / GraphicsConverter.C
1 /**
2  * \file GraphicsConverter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GraphicsConverter.h"
14
15 #include "converter.h"
16 #include "debug.h"
17 #include "format.h"
18
19 #include "support/filetools.h"
20 #include "support/forkedcallqueue.h"
21 #include "support/convert.h"
22 #include "support/lstrings.h"
23 #include "support/lyxlib.h"
24 #include "support/os.h"
25
26 #include <boost/bind.hpp>
27
28 #include <sstream>
29 #include <fstream>
30
31 namespace support = lyx::support;
32
33 using support::changeExtension;
34 using support::Forkedcall;
35 using support::ForkedCallQueue;
36 using support::libFileSearch;
37 using support::libScriptSearch;
38 using support::onlyPath;
39 using support::onlyFilename;
40 using support::quoteName;
41 using support::subst;
42 using support::tempName;
43 using support::unlink;
44
45 using std::endl;
46 using std::ostream;
47 using std::ostringstream;
48 using std::string;
49
50
51 namespace lyx {
52 namespace graphics {
53
54 class Converter::Impl : public boost::signals::trackable {
55 public:
56         ///
57         Impl(string const &, string const &, string const &, string const &);
58
59         ///
60         void startConversion();
61
62         /** This method is connected to a signal passed to the forked call
63          *  class, passing control back here when the conversion is completed.
64          *  Cleans-up the temporary files, emits the finishedConversion
65          *  signal and removes the Converter from the list of all processes.
66          */
67         void converted(pid_t pid, int retval);
68
69         /** At the end of the conversion process inform the outside world
70          *  by emitting a signal.
71          */
72         typedef boost::signal<void(bool)> SignalType;
73         ///
74         SignalType finishedConversion;
75
76         ///
77         string script_command_;
78         ///
79         string script_file_;
80         ///
81         string to_file_;
82         ///
83         bool valid_process_;
84         ///
85         bool finished_;
86 };
87
88
89 bool Converter::isReachable(string const & from_format_name,
90                             string const & to_format_name)
91 {
92         return converters.isReachable(from_format_name, to_format_name);
93 }
94
95
96 Converter::Converter(string const & from_file,   string const & to_file_base,
97                      string const & from_format, string const & to_format)
98         : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
99 {}
100
101
102 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
103 Converter::~Converter()
104 {}
105
106
107 void Converter::startConversion() const
108 {
109         pimpl_->startConversion();
110 }
111
112
113 boost::signals::connection Converter::connect(slot_type const & slot) const
114 {
115         return pimpl_->finishedConversion.connect(slot);
116 }
117
118
119 string const & Converter::convertedFile() const
120 {
121         static string const empty;
122         return pimpl_->finished_ ? pimpl_->to_file_ : empty;
123 }
124
125 } // namespace graphics
126 } // namespace lyx
127
128
129 //------------------------------
130 // Implementation details follow
131 //------------------------------
132
133 namespace {
134
135 /** Build the conversion script, returning true if able to build it.
136  *  The script is output to the ostringstream 'script'.
137  */
138 bool build_script(string const & from_file, string const & to_file_base,
139                   string const & from_format, string const & to_format,
140                   ostream & script);
141
142 } // namespace anon
143
144
145 namespace lyx {
146 namespace graphics {
147
148 Converter::Impl::Impl(string const & from_file,   string const & to_file_base,
149                       string const & from_format, string const & to_format)
150         : valid_process_(false), finished_(false)
151 {
152         lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
153                 << "\tfrom_file:      " << from_file
154                 << "\n\tto_file_base: " << to_file_base
155                 << "\n\tfrom_format:  " << from_format
156                 << "\n\tto_format:    " << to_format << endl;
157
158         // The converted image is to be stored in this file (we do not
159         // use ChangeExtension because this is a basename which may
160         // nevertheless contain a '.')
161         to_file_ = to_file_base + '.' +  formats.extension(to_format);
162
163         // The conversion commands are stored in a stringstream
164         ostringstream script;
165         bool const success = build_script(from_file, to_file_base,
166                                           from_format, to_format, script);
167
168         if (!success) {
169                 script_command_ =
170                         support::os::python() + ' ' +
171                         quoteName(libFileSearch("scripts", "convertDefault.py")) +
172                         ' ' +
173                         quoteName((from_format.empty() ? "" : from_format + ':') + from_file) +
174                         ' ' +
175                         quoteName(to_format + ':' + to_file_);
176
177                 lyxerr[Debug::GRAPHICS]
178                         << "\tNo converter defined! I use convertDefault.py\n\t"
179                         << script_command_ << endl;
180
181         } else {
182
183                 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
184                         << "\n--------------------------------------\n"
185                         << script.str()
186                         << "\n--------------------------------------\n";
187
188                 // Output the script to file.
189                 static int counter = 0;
190                 script_file_ = onlyPath(to_file_base) + "lyxconvert" +
191                         convert<string>(counter++) + ".py";
192
193                 std::ofstream fs(script_file_.c_str());
194                 if (!fs.good()) {
195                         lyxerr << "Unable to write the conversion script to \""
196                                << script_file_ << '\n'
197                                << "Please check your directory permissions."
198                                << std::endl;
199                         return;
200                 }
201
202                 fs << script.str();
203                 fs.close();
204
205                 // The command needed to run the conversion process
206                 // We create a dummy command for ease of understanding of the
207                 // list of forked processes.
208                 // Note: 'sh ' is absolutely essential, or execvp will fail.
209                 script_command_ = support::os::python() + ' ' +
210                         quoteName(script_file_) + ' ' +
211                         quoteName(onlyFilename(from_file)) + ' ' +
212                         quoteName(to_format);
213         }
214         // All is ready to go
215         valid_process_ = true;
216 }
217
218
219 void Converter::Impl::startConversion()
220 {
221         if (!valid_process_) {
222                 converted(0, 1);
223                 return;
224         }
225
226         Forkedcall::SignalTypePtr
227                 ptr = ForkedCallQueue::get().add(script_command_);
228
229         ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
230
231 }
232
233 void Converter::Impl::converted(pid_t /* pid */, int retval)
234 {
235         if (finished_)
236                 // We're done already!
237                 return;
238
239         finished_ = true;
240         // Clean-up behind ourselves
241         unlink(script_file_);
242
243         if (retval > 0) {
244                 unlink(to_file_);
245                 to_file_.erase();
246                 finishedConversion(false);
247         } else {
248                 finishedConversion(true);
249         }
250 }
251
252 } // namespace graphics
253 } // namespace lyx
254
255 namespace {
256
257 string const move_file(string const & from_file, string const & to_file)
258 {
259         if (from_file == to_file)
260                 return string();
261
262         ostringstream command;
263         command << "fromfile = " << from_file << "\n"
264                 << "tofile = "   << to_file << "\n\n"
265                 << "try:\n"
266                 << "  os.rename(fromfile, tofile)\n"
267                 << "except:\n"
268                 << "  import shutil\n"
269                 << "  try:\n"
270                 << "    shutil.copy(fromfile, tofile)\n"
271                 << "  except:\n"
272                 << "    sys.exit(1)\n"
273                 << "  unlinkNoThrow(fromfile)\n";
274
275         return command.str();
276 }
277
278
279 /*
280 A typical script looks like:
281
282 #!/usr/bin/env python -tt
283 import os, sys
284
285 def unlinkNoThrow(file):
286   ''' remove a file, do not throw if error occurs '''
287   try:
288     os.unlink(file)
289   except:
290     pass
291
292 infile = '/home/username/Figure3a.eps'
293 infile_base = '/home/username/Figure3a'
294 outfile = '/tmp/lyx_tmpdir12992hUwBqt/gconvert0129929eUBPm.pdf'
295
296 if os.system(r'epstopdf ' + '"' + infile + '"' + ' --output ' + '"' + outfile + '"' + '') != 0:
297   unlinkNoThrow(outfile)
298   sys.exit(1)
299
300 if not os.path.isfile(outfile):
301   if os.path.isfile(outfile + '.0'):
302     os.rename(outfile + '.0', outfile)
303     import glob
304     for file in glob.glob(outfile + '.?'):
305       unlinkNoThrow(file)
306   else:
307     sys.exit(1)
308
309 fromfile = outfile
310 tofile = '/tmp/lyx_tmpdir12992hUwBqt/Figure3a129927ByaCl.ppm'
311
312 try:
313   os.rename(fromfile, tofile)
314 except:
315   import shutil
316   try:
317     shutil.copy(fromfile, tofile)
318   except:
319     sys.exit(1)
320   unlinkNoThrow(fromfile)
321
322 */
323 bool build_script(string const & from_file,
324                   string const & to_file_base,
325                   string const & from_format,
326                   string const & to_format,
327                   ostream & script)
328 {
329         BOOST_ASSERT(from_format != to_format);
330         lyxerr[Debug::GRAPHICS] << "build_script ... ";
331         typedef Converters::EdgePath EdgePath;
332
333         if (from_format.empty())
334                 return false;
335
336         script << "#!/usr/bin/env python -tt\n"
337                   "import os, sys\n\n"
338                   "def unlinkNoThrow(file):\n"
339                   "  ''' remove a file, do not throw if an error occurs '''\n"
340                   "  try:\n"
341                   "    os.unlink(file)\n"
342                   "  except:\n"
343                   "    pass\n\n";
344
345         // we do not use ChangeExtension because this is a basename
346         // which may nevertheless contain a '.'
347         string const to_file = to_file_base + '.'
348                 + formats.extension(to_format);
349
350         EdgePath edgepath = converters.getPath(from_format, to_format);
351
352         if (edgepath.empty()) {
353                 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
354                 return false;
355         }
356
357         // Create a temporary base file-name for all intermediate steps.
358         // Remember to remove the temp file because we only want the name...
359         static int counter = 0;
360         string const tmp = "gconvert" + convert<string>(counter++);
361         string const to_base = tempName(string(), tmp);
362         unlink(to_base);
363
364         string outfile = from_file;
365
366         // The conversion commands may contain these tokens that need to be
367         // changed to infile, infile_base, outfile respectively.
368         string const token_from("$$i");
369         string const token_base("$$b");
370         string const token_to("$$o");
371
372         EdgePath::const_iterator it  = edgepath.begin();
373         EdgePath::const_iterator end = edgepath.end();
374
375         for (; it != end; ++it) {
376                 ::Converter const & conv = converters.get(*it);
377
378                 // Build the conversion command
379                 string const infile      = outfile;
380                 string const infile_base = changeExtension(infile, string());
381                 outfile = changeExtension(to_base, conv.To->extension());
382
383                 // Store these names in the shell script
384                 script << "infile = "      << quoteName(infile) << '\n'
385                        << "infile_base = " << quoteName(infile_base) << '\n'
386                        << "outfile = "     << quoteName(outfile) << '\n';
387
388                 string command = conv.command;
389                 command = subst(command, token_from, "' + '\"' + infile + '\"' + '");
390                 command = subst(command, token_base, "' + '\"' + infile_base + '\"' + '");
391                 command = subst(command, token_to,   "' + '\"' + outfile + '\"' + '");
392                 command = libScriptSearch(command);
393
394                 // Store in the shell script
395                 script << "\nif os.system(r'" << command << "') != 0:\n";
396
397                 // Test that this was successful. If not, remove
398                 // ${outfile} and exit the shell script
399                 script << "  unlinkNoThrow(outfile)\n"
400                        << "  sys.exit(1)\n\n";
401
402                 // Test that the outfile exists.
403                 // ImageMagick's convert will often create ${outfile}.0,
404                 // ${outfile}.1.
405                 // If this occurs, move ${outfile}.0 to ${outfile}
406                 // and delete ${outfile}.? (ignore errors)
407                 script << "if not os.path.isfile(outfile):\n"
408                           "  if os.path.isfile(outfile + '.0'):\n"
409                           "    os.rename(outfile + '.0', outfile)\n"
410                           "    import glob\n"
411                           "    for file in glob.glob(outfile + '.?'):\n"
412                           "      unlinkNoThrow(file)\n"
413                           "  else:\n"
414                           "    sys.exit(1)\n\n";
415
416                 // Delete the infile, if it isn't the original, from_file.
417                 if (infile != from_file)
418                         script << "unlinkNoThrow(infile)\n\n";
419         }
420
421         // Move the final outfile to to_file
422         script << move_file("outfile", quoteName(to_file));
423         lyxerr[Debug::GRAPHICS] << "ready!" << endl;
424
425         return true;
426 }
427
428 } // namespace anon