Code

Merge from fe-moved
[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 file from an input file.
27 # On success, outputs the contents of the SVG 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     # In order to get a return code from the process, we use subprocess.Popen
36     # if it's available (Python 2.4 onwards) and otherwise use popen2.Popen3
37     # (Unix only).  As the Inkscape package for Windows includes Python 2.5,
38     # this should cover all supported platforms.
39     try:
40         try:
41             from subprocess import Popen, PIPE
42             p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
43             rc = p.wait()
44             out = p.stdout.read()
45             err = p.stderr.read()
46         except ImportError:
47             try:
48                 from popen2 import Popen3
49                 p = Popen3(command, True)
50                 p.wait()
51                 rc = p.poll()
52                 out = p.fromchild.read()
53                 err = p.childerr.read()
54             except ImportError:
55                 # shouldn't happen...
56                 msg = "Neither subprocess.Popen nor popen2.Popen3 is available"
57         if rc and msg is None:
58             msg = "%s failed:\n%s\n%s\n" % (prog_name, out, err)
59     except Exception, inst:
60         msg = "Error attempting to run %s: %s" % (prog_name, str(inst))
62     # If successful, copy the SVG file to stdout.
63     if msg is None:
64         if os.name == 'nt':  # make stdout work in binary on Windows
65             import msvcrt
66             msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
67         try:
68             f = open(svgfile, "rb")
69             data = f.read()
70             sys.stdout.write(data)
71             f.close()
72         except IOError, inst:
73             msg = "Error reading temporary SVG file: %s" % str(inst)
75     # Clean up.
76     try:
77         os.remove(svgfile)
78     except Exception:
79         pass
81     # Ouput error message (if any) and exit.
82     if msg is not None:
83         sys.stderr.write(msg + "\n")
84         sys.exit(1)
85     else:
86         sys.exit(0)
89 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99