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

Fix astacus/version issues #193

Closed
wants to merge 6 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ jobs:
python-version: '3.11'
cache: 'pip'

- name: Install testing requirements
run: pip install -r requirements.testing.txt

- name: generate version
run: make generate_version_from_git

- name: Install requirements
run: pip install -r requirements.txt

Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ jobs:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install testing requirements
run: pip install -r requirements.testing.txt

- name: generate version
run: make generate_version_from_git

- name: Install requirements
run: pip install -e '.[cassandra]'

- name: Install testing requirements (not included by default in normal install)
run: pip install -r requirements.testing.txt

- name: Execute unit-tests
run: make unittest

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.fedora
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MAINTAINER "Markus Stenberg <mstenber@aiven.io>"
RUN dnf install -y sudo make

ADD Makefile astacus.spec /build/
RUN cd /build && make build-dep-fedora
RUN cd /build && make generate_version_from_git && make build-dep-fedora

ADD README.md setup.cfg setup.py requirements*.txt /build/
RUN cd /build && pip3 install -e '.[cassandra]'
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN apt-get update
RUN apt-get install -y sudo make

ADD Makefile /build/
RUN cd /build && make build-dep-ubuntu
RUN cd /build && make generate_version_from_git && make build-dep-ubuntu

ADD README.md setup.cfg setup.py requirements*.txt /build/
RUN cd /build && pip3 install -e '.[cassandra]'
Expand Down
24 changes: 9 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PROTODIR=astacus/proto
PROTOBUFS = $(wildcard $(PROTODIR)/*.proto)
GENERATED_PROTOBUFS = $(patsubst %.proto,%_pb2.py,$(PROTOBUFS))

GENERATED = astacus/version.py $(GENERATED_PROTOBUFS)
GENERATED = $(GENERATED_PROTOBUFS)

PYTHON = python3
DNF_INSTALL = sudo dnf install -y
Expand Down Expand Up @@ -134,20 +134,14 @@ run-server:
echo bar > $(BACKUPROOT)/bar
astacus server -c examples/astacus-files-local.yaml

.PHONY: rpm
rpm: $(GENERATED) /usr/bin/rpmbuild /usr/lib/rpm/check-buildroot
git archive --output=astacus-rpm-src.tar --prefix=astacus/ HEAD
# add generated files to the tar, they're not in git repository
tar -r -f astacus-rpm-src.tar --transform=s,astacus/,astacus/astacus/, $(GENERATED)
rpmbuild -bb astacus.spec \
--define '_topdir $(PWD)/rpm' \
--define '_sourcedir $(CURDIR)' \
--define 'major_version $(SHORT_VER)' \
--define 'minor_version $(subst -,.,$(subst $(SHORT_VER)-,,$(LONG_VER)))'
$(RM) astacus-rpm-src.tar

astacus/version.py: version_from_git.py
$(PYTHON) $^ $@
.PHONY: generate_version_from_git
generate_version_from_git: version_setter.py
@$(PYTHON) version_setter.py from-git

.PHONY: set_version
set_version:
@test "$(SET_VERSION)" || { echo "SET_VERSION must be passed as an env variable"; exit 1; }
$(PYTHON) version_setter.py set-version $(SET_VERSION)

%_pb2.py: %.proto
protoc -I $(PROTODIR) $< --python_out=$(PROTODIR)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sudo pip3 install -e '.[cassandra]'

```
sudo dnf install -y make
make generate_version_from_git
make build-dep-fedora
sudo python3 ./setup.py install
```
Expand All @@ -64,6 +65,7 @@ sudo python3 ./setup.py install
```
sudo apt-get update
sudo apt-get install -y make sudo
make generate_version_from_git
make build-dep-ubuntu
sudo python3 ./setup.py install
```
Expand Down
3 changes: 3 additions & 0 deletions requirements.testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ types-ujson>=5.9.0.0
types-urllib3>=1.26.25.4
typing_extensions>=4.7.1

# packaging
packaging>=23.0

# F38
coverage==7.0.5

Expand Down
8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@


def _run():
try:
import version # pylint: disable=import-outside-toplevel

version_for_setup_py = version.update_project_version("astacus/version.py")
version_for_setup_py = ".dev".join(version_for_setup_py.split("-", 2)[:2])
except ImportError:
version_for_setup_py = "0.0.1" # tox
from astacus.version import __version__ as version_for_setup_py # pylint: disable=import-outside-toplevel

setuptools.setup(
version=version_for_setup_py,
Expand Down
51 changes: 0 additions & 51 deletions version_from_git.py

This file was deleted.

68 changes: 68 additions & 0 deletions version_setter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
version

Copyright (c) 2019 Aiven Ltd
See LICENSE for details
"""
from packaging import version as packaging_version

import importlib.util
import os
import subprocess

ASTACUS_VERSION_FILE = os.path.join(os.path.dirname(__file__), "astacus", "version.py")


def save_version(*, new_ver, old_ver, version_file):
"Save new version file, if old_ver != new_ver"
if not new_ver:
return False
packaging_version.parse(new_ver) # just a validation step, we don't actually care about the result
if not old_ver or new_ver != old_ver:
with open(version_file, "w") as file_handle:
file_handle.write(f'"""{__doc__}"""\n__version__ = "{new_ver}"\n')
return True


def update_project_version_from_git(version_file):
"Update the version_file, and return the version number stored in the file"
project_root_directory = os.path.dirname(os.path.realpath(__file__))
version_file_full_path = os.path.join(project_root_directory, version_file)
module_spec = importlib.util.spec_from_file_location("verfile", version_file_full_path)
module = importlib.util.module_from_spec(module_spec)
file_ver = getattr(module, "__version__", None)

os.chdir(os.path.dirname(__file__) or ".")
try:
git_out = subprocess.check_output(
["git", "--git-dir", os.path.join(project_root_directory, ".git"), "describe", "--always"],
stderr=getattr(subprocess, "DEVNULL", None),
)
except (OSError, subprocess.CalledProcessError):
pass
else:
git_ver = git_out.splitlines()[0].strip().decode("utf-8")
if "." not in git_ver:
git_ver = f"0.0.1-0-unknown{git_ver}"
version, count_since_release, git_hash = git_ver.split("-")
# this version will never be picked as a more recent version than the release version
# which is what we probably want to prevent silly mistakes.
new_version = f"{version}.dev{count_since_release}+{git_hash}"
if save_version(new_ver=new_version, old_ver=file_ver, version_file=version_file):
return git_ver

if not file_ver:
raise ValueError(f"version not available from git or from file {version_file!r}")

return file_ver


if __name__ == "__main__":
import sys

if sys.argv[1] == "from-git":
update_project_version_from_git(ASTACUS_VERSION_FILE)
elif sys.argv[1] == "set-version":
save_version(new_ver=sys.argv[2], old_ver=None, version_file=ASTACUS_VERSION_FILE)
else:
raise ValueError(f"Unknown command {sys.argv[1]!r}")
Loading