File: Call-external-commands-via-Popen.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 (75 lines) | stat: -rw-r--r-- 2,927 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
71
72
73
74
75
From: Andrea Bolognani <eof@kiyuko.org>
Date: Wed, 1 Feb 2012 12:22:31 +0100
Subject: Call external commands via Popen

Use the subprocess.Popen() method instead on relying on the less
secure os.system() method when calling external commands.

This also allows us to stop using an insecure temporary file and
notify the user if printing failed.

Forwarded: yes
---
 rhinote.py | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/rhinote.py b/rhinote.py
index 376cb2c..7e48c52 100644
--- a/rhinote.py
+++ b/rhinote.py
@@ -25,6 +25,7 @@ import tkinter
 import tkinter.filedialog as tkFileDialog
 import tkinter.messagebox as tkMessageBox
 import os
+import subprocess
 
 
 # the root window:
@@ -89,12 +90,23 @@ class TextWidget(tkinter.Text):
         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('%s -B --word-wrap %s > %s &' % (self.formatcommand, self.printfilename, self.printcommand))
+        # Prepare the format command
+        formatargv = [self.formatcommand]
+        formatargv.extend(self.formatargs)
+        # Prepare the print command
+        printargv = [self.printcommand]
+        printargv.extend(self.printargs)
+        # Spawn both commands, piping the output of the format command
+        # to the input of the print command
+        pp = subprocess.Popen(printargv, stdin=subprocess.PIPE)
+        fp = subprocess.Popen(formatargv, stdin=subprocess.PIPE, stdout=pp.stdin)
+        # Feed the contents of the text area to the format command and wait
+        # for both commands to terminate
+        fp.communicate(input=self.get('1.0', 'end').encode())
+        pp.communicate()
+        # Notify the user of the outcome of the printing operation
+        if fp.returncode > 0 or pp.returncode > 0:
+            tkMessageBox.showerror('Print error', 'Printing failed')
 
     def help(self, whatever=None):
         tkMessageBox.showinfo('Rhinote Help', message='''
@@ -153,7 +165,6 @@ http://rhinote.tuxfamily.org
         self.bind('<Control-H>', self.help)
         self.master = master
         self.filename = ''
-        self.printfilename = os.environ['HOME'] + '/.Rhinoteprintfile'
         self._filetypes = [
             ('Text/ASCII', '*.txt'),
             ('Rhinote files', '*.rhi'),
@@ -161,7 +172,9 @@ http://rhinote.tuxfamily.org
         ]
         # Find print and format commands
         self.printcommand = self.which('lpr')
+        self.printargs = []
         self.formatcommand = self.which('enscript')
+        self.formatargs = ['-B', '--word-wrap', '-o', '-']
 
 
 # make it so: