Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build target to the noxfile #21

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ jobs:
- 3.8
- 3.9
salt-version:
- 3003
- 3004
- "3003.0"
- "3004.0"

steps:
- uses: actions/checkout@v2
Expand All @@ -119,13 +119,13 @@ jobs:

- name: Install Test Requirements
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
run: |
nox --force-color -e tests-3 --install-only

- name: Test
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
SKIP_REQUIREMENTS_INSTALL: YES
run: |
nox --force-color -e tests-3 -- -vv tests/
Expand Down Expand Up @@ -271,8 +271,8 @@ jobs:
python-version:
- 3.7
salt-version:
- 3003
- 3004
- "3003.0"
- "3004.0"

steps:
- uses: actions/checkout@v2
Expand All @@ -290,7 +290,7 @@ jobs:
- name: Install Test Requirements
shell: bash
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
EXTRA_REQUIREMENTS_INSTALL: Cython
run: |
export PATH="/C/Program Files (x86)/Windows Kits/10/bin/10.0.18362.0/x64;$PATH"
Expand All @@ -299,7 +299,7 @@ jobs:
- name: Test
shell: bash
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
SKIP_REQUIREMENTS_INSTALL: YES
run: |
export PATH="/C/Program Files (x86)/Windows Kits/10/bin/10.0.18362.0/x64;$PATH"
Expand Down Expand Up @@ -410,8 +410,8 @@ jobs:
python-version:
- 3.7
salt-version:
- 3003
- 3004
- "3003.0"
- "3004.0"

steps:
- uses: actions/checkout@v2
Expand All @@ -428,13 +428,13 @@ jobs:

- name: Install Test Requirements
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
run: |
nox --force-color -e tests-3 --install-only

- name: Test
env:
SALT_REQUIREMENT: salt==${{ matrix.salt-version }}
SALT_REQUIREMENT: salt~=${{ matrix.salt-version }}
SKIP_REQUIREMENTS_INSTALL: YES
run: |
nox --force-color -e tests-3 -- -vv tests/
Expand Down Expand Up @@ -565,3 +565,35 @@ jobs:
with:
name: runtests-${{ steps.codecov.outputs.report-name }}.log
path: artifacts/runtests-*.log

Build:
runs-on: ubuntu-latest
needs:
- Docs
- PyLint
- Linux
- Windows
- macOS
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install Nox
run: |
python -m pip install nox
- name: Build a binary wheel and a source tarball
run: |
nox -e build

# - name: Publish distribution 📦 to Test PyPI
# if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# user: __token__
# password: ${{ secrets.TEST_PYPI_TOKEN }}
# repository_url: https://test.pypi.org/legacy/
# print_hash: true
2 changes: 2 additions & 0 deletions .pylint-spelling-words
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lru
lsof
macos
metric's
mtime
namespace
netbsd
noop
Expand All @@ -72,6 +73,7 @@ rst
rtype
saf
schemas
sdist
sid
sids
sitecustomize
Expand Down
113 changes: 112 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#
# pylint: disable=protected-access
import datetime
import gzip
import json
import os
import pathlib
import shutil
import sys
import tarfile
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -128,7 +130,7 @@ def _install_requirements(

@nox.session(python=PYTHON_VERSIONS)
def tests(session):
_install_requirements(session, install_source=True)
_install_requirements(session, "jinja2<3.1", install_source=True)

sitecustomize_dir = session.run("salt-factories", "--coverage", silent=True, log=False)
python_path_env_var = os.environ.get("PYTHONPATH") or None
Expand Down Expand Up @@ -614,3 +616,112 @@ def release(session):
except CommandFailed:
session.error("Failed to overwrite the temporary tag")
session.warn("Don't forget to push the newly created tag")


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 gzfile:
while True:
chunk = rfh.read(1024)
if not chunk:
break
gzfile.write(chunk)
targz.unlink()
shutil.move(str(d_targz), str(targz))


@nox.session(python="3")
def build(session):
"""
Build source and binary distributions based off the current commit author date UNIX timestamp.

The reason being, reproducible packages.

.. code-block: shell

git show -s --format=%at HEAD
"""
if SKIP_REQUIREMENTS_INSTALL is False:
requirements_file = REPO_ROOT / "requirements" / "build.txt"
session.install(
"--progress-bar=off",
"-r",
str(requirements_file.relative_to(REPO_ROOT)),
silent=PIP_INSTALL_SILENT,
)
timestamp = session.run(
"git",
"show",
"-s",
"--format=%at",
"HEAD",
silent=True,
log=False,
stderr=None,
).strip()
env = {"SOURCE_DATE_EPOCH": str(timestamp)}
session.run(
"python",
"-m",
"build",
"--sdist",
"--wheel",
str(REPO_ROOT),
env=env,
)
# Recreate sdist to be reproducible
recompress = Recompress(timestamp)
for targz in REPO_ROOT.joinpath("dist").glob("*.tar.gz"):
session.log("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()]
session.run("sha256sum", *packages, external=True)
session.run("python", "-m", "twine", "check", "dist/*")
2 changes: 2 additions & 0 deletions requirements/build.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
twine
build>=0.7.0
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extras_require =
docs = requirements/docs.txt
docsauto = requirements/docs-auto.txt
changelog = requirements/changelog.txt
build = requirements/build.txt


[bdist_wheel]
Expand Down