# DEV: Use `debian:slim` instead of an `alpine` image to support installing wheels from PyPI
#      this drastically improves test execution time since python dependencies don't all
#      have to be built from source all the time (grpcio takes forever to install)
FROM debian:bookworm-slim
# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8

# https://support.circleci.com/hc/en-us/articles/360045268074-Build-Fails-with-Too-long-with-no-output-exceeded-10m0s-context-deadline-exceeded-
ENV PYTHONUNBUFFERED=1
# Configure PATH environment for pyenv
ENV PYTHON_SOURCE=/root/python_source
ENV PYTHON_DEBUG=/root/env/python_debug
ENV PATH=$PATH:${PYTHON_DEBUG}/bin
ENV PYTHON_CONFIGURE_OPTS=--enable-shared

RUN \
  # Install system dependencies
  apt-get update \
  && apt-get install -y --no-install-recommends \
      apt-transport-https \
      build-essential \
      ca-certificates \
      clang-format \
      curl \
      git \
      gnupg \
      jq \
      libbz2-dev \
      libffi-dev \
      liblzma-dev \
      libmemcached-dev \
      libncurses5-dev \
      libncursesw5-dev \
      libpq-dev \
      libreadline-dev \
      libsasl2-dev \
      libsqlite3-dev \
      libsqliteodbc \
      libssh-dev \
      libssl-dev \
      patch \
      unixodbc-dev \
      wget \
      zlib1g-dev \
      valgrind \
      gawk \
      wget \
      software-properties-common

# Install pyenv and necessary Python versions
# `--with-pydebug`: [Add options](https://docs.python.org/3/using/configure.html#python-debug-build) like count references, sanity checks...
# `--with-valgrind`: Enable Valgrind support (default is no).
# `--without-pymalloc`: Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short lifetime. We remove this functionality to not hide errors
RUN git clone --depth 1 --branch v3.11.6 https://github.com/python/cpython/ "${PYTHON_SOURCE}" \
  && cd ${PYTHON_SOURCE} \
  && ./configure --with-pydebug --without-pymalloc --with-valgrind --prefix ${PYTHON_DEBUG} \
  && make OPT=-g \
  && make install \
  && cd -

RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3.11d get-pip.py --break-system-packages

# Verify Python and pip installation
RUN python3.11d --version && python3.11d -m pip --version

RUN python3.11d -c "import sys;sys.gettotalrefcount()"


RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain stable -y
ENV PATH="/root/.cargo/bin:$PATH"

RUN python3.11d -m pip install --break-system-packages setuptools cython wheel cmake pytest pytest-cov hypothesis pytest-memray\
    "memray==1.14.0" \
    "requests==2.31.0" \
    "bytecode>=0.14.0" \
    "envier~=0.5" \
    "opentelemetry-api>=1" \
    "protobuf>=3" \
    typing_extensions \
    "xmltodict>=0.12" \
    setuptools-rust \
    wrapt


CMD ["/bin/bash"]
#docker build . -f docker/Dockerfile_py311_debug_mode -t python_311_debug
#docker run --rm -it -v ${PWD}:/ddtrace python_311_debug
#
# Now, you can check IAST leaks:
#cd /ddtrace
#export PATH=$PATH:$PWD
#export PYTHONPATH=$PYTHONPATH:$PWD
#export PYTHONMALLOC=malloc
#python3.11 ddtrace/appsec/_iast/leak.py
#python3.11 -m memray run --trace-python-allocators --native -o lel.bin -f prueba.py
#python3.11 -m memray flamegraph lel.bin --leaks -f
