<commit_before>{ lib
, python3
, fetchurl
, zlib
, mkYarnModules
, sphinx
, nixosTests
, pkgs
}:

let

  pname = "pgadmin";
  version = "6.9";

  src = fetchurl {
    url = "https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v${version}/source/pgadmin4-${version}.tar.gz";
    sha256 = "sha256-C/0UDbPUuKCF9BBrDdlETGl7ALpFOmGaaxHVHxrIPVo=";
  };

  yarnDeps = mkYarnModules {
    pname = "${pname}-yarn-deps";
    inherit version;
    packageJSON = ./package.json;
    yarnLock = ./yarn.lock;
    yarnNix = ./yarn.nix;
  };

  # move buildDeps here to easily pass to test suite
  buildDeps = with pythonPackages; [
    flask
    flask-gravatar
    flask_login
    flask_mail
    flask_migrate
    flask_sqlalchemy
    flask-wtf
    flask-compress
    passlib
    pytz
    simplejson
    six
    sqlparse
    wtforms
    flask-paranoid
    psutil
    psycopg2
    python-dateutil
    sqlalchemy
    itsdangerous
    flask-security-too
    bcrypt
    cryptography
    sshtunnel
    ldap3
    flask-babelex
    flask-babel
    gssapi
    flask-socketio
    eventlet
    httpagentparser
    user-agents
    wheel
    authlib
    qrcode
    pillow
    pyotp
    botocore
    boto3
  ];

  # override necessary on pgadmin4 6.9
  pythonPackages = python3.pkgs.overrideScope (final: prev: rec {
    flask = prev.flask.overridePythonAttrs (oldAttrs: rec {
      version = "2.0.3";
      src = oldAttrs.src.override {
        inherit version;
        sha256 = "sha256-4RIMIoyi9VO0cN9KX6knq2YlhGdSYGmYGz6wqRkCaH0=";
      };
      disabledTests = (oldAttrs.disabledTests or [ ]) ++ [
        "test_aborting"
      ];
    });
    flask-paranoid = prev.flask-paranoid.overridePythonAttrs (oldAttrs: rec {
      # tests fail due to downgrades here
      doCheck = false;
    });
    werkzeug = prev.werkzeug.overridePythonAttrs (oldAttrs: rec {
      version = "2.0.3";
      src = oldAttrs.src.override {
        inherit version;
        sha256 = "sha256-uGP4/wV8UiFktgZ8niiwQRYbS+W6TQ2s7qpQoWOCLTw=";
      };
    });
  });

in

