[diffoscope] 01/08: comparators: utils: command: replaces subprocess.Popen by .run

Juliana Oliveira jwnx-guest at moszumanska.debian.org
Wed Jan 31 16:51:31 CET 2018


This is an automated email from the git hooks/post-receive script.

jwnx-guest pushed a commit to branch jwnx_subprocess_merge
in repository diffoscope.

commit fa7f0fd6972df4f88cc08fc6fdbd6d3f87ba89c5
Author: Juliana Oliveira <juliana.orod at gmail.com>
Date:   Sun Jan 14 17:11:07 2018 -0200

    comparators: utils: command: replaces subprocess.Popen by .run
    
    Popen doesnt support multiprocessing. Also, run is being recommended
    over Popen since python3.5.
    
    Signed-off-by: Juliana Oliveira <juliana.orod at gmail.com>
---
 diffoscope/comparators/utils/command.py | 56 ++++++++++++++-------------------
 diffoscope/feeders.py                   |  6 ++--
 2 files changed, 25 insertions(+), 37 deletions(-)

diff --git a/diffoscope/comparators/utils/command.py b/diffoscope/comparators/utils/command.py
index 1d4cfde..c276bfe 100644
--- a/diffoscope/comparators/utils/command.py
+++ b/diffoscope/comparators/utils/command.py
@@ -33,6 +33,7 @@ class Command(object, metaclass=abc.ABCMeta):
 
     def start(self):
         logger.debug("Executing %s", ' '.join([shlex.quote(x) for x in self.cmdline()]))
+
         self._stdin = self.stdin()
         # "stdin" used to be a feeder but we didn't need the functionality so
         # it was simplified into the current form. it can be recovered from git
@@ -40,17 +41,13 @@ class Command(object, metaclass=abc.ABCMeta):
         # consider using a shell pipeline ("sh -ec $script") to implement what
         # you need, because that involves much less code - like it or not (I
         # don't) shell is still the most readable option for composing processes
-        self._process = subprocess.Popen(self.cmdline(),
-                                         shell=False, close_fds=True,
-                                         env=self.env(),
-                                         stdin=self._stdin,
-                                         stdout=subprocess.PIPE,
-                                         stderr=subprocess.PIPE)
-        self._stderr = io.BytesIO()
-        self._stderr_line_count = 0
-        self._stderr_reader = threading.Thread(target=self._read_stderr)
-        self._stderr_reader.daemon = True
-        self._stderr_reader.start()
+        self._process = subprocess.run(self.cmdline(),
+                                       shell=False, close_fds=True,
+                                       env=self.env(),
+                                       stdin=self._stdin,
+                                       stdout=subprocess.PIPE,
+                                       stderr=subprocess.PIPE)
+
 
     @property
     def path(self):
@@ -74,42 +71,35 @@ class Command(object, metaclass=abc.ABCMeta):
         return line
 
     def poll(self):
-        return self._process.poll()
+        pass
 
     def terminate(self):
-        return self._process.terminate()
+        pass
 
     def wait(self):
-        self._stderr_reader.join()
-        returncode = self._process.wait()
-        logger.debug(
-            "%s returned (exit code: %d)",
-            ' '.join([shlex.quote(x) for x in self.cmdline()]),
-            returncode,
-        )
-        if self._stdin:
-            self._stdin.close()
-        return returncode
+        pass
 
     MAX_STDERR_LINES = 50
 
+    # TODO: Implement MAX_STDERR_LINES somewhere
     def _read_stderr(self):
-        for line in iter(self._process.stderr.readline, b''):
-            self._stderr_line_count += 1
-            if self._stderr_line_count <= Command.MAX_STDERR_LINES:
-                self._stderr.write(line)
-        if self._stderr_line_count > Command.MAX_STDERR_LINES:
-            self._stderr.write('[ {} lines ignored ]\n'.format(self._stderr_line_count - Command.MAX_STDERR_LINES).encode('utf-8'))
-        self._process.stderr.close()
+        pass
+
+    def _stderr_reader(self):
+        pass
 
     @property
     def stderr_content(self):
-        return self._stderr.getvalue().decode('utf-8', errors='replace')
+        return self._process.stderr.decode('utf-8', errors='replace')
 
     @property
     def stderr(self):
-        return self._stderr
+        return self._process.stderr
+
+    @property
+    def returncode(self):
+        return self._process.returncode
 
     @property
     def stdout(self):
-        return self._process.stdout
+        return [self._process.stdout]
diff --git a/diffoscope/feeders.py b/diffoscope/feeders.py
index 019ece5..8522584 100644
--- a/diffoscope/feeders.py
+++ b/diffoscope/feeders.py
@@ -86,14 +86,12 @@ def from_command(command):
                 command.filter,
             )
             end_nl = feeder(out_file)
-            if command.poll() is None:
-                command.terminate()
-            returncode = command.wait()
+            returncode = command.returncode
         if returncode not in (0, -signal.SIGTERM):
             raise subprocess.CalledProcessError(
                 returncode,
                 command.cmdline(),
-                output=command.stderr.getvalue(),
+                output=command.stderr,
             )
         return end_nl
     return feeder

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list