swc-installation-test-2.py: Add CommandDependency.stdin
authorW. Trevor King <wking@tremily.us>
Tue, 1 Jan 2013 14:31:56 +0000 (09:31 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 1 Jan 2013 14:49:49 +0000 (09:49 -0500)
I'm about to implement a version check for 'make' where I want to pass
a test Makefile in via stdin.

swc-installation-test-2.py

index df173e71ef27e18ae647d6ea9f4d8bf29ac384db..a2d7f0163b66c0ed72341c77e2a1b448408f54bd 100755 (executable)
@@ -248,24 +248,31 @@ CHECKER['python'] = PythonDependency()
 class CommandDependency (Dependency):
     exe_extension = _distutils_ccompiler.new_compiler().exe_extension
 
-    def __init__(self, command, version_options=('--version',),
+    def __init__(self, command, version_options=('--version',), stdin=None,
                  version_regexp=None, version_stream='stdout', **kwargs):
         if 'name' not in kwargs:
             kwargs['name'] = command
         super(CommandDependency, self).__init__(**kwargs)
         self.command = command
         self.version_options = version_options
+        self.stdin = None
         if not version_regexp:
             regexp = r'([\d][\d{0}]*[\d])'.format(self.version_delimiter)
             version_regexp = _re.compile(regexp)
         self.version_regexp = version_regexp
         self.version_stream = version_stream
 
-    def _get_version_stream(self, expect=(0,)):
+    def _get_version_stream(self, stdin=None, expect=(0,)):
+        if not stdin:
+            stdin = self.stdin
+        if stdin:
+            popen_stdin = _subprocess.PIPE
+        else:
+            popen_stdin = None
         command = self.command + (self.exe_extension or '')
         try:
             p = _subprocess.Popen(
-                [command] + list(self.version_options),
+                [command] + list(self.version_options), stdin=popen_stdin,
                 stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
                 close_fds=True, shell=False, universal_newlines=True)
         except OSError as e:
@@ -273,7 +280,7 @@ class CommandDependency (Dependency):
                 checker=self,
                 message="could not find '{0}' executable".format(command),
                 )# from e
-        stdout,stderr = p.communicate()
+        stdout,stderr = p.communicate(stdin)
         status = p.wait()
         if status not in expect:
             lines = [