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