Skip to content

Commit

Permalink
Merge pull request #70 from adfinis/jlf/feat-build-packages
Browse files Browse the repository at this point in the history
feat: build packages
  • Loading branch information
Jean-Louis Fuchs authored Mar 4, 2024
2 parents aa82c4d + d5caaf7 commit f420f7f
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,36 @@ jobs:
- name: Run tests
run: |
make test
pacakge:
runs-on: ubuntu-latest
needs: [test]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug

- name: Build container
uses: docker/build-push-action@v5
with:
context: compose
push: false
load: true
tags: ghcr.io/adfinis/pyaptly/cache:latest
cache-from: type=registry,ref=ghcr.io/adfinis/pyaptly/cache:gha

- name: Run tests
run: |
make build-packages
- name: 'Upload Artifact'
uses: actions/upload-artifact@v4
with:
name: packages
path: dist
retention-days: 60
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__
/.hypothesis
/.dmypy.json
/.coverage
/.coverage
/dist
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ entr-flake8: poetry-install ## run flake8 with entr
.PHONY: local-mypy
local-mypy: ## Run mypy as daemon locally (requires local-dev)
@poetry run dmypy run -- pyaptly

.PHONY: build-packages
build-packages: poetry-install ## build-rpm
@docker compose exec testing bash -c "poetry run ./tools/build-rpm"

1 change: 1 addition & 0 deletions compose/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RUN install_packages \
cargo \
git \
make \
rpm \
golang
ENV PATH="${PATH}:/root/go/bin"
ADD setup /setup
Expand Down
124 changes: 123 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ flake8-pyproject = "^1.2.3"
types-toml = "^0.10.8.7"
types-pyyaml = "^6.0.12.12"
pytest-coverage = "^0.0"
pyp2rpm = "^3.3.10"

[build-system]
requires = ["poetry-core"]
Expand Down
84 changes: 84 additions & 0 deletions tools/build-rpm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3

import os
import shutil
from pathlib import Path
from subprocess import PIPE, run
from tempfile import NamedTemporaryFile

from tomli import load
from tomli_w import dump

base = Path(__file__).resolve().absolute().parent.parent

version_tag = "%global pypi_version "
source_tag = "Source0: "


def fix_spec(spec, version, package):
with spec.open("r", encoding="UTF-8") as f:
data = f.read()
with spec.open("w", encoding="UTF-8") as f:
for line in data.splitlines():
if line.startswith("%changelog"):
break
if line.startswith(version_tag):
f.write(f"{version_tag}{version}\n")
elif line.startswith(source_tag):
f.write(f"{source_tag}{package}\n")
else:
f.write(f"{line}\n")


def main():
build_id = run(
["git", "rev-parse", "--short", "HEAD"],
stdout=PIPE,
check=True,
encoding="UTF-8",
).stdout.strip()
dist = base / "dist"
shutil.rmtree(dist, ignore_errors=True)
target = Path(os.path.expanduser("~/rpmbuild"))
shutil.rmtree(target, ignore_errors=True)
pyproject = base / "pyproject.toml"
with pyproject.open("rb") as f:
project = load(f)
version_orig = project["tool"]["poetry"]["version"]
version = f"{version_orig}+{build_id}"
project["tool"]["poetry"]["version"] = version
try:
temp_toml_path = None
with NamedTemporaryFile("wb", delete=False) as temp_toml:
temp_toml_path = Path(temp_toml.name)
with pyproject.open("rb") as f:
temp_toml.write(f.read())
with pyproject.open("wb") as f:
dump(project, f)
# hack to disable git: poetry has a bug
git = Path("/usr/bin/git")
git_dis = Path("/usr/bin/git_dis")
git.rename(git_dis)
run(["poetry", "build"], check=True)
finally:
git_dis.rename(git)
if temp_toml_path:
with temp_toml_path.open("rb") as temp_file:
with pyproject.open("wb") as f:
f.write(temp_file.read())

package = Path(list(dist.glob("pyaptly-*+*.tar.gz"))[0])
run(["pyp2rpm", "-s", "-d", target, package], check=True)
spec = target / "SPECS" / "python-pyaptly.spec"
sources = target / "SOURCES"
sources.mkdir()
shutil.copy2(package, sources)
fix_spec(spec, version.replace("+", "^"), package.name)
run(["rpmbuild", "-bs", spec], check=True)
srpms = target / "SRPMS"
rpm = str(list(srpms.glob("python-pyaptly-*.src.rpm"))[0])
shutil.copy2(rpm, dist)


if __name__ == "__main__":
main()

0 comments on commit f420f7f

Please sign in to comment.