๐Ÿ“ฆ astral-sh / python-build-standalone

๐Ÿ“„ test-distribution.py ยท 53 lines
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#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Script to run Python tests from a distribution archive."""

import json
import pathlib
import subprocess
import sys
import tarfile
import tempfile

import zstandard


def main(args):
    if not args:
        print("Usage: test-distribution.py path/to/distribution.tar.zst")
        return 1

    distribution_path = args[0]

    with tempfile.TemporaryDirectory() as td:
        td = pathlib.Path(td)

        with open(distribution_path, "rb") as fh:
            dctx = zstandard.ZstdDecompressor()
            with dctx.stream_reader(fh) as reader:
                with tarfile.open(mode="r|", fileobj=reader) as tf:
                    tf.extractall(td)

        root = td / "python"

        python_json = root / "PYTHON.json"

        with python_json.open("rb") as fh:
            info = json.load(fh)

        test_args = [
            str(root / info["python_exe"]),
            str(root / info["run_tests"]),
        ]

        test_args.extend(args[1:])

        return subprocess.run(test_args).returncode


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))