|
2 | 2 | Compile the externals
|
3 | 3 | """
|
4 | 4 | import platform
|
5 |
| -import sys |
6 |
| -import os |
7 |
| -import re |
8 |
| - |
9 |
| -# We need to patch python platform module. It does a string comparison for the libc versions. |
10 |
| -# it fails when going from 2.9 to 2.10, |
11 |
| -# the fix converts the version to a tuple and attempts a numeric comparison |
12 |
| - |
13 |
| -_libc_search = re.compile(b"(__libc_init)" b"|" b"(GLIBC_([0-9.]+))" b"|" b"(libc(_\\w+)?\\.so(?:\\.(\\d[0-9.]*))?)") |
14 |
| - |
15 |
| - |
16 |
| -def libc_ver(executable=sys.executable, lib="", version="", chunksize=2048): |
17 |
| - """Tries to determine the libc version that the file executable |
18 |
| - (which defaults to the Python interpreter) is linked against. |
19 |
| -
|
20 |
| - Returns a tuple of strings (lib,version) which default to the |
21 |
| - given parameters in case the lookup fails. |
22 |
| -
|
23 |
| - Note that the function has intimate knowledge of how different |
24 |
| - libc versions add symbols to the executable and thus is probably |
25 |
| - only useable for executables compiled using gcc. |
26 |
| -
|
27 |
| - The file is read and scanned in chunks of chunksize bytes. |
28 |
| -
|
29 |
| - """ |
30 |
| - with open(executable, "rb") as f: |
31 |
| - binary = f.read(chunksize) |
32 |
| - pos = 0 |
33 |
| - version = [0, 0, 0] |
34 |
| - while True: |
35 |
| - m = _libc_search.search(binary, pos) |
36 |
| - if not m: |
37 |
| - binary = f.read(chunksize) |
38 |
| - if not binary: |
39 |
| - break |
40 |
| - pos = 0 |
41 |
| - continue |
42 |
| - libcinit, glibc, glibcversion, so, threads, soversion = m.groups() |
43 |
| - if libcinit and not lib: |
44 |
| - lib = b"libc" |
45 |
| - elif glibc: |
46 |
| - glibcversion_parts = glibcversion.split(b".") |
47 |
| - for i in range(len(glibcversion_parts)): |
48 |
| - try: |
49 |
| - glibcversion_parts[i] = int(glibcversion_parts[i]) |
50 |
| - except ValueError: |
51 |
| - glibcversion_parts[i] = 0 |
52 |
| - if libcinit and not lib: |
53 |
| - lib = b"libc" |
54 |
| - elif glibc: |
55 |
| - if lib != b"glibc": |
56 |
| - lib = b"glibc" |
57 |
| - version = glibcversion_parts |
58 |
| - elif glibcversion_parts > version: |
59 |
| - version = glibcversion_parts |
60 |
| - elif so: |
61 |
| - if lib != b"glibc": |
62 |
| - lib = b"libc" |
63 |
| - version = max(version, [int(soversion)]).pop() |
64 |
| - if threads and version[-len(threads) :] != threads: |
65 |
| - version = version + threads |
66 |
| - pos = m.end() |
67 |
| - return lib.decode(), ".".join(map(str, version)) |
68 |
| - |
69 |
| - |
70 |
| -# ## Command line interface |
71 | 5 |
|
72 | 6 |
|
73 | 7 | def getPlatformString():
|
74 | 8 | # Modified to return our desired platform string, R. Graciani
|
75 | 9 | platformTuple = (platform.system(), platform.machine())
|
76 | 10 | if platformTuple[0] == "Linux":
|
77 |
| - try: |
78 |
| - import subprocess |
79 |
| - |
80 |
| - sp = subprocess.Popen(["/sbin/ldconfig", "--print-cache"], stdout=subprocess.PIPE, universal_newlines=True) |
81 |
| - spStdout = sp.stdout |
82 |
| - except Exception: |
83 |
| - sp = None |
84 |
| - spStdout = os.popen("/sbin/ldconfig --print-cache", "r") |
85 |
| - ldre = re.compile(r".*=> (.*/libc\.so\..*$)") |
86 |
| - libs = [] |
87 |
| - for line in spStdout.readlines(): |
88 |
| - reM = ldre.match(line) |
89 |
| - if reM: |
90 |
| - libs.append(reM.groups()[0]) |
91 |
| - if sp: |
92 |
| - if "terminate" in dir(sp): |
93 |
| - sp.terminate() |
94 |
| - sp.wait() |
95 |
| - |
96 |
| - if not libs: |
97 |
| - # get version of higher libc installed |
98 |
| - if platform.machine().find("64") != -1: |
99 |
| - lib = "/lib64" |
100 |
| - else: |
101 |
| - lib = "/lib" |
102 |
| - for libFile in os.listdir(lib): |
103 |
| - if libFile.find("libc-") == 0 or libFile.find("libc.so") == 0: |
104 |
| - libs.append(os.path.join(lib, libFile)) |
105 |
| - newest_lib = [0, 0, 0] |
106 |
| - for lib in libs: |
107 |
| - lib_parts = libc_ver(lib)[1].split(".") |
108 |
| - for i in range(len(lib_parts)): |
109 |
| - try: |
110 |
| - lib_parts[i] = int(lib_parts[i]) |
111 |
| - except ValueError: |
112 |
| - lib_parts[i] = 0 |
113 |
| - # print "non integer version numbers" |
114 |
| - if lib_parts > newest_lib: |
115 |
| - newest_lib = lib_parts |
116 |
| - |
117 |
| - platformTuple += ("glibc-" + ".".join(map(str, newest_lib)),) |
| 11 | + platformTuple += ("-".join(platform.libc_ver()),) |
118 | 12 | elif platformTuple[0] == "Darwin":
|
119 | 13 | platformTuple += (".".join(platform.mac_ver()[0].split(".")[:2]),)
|
120 | 14 | elif platformTuple[0] == "Windows":
|
|
0 commit comments