pythonPackages.buildPythonApplication rec {
  inherit pname version src;

  # from Dockerfile
  CPPFLAGS = "-DPNG_ARM_NEON_OPT=0";

  format = "setuptools";

  patches = [
    # Expose setup.py for later use
    ./expose-setup.py.patch
  ];

  postPatch = ''
    # patching Makefile, so it doesn't try to build sphinx documentation here
    # (will do so later)
    substituteInPlace Makefile --replace 'LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $(MAKE) -C docs/en_US -f Makefile.sphinx html' "true"
    # fix document which refers a non-existing document and fails
    substituteInPlace docs/en_US/contributions.rst --replace "code_snippets" ""
    patchShebangs .
    # relax dependencies
    substituteInPlace requirements.txt \
      --replace "psycopg2==2.9.*" "psycopg2>=2.9" \
      --replace "cryptography==3.*" "cryptography>=3.0" \
      --replace "requests==2.25.*" "requests>=2.25.0" \
      --replace "boto3==1.20.*" "boto3>=1.20" \
      --replace "botocore==1.23.*" "botocore>=1.23" \
      --replace "pytz==2021.*" "pytz" \
      --replace "Werkzeug==2.0.3" "werkzeug>=2.*"
    # don't use Server Mode (can be overridden later)
    substituteInPlace pkg/pip/setup_pip.py \
      --replace "req = req.replace('psycopg2', 'psycopg2-binary')" "req = req" \
      --replace "builtins.SERVER_MODE = None" "builtins.SERVER_MODE = False"
  '';

  preBuild = ''
    # Adapted from pkg/pip/build.sh
    echo Creating required directories...
    mkdir -p pip-build/pgadmin4/docs

    # build the documentation
    cd docs/en_US
    ${sphinx}/bin/sphinx-build -W -b html -d _build/doctrees . _build/html

    # Build the clean tree
    cd ../../web
    cp -r * ../pip-build/pgadmin4
    cd ../docs
    cp -r * ../pip-build/pgadmin4/docs
    for DIR in `ls -d ??_??/`
    do
      if [ -d ''${DIR}_build/html ]; then
          mkdir -p ../pip-build/pgadmin4/docs/''${DIR}_build
          cp -Rv ''${DIR}_build/html ../pip-build/pgadmin4/docs/''${DIR}_build
      fi
    done
    cd ../

    cp -r ${yarnDeps}/* pip-build/pgadmin4

    echo Creating distro config...
    echo HELP_PATH = \'../../docs/en_US/_build/html/\' > pip-build/pgadmin4/config_distro.py
    echo MINIFY_HTML = False >> pip-build/pgadmin4/config_distro.py

    echo Creating manifest...
    echo recursive-include pgadmin4 \* > pip-build/MANIFEST.in

    echo Building wheel...
    cd pip-build
    # copy non-standard setup.py to local directory
    # so setuptools-build-hook can call it
    cp -v ../pkg/pip/setup_pip.py setup.py
  '';

  nativeBuildInputs = with pythonPackages; [ cython pip ];
  buildInputs = [
    zlib
    pythonPackages.wheel
  ];

  # tests need an own data, log directory
  # and a working and correctly setup postgres database
  # checks will be run through nixos/tests
  doCheck = false;

  # speaklater3 is seperate because when passing buildDeps
  # to the test, it fails there due to a collision with speaklater
  propagatedBuildInputs = buildDeps ++ [ pythonPackages.speaklater3 ];

  passthru.tests = {
    standalone = nixosTests.pgadmin4-standalone;
    # regression and function tests of the package itself
    package = (import ../../../../nixos/tests/pgadmin4.nix ({ inherit pkgs; buildDeps = buildDeps; pythonEnv = pythonPackages; }));
  };

  meta = with lib; {
    description = "Administration and development platform for PostgreSQL";
    homepage = "https://www.pgadmin.org/";
    license = licenses.mit;
    changelog = "https://www.pgadmin.org/docs/pgadmin4/latest/release_notes_${lib.versions.major version}_${lib.versions.minor version}.html";
    maintainers = with maintainers; [ gador ];
    mainProgram = "pgadmin4";
  };
}<commit_msg>pgadmin: unpin eventlet dependency, cleanup<commit_after>{ lib
, python3
, fetchurl
, zlib
, mkYarnModules
, sphinx
, nixosTests
, pkgs
}:

let
  pname = "pgadmin";
  version = "6.9";

  src = fetchurl {
    url = "https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v${version}/source/pgadmin4-${version}.tar.gz";
    sha256 = "sha256-C/0UDbPUuKCF9BBrDdlETGl7ALpFOmGaaxHVHxrIPVo=";
  };

  yarnDeps = mkYarnModules {
    pname = "${pname}-yarn-deps";
    inherit version;
    packageJSON = ./package.json;
    yarnLock = ./yarn.lock;
    yarnNix = ./yarn.nix;
  };

  # move buildDeps here to easily pass to test suite
  buildDeps = with pythonPackages; [
    flask
    flask-gravatar
    flask_login
    flask_mail
    flask_migrate
    flask_sqlalchemy
    flask-wtf
    flask-compress
    passlib
    pytz
    simplejson
    six
    sqlparse
    wtforms
    flask-paranoid
    psutil
    psycopg2
    python-dateutil
    sqlalchemy
    itsdangerous
    flask-security-too
    bcrypt
    cryptography
    sshtunnel
    ldap3
    flask-babelex
    flask-babel
    gssapi
    flask-socketio
    eventlet
    httpagentparser
    user-agents
    wheel
    authlib
    qrcode
    pillow
    pyotp
    botocore
    boto3
  ];

  # override necessary on pgadmin4 6.9
  pythonPackages = python3.pkgs.overrideScope (final: prev: rec {
    flask = prev.flask.overridePythonAttrs (oldAttrs: rec {
      version = "2.0.3";
      src = oldAttrs.src.override {
        inherit version;
        sha256 = "sha256-4RIMIoyi9VO0cN9KX6knq2YlhGdSYGmYGz6wqRkCaH0=";
      };
      disabledTests = (oldAttrs.disabledTests or [ ]) ++ [
        "test_aborting"
      ];
    });
    flask-paranoid = prev.flask-paranoid.overridePythonAttrs (oldAttrs: rec {
      # tests fail due to downgrades here
      doCheck = false;
    });
    werkzeug = prev.werkzeug.overridePythonAttrs (oldAttrs: rec {
      version = "2.0.3";
      src = oldAttrs.src.override {
        inherit version;
        sha256 = "sha256-uGP4/wV8UiFktgZ8niiwQRYbS+W6TQ2s7qpQoWOCLTw=";
      };
    });
  });

in

pythonPackages.buildPythonApplication rec {
  inherit pname version src;

  # from Dockerfile
  CPPFLAGS = "-DPNG_ARM_NEON_OPT=0";

  format = "setuptools";

  patches = [
    # Expose setup.py for later use
    ./expose-setup.py.patch
  ];

  postPatch = ''
    # patching Makefile, so it doesn't try to build sphinx documentation here
    # (will do so later)
    substituteInPlace Makefile \
      --replace 'LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $(MAKE) -C docs/en_US -f Makefile.sphinx html' "true"

    # fix document which refers a non-existing document and fails
    substituteInPlace docs/en_US/contributions.rst \
      --replace "code_snippets" ""
    patchShebangs .

    # relax dependencies
    substituteInPlace requirements.txt \
      --replace "eventlet==0.33.0" "eventlet>=0.33.0" \
      --replace "psycopg2==2.9.*" "psycopg2>=2.9" \
      --replace "cryptography==3.*" "cryptography>=3.0" \
      --replace "requests==2.25.*" "requests>=2.25.0" \
      --replace "boto3==1.20.*" "boto3>=1.20" \
      --replace "botocore==1.23.*" "botocore>=1.23" \
      --replace "pytz==2021.*" "pytz" \
      --replace "Werkzeug==2.0.3" "werkzeug>=2.*"
    # don't use Server Mode (can be overridden later)
    substituteInPlace pkg/pip/setup_pip.py \
      --replace "req = req.replace('psycopg2', 'psycopg2-binary')" "req = req" \
      --replace "builtins.SERVER_MODE = None" "builtins.SERVER_MODE = False"
  '';

  preBuild = ''
    # Adapted from pkg/pip/build.sh
    echo Creating required directories...
    mkdir -p pip-build/pgadmin4/docs

    # build the documentation
    cd docs/en_US
    ${sphinx}/bin/sphinx-build -W -b html -d _build/doctrees . _build/html

    # Build the clean tree
    cd ../../web
    cp -r * ../pip-build/pgadmin4
    cd ../docs
    cp -r * ../pip-build/pgadmin4/docs
    for DIR in `ls -d ??_??/`
    do
      if [ -d ''${DIR}_build/html ]; then
          mkdir -p ../pip-build/pgadmin4/docs/''${DIR}_build
          cp -Rv ''${DIR}_build/html ../pip-build/pgadmin4/docs/''${DIR}_build
      fi
    done
    cd ../

    cp -r ${yarnDeps}/* pip-build/pgadmin4

    echo Creating distro config...
    echo HELP_PATH = \'../../docs/en_US/_build/html/\' > pip-build/pgadmin4/config_distro.py
    echo MINIFY_HTML = False >> pip-build/pgadmin4/config_distro.py

    echo Creating manifest...
    echo recursive-include pgadmin4 \* > pip-build/MANIFEST.in

    echo Building wheel...
    cd pip-build
    # copy non-standard setup.py to local directory
    # so setuptools-build-hook can call it
    cp -v ../pkg/pip/setup_pip.py setup.py
  '';

  nativeBuildInputs = with pythonPackages; [ cython pip ];
  buildInputs = [
    zlib
    pythonPackages.wheel
  ];

  # tests need an own data, log directory
  # and a working and correctly setup postgres database
  # checks will be run through nixos/tests
  doCheck = false;

  # speaklater3 is seperate because when passing buildDeps
  # to the test, it fails there due to a collision with speaklater
  propagatedBuildInputs = buildDeps ++ [ pythonPackages.speaklater3 ];

  passthru.tests = {
    standalone = nixosTests.pgadmin4-standalone;
    # regression and function tests of the package itself
    package = import ../../../../nixos/tests/pgadmin4.nix { inherit pkgs buildDeps; pythonEnv = pythonPackages; };
  };

  meta = with lib; {
    description = "Administration and development platform for PostgreSQL";
    homepage = "https://www.pgadmin.org/";
    license = licenses.mit;
    changelog = "https://www.pgadmin.org/docs/pgadmin4/latest/release_notes_${lib.versions.major version}_${lib.versions.minor version}.html";
    maintainers = with maintainers; [ gador ];
    mainProgram = "pgadmin4";
  };
}
{'fraction_non_alphanumeric': 0.12416870843542177, 'fraction_numerical': 0.020126006300315014, 'mean_word_length': 2.6042257962787763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '957487', 'n_tokens_mistral': 4236, 'n_tokens_neox': 4046, 'n_words': 882}