Skip to content

Commit 60c71a4

Browse files
committed
move export-libs to gardenlinux.build and use pyelftools to retrieve interpreter
1 parent 9e14a1d commit 60c71a4

File tree

6 files changed

+51
-33
lines changed

6 files changed

+51
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ __pycache__/
1212
# Distribution / packaging
1313
.Python
1414
build/
15+
!/src/gardenlinux/build
1516
develop-eggs/
1617
dist/
1718
downloads/

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cryptography = "^46.0.1"
1616
jsonschema = "^4.25.1"
1717
networkx = "^3.5"
1818
oras = "^0.2.38"
19+
pyelftools = "^0.32"
1920
pygit2 = "^1.19.0"
2021
pygments = "^2.19.2"
2122
PyYAML = "^6.0.2"
@@ -42,7 +43,7 @@ gl-flavors-parse = "gardenlinux.flavors.__main__:main"
4243
gl-oci = "gardenlinux.oci.__main__:main"
4344
gl-s3 = "gardenlinux.s3.__main__:main"
4445
gl-gh = "gardenlinux.github.__main__:main"
45-
gl-python-exportlibs = "gardenlinux.export_libs.__main__:main"
46+
gl-build = "gardenlinux.build.__main__:main"
4647

4748
[tool.pytest.ini_options]
4849
pythonpath = ["src"]
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# -*- coding: utf-8 -*-
33

44
"""
5-
gl-python-exportlibs main entrypoint
5+
gl-build main entrypoint
66
"""
77

88
from argparse import ArgumentParser
99

1010
from .exporter import _get_default_package_dir, export
1111

1212
_ARGS_TYPE_ALLOWED = [
13-
"copy",
13+
"export-python-libs",
1414
]
1515

1616

@@ -43,14 +43,18 @@ def parse_args():
4343

4444
def main():
4545
"""
46-
gl-python-exportlibs main()
46+
gl-gl-build main()
4747
4848
:since: TODO
4949
"""
5050

5151
args = parse_args()
5252

53-
export(output_dir=args.output_dir, package_dir=args.package_dir)
53+
match args.type:
54+
case "export-python-libs":
55+
export(output_dir=args.output_dir, package_dir=args.package_dir)
56+
case _:
57+
raise NotImplementedError
5458

5559

5660
if __name__ == "__main__":
Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# -*- coding: utf-8 -*-
22

33
import os
4+
import pathlib
45
import re
56
import shutil
67
import subprocess
78
from os import PathLike
89

10+
from elftools.elf.elffile import ELFFile
11+
912
from ..logger import LoggerSetup
1013

1114
# Parses dependencies from ld output
@@ -41,32 +44,27 @@ def _getInterpreter(path: str, logger) -> str:
4144
"""
4245

4346
with open(path, "rb") as f:
44-
head = f.read(19)
47+
elf = ELFFile(f)
48+
interp = elf.get_section_by_name(".interp")
4549

46-
if head[5] == 1:
47-
arch = head[17:]
48-
elif head[5] == 2:
49-
arch = head[17:][::-1]
50-
else:
51-
logger.error(
52-
f"Error: Unknown endianess value for {path}: expected 1 or 2, but was {head[5]}"
53-
)
54-
exit(1)
55-
56-
if arch == b"\x00\xb7": # 00b7: aarch64
57-
return "/lib/ld-linux-aarch64.so.1"
58-
elif arch == b"\x00\x3e": # 003e: x86_64
59-
return "/lib64/ld-linux-x86-64.so.2"
60-
elif arch == b"\x00\x03": # 0003: i686
61-
return "/lib/ld-linux.so.2"
50+
if interp:
51+
return interp.data().split(b"\x00")[0].decode()
6252
else:
63-
logger.error(
64-
f"Error: Unsupported architecture for {path}: only support x86_64 (003e), aarch64 (00b7) and i686 (0003), but was {arch}"
65-
)
66-
exit(1)
67-
68-
69-
def _get_default_package_dir() -> str:
53+
match elf.header["e_machine"]:
54+
case "EM_AARCH64":
55+
return "/lib/ld-linux-aarch64.so.1"
56+
case "EM_386":
57+
return "/lib/ld-linux.so.2"
58+
case "EM_X86_64":
59+
return "/lib64/ld-linux-x86-64.so.2"
60+
case arch:
61+
logger.error(
62+
f"Error: Unsupported architecture for {path}: only support x86_64 (003e), aarch64 (00b7) and i686 (0003), but was {arch}"
63+
)
64+
exit(1)
65+
66+
67+
def _get_default_package_dir() -> PathLike[str]:
7068
"""
7169
Finds the default site-packages or dist-packages directory of the default python3 environment
7270
@@ -79,12 +77,12 @@ def _get_default_package_dir() -> str:
7977
["/bin/sh", "-c", 'python3 -c "import site; print(site.getsitepackages()[0])"'],
8078
stdout=subprocess.PIPE,
8179
)
82-
return out.stdout.decode().strip()
80+
return pathlib.Path(out.stdout.decode().strip())
8381

8482

8583
def export(
8684
output_dir: str | PathLike[str] = "/required_libs",
87-
package_dir: str | PathLike[str] = None,
85+
package_dir: str | PathLike[str] | None = None,
8886
logger=None,
8987
) -> None:
9088
"""

src/gardenlinux/s3/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ def main() -> None:
3636
if args.action == "download-artifacts-from-bucket":
3737
S3Artifacts(args.bucket).download_to_directory(args.cname, args.path)
3838
elif args.action == "upload-artifacts-to-bucket":
39-
S3Artifacts(args.bucket).upload_from_directory(args.cname, args.path, dry_run=args.dry_run)
39+
S3Artifacts(args.bucket).upload_from_directory(
40+
args.cname, args.path, dry_run=args.dry_run
41+
)

0 commit comments

Comments
 (0)