diff options
l--------- | .dockerignore | 1 | ||||
-rw-r--r-- | Dockerfile | 110 | ||||
-rwxr-xr-x | docker/install_python_versions.sh | 37 | ||||
-rw-r--r-- | tox.ini | 9 |
4 files changed, 154 insertions, 3 deletions
diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 0000000..3e4e48b --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore
\ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e29d611 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright (c) 2013-2019 Yamashita, Yuu +# SPDX-FileCopyrightText: Copyright (c) 2013 Sam Stephenson +# SPDX-License-Identifier: Apache-2.0 AND MIT + +# Execution environment for self-check and tests of this repository. +# The build context should be the MLIA repository. +# +# Example - Build the image with Python 3.10 installed: +# +# docker build \ +# --build-arg UID="$(id -u)" \ +# --build-arg GID="$(id -g)" \ +# --build-arg PYTHON_VERSIONS="3.10" \ +# --build-arg BASE_IMAGE="ubuntu:20.04" \ +# -t "mlia-test" \ +# . +# +# Example - Run the linters in the container: +# +# docker run --rm --user "$(id -u):$(id -g)" --pid=host \ +# -v "$PWD:/workspace" \ +# -w "/workspace" \ +# "mlia-test" \ +# tox --workdir /home/foo/tox/ -e lint + +ARG BASE_IMAGE=ubuntu:20.04 + +FROM ${BASE_IMAGE} + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y \ + git \ + locales \ + ruby-dev \ + shellcheck \ + cmake \ + # Dependencies required by pyenv to build Python + make \ + build-essential \ + libssl-dev \ + zlib1g-dev \ + libbz2-dev \ + libreadline-dev \ + libsqlite3-dev \ + wget \ + curl \ + llvm \ + libncursesw5-dev \ + xz-utils \ + tk-dev \ + libxml2-dev \ + libxmlsec1-dev \ + libffi-dev \ + liblzma-dev \ + && rm -rf /var/lib/apt/lists/* + +RUN locale-gen en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + +ARG UID +ARG GID + +RUN groupadd -g ${GID} -o foo +RUN useradd -m -l -u ${UID} -g foo foo + +USER foo +ENV HOME=/home/foo +ENV USER=foo +ENV PATH="/home/foo/.local/bin:${PATH}" + +# Install pyenv +ENV PYENV_GIT_TAG v2.3.3 +RUN curl https://pyenv.run | bash + +ENV PYENV_ROOT /home/foo/.pyenv +ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH + +# Python versions separated by semicolons. E.g. "3.8;3.9" +ARG PYTHON_VERSIONS +# Install Python versions and set them to be available globally +COPY docker/install_python_versions.sh /home/foo +RUN /home/foo/install_python_versions.sh "${PYTHON_VERSIONS}" --set-all-global + +# Install tox +RUN pip3 install tox + +# Create a temporary mock MLIA repository to setup the tox and pre-commit +# environments. Copy only relevant files to facilitate caching. +ENV TMP_REPO /tmp/mlia/ +RUN mkdir $TMP_REPO +WORKDIR $TMP_REPO +RUN git init \ + && mkdir src \ + && touch README.md +COPY pyproject.toml setup.cfg setup.py tox.ini ./ + +# Create tox environment for linting (as this takes most time to set up) in a +# specific cache directory so that it is cached in the docker image. +ENV TOX_WORK_DIR $HOME/tox +RUN tox --workdir $TOX_WORK_DIR --notest --recreate -e lint +# Set up pre-commit hooks to cache the hook environments +COPY .pre-commit-config.yaml . +RUN tox --workdir $TOX_WORK_DIR -e lint_setup + +WORKDIR $HOME +RUN rm -r $TMP_REPO diff --git a/docker/install_python_versions.sh b/docker/install_python_versions.sh new file mode 100755 index 0000000..c3cd976 --- /dev/null +++ b/docker/install_python_versions.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 + +set -e +set -u +set -o pipefail + +# Set one or multiple python versions through pyenv to be available globally +function set_python_global_versions() { + py_versions=$1 + parsed_versions="" + + # Find correct parsed subversion for each provided version + for i in $(echo "$py_versions" | tr ";" "\n") + do + parsed_versions+=$(pyenv versions | grep "$i") + parsed_versions+=" " + done + # Set global versions + echo "$parsed_versions" | xargs pyenv global +} + +# Install all python versions provided as parameter using pyenv +# if the --set-all-global flag is set, make all installed versions +# available globally. +py_versions=$1 +parameters=$2 +for i in $(echo "$py_versions" | tr ";" "\n") +do + pyenv install "$i":latest +done + +if [ "$parameters" == "--set-all-global" ]; then + set_python_global_versions "$1" +fi @@ -19,15 +19,18 @@ deps = commands = pytest --cov=mlia --cov-report term-missing --cov-fail-under=95 tests/ -[testenv:lint] -description = Run the pre-commit hooks. +[testenv:{lint,lint_setup}] +description = Run and setup the pre-commit hooks. +# Re-use the same env for both lint and lint_setup +envdir={toxworkdir}/lint usedevelop = True extras = dev # Workaround to resolve an issue with markdownlint in a docker environment passenv = HOME commands = - pre-commit run --all-files --hook-stage=push {posargs} + lint_setup: pre-commit install-hooks + lint: pre-commit run --all-files --hook-stage=push {posargs} [testenv:build] description = Build a wheel file (platform name as optional argument [manylinux2014_aarch64, manylinux2014_x86_64]). |