From 60a98f47209c12b5e2a6897ee4a463f4ed43af3c Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Mon, 16 Apr 2018 18:48:26 +0800 Subject: [PATCH] v1.1.0 add docker support run "make docker" to create a docker image. docker image is based on debian stretch image and just install the deb file. This doesn't change the dev workflow. Your venv, pip still work as before. --- project.clj | 2 +- src/leiningen/new/python.clj | 3 +- src/leiningen/new/python/.gitignore | 2 + src/leiningen/new/python/Dockerfile.template | 14 +++++ src/leiningen/new/python/Makefile | 7 ++- src/leiningen/new/python/utils/build-deb | 17 ++++++ .../new/python/utils/choose_default_python.py | 43 --------------- .../new/python/utils/choose_default_python.sh | 54 +++++++++++++++++++ 8 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/leiningen/new/python/Dockerfile.template delete mode 100755 src/leiningen/new/python/utils/choose_default_python.py create mode 100755 src/leiningen/new/python/utils/choose_default_python.sh diff --git a/project.clj b/project.clj index 0c24e20..824218f 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject python/lein-template "1.0.0" +(defproject python/lein-template "1.1.0" :description "lein template for a python project" :repositories [["snapshots" {:url "http://devserv.game.yy.com/nexus/content/repositories/snapshots" diff --git a/src/leiningen/new/python.clj b/src/leiningen/new/python.clj index be616ff..4ac8f8a 100644 --- a/src/leiningen/new/python.clj +++ b/src/leiningen/new/python.clj @@ -45,6 +45,7 @@ "requirements.txt" "requirements-dev.txt" "setup.py" + "Dockerfile.template" "README.rst" "utils/versionutils.py" "utils/build-deb" @@ -64,7 +65,7 @@ "git-hooks/pre-commit" ".gitignore"] plain-files ["utils/__init__.py" - "utils/choose_default_python.py" + "utils/choose_default_python.sh" "utils/virtualenv-15.1.0/virtualenv.py" "utils/virtualenv-15.1.0/virtualenv_support/argparse-1.4.0-py2.py3-none-any.whl" "utils/virtualenv-15.1.0/virtualenv_support/setuptools-28.8.0-py2.py3-none-any.whl" diff --git a/src/leiningen/new/python/.gitignore b/src/leiningen/new/python/.gitignore index 07a0db8..56b5c4c 100644 --- a/src/leiningen/new/python/.gitignore +++ b/src/leiningen/new/python/.gitignore @@ -18,3 +18,5 @@ __pycache__/ doc/_build/ TAGS # ENG regular python ignore list +Dockerfile +.dockerignore diff --git a/src/leiningen/new/python/Dockerfile.template b/src/leiningen/new/python/Dockerfile.template new file mode 100644 index 0000000..dda64ee --- /dev/null +++ b/src/leiningen/new/python/Dockerfile.template @@ -0,0 +1,14 @@ +FROM sylecn/debian:stretch-slim-cn + +#LABEL description="TODO add image description and uncomment this" + +RUN set -x \ + && apt-get update \ + && apt-get install -y --no-install-recommends --no-install-suggests \ + DEPENDS \ + && rm -rf /var/lib/apt/lists/* + +COPY DEB_PKG / +RUN ["dpkg", "-i", "DEB_PKG"] +CMD ["make", "run", "-C", "/opt/{{name}}"] +#EXPOSE 8082 diff --git a/src/leiningen/new/python/Makefile b/src/leiningen/new/python/Makefile index 9237c12..e39989b 100644 --- a/src/leiningen/new/python/Makefile +++ b/src/leiningen/new/python/Makefile @@ -7,13 +7,15 @@ PEP8 := env PYTHONPATH=$(PYTHONPATH) $(VENV)/bin/pep8 --repeat --ignore=E202,E50 PYTHON := env PYTHONPATH=$(PYTHONPATH) $(VENV)/bin/python PIP := $(VENV)/bin/pip -DEFAULT_PYTHON := $(shell ./utils/choose_default_python.py) +DEFAULT_PYTHON := $(shell ./utils/choose_default_python.sh) VIRTUALENV := ./utils/virtualenv-15.1.0/virtualenv.py REQUIREMENTS := -r requirements.txt DEV_REQUIREMENTS := -r requirements-dev.txt VERSION := $(shell grep '__version__' {{python-pkg-name}}/__init__.py |cut -d'"' -f 2) +# docker hub user name or private registry URL with username. +DOCKER_IMAGE_PREFIX := sylecn default: check-coding-style build: @@ -49,6 +51,9 @@ install: bootstrap $(PIP) install -q --no-index . uninstall: bootstrap $(PIP) uninstall -q -y {{python-pkg-name}} +docker: deb + docker build -t $(DOCKER_IMAGE_PREFIX)/{{name}}:$(VERSION) . + @echo "You may push it with: docker push $(DOCKER_IMAGE_PREFIX)/{{name}}:$(VERSION)" deb: utils/build-deb pipfreeze: diff --git a/src/leiningen/new/python/utils/build-deb b/src/leiningen/new/python/utils/build-deb index db0061b..5748095 100644 --- a/src/leiningen/new/python/utils/build-deb +++ b/src/leiningen/new/python/utils/build-deb @@ -34,3 +34,20 @@ fpm -t deb -s dir -n "$DEB_PKG_NAME" -v "$VERSION" --prefix "$DEST_DIR" -f \ --before-remove deb-scripts/before-remove.sh \ Makefile README.rst setup.py utils requirements*.txt wheelhouse conf \ $PYTHON_MODULES + +# create Dockerfile and .dockerignore file +DEB_PKG="{{name}}_${VERSION}_amd64.deb" +DEPENDS=`dpkg-deb -f "$DEB_PKG" Depends` +if ! echo "$DEPENDS" | grep python; then + DEPENDS="$DEPENDS python3" +fi +sed "s/DEB_PKG/$DEB_PKG/; s/DEPENDS/$DEPENDS/" Dockerfile.template > Dockerfile + +if [ -e .dockerignore ]; then + sed -i 2s/'.*'/!$DEB_PKG/ .dockerignore +else + cat > .dockerignore < /opt dir > /usr dir -prefer python3 over python2.7, - -""" - -from __future__ import print_function - -import os - - -def first_existing_file(files): - """return the first existing file in given list. - - """ - for f in files: - if os.path.exists(os.path.expanduser(f)): - return f - - -def main(): - print(first_existing_file([ - "~/bin/python3", - "~/bin/python2.7", - "~/bin/python", - "~/opt/bin/python3", - "~/opt/bin/python2.7", - "/opt/python3.5/bin/python", - "/usr/bin/python3", - "/opt/python2.7/bin/python", - "/usr/bin/python2.7", - "/usr/bin/python", - ])) - - -if __name__ == '__main__': - main() diff --git a/src/leiningen/new/python/utils/choose_default_python.sh b/src/leiningen/new/python/utils/choose_default_python.sh new file mode 100755 index 0000000..acdc733 --- /dev/null +++ b/src/leiningen/new/python/utils/choose_default_python.sh @@ -0,0 +1,54 @@ +#!/bin/sh +set -e + +print_help_and_exit() { + echo "Usage: choose_default_python.sh +choose a default python command for creating virtualenv. + +Current preferences is: +python in /home dir > /opt dir > /usr dir +prefer python3 over python2.7, +" + exit 1 +} + +# main() +if [ "$1" = "--help" ]; then + print_help_and_exit +fi + +first_existing_file() +{ + for f in "$@" + do + if [ -e "$f" ]; then + re="$f" + return 0 + fi + done + re="" + return 1 +} + +# main() +if first_existing_file \ + ~/bin/python3 \ + ~/opt/bin/python3 \ + /opt/python3.7/bin/python \ + /opt/python3.6/bin/python \ + /opt/python3.5/bin/python \ + /usr/local/bin/python3 \ + /usr/bin/python3 \ + ~/bin/python2.7 \ + ~/bin/python \ + ~/opt/bin/python2.7 \ + /opt/python2.7/bin/python \ + /usr/bin/python2.7 \ + /usr/bin/local/python2.7 \ + /usr/bin/local/python \ + /usr/bin/python2.7 \ + /usr/bin/python; then + echo "$re" +else + echo "Error: failed to locate python" > /dev/stderr +fi -- GitLab