File: glibc_check.py

package info (click to toggle)
borgbackup2 2.0.0b20-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,848 kB
  • sloc: python: 33,830; pascal: 3,599; sh: 215; makefile: 160; tcl: 94; ansic: 21
file content (61 lines) | stat: -rwxr-xr-x 1,616 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""
Check whether all given binaries work with the specified glibc version.

Usage: glibc_check.py 2.11 BIN [BIN ...]

Exit code 0 means "yes"; exit code 1 means "no".
"""

import re
import subprocess
import sys

verbose = True
glibc_re = re.compile(r"GLIBC_([0-9]\.[0-9]+)")


def parse_version(v):
    major, minor = v.split(".")
    return int(major), int(minor)


def format_version(version):
    return "%d.%d" % version


def main():
    given = parse_version(sys.argv[1])
    filenames = sys.argv[2:]

    overall_versions = set()
    for filename in filenames:
        try:
            output = subprocess.check_output(["objdump", "-T", filename], stderr=subprocess.STDOUT)
            output = output.decode()
            versions = {parse_version(match.group(1)) for match in glibc_re.finditer(output)}
            requires_glibc = max(versions)
            overall_versions.add(requires_glibc)
            if verbose:
                print(f"{filename} {format_version(requires_glibc)}")
        except subprocess.CalledProcessError:
            if verbose:
                print("%s failed." % filename)

    wanted = max(overall_versions)
    ok = given >= wanted

    if verbose:
        if ok:
            print("The binaries work with the given glibc %s." % format_version(given))
        else:
            print(
                "The binaries do not work with the given glibc %s. "
                "Minimum required is %s." % (format_version(given), format_version(wanted))
            )
    return ok


if __name__ == "__main__":
    ok = main()
    sys.exit(0 if ok else 1)