Code

Extensions. XAML export improvements.
[inkscape.git] / share / extensions / run_command.py
1 #!/usr/bin/env python
2 import os
3 import sys
4 import tempfile
6 """
7 run_command.py
8 Module for running SVG-generating commands in Inkscape extensions
10 Copyright (C) 2008 Stephen Silver
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
25 """
27 # Run a command that generates an SVG (or PDF) file from an input file.
28 # On success, outputs the contents of the resulting file to stdout, and exits
29 # with a return code of 0.
30 # On failure, outputs an error message to stderr, and exits with a return
31 # code of 1.
32 def run(command_format, prog_name):
33     svgfile = tempfile.mktemp(".svg")
34     command = command_format % svgfile
35     msg = None
36     # ps2pdf may attempt to write to the current directory, which may not
37     # be writeable, so we switch to the temp directory first.
38     try:
39         os.chdir(tempfile.gettempdir())
40     except Exception:
41         pass
42     # In order to get a return code from the process, we use subprocess.Popen
43     # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
44     # (Unix only).  As the Inkscape package for Windows includes Python 2.5,
45     # this should cover all supported platforms.
46     try:
47         try:
48             from subprocess import Popen, PIPE
49             p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
50             rc = p.wait()
51             out = p.stdout.read()
52             err = p.stderr.read()
53         except ImportError:
54             try:
55                 from popen2 import Popen3
56                 p = Popen3(command, True)
57                 p.wait()
58                 rc = p.poll()
59                 out = p.fromchild.read()
60                 err = p.childerr.read()
61             except ImportError:
62                 # shouldn't happen...
63                 msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
64         if rc and msg is None:
65             msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
66     except Exception, inst:
67         msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
69     # If successful, copy the output file to stdout.
70     if msg is None:
71         if os.name == 'nt':  # make stdout work in binary on Windows
72             import msvcrt
73             msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
74         try:
75             f = open(svgfile, "rb")
76             data = f.read()
77             sys.stdout.write(data)
78             f.close()
79         except IOError, inst:
80             msg = "Error reading temporary file: %s" % str(inst)
82     # Clean up.
83     try:
84         os.remove(svgfile)
85     except Exception:
86         pass
88     # Ouput error message (if any) and exit.
89     if msg is not None:
90         sys.stderr.write(msg + "\n")
91         sys.exit(1)
92     else:
93         sys.exit(0)
96 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99