xref: /aosp_15_r20/external/pigweed/pw_ide/py/pw_ide/python.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Configure Python IDE support for Pigweed projects."""
15
16from collections import defaultdict
17import os
18from pathlib import Path
19import platform
20from typing import NamedTuple
21
22_PYTHON_VENV_PATH = (
23    Path(os.path.expandvars('$_PW_ACTUAL_ENVIRONMENT_ROOT')) / 'pigweed-venv'
24)
25
26
27class _PythonPathsForPlatform(NamedTuple):
28    bin_dir_name: str = 'bin'
29    interpreter_name: str = 'python3'
30
31
32# When given a platform (e.g. the output of platform.system()), this dict gives
33# the platform-specific virtualenv path names.
34_PYTHON_PATHS_FOR_PLATFORM: dict[str, _PythonPathsForPlatform] = defaultdict(
35    _PythonPathsForPlatform
36)
37_PYTHON_PATHS_FOR_PLATFORM['Windows'] = _PythonPathsForPlatform(
38    bin_dir_name='Scripts', interpreter_name='pythonw.exe'
39)
40
41
42class PythonPaths:
43    """Holds the platform-specific Python environment paths.
44
45    The directory layout of Python virtual environments varies among
46    platforms. This class holds the data needed to find the right paths
47    for a specific platform.
48    """
49
50    def __init__(self, system=platform.system()):
51        (bin_dir_name, interpreter_name) = _PYTHON_PATHS_FOR_PLATFORM[system]
52        self.bin_dir = _PYTHON_VENV_PATH / bin_dir_name
53        self.interpreter = self.bin_dir / interpreter_name
54