## START: Set by rpmautospec
## (rpmautospec version 0.3.5)
## RPMAUTOSPEC: autorelease, autochangelog
%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
    release_number = 2;
    base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));
    print(release_number + base_release_number - 1);
}%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}
## END: Set by rpmautospec

%global forgeurl https://github.com/BlueBrain/BluePyOpt

# versioneer is used, so no tags for patch versions
# use git tar since pypi does not include examples that are needed for tests.
# Upstream tags with every push to master (why?)
# Use commit found in bluepyopt/_version.py of the PyPI release
%global commit 6aa893757e50a96a7e638d1c72a04f97424a2a21

%bcond_with tests

%global _description %{expand:
The Blue Brain Python Optimisation Library (BluePyOpt) is an extensible
framework for data-driven model parameter optimisation that wraps and
standardises several existing open-source tools. It simplifies the task of
creating and sharing these optimisations, and the associated techniques and
knowledge. This is achieved by abstracting the optimisation and evaluation
tasks into various reusable and flexible discrete elements according to
established best-practices.}

Name: python-bluepyopt
Version: 1.14.3
Release: %autorelease
Summary: Bluebrain Python Optimisation Library (bluepyopt)

# This package is not noarch because it's tests are arch dependent but it does
# not install any arch dependent files and so does not generate debuginfo
%global debug_package %{nil}

# pyedflib excludes s390x, so all deps also exclude it
# https://fedoraproject.org/wiki/Changes/EncourageI686LeafRemoval
ExcludeArch:    s390x %{ix86}

License: LGPL-3.0-only

%forgemeta

URL: %forgeurl
Source0: %forgesource
# use _version file from pypi tar to trick versioneer
Source1: _version.py

# Update Versioneer to fix AttributeError in configparser
# https://github.com/BlueBrain/BluePyOpt/pull/466
Patch:          https://github.com/BlueBrain/BluePyOpt/pull/466.patch

# Fix exclusion in setup.py (these are globs)
# https://github.com/BlueBrain/BluePyOpt/pull/467
Patch:          https://github.com/BlueBrain/BluePyOpt/pull/467.patch

# Not all requirements are listed, so we need to use explicit BRs also
BuildRequires:  python3-devel
# Required to compile neuron based models
BuildRequires:  neuron-devel
BuildRequires:  gcc-c++
BuildRequires:  libX11-devel
BuildRequires:  libXext-devel
BuildRequires:  %{py3_dist matplotlib}

# To run tests
%if %{with tests}
BuildRequires:  %{py3_dist future}
BuildRequires:  python3-jupyter-client
BuildRequires:  python3-nbconvert
BuildRequires:  %{py3_dist mock}
BuildRequires:  python3-neuron
BuildRequires:  %{py3_dist pytest}
BuildRequires:  %{py3_dist lfpy}
BuildRequires:  %{py3_dist papermill}
BuildRequires:  %{py3_dist meautility}
%endif

%description %_description

%package -n python3-bluepyopt
Summary:        %{summary}

# Only need to list ones not listed in setup.py
Requires: neuron-devel
Requires: python3-neuron
Requires: python3dist(setuptools)

%description -n python3-bluepyopt %_description

%package -n python3-bluepyopt-doc
BuildArch:      noarch
Summary:        Documentation for bluepyopt


%description -n python3-bluepyopt-doc %_description

%prep
%forgeautosetup -p1
cp -v %{SOURCE1} "bluepyopt/_version.py"

# Optional dependency, remove so that automatic dep generator does not pick it up
sed -i '/scoop/ d' setup.py

# For tests, we install jupyter as BuildRequires
# remove all Makefile deps on the jupyter target
# need to check this for each update, in case the makefile changes
sed -i 's/^\(.*:.*\)jupyter$/\1/' Makefile
# convert python to python3
sed -i 's/python l5pc_validate_neuron_arbor_pm.py/python3 l5pc_validate_neuron_arbor_pm.py/g' Makefile

# remove neuroml test script: pyneuroml cannot be packaged for Fedora because of java issues
rm -f bluepyopt/tests/test_neuroml_fcts.py



# Remove gitignore files in the examples
find examples/ -name ".gitignore" -delete
# Remove dummy files that keep the folder tracked in git for upstream
find examples/ -name "dummy.inc" -delete

# Correct shebangs
find examples/ -type f -name "*.py" -exec sed -i 's|^#![  ]*/usr/bin/env.*$|#!/usr/bin/python3/|' '{}' 2>/dev/null \;
find examples/ -type f -name "*.py" -exec chmod -x '{}' \;

# Correct end of line encodings
find examples/ -type f -name "*.mod" -exec sed -i 's/\r$//' '{}' \;
find examples/ -type f -name "*.asc" -exec sed -i 's/\r$//' '{}' \;
find examples/ -type f -name "*.csv" -exec sed -i 's/\r$//' '{}' \;

# Makefile hard codes x86_64
# Tests also hard code the platform
# Need to make sure it matches the arch used in NEURON
# Macros don't work on 32bit builds, where we may get 1686 packages on i386 etc.
export MODSUBDIR=$(grep "^MODSUBDIR" /usr/bin/nrnivmodl | cut -d "=" -f2)
sed -i "s/x86_64/$MODSUBDIR/" Makefile
sed -i "s/x86_64/$MODSUBDIR/" bluepyopt/tests/test_l5pc.py
sed -i "s/x86_64/$MODSUBDIR/" bluepyopt/tests/test_stochkv.py

%generate_buildrequires
%pyproject_buildrequires %{?with_tests: -r}

%build
%pyproject_wheel

%install
%pyproject_install
%pyproject_save_files bluepyopt

%check
%pyproject_check_import -e *neuroml* -e bluepyopt.tests*
%if %{with tests}
# Prepare for tests
# Refer to: https://github.com/BlueBrain/BluePyOpt/blob/master/tox.ini
# and https://github.com/BlueBrain/BluePyOpt/blob/master/Makefile
%{py3_test_envvars} %make_build \
    stochkv_prepare l5pc_prepare sc_prepare meta_prepare
# test fail with a very slight approximation error
# neuroml test requires pyneuroml which cannot be included in fedora because of java things
k="not test_metaparameter and not test_NrnRampPulse_instantiate"
k="$k and not test_DEAPOptimisation_run and not test_DEAPOptimisation_run_from_parents"
k="$k and not test_optimisationsCMA_SO_run and not test_optimisationsCMA_MO_run"
# arbor and other related tests fail
k="$k and not test_arbor_labels"
k="$k and not test_create_acc and not test_create_acc_replace_axon and not test_cell_model_write_and_read_acc and not test_cell_model_write_and_read_acc_replace_axon and not test_write_acc_simple and not test_CellEvaluator_evaluate and not test_sequenceprotocol_run and not test_sweepprotocol_run_isolated and not test_LFPySquarePulse_instantiate and not test_nrnsimulator_cvode_minstep and not test_distloc_exception"
# need pebble 4.6+: disable temporarily
k="$k and not test_exec and not test_eval and not test_l5pc_validate_neuron_arbor and not "
%{pytest} bluepyopt/tests/ -m "unit and not neuroml" "${k:+-k $k}"
%{pytest} bluepyopt/tests/ -m "not unit and not neuroml" "${k:+-k $k}"
# clean up whatever files were temporarily generated for tests
make clean
%endif

%files -n python3-bluepyopt -f %{pyproject_files}
%doc README.rst
%{_bindir}/bpopt_tasksdb

%files -n python3-bluepyopt-doc
%license COPYING COPYING.lesser
%doc examples

%changelog
* Wed Oct 18 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 1.14.3-2
- F38+: Use %%%%{py3_test_envvars} to set up test environments

* Sun Sep 10 2023 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.14.3-1
- feat: update to 1.14.3 (fixes rh#2233456)

* Sat Jul 29 2023 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.14.0-2
- feat: limit tests to import checks (fixes rhbz#2220136, rhbz#2226532,
  rhbz#2145122)

* Fri Jul 21 2023 Sandro <devel@penguinpee.nl> - 1.14.0-1
- Update to 1.14.0 (RHBZ#2145122)
- Remove unused patch (merged upstream)
- Add patch for updating Versioneer (RHBZ#2220136)
- Add patch for excluding examples/
- Update bundled _version.py

* Fri Jul 21 2023 Fedora Release Engineering <releng@fedoraproject.org> - 1.13.3-6
- Rebuilt for https://fedoraproject.org/wiki/Fedora_39_Mass_Rebuild

* Mon Feb 20 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 1.13.3-5
- Work around numpy.int/numpy.float deprecation

* Fri Feb 03 2023 Benjamin A. Beasley <code@musicinmybrain.net> - 1.13.3-4
- Leaf package on ix86: drop ix86 support

* Fri Jan 20 2023 Fedora Release Engineering <releng@fedoraproject.org> - 1.13.3-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_38_Mass_Rebuild

* Sun Oct 09 2022 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.13.3-2
- feat: exclude s390x

* Sun Oct 09 2022 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.13.3-1
- feat: update to 1.13.3 (fixes rh#2059645)

* Fri Jul 22 2022 Fedora Release Engineering <releng@fedoraproject.org> - 1.11.15-5
- Rebuilt for https://fedoraproject.org/wiki/Fedora_37_Mass_Rebuild

* Tue Jun 21 2022 Python Maint <python-maint@redhat.com> - 1.11.15-4
- Rebuilt for Python 3.11

* Wed Feb 09 2022 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.11.15-3
- chore: bump for branch

* Tue Feb 08 2022 Ankur Sinha (Ankur Sinha Gmail) <sanjay.ankur@gmail.com> - 1.11.15-2
- bump

* Fri Jul 23 2021 Fedora Release Engineering <releng@fedoraproject.org> - 1.10.36-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_35_Mass_Rebuild

* Fri Jun 04 2021 Python Maint <python-maint@redhat.com> - 1.10.36-3
- Rebuilt for Python 3.10

* Thu May 27 2021 Ankur Sinha <ankursinha AT fedoraproject DOT org> - 1.10.36-2
- make package archful, since tests are arch dependent

* Thu May 27 2021 Ankur Sinha <ankursinha AT fedoraproject DOT org> - 1.10.36-1
- ignore non-unit tests, these fail on ppc64le etc.

* Thu May 27 2021 Ankur Sinha <ankursinha AT fedoraproject DOT org> - 1.10.36-1
- Update to latest release
- Use pytest and enable tests by default

* Wed Apr 14 2021 Ankur Sinha <ankursinha AT fedoraproject DOT org> - 1.9.149-1
- Correct pyprovide macro
- Include examples
- Split examples to sub-package

* Sun Apr 11 2021 Ankur Sinha <ankursinha AT fedoraproject DOT org> - 1.9.149-1
- Update to latest release

* Mon Jul 06 2020 Anil Tuncel <tuncel.manil@gmail.com> - 1.9.48-1
- Move neuron requirement to subpackage
- Enable tests
- Use github tar since pypi tar does not include examples

* Thu Jun 25 2020 Anil Tuncel <tuncel.manil@gmail.com> - 1.9.48-1
- Removed INSTALLED_FILES method
- Updated file checks
- Added check to run tests
- Removed the %%clean tag
- use autosetup, py3_build, py3_install
- use pypi_source macro
- removed deprecated release, vengor and group tags

* Wed Jun 17 2020 Anil Tuncel <tuncel.manil@gmail.com> - 1.9.48-1
- Initial package generated using python setup.py bdist --formats=rpm