From 3cea7534ef37321286f3309806da98dd8c91234d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Fri, 17 Feb 2017 16:13:11 +0100 Subject: [PATCH] New versioning Use git to automatically fetch the version --- .gitignore | 3 ++- .gitlab-ci.yml | 1 + Makefile | 8 ++++---- senpy/VERSION | 1 - senpy/version.py | 34 ++++++++++++++++++++++++++++++++-- 5 files changed, 39 insertions(+), 8 deletions(-) delete mode 100644 senpy/VERSION diff --git a/.gitignore b/.gitignore index 0299471..4617294 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ dist build README.html -__pycache__ \ No newline at end of file +__pycache__ +VERSION diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index baa51d7..2b14f37 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,6 +29,7 @@ stages: - pip install --use-wheel -r requirements.txt - pip install --use-wheel -r test-requirements.txt - py.test --cov=senpy --cov-report term-missing + - python test-3.5: <<: *test_definition diff --git a/Makefile b/Makefile index 802bc29..fb6e75d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PYVERSIONS=3.5 3.4 2.7 PYMAIN=$(firstword $(PYVERSIONS)) NAME=senpy REPO=gsiupm -VERSION=$(shell cat $(NAME)/VERSION) +VERSION=$(shell git describe --tags) TARNAME=$(NAME)-$(subst -,.,$(VERSION)).tar.gz IMAGENAME=$(REPO)/$(NAME):$(VERSION) TEST_COMMAND=gitlab-runner exec docker --cache-dir=/tmp/gitlabrunner --docker-volumes /tmp/gitlabrunner:/tmp/gitlabrunner --env CI_PROJECT_NAME=$(NAME) @@ -36,7 +36,7 @@ quick_test: $(addprefix test-,$(PYMAIN)) test: $(addprefix test-,$(PYVERSIONS)) debug-%: - (docker start $(NAME)-debug && docker attach $(NAME)-debug) || docker run -w /usr/src/app/ -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti --name $(NAME)-debug '$(IMAGENAME)-python$*' + (docker start $(NAME)-debug && docker attach $(NAME)-debug) || docker run -w /usr/src/app/ -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti --name $(NAME)-debug '$(IMAGENAME)-python$* pip install -r test-requirements.txt' debug: debug-$(PYMAIN) @@ -63,8 +63,8 @@ upload: test $(addprefix upload-,$(PYVERSIONS)) docker push '$(REPO)/$(NAME)' clean: - @docker ps -a | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[1] != "${VERSION}"){ print $$1;}}' | xargs docker rm 2>/dev/null|| true - @docker images | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[1] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true + @docker ps -a | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1;}}' | xargs docker rm -v 2>/dev/null|| true + @docker images | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true @docker rmi $(NAME)-debug 2>/dev/null || true upload_git: diff --git a/senpy/VERSION b/senpy/VERSION deleted file mode 100644 index a48e2db..0000000 --- a/senpy/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.7.2-dev1 diff --git a/senpy/version.py b/senpy/version.py index 8d3f7ec..c527fd0 100644 --- a/senpy/version.py +++ b/senpy/version.py @@ -1,4 +1,34 @@ import os +import subprocess +import logging -with open(os.path.join(os.path.dirname(__file__), 'VERSION')) as f: - __version__ = f.read().strip() +logger = logging.getLogger(__name__) + +ROOT = os.path.dirname(__file__) +DEFAULT_FILE = os.path.join(ROOT, 'VERSION') + + +def git_version(): + try: + res = subprocess.check_output(['git', 'describe', + '--tags', '--dirty']).decode('utf-8') + return res.strip() + except subprocess.CalledProcessError: + return None + + +def read_version(versionfile=DEFAULT_FILE): + with open(versionfile) as f: + return f.read().strip() + + +def write_version(version, versionfile=DEFAULT_FILE): + version = version or git_version() + if not version: + raise ValueError('You need to provide a valid version') + with open(versionfile, 'w') as f: + f.write(version) + + +__version__ = git_version() or read_version() +write_version(__version__)