diff options
| author | 2018-04-04 04:50:04 +0200 | |
|---|---|---|
| committer | 2018-04-04 04:50:04 +0200 | |
| commit | 1cd7d0f0248e2fe317b2e3da9f597059cf30b4d9 (patch) | |
| tree | ec37a444ed314f7bfd4395cd42ee54d7b70debf0 | |
| parent | 11bda2c49979375ab7cdea5deac04f4bcb2a5dae (diff) | |
Switch to using the subprocess module.
| -rwxr-xr-x | mini-dinstall | 53 | ||||
| -rw-r--r-- | minidinstall/GPGSigVerifier.py | 43 |
2 files changed, 32 insertions, 64 deletions
diff --git a/mini-dinstall b/mini-dinstall index 8d53edc..4ad64a1 100755 --- a/mini-dinstall +++ b/mini-dinstall @@ -21,7 +21,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os, sys, re, glob, getopt, time, traceback, lzma, getpass, socket -import shutil, threading, select, queue, socketserver, datetime +import shutil, threading, select, queue, socketserver, datetime, subprocess import logging, logging.handlers import apt_pkg apt_pkg.init() @@ -977,13 +977,10 @@ class ArchiveDir: if not os.access(script, os.X_OK): self._logger.error("Can't execute script \"%s\"" % script) return True - pid = os.fork() - if pid == 0: - os.execlp(script, script, changefilename) - sys.exit(1) - (pid, status) = os.waitpid(pid, 0) - if status or (not os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0): - self._logger.error('script "%s" exited with error code %d' % (cmd, os.WEXITSTATUS(status))) + try: + subprocess.check_call([script, changefilename]) + except subprocess.CalledProcessError as e: + self._logger.error('script "%s" exited with error code %d' % (cmd, e.returncode)) return True return False @@ -1101,17 +1098,6 @@ class ArchiveDirIndexer(threading.Thread): self._logger.debug("Running: %s" % ' '.join(cmdline)) if no_act: return - (infd, outfd) = os.pipe() - pid = os.fork() - if pid == 0: - os.chdir(self._dir) - os.chdir('..') - os.close(infd) - misc.dup2(outfd, 1) - os.execvp('apt-ftparchive', cmdline) - os.exit(1) - os.close(outfd) - stdout = os.fdopen(infd) if arch: packagesfilename = os.path.join(dir, '%s-%s' % (name, arch)) else: @@ -1119,17 +1105,17 @@ class ArchiveDirIndexer(threading.Thread): newpackagesfilename = packagesfilename + '.new' xzpackagesfilename = packagesfilename + '.xz' newxzpackagesfilename = newpackagesfilename + '.xz' - newpackagesfile = open(newpackagesfilename, 'w') - newxzpackagesfile = lzma.open(newxzpackagesfilename, 'wt') - buf = stdout.read(8192) + newpackagesfile = open(newpackagesfilename, 'wb') + newxzpackagesfile = lzma.open(newxzpackagesfilename, 'w') + process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.join(self._dir, '..')) + buf = process.stdout.read(8192) while buf: newpackagesfile.write(buf) newxzpackagesfile.write(buf) - buf = stdout.read(8192) - stdout.close() - (pid, status) = os.waitpid(pid, 0) - if status or (not os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0): - raise DinstallException("apt-ftparchive exited with status code %d" % status) + buf = process.stdout.read(8192) + process.wait() + if process.returncode != 0: + raise DinstallException("apt-ftparchive exited with status code %d" % process.returncode) newpackagesfile.close() newxzpackagesfile.close() shutil.move(newpackagesfilename, packagesfilename) @@ -1176,15 +1162,10 @@ class ArchiveDirIndexer(threading.Thread): if not os.access(script, os.X_OK): self._logger.error("Can't execute script \"%s\"" % script) return True - pid = os.fork() - if pid == 0: - if dir: - os.chdir(dir) - os.execlp(script, script, changefilename) - sys.exit(1) - (pid, status) = os.waitpid(pid, 0) - if status or (not os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0): - self._logger.error('script "%s" exited with error code %d' % (cmd, os.WEXITSTATUS(status))) + try: + subprocess.check_call([script, changefilename], cwd=dir) + except subprocess.CalledProcessError as e: + self._logger.error('script "%s" exited with error code %d' % (cmd, e.returncode)) return True return False diff --git a/minidinstall/GPGSigVerifier.py b/minidinstall/GPGSigVerifier.py index 0d47379..8c17bfe 100644 --- a/minidinstall/GPGSigVerifier.py +++ b/minidinstall/GPGSigVerifier.py @@ -18,8 +18,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import os -from . import misc +import os, subprocess class GPGSigVerifierException(Exception): def __init__(self, value): @@ -47,31 +46,19 @@ class GPGSigVerifier: self._gpgv = gpgv def verify(self, filename, sigfilename=None): - (stdin, stdout) = os.pipe() - pid = os.fork() - if pid == 0: - os.close(stdin) - misc.dup2(stdout, 1) - misc.dup2(stdout, 2) - args = [self._gpgv] - for keyring in self._keyrings: - args.extend(['--keyring', keyring]) - if sigfilename: - args.append(sigfilename) - args.append(filename) - os.execv(self._gpgv, args) - os.exit(1) - os.close(stdout) - output = os.fdopen(stdin).readlines() - (pid, status) = os.waitpid(pid, 0) - if status or (not os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0): - if os.WIFEXITED(status): - msg = "gpgv exited with error code %d" % os.WEXITSTATUS(status) - elif os.WIFSTOPPED(status): - msg = "gpgv stopped unexpectedly with signal %d" % os.WSTOPSIG(status) - elif os.WIFSIGNALED(status): - msg = "gpgv died with signal %d" % os.WTERMSIG(status) - raise GPGSigVerificationFailure(msg, output) - return output + cmdline = [self._gpgv] + for keyring in self._keyrings: + cmdline.extend(['--keyring', keyring]) + if sigfilename: + cmdline.append(sigfilename) + cmdline.append(filename) + try: + return subprocess.check_output(cmdline).decode('utf-8') + except subprocess.CalledProcessError as e: + if e.returncode > 0: + msg = "gpgv exited with error code %d" % e.returncode + else: + msg = "gpgv died with signal %d" % -e.returncode + raise GPGSigVerificationFailure(msg, e.output) # vim:ts=4:sw=4:et: |
