File: build-project.py

package info (click to toggle)
python-pip 25.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,048 kB
  • sloc: python: 82,877; sh: 75; makefile: 25
file content (72 lines) | stat: -rwxr-xr-x 1,968 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
"""Build pip using pinned build requirements."""

import subprocess
import tempfile
import venv
from os import PathLike
from pathlib import Path
from types import SimpleNamespace
from typing import Union


class EnvBuilder(venv.EnvBuilder):
    """A subclass of venv.EnvBuilder that exposes the python executable command."""

    def ensure_directories(
        self, env_dir: Union[str, bytes, "PathLike[str]", "PathLike[bytes]"]
    ) -> SimpleNamespace:
        context = super().ensure_directories(env_dir)
        self.env_exec_cmd = context.env_exec_cmd
        return context


def get_git_head_timestamp() -> str:
    return subprocess.run(
        [
            "git",
            "log",
            "-1",
            "--pretty=format:%ct",
        ],
        text=True,
        stdout=subprocess.PIPE,
    ).stdout.strip()


def main() -> None:
    with tempfile.TemporaryDirectory() as build_env:
        env_builder = EnvBuilder(with_pip=True)
        # If this venv creation step fails, you may be hitting
        # https://github.com/astral-sh/python-build-standalone/issues/381
        # Try running with a another Python distribution.
        env_builder.create(build_env)
        subprocess.run(
            [
                env_builder.env_exec_cmd,
                "-Im",
                "pip",
                "install",
                "--no-deps",
                "--only-binary=:all:",
                "--require-hashes",
                "-r",
                Path(__file__).parent / "build-requirements.txt",
            ],
            check=True,
        )
        subprocess.run(
            [
                env_builder.env_exec_cmd,
                "-Im",
                "build",
                "--no-isolation",
            ],
            check=True,
            env={"SOURCE_DATE_EPOCH": get_git_head_timestamp()},
            cwd=Path(__file__).parent.parent,
        )


if __name__ == "__main__":
    main()