Coverage for pipxl/compile.py: 100%

1# SPDX-FileCopyrightText: 2022-present Jeroen van Zundert <mail@jeroenvanzundert.nl>

2#

3# SPDX-License-Identifier: MIT

4

5 from __future__ import annotations

6

7 from pathlib import Path

8

9 from pipxl.__about__ import __version__

10 from pipxl.data import Environment, ReqFileEntry

11 from pipxl.resolver import pip_resolve

12

13

14 def compile(

15 files_in: list[Path] | None = None,

16 package_spec: list[str] | None = None,

17 file_out: Path = Path("."),

18) -> None:

19 reqs, env = pip_resolve(files_in, package_spec)

20

21 # for packages specified in the target(s), we do not have a specifier. We add the files. We do not add

22 # the package_spec.

23 if (files_in is not None) and len(files_in):

24 target_str = " ".join([f"-r {p}" for p in files_in])

25 for r in reqs:

26 if r.requested:

27 r.required_by = {target_str: ""} | r.required_by

28

29 text = _format_file(reqs, env)

30 with file_out.open("w") as fp:

31 fp.write(text)

32

33

34 def _format_file(reqs: list[ReqFileEntry], env: Environment) -> str:

35 out = f"""# This file has been generated by pipxl {__version__}

36#

37# Environment at time of generation:

38"""

39 for name, val in env.__dict__.items():

40 out += f"#\t{name}: {val}\n"

41

42 out += "#\n\n"

43

44 out += "\n".join([req.to_string() for req in reqs])

45 out += "\n"

46

47 return out