~cjwatson/debconf/dbus

« back to all changes in this revision

Viewing changes to debconf.py

  • Committer: cjwatson
  • Date: 2009-03-03 16:27:56 UTC
  • Revision ID: vcs-imports@canonical.com-20090303162756-xlgq6tn3va8w35ci
debconf.py: Use subprocess rather than popen2 if it's available. popen2
is deprecated in Python 2.6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import sys, os
27
27
import errno
28
28
import re
29
 
import popen2
 
29
try:
 
30
    import subprocess
 
31
    using_subprocess = True
 
32
except ImportError:
 
33
    import popen2
 
34
    using_subprocess = False
30
35
import fcntl
31
36
 
32
37
class DebconfError(Exception):
118
123
 
119
124
class DebconfCommunicator(Debconf, object):
120
125
    def __init__(self, owner, title=None, cloexec=False):
121
 
        self.dccomm = popen2.Popen3(['debconf-communicate', '-fnoninteractive',
122
 
                                     owner])
 
126
        args = ['debconf-communicate', '-fnoninteractive', owner]
 
127
        if using_subprocess:
 
128
            self.dccomm = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
 
129
            read = self.dccomm.stdout
 
130
            write = self.dccomm.stdin
 
131
        else:
 
132
            self.dccomm = popen2.Popen3(args)
 
133
            read = self.dccomm.fromchild
 
134
            write = self.dccomm.tochild
123
135
        super(DebconfCommunicator, self).__init__(title=title,
124
 
                                                  read=self.dccomm.fromchild,
125
 
                                                  write=self.dccomm.tochild)
 
136
                                                  read=read,
 
137
                                                  write=write)
126
138
        if cloexec:
127
139
            fcntl.fcntl(self.read.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
128
140
            fcntl.fcntl(self.write.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
129
141
 
130
142
    def shutdown(self):
131
143
        if self.dccomm is not None:
132
 
            self.dccomm.tochild.close()
133
 
            self.dccomm.fromchild.close()
 
144
            if using_subprocess:
 
145
                self.dccomm.stdin.close()
 
146
                self.dccomm.stdout.close()
 
147
            else:
 
148
                self.dccomm.tochild.close()
 
149
                self.dccomm.fromchild.close()
134
150
            self.dccomm.wait()
135
151
            self.dccomm = None
136
152