From 15a0a3a184d1a49abb56f7caeb1b4d0b66768ebd Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 15 Sep 2018 02:12:16 +0200 Subject: [PATCH] setup.py optimization and debian packaging --- .gitlab-ci.yml | 67 ++++++++++++++++--- matemat/__init__.py | 2 +- matemat/__main__.py | 15 +++-- package/debian/matemat/DEBIAN/changelog | 5 ++ package/debian/matemat/DEBIAN/compat | 1 + package/debian/matemat/DEBIAN/control | 11 +++ package/debian/matemat/DEBIAN/copyright | 16 +++++ package/debian/matemat/DEBIAN/matemat.links | 1 + package/debian/matemat/DEBIAN/postinst | 22 ++++++ package/debian/matemat/DEBIAN/rules | 3 + .../usr/lib/systemd/system/matemat.service | 10 +++ Dockerfile => package/docker/Dockerfile | 4 +- requirements.txt | 4 +- setup.py | 32 +++++++-- 14 files changed, 167 insertions(+), 26 deletions(-) create mode 100644 package/debian/matemat/DEBIAN/changelog create mode 100644 package/debian/matemat/DEBIAN/compat create mode 100644 package/debian/matemat/DEBIAN/control create mode 100644 package/debian/matemat/DEBIAN/copyright create mode 100644 package/debian/matemat/DEBIAN/matemat.links create mode 100755 package/debian/matemat/DEBIAN/postinst create mode 100755 package/debian/matemat/DEBIAN/rules create mode 100644 package/debian/matemat/usr/lib/systemd/system/matemat.service rename Dockerfile => package/docker/Dockerfile (84%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6e1f340..9dc1e6e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,31 +6,80 @@ stages: - build - staging + + +before_script: +- export MATEMAT_VERSION=$(python -c 'from matemat import __version__; print(__version__)') + + + test: stage: test script: - - pip3 install -r requirements.txt - - sudo -u matemat python3 -m coverage run --rcfile=setup.cfg -m unittest discover matemat - - sudo -u matemat python3 -m coverage combine - - sudo -u matemat python3 -m coverage report --rcfile=setup.cfg + - pip3 install -u . + - sudo -u matemat python3.7 -m coverage run --rcfile=setup.cfg -m unittest discover matemat + - sudo -u matemat python3.7 -m coverage combine + - sudo -u matemat python3.7 -m coverage report --rcfile=setup.cfg codestyle: stage: test script: - - pip3 install -r requirements.txt + - pip3 install -u . - sudo -u matemat pycodestyle matemat -build: + + +build_docker: stage: build script: - - docker build -t "registry.gitlab.com/s3lph/matemat:$(git rev-parse HEAD)" . - - docker tag "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_SHA" "registry.gitlab.com/s3lph/matemat:latest-$([[ $CI_COMMIT_REF_NAME == "master" ]] && echo stable || echo staging)" + - docker build -t "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_SHA" package/docker + - docker tag "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_SHA" "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_REF_NAME" - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN registry.gitlab.com - docker push "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_SHA" - - docker push registry.gitlab.com/s3lph/matemat:latest-$([[ $CI_COMMIT_REF_NAME == "master" ]] && echo stable || echo staging) + - docker push "registry.gitlab.com/s3lph/matemat:$CI_COMMIT_REF_NAME" only: - staging - master + - tags + +build_wheel: + stage: build + script: + - python3.7 setup.py egg_info --tag-build "+$CI_COMMIT_SHA" --tag-date bdist_wheel + - python3.7 setup.py egg_info --tag-build "+$CI_COMMIT_REF_NAME" --tag-date bdist_wheel + artifacts: + paths: + - "dist/*.whl" + only: + - staging + - master + - tags + +# This is only useful once Debian either defaults python3 to 3.7 or provides python3.7- packages +# for the needed dependencies... But it SHOULD work... +#build_deb: +# stage: build +# script: +# - cp -r static/ package/debian/matemat/usr/lib/matemat/static/ +# - cp -r templates/ package/debian/matemat/usr/lib/matemat/templates/ +# - python3.7 setup.py egg_info -d -b +master install --root=package/debian/matemat/ --prefix=/usr --optimize=1 +# - export PYTHON_BIN=$(which python3) +# - cd package/debian +# - mv matemat/usr/lib/python3.7/{site,dist}-packages +# - mv matemat/usr/bin/matemat matemat/usr/lib/matemat/matemat +# - rm -r matemat/usr/bin +# - find . -type f -exec sed -re "s#${PYTHON_BIN}#/usr/bin/python3.7#g" -i {} \; +# - dpkg-deb --build matemat +# - mv matemat.deb "matemat_${MATEMAT_VERSION}+${CI_COMMIT_REF_NAME}-1_all.deb" +# artifacts: +# paths: +# - "package/debian/*.deb" +# only: +# - staging +# - master +# - tags + + staging: stage: staging diff --git a/matemat/__init__.py b/matemat/__init__.py index a3332a5..373d726 100644 --- a/matemat/__init__.py +++ b/matemat/__init__.py @@ -1,2 +1,2 @@ -__version__ = '2.0' +__version__ = '0.1' diff --git a/matemat/__main__.py b/matemat/__main__.py index 8876386..b10f837 100644 --- a/matemat/__main__.py +++ b/matemat/__main__.py @@ -3,14 +3,15 @@ from typing import Any, Dict, Iterable, Union import sys +from matemat.webserver import MatematWebserver from matemat.webserver import parse_config_file -if __name__ == '__main__': - # Those imports are actually needed, as they implicitly register pagelets. - # noinspection PyUnresolvedReferences - from matemat.webserver.pagelets import * - from matemat.webserver import MatematWebserver +# Those imports are actually needed, as they implicitly register pagelets. +# noinspection PyUnresolvedReferences +from matemat.webserver.pagelets import * + +def main(): # Use config file name from command line, if present configfile: Union[str, Iterable[str]] = '/etc/matemat.conf' if len(sys.argv) > 1: @@ -21,3 +22,7 @@ if __name__ == '__main__': # Start the web server MatematWebserver(**config).start() + + +if __name__ == '__main__': + main() diff --git a/package/debian/matemat/DEBIAN/changelog b/package/debian/matemat/DEBIAN/changelog new file mode 100644 index 0000000..8b78df4 --- /dev/null +++ b/package/debian/matemat/DEBIAN/changelog @@ -0,0 +1,5 @@ +matemat (0.1-1) UNRELEASED; urgency=medium + + * Initial release. + + -- s3lph Wed, 12 Sep 2018 02:04:12 +0000 diff --git a/package/debian/matemat/DEBIAN/compat b/package/debian/matemat/DEBIAN/compat new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/package/debian/matemat/DEBIAN/compat @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/package/debian/matemat/DEBIAN/control b/package/debian/matemat/DEBIAN/control new file mode 100644 index 0000000..08cbd9b --- /dev/null +++ b/package/debian/matemat/DEBIAN/control @@ -0,0 +1,11 @@ +Package: matemat +Version: 0.1 +Maintainer: s3lph +Section: web +Priority: extra +Architecture: all +Depends: python3 >= 3.7, python3-jinja2, python3-magic, python3-pil +Description: A soda machine stock-keeping webservice + A web service for automated stock-keeping of a soda machine written in Python. + It provides a touch-input-friendly user interface (as most input happens through the + soda machine's touch screen). diff --git a/package/debian/matemat/DEBIAN/copyright b/package/debian/matemat/DEBIAN/copyright new file mode 100644 index 0000000..a38a9a5 --- /dev/null +++ b/package/debian/matemat/DEBIAN/copyright @@ -0,0 +1,16 @@ +Copyright 2018 s3lph + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT +OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package/debian/matemat/DEBIAN/matemat.links b/package/debian/matemat/DEBIAN/matemat.links new file mode 100644 index 0000000..e789bc0 --- /dev/null +++ b/package/debian/matemat/DEBIAN/matemat.links @@ -0,0 +1 @@ +/usr/lib/matemat/static/upload /var/lib/matemat/upload \ No newline at end of file diff --git a/package/debian/matemat/DEBIAN/postinst b/package/debian/matemat/DEBIAN/postinst new file mode 100755 index 0000000..dcf706d --- /dev/null +++ b/package/debian/matemat/DEBIAN/postinst @@ -0,0 +1,22 @@ +#!/bin/bash + +set -e + +if [[ "$1" == "configure" ]]; then + + if ! getent group matemat >/dev/null; then + addgroup --quiet --system matemat + fi + + if ! getent passwd matemat >/dev/null; then + adduser --quiet --system --create-home --ingroup matemat --home /var/lib/matemat --shell /usr/sbin/nologin \ + matemat + fi + + chown matemat:matemat -R /var/lib/matemat + find /var/lib/matemat -type d -exec chmod 0750 {} + find /var/lib/matemat -type f -exec chmod 0640 {} + + setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/matemat + +fi diff --git a/package/debian/matemat/DEBIAN/rules b/package/debian/matemat/DEBIAN/rules new file mode 100755 index 0000000..cbe925d --- /dev/null +++ b/package/debian/matemat/DEBIAN/rules @@ -0,0 +1,3 @@ +#!/usr/bin/make -f +%: + dh $@ diff --git a/package/debian/matemat/usr/lib/systemd/system/matemat.service b/package/debian/matemat/usr/lib/systemd/system/matemat.service new file mode 100644 index 0000000..ae2283f --- /dev/null +++ b/package/debian/matemat/usr/lib/systemd/system/matemat.service @@ -0,0 +1,10 @@ +[Unit] +Description=matemat +After=networking.target + +[Service] +Exec=/usr/lib/matemat/matemat /etc/matemat.conf /usr/lib/matemat/matemat.conf +User=matemat + +[Install] +WantedBy=multi-user.target diff --git a/Dockerfile b/package/docker/Dockerfile similarity index 84% rename from Dockerfile rename to package/docker/Dockerfile index 7338327..fae8463 100644 --- a/Dockerfile +++ b/package/docker/Dockerfile @@ -1,10 +1,10 @@ FROM python:3.7-alpine -ADD . / +ADD ../.. / RUN mkdir -p /var/matemat/db /var/matemat/upload \ && apk --update add libmagic zlib jpeg zlib-dev jpeg-dev build-base \ - && pip3 install -r /requirements.txt \ + && pip3 install -u . \ && apk del zlib-dev jpeg-dev build-base \ && rm -rf /var/cache/apk /root/.cache/pip diff --git a/requirements.txt b/requirements.txt index 0da0f5a..9c558e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1 @@ -file-magic -jinja2 -Pillow +. diff --git a/setup.py b/setup.py index 0fc605f..265db5c 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,32 @@ -from setuptools import setup +from setuptools import setup, find_packages + +from matemat import __version__ setup( name='matemat', - version='2.0', - packages=['matemat'], - url='', - license='', + version=__version__, + url='https://gitlab.com/s3lph/matemat', + license='MIT', author='s3lph', author_email='', - description='Replacement for the original Ruby matemat software.' + description='Soda machine stock-keeping webservice', + long_description=''' +A web service for automated stock-keeping of a soda machine written in Python. +It provides a touch-input-friendly user interface (as most input happens through the +soda machine's touch screen). +''', + + packages=find_packages(exclude=['*.test']), + python_requires='>=3.7', + install_requires=[ + 'file-magic', + 'jinja2', + 'Pillow' + ], + test_loader='unittest:TestLoader', + entry_points={ + 'console_scripts': [ + 'matemat = matemat.__main__:main' + ] + } )