From 89c1f4bafb51dbbed705b6960810d90825318b13 Mon Sep 17 00:00:00 2001 From: Benjamin Klimczak Date: Fri, 16 Sep 2022 18:09:32 +0100 Subject: MLIA-646 Add environment to setup pre-commit hooks - Add new environment 'lint_setup' to set up the pre-commit hooks used by the environment 'lint' without running the actual tests. - Add docker file for linting and testing to the project root. - Add helper script 'install_python_versions.sh' for docker creation. Change-Id: I5f264df72a72d7a118ab798eddcf0febd4e1125b --- Dockerfile | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Dockerfile (limited to 'Dockerfile') 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 -- cgit v1.2.1