|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
| 17 | +import argparse |
17 | 18 | import json
|
18 | 19 | import os
|
| 20 | +import pathlib |
19 | 21 | import shutil
|
20 | 22 | import subprocess
|
| 23 | +from typing import Optional |
21 | 24 |
|
22 | 25 |
|
23 | 26 | class Config:
|
24 |
| - def __init__(self): |
25 |
| - cipd_install_dir = os.getenv("PW_PYTHON_CIPD_INSTALL_DIR") |
26 |
| - self.python_exe = os.path.join(cipd_install_dir, "bin", "python3") |
27 |
| - if not os.path.exists(self.python_exe): |
28 |
| - self.python_exe = shutil.which("python3") |
29 |
| - if not self.python_exe: |
30 |
| - raise FileNotFoundError("Python3 not found") |
31 |
| - |
32 |
| - def get_version(self): |
33 |
| - out = subprocess.check_output( |
34 |
| - [self.python_exe, "-c", "import sys; print(sys.version_info.major, sys.version_info.minor)"], text=True |
35 |
| - ) |
36 |
| - return out.strip().split(" ") |
| 27 | + def __init__(self, python_exe: Optional[pathlib.Path] = None): |
| 28 | + self._paths: Optional[dict] = None |
| 29 | + self._version: Optional[list] = None |
37 | 30 |
|
| 31 | + if python_exe: |
| 32 | + self.python_exe = python_exe |
| 33 | + else: |
| 34 | + cipd_install_dir = os.getenv("PW_PYTHON_CIPD_INSTALL_DIR") |
| 35 | + if not cipd_install_dir: |
| 36 | + raise RuntimeError("PW_PYTHON_CIPD_INSTALL_DIR not set") |
| 37 | + self.python_exe = os.path.join(cipd_install_dir, "bin", "python3") |
| 38 | + if not os.path.exists(self.python_exe): |
| 39 | + self.python_exe = shutil.which("python3") |
| 40 | + if not self.python_exe: |
| 41 | + raise FileNotFoundError("Python3 not found") |
38 | 42 |
|
39 |
| -if __name__ == "__main__": |
40 |
| - config = Config() |
41 |
| - version = config.get_version() |
| 43 | + @property |
| 44 | + def paths(self) -> dict: |
| 45 | + if not self._paths: |
| 46 | + out = subprocess.check_output( |
| 47 | + [self.python_exe, "-c", "from sysconfig import get_paths; import json; print(json.dumps(get_paths()))"], text=True |
| 48 | + ).strip() |
| 49 | + self._paths = json.loads(out) |
| 50 | + return self._paths |
| 51 | + |
| 52 | + @property |
| 53 | + def version(self) -> list[str]: |
| 54 | + if not self._version: |
| 55 | + self._version = ( |
| 56 | + subprocess.check_output( |
| 57 | + [self.python_exe, "-c", "import sys; print(sys.version_info.major, sys.version_info.minor)"], text=True |
| 58 | + ) |
| 59 | + .strip() |
| 60 | + .split(" ") |
| 61 | + ) |
| 62 | + return self._version |
| 63 | + |
| 64 | + @property |
| 65 | + def include(self) -> str: |
| 66 | + return self.paths["include"] |
| 67 | + |
| 68 | + |
| 69 | +def parse_args(): |
| 70 | + parser = argparse.ArgumentParser(description="Generate Python config") |
| 71 | + parser.add_argument("--python", type=pathlib.Path, help="Python executable to use", required=False) |
| 72 | + return parser.parse_args() |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + args = parse_args() |
| 77 | + config = Config(args.python) |
42 | 78 | print(
|
43 | 79 | json.dumps(
|
44 | 80 | {
|
45 | 81 | "version": {
|
46 |
| - "major": version[0], |
47 |
| - "minor": version[1], |
48 |
| - } |
| 82 | + "major": config.version[0], |
| 83 | + "minor": config.version[1], |
| 84 | + }, |
| 85 | + "include": config.include, |
49 | 86 | }
|
50 | 87 | )
|
51 | 88 | )
|
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments