File: check_wheel_bin_arch.py

package info (click to toggle)
capstone 5.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-proposed-updates
  • size: 58,188 kB
  • sloc: ansic: 96,086; cpp: 67,489; cs: 29,510; python: 25,829; pascal: 24,412; java: 15,582; ml: 14,473; makefile: 1,275; sh: 479; ruby: 386
file content (80 lines) | stat: -rwxr-xr-x 2,319 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# Copyright © 2024 Rot127 <unisono@quyllur.org>
# SPDX-License-Identifier: BSD-3

import logging as log
import subprocess as sp
import re
import os
import sys
from pathlib import Path

if len(sys.argv) != 2:
    print(f"{sys.argv[0]} <wheel_dir>")
    exit(-1)

log.basicConfig(
    level=log.INFO,
    stream=sys.stdout,
    format="%(levelname)-5s - %(message)s",
    force=True,
)

archs = {
    "universal2": ["x86_64", "arm64"],
    "x86_64": [r"x86[_-]64"],
    "arm64": ["arm64"],
    "aarch64": ["ARM aarch64"],
    "i686": ["Intel 80386"],
    "win32": [r"INVALID"],
    "amd64": [r"x86[_-]64"],
}

filename = {
    "macosx": "libcapstone.dylib",
    "manylinux": "libcapstone.so",
    "musllinux": "libcapstone.so",
    "win": "capstone.dll",
}

success = True
wheel_seen = False
for root, dir, files in os.walk(sys.argv[1]):
    for file in files:
        f = Path(root).joinpath(file)
        if f.suffix != ".whl":
            continue
        wheel_seen = True
        target = re.search(r"py3-none-(.+).whl", f"{f}").group(1)
        platform = re.search("^(win|manylinux|musllinux|macosx)", target).group(1)

        arch = re.search(
            "(universal2|x86_64|arm64|aarch64|i686|win32|amd64)$", target
        ).group(1)
        log.info(f"Target: {target} - Platform: {platform} - Arch: {archs[arch]}")

        out_dir = f"{platform}__{arch}"
        sp.run(["unzip", "-q", f"{f}", "-d", out_dir], check=True)
        lib_path = Path(out_dir).joinpath(f"capstone/lib/{filename[platform]}")
        result = sp.run(["file", "-b", f"{lib_path}"], capture_output=True, check=True)
        stdout = result.stdout.decode("utf8").strip()
        if any([not re.search(a, stdout) for a in archs[arch]]):
            success = False
            log.error(f"The wheel '{file}' is not compiled for '{archs[arch]}'.")
            log.error(f"Binary is: {stdout}")
            print()
        else:
            log.info(f"OK: Arch: {arch} - {lib_path}")
            log.info(f"Binary is: {stdout}")
            log.info(f"Delete {out_dir}")
            print()
        os.system(f"rm -r {out_dir}")
    break

if not wheel_seen:
    log.error("No wheel was checked.")
    exit(-1)

if not success:
    log.error("Binary files are compiled for the wrong architecture.")
    exit(-1)