swc-installation-test-2.py: Convert .version_option to .version_options
authorW. Trevor King <wking@tremily.us>
Tue, 1 Jan 2013 14:13:18 +0000 (09:13 -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 a single
option doesn't cut it.  For version of Python before 3.3,
shlex.quote() doesn't exist [1], so use pipes.quote() instead [2].

[1]: http://docs.python.org/3/library/shlex.html#shlex.quote
[2]: http://bugs.python.org/issue9723

swc-installation-test-2.py

index 373230d64283d4f6bf18a63f3c5dafa7e6a35a8d..df173e71ef27e18ae647d6ea9f4d8bf29ac384db 100755 (executable)
@@ -32,10 +32,17 @@ import logging as _logging
 import os as _os
 import platform as _platform
 import re as _re
+import shlex as _shlex
 import subprocess as _subprocess
 import sys as _sys
 
 
+if not hasattr(_shlex, 'quote'):  # Python versions older than 3.3
+    # Use the undocumented pipes.quote()
+    import pipes as _pipes
+    _shlex.quote = _pipes.quote
+
+
 __version__ = '0.1'
 
 # Comment out any entries you don't need
@@ -241,13 +248,13 @@ CHECKER['python'] = PythonDependency()
 class CommandDependency (Dependency):
     exe_extension = _distutils_ccompiler.new_compiler().exe_extension
 
-    def __init__(self, command, version_option='--version',
+    def __init__(self, command, version_options=('--version',),
                  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_option = version_option
+        self.version_options = version_options
         if not version_regexp:
             regexp = r'([\d][\d{0}]*[\d])'.format(self.version_delimiter)
             version_regexp = _re.compile(regexp)
@@ -258,7 +265,7 @@ class CommandDependency (Dependency):
         command = self.command + (self.exe_extension or '')
         try:
             p = _subprocess.Popen(
-                [command, self.version_option],
+                [command] + list(self.version_options),
                 stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
                 close_fds=True, shell=False, universal_newlines=True)
         except OSError as e:
@@ -270,8 +277,10 @@ class CommandDependency (Dependency):
         status = p.wait()
         if status not in expect:
             lines = [
-                "failed to execute '{0} {1}':".format(
-                    command, self.version_option),
+                "failed to execute: {0} {1}".format(
+                    command,
+                    ' '.join(_shlex.quote(arg)
+                             for arg in self.version_options)),
                 'status: {0}'.format(status),
                 ]
             for name,string in [('stdout', stdout), ('stderr', stderr)]: