Code

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