Skip to content

Commit

Permalink
Use tar instead of tarfile to create toolchain tarball
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Sep 20, 2024
1 parent 3358b93 commit a395464
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/package-action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Relenv Python Package
name: Build ppbt python package

on:
workflow_call:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.1.7
=====
- Use tar instead of tarfile to create toolchain tarballs


0.1.6
=====

Expand Down
4 changes: 1 addition & 3 deletions src/ppbt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""
Portable Python Build Toolchains.
"""
from .common import environ, extract

__version__ = "0.1.6"
from .common import __version__, environ, extract

ALL = ("environ", "extract")
44 changes: 19 additions & 25 deletions src/ppbt/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
CT_NG_VER = "1.26.0"
CT_URL = "http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-{version}.tar.bz2"
CT_GIT_REPO = "https://github.com/crosstool-ng/crosstool-ng.git"
TC_URL = "https://{hostname}/relenv/{version}/toolchain/{host}/{triplet}.tar.xz"
CICD = "CI" in os.environ
PATCHELF_VERSION = "0.18.0"
PATCHELF_SOURCE = (
Expand Down Expand Up @@ -308,37 +307,32 @@ def build_ppbt(branch=None, use_tempdir=True):
subprocess.run(["make"])

shutil.copy(source / "src" / "patchelf", build / triplet / "bin" / "patchelf")

os.chdir(build)
archive = f"{ triplet }.tar.xz"
record = f"{ triplet }.tar.xz.record"
print(f"Archive is {archive}")
cmd = ["tar", "-C", f"{ build }", "-cJf", f"{ archive }", f"{ triplet }"]
subprocess.run(cmd)

# XXX: Figure out why things break when using the tarfile module.
with open(record, "w") as rfp:
rwriter = csv.writer(rfp)
with tarfile.open(archive, mode="w:xz", dereference=True) as fp:
for root, _dirs, files in os.walk(archdir):
relroot = pathlib.Path(root).relative_to(build)
for f in files:
print(f"Archive {relroot} / {f}")
relpath = relroot / f
with open(relpath, "rb") as dfp:
data = dfp.read()
hsh = (
base64.urlsafe_b64encode(hashlib.sha256(data).digest())
.rstrip(b"=")
.decode()
)
hashpath = str(
pathlib.Path("ppbt") / "_toolchain" / relpath
)
rwriter.writerow([hashpath, f"sha256={hsh}", len(data)])
try:
fp.add(relpath, relpath, recursive=False)
except FileNotFoundError:
print(f"File not found while archiving: {relpath}")
for root, _dirs, files in os.walk(archdir):
relroot = pathlib.Path(root).relative_to(build)
for f in files:
print(f"Archive {relroot} / {f}")
relpath = relroot / f
with open(relpath, "rb") as fp:
data = fp.read()
hsh = (
base64.urlsafe_b64encode(hashlib.sha256(data).digest())
.rstrip(b"=")
.decode()
)
hashpath = str(pathlib.Path("ppbt") / "_toolchain" / relpath)
rwriter.writerow([hashpath, f"sha256={hsh}", len(data)])
print(f"Copying {archive} to {toolchain}")
print(f"Copying {record} to {toolchain}")
shutil.copy(archive, toolchain)
print(f"Copying {record} to {toolchain}")
shutil.copy(record, toolchain)
finally:
# if archdir and archdir.exists():
Expand Down
9 changes: 7 additions & 2 deletions src/ppbt/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@

from .build import build_arch, extract_archive, get_triplet

__version__ = "0.1.7"

triplet = get_triplet(build_arch())

archive = pathlib.Path(__file__).parent / "_toolchain" / f"{triplet}.tar.xz"
toolchain = pathlib.Path(__file__).parent / "_toolchain" / triplet
toolchain_root = pathlib.Path(__file__).parent / "_toolchain"


distinfo = pathlib.Path(__file__).resolve().parent.parent / "ppbt-0.1.0.dist-info"
# This is not reliable, the version can be modified by setuptools at build time.
distinfo = (
pathlib.Path(__file__).resolve().parent.parent / f"ppbt-{__version__}.dist-info"
)

log = logging.getLogger(__name__)

Expand All @@ -35,6 +39,7 @@ def extract(overwrite=False):
record = distinfo / "RECORD"
if record.exists():
records = []
log.info("Update pkg metadata")
with open(record, "r") as fp:
for row in csv.reader(fp):
records.append(row)
Expand Down

0 comments on commit a395464

Please sign in to comment.