Edgewall Software

Ticket #7748: enscript.not_installed.patch.txt

File enscript.not_installed.patch.txt, 2.7 KB (added by techtonik <techtonik@…>, 4 years ago)

check if enscript command is available

Line 
1Index: trac/mimeview/enscript.py
2===================================================================
3--- trac/mimeview/enscript.py   (revision 7601)
4+++ trac/mimeview/enscript.py   (working copy)
5@@ -20,7 +20,7 @@
6 from trac.config import Option, ListOption
7 from trac.core import *
8 from trac.mimeview.api import IHTMLPreviewRenderer, Mimeview
9-from trac.util import NaivePopen
10+from trac.util import find_executable, NaivePopen
11 from trac.util.html import escape, Deuglifier
12 
13 __all__ = ['EnscriptRenderer']
14@@ -126,9 +126,10 @@
15         # Extend default MIME type to mode mappings with configured ones
16         if not self._types:
17             self._types = {}
18-            self._types.update(types)
19-            self._types.update(
20-                Mimeview(self.env).configured_modes_mapping('enscript'))
21+            if find_executable(self.path) != None:
22+                self._types.update(types)
23+                self._types.update(
24+                    Mimeview(self.env).configured_modes_mapping('enscript'))
25         return self._types.get(mimetype, (None, 0))[1]
26 
27     def render(self, context, mimetype, content, filename=None, rev=None):
28Index: trac/util/__init__.py
29===================================================================
30--- trac/util/__init__.py       (revision 7601)
31+++ trac/util/__init__.py       (working copy)
32@@ -84,6 +84,38 @@
33                 raise Exception('Failed to create unique name: ' + path)
34             path = '%s.%d%s' % (parts[0], idx, parts[1])
35 
36+def find_executable(executable, path=None):
37+    """Try to find 'executable' in the directories listed in 'path' (a
38+    string listing directories separated by 'os.pathsep'; defaults to
39+    os.environ['PATH']).  Returns the complete filename or None if not
40+    found
41+    """
42+    if path is None:
43+        path = os.environ['PATH']
44+    paths = path.split(os.pathsep)
45+    extlist = ['']
46+    if os.name == 'os2':
47+        (base, ext) = os.path.splitext(executable)
48+        # executable files on OS/2 can have an arbitrary extension, but
49+        # .exe is automatically appended if no dot is present in the name
50+        if not ext:
51+            executable = executable + ".exe"
52+    elif sys.platform == 'win32':
53+        pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
54+        (base, ext) = os.path.splitext(executable)
55+        if ext.lower() not in pathext:
56+            extlist = pathext
57+    for ext in extlist:
58+        execname = executable + ext
59+        if os.path.isfile(execname):
60+            return execname
61+        else:
62+            for p in paths:
63+                f = os.path.join(p, execname)
64+                if os.path.isfile(f):
65+                    return f
66+    else:
67+        return None
68 
69 class NaivePopen:
70     """This is a deadlock-safe version of popen that returns an object with