summaryrefslogtreecommitdiffstats
path: root/Lib/distutils/util.py
diff options
context:
space:
mode:
authorTarek Ziadé <ziade.tarek@gmail.com>2009-07-16 16:18:19 (GMT)
committerTarek Ziadé <ziade.tarek@gmail.com>2009-07-16 16:18:19 (GMT)
commitf8926b2efeb1cd719736c42a14e2a92558c81aed (patch)
tree393cdcff8d8b588f4549430923e90dade43d9495 /Lib/distutils/util.py
parent0027d4b4feda9c4e8e2cfa30dcb19cc126706c84 (diff)
downloadcpython-f8926b2efeb1cd719736c42a14e2a92558c81aed.zip
cpython-f8926b2efeb1cd719736c42a14e2a92558c81aed.tar.gz
cpython-f8926b2efeb1cd719736c42a14e2a92558c81aed.tar.bz2
Merged revisions 74024 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r74024 | tarek.ziade | 2009-07-16 17:35:45 +0200 (Thu, 16 Jul 2009) | 1 line #6466 refactored distutils duplicate get_versions() functions (used to get gcc/ld/dllwrap versions) ........
Diffstat (limited to 'Lib/distutils/util.py')
-rw-r--r--Lib/distutils/util.py54
1 files changed, 53 insertions, 1 deletions
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index ad7ae08..0c88b81 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -7,10 +7,12 @@ one of the other *util.py modules.
__revision__ = "$Id$"
import sys, os, string, re
+
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
-from distutils.spawn import spawn
+from distutils.spawn import spawn, find_executable
from distutils import log
+from distutils.version import LooseVersion
def get_platform():
"""Return a string that identifies the current platform.
@@ -539,6 +541,56 @@ def rfc822_escape(header):
sep = '\n' + 8*' '
return sep.join(lines)
+_RE_VERSION = re.compile(b'(\d+\.\d+(\.\d+)*)')
+_MAC_OS_X_LD_VERSION = re.compile(b'^@\(#\)PROGRAM:ld PROJECT:ld64-((\d+)(\.\d+)*)')
+
+def _find_ld_version():
+ """Finds the ld version. The version scheme differs under Mac OSX."""
+ if sys.platform == 'darwin':
+ return _find_exe_version('ld -v', _MAC_OS_X_LD_VERSION)
+ else:
+ return _find_exe_version('ld -v')
+
+def _find_exe_version(cmd, pattern=_RE_VERSION):
+ """Find the version of an executable by running `cmd` in the shell.
+
+ `pattern` is a compiled regular expression. If not provided, default
+ to _RE_VERSION. If the command is not found, or the output does not
+ match the mattern, returns None.
+ """
+ from subprocess import Popen, PIPE
+ executable = cmd.split()[0]
+ if find_executable(executable) is None:
+ return None
+ pipe = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
+ try:
+ stdout, stderr = pipe.stdout.read(), pipe.stderr.read()
+ finally:
+ pipe.stdout.close()
+ pipe.stderr.close()
+ # some commands like ld under MacOS X, will give the
+ # output in the stderr, rather than stdout.
+ if stdout != b'':
+ out_string = stdout
+ else:
+ out_string = stderr
+
+ result = pattern.search(out_string)
+ if result is None:
+ return None
+ return LooseVersion(result.group(1).decode())
+
+def get_compiler_versions():
+ """Returns a tuple providing the versions of gcc, ld and dllwrap
+
+ For each command, if a command is not found, None is returned.
+ Otherwise a LooseVersion instance is returned.
+ """
+ gcc = _find_exe_version('gcc -dumpversion')
+ ld = _find_ld_version()
+ dllwrap = _find_exe_version('dllwrap --version')
+ return gcc, ld, dllwrap
+
# 2to3 support
def run_2to3(files, fixer_names=None, options=None, explicit=None):