Add source tarball to tools package

This commit is contained in:
Daniel A. Wozniak 2023-01-24 22:33:07 -07:00 committed by Megan Wilhite
parent 07d4f0edb0
commit ec88f469c4

View file

@ -8,9 +8,13 @@ import fnmatch
import hashlib
import json
import logging
import gzip
import os
import pathlib
import tempfile
import tarfile
import shutil
import subprocess
import sys
import yaml
@ -24,6 +28,63 @@ REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
pkg = command_group(name="pkg", help="Packaging Related Commands", description=__doc__)
class Recompress:
"""
Helper class to re-compress a ``.tag.gz`` file to make it reproducible.
"""
def __init__(self, mtime):
self.mtime = int(mtime)
def tar_reset(self, tarinfo):
"""
Reset user, group, mtime, and mode to create reproducible tar.
"""
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
tarinfo.mtime = self.mtime
if tarinfo.type == tarfile.DIRTYPE:
tarinfo.mode = 0o755
else:
tarinfo.mode = 0o644
if tarinfo.pax_headers:
raise ValueError(tarinfo.name, tarinfo.pax_headers)
return tarinfo
def recompress(self, targz):
"""
Re-compress the passed path.
"""
tempd = pathlib.Path(tempfile.mkdtemp()).resolve()
d_src = tempd.joinpath("src")
d_src.mkdir()
d_tar = tempd.joinpath(targz.stem)
d_targz = tempd.joinpath(targz.name)
with tarfile.open(d_tar, "w|") as wfile:
with tarfile.open(targz, "r:gz") as rfile:
rfile.extractall(d_src)
extracted_dir = next(pathlib.Path(d_src).iterdir())
for name in sorted(extracted_dir.rglob("*")):
wfile.add(
str(name),
filter=self.tar_reset,
recursive=False,
arcname=str(name.relative_to(d_src)),
)
with open(d_tar, "rb") as rfh:
with gzip.GzipFile(
fileobj=open(d_targz, "wb"), mode="wb", filename="", mtime=self.mtime
) as gz: # pylint: disable=invalid-name
while True:
chunk = rfh.read(1024)
if not chunk:
break
gz.write(chunk)
targz.unlink()
shutil.move(str(d_targz), str(targz))
@pkg.command(
name="set-salt-version",
arguments={
@ -220,3 +281,41 @@ def generate_hashes(ctx: Context, files: list[pathlib.Path]):
ctx.info(f" * Writing {hashes_json_path} ...")
hashes_json_path.write_text(json.dumps(hashes))
ctx.info("Done")
@pkg.command(
name="source-tarball",
)
def source_tarball(ctx: Context):
shutil.rmtree("dist/", ignore_errors=True)
timestamp = ctx.run(
"git",
"show",
"-s",
"--format=%at",
"HEAD",
capture=True,
).stdout.strip()
env = {**os.environ, **{"SOURCE_DATE_EPOCH": str(timestamp)}}
ctx.run(
"python3",
"-m",
"build",
"--sdist",
str(REPO_ROOT),
env=env,
check=True,
)
# Recreate sdist to be reproducible
recompress = Recompress(timestamp)
for targz in REPO_ROOT.joinpath("dist").glob("*.tar.gz"):
ctx.info("Re-compressing %s...", targz.relative_to(REPO_ROOT))
recompress.recompress(targz)
sha256sum = shutil.which("sha256sum")
if sha256sum:
packages = [
str(pkg.relative_to(REPO_ROOT))
for pkg in REPO_ROOT.joinpath("dist").iterdir()
]
ctx.run("sha256sum", *packages) #, external=True)
ctx.run("python3", "-m", "twine", "check", "dist/*", check=True)