Skip to content

Commit e5cab98

Browse files
committed
use pathlib.Path and retrieve primary python interpreter directly from script
1 parent 2e2f289 commit e5cab98

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/gardenlinux/build/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
gl-build main entrypoint
66
"""
77

8+
import pathlib
89
from argparse import ArgumentParser
910

1011
from .exporter import _get_default_package_dir, export
@@ -52,7 +53,7 @@ def main():
5253

5354
match args.type:
5455
case "export-python-libs":
55-
export(output_dir=args.output_dir, package_dir=args.package_dir)
56+
export(output_dir=pathlib.Path(args.output_dir), package_dir=pathlib.Path(args.package_dir))
5657
case _:
5758
raise NotImplementedError
5859

src/gardenlinux/build/exporter.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
# Check for ELF header
22-
def _isElf(path: str) -> bool:
22+
def _isElf(path: str | PathLike[str]) -> bool:
2323
"""
2424
Checks if a file is an ELF by looking for the ELF header.
2525
@@ -37,7 +37,7 @@ def _isElf(path: str) -> bool:
3737
return False
3838

3939

40-
def _getInterpreter(path: str, logger) -> str:
40+
def _getInterpreter(path: str | PathLike[str], logger) -> PathLike[str]:
4141
"""
4242
Returns the interpreter of an ELF. Supported architectures: x86_64, aarch64, i686.
4343
@@ -53,36 +53,48 @@ def _getInterpreter(path: str, logger) -> str:
5353
interp = elf.get_section_by_name(".interp")
5454

5555
if interp:
56-
return interp.data().split(b"\x00")[0].decode()
56+
return pathlib.Path(interp.data().split(b"\x00")[0].decode())
5757
else:
5858
match elf.header["e_machine"]:
5959
case "EM_AARCH64":
60-
return "/lib/ld-linux-aarch64.so.1"
60+
return pathlib.Path("/lib/ld-linux-aarch64.so.1")
6161
case "EM_386":
62-
return "/lib/ld-linux.so.2"
62+
return pathlib.Path("/lib/ld-linux.so.2")
6363
case "EM_X86_64":
64-
return "/lib64/ld-linux-x86-64.so.2"
64+
return pathlib.Path("/lib64/ld-linux-x86-64.so.2")
6565
case arch:
6666
logger.error(
6767
f"Error: Unsupported architecture for {path}: only support x86_64 (003e), aarch64 (00b7) and i686 (0003), but was {arch}"
6868
)
6969
exit(1)
7070

71+
def _get_python_from_path() -> str | None:
72+
interpreter = None
73+
for dir in os.environ["PATH"].split(":"):
74+
binary = os.path.join(dir, "python3")
75+
if os.path.isfile(binary):
76+
interpreter = binary
77+
break
78+
return interpreter
7179

72-
def _get_default_package_dir() -> PathLike[str]:
80+
def _get_default_package_dir() -> PathLike[str] | None:
7381
"""
7482
Finds the default site-packages or dist-packages directory of the default python3 environment
7583
7684
:return: (str) Path to directory
7785
:since: TODO
7886
"""
79-
87+
8088
# Needs to escape the virtual environment python-gardenlinx-lib is running in
81-
out = subprocess.run(
82-
["/bin/sh", "-c", 'python3 -c "import site; print(site.getsitepackages()[0])"'],
83-
stdout=subprocess.PIPE,
84-
)
85-
return pathlib.Path(out.stdout.decode().strip())
89+
interpreter = _get_python_from_path()
90+
if interpreter:
91+
out = subprocess.run(
92+
[interpreter, "-c", "import site; print(site.getsitepackages()[0])"],
93+
stdout=subprocess.PIPE,
94+
)
95+
return pathlib.Path(out.stdout.decode().strip())
96+
else:
97+
return None
8698

8799

88100
def export(
@@ -102,13 +114,19 @@ def export(
102114

103115
if not package_dir:
104116
package_dir = _get_default_package_dir()
117+
if not package_dir:
118+
logger.error(
119+
f"Error: Couldn't identify a default python package directory. Please specifiy one using the --package-dir option. Use -h for more information."
120+
)
121+
exit(1)
122+
105123
if logger is None or not logger.hasHandlers():
106124
logger = LoggerSetup.get_logger("gardenlinux.export_libs")
107125
# Collect ld dependencies for installed pip packages
108126
dependencies = set()
109127
for root, dirs, files in os.walk(package_dir):
110128
for file in files:
111-
path = f"{root}/{file}"
129+
path = os.path.join(root, file)
112130
if not os.path.islink(path) and _isElf(path):
113131
out = subprocess.run(
114132
[_getInterpreter(path, logger), "--inhibit-cache", "--list", path],

0 commit comments

Comments
 (0)