File: Make-sure-external-commands-are-available.patch

package info (click to toggle)
rhinote 0.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: python: 212; sh: 43; makefile: 32
file content (70 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
From: Andrea Bolognani <eof@kiyuko.org>
Date: Wed, 1 Feb 2012 11:33:54 +0100
Subject: Make sure external commands are available

External commands are not guaranteed to be available on the
system, so test for their existence before running them.

Forwarded: yes
---
 rhinote.py | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/rhinote.py b/rhinote.py
index 93e2363..376cb2c 100644
--- a/rhinote.py
+++ b/rhinote.py
@@ -83,12 +83,18 @@ class TextWidget(tkinter.Text):
         Rhinote()
 
     def printfile(self, whatever=None):
+        if not self.printcommand:
+            tkMessageBox.showerror('Print error', 'Print command (lpr) not found')
+            return
+        if not self.formatcommand:
+            tkMessageBox.showerror('Print error', 'Format command (enscript) not found')
+            return
         f = open(self.printfilename, 'w')
         f.write(self.get('1.0', 'end'))
         f.close
         # 'enscript' formats the text; lpr sends it to the default printer;
         # enscript's -B option suppresses page headers.
-        os.system('enscript -B --word-wrap $HOME/.Rhinoteprintfile > lpr &')
+        os.system('%s -B --word-wrap %s > %s &' % (self.formatcommand, self.printfilename, self.printcommand))
 
     def help(self, whatever=None):
         tkMessageBox.showinfo('Rhinote Help', message='''
@@ -114,6 +120,23 @@ Free Software distributed under the GNU General Public License
 http://rhinote.tuxfamily.org
 ''')
 
+    def which(self, cmd):
+        # Abort immediately if PATH is not set
+        path = os.getenv('PATH')
+        if not path:
+            return None
+        # Look in all directories listed in PATH
+        dirs = path.split(os.pathsep)
+        path = None
+        for d in dirs:
+            f = os.path.join(d, cmd)
+            # f must be an executable file
+            if os.path.isfile(f) and os.access(f, os.X_OK):
+                # Stop after the first success
+                path = f
+                break
+        return path
+
     def __init__(self, master, **kw):
         tkinter.Text.__init__(self, master, **kw)
         self.bind('<Control-n>', self.new_window)
@@ -136,6 +159,9 @@ http://rhinote.tuxfamily.org
             ('Rhinote files', '*.rhi'),
             ('All files', '*'),
         ]
+        # Find print and format commands
+        self.printcommand = self.which('lpr')
+        self.formatcommand = self.which('enscript')
 
 
 # make it so: