##
##  jupyter.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package versions
%define       V_jupyter_base  4.11.1
%define       V_jupyter_snap  20220709

#   package information
Name:         jupyter
Summary:      Interactive Notebooks
URL:          https://jupyter.org/
Vendor:       IPython Development Team, Jupyter Development Team
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      BSD
Version:      %{V_jupyter_base}.%{V_jupyter_snap}
Release:      20220710

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/jupyter/jupyter-%{V_jupyter_snap}.tar.xz

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  python, python-setup
PreReq:       python, python-setup
BuildPreReq:  zmq
PreReq:       zmq

%description
    The Jupyter Notebook is a Web application that allows you to
    create and share documents that contain live code, equations,
    visualizations and narrative text. Uses include: data cleaning and
    transformation, numerical simulation, statistical modeling, data
    visualization, machine learning, and much more.

%track
    prog jupyter:base = {
        version   = %{V_jupyter_base}
        url       = https://pypi.org/project/jupyter_core/
        regex     = jupyter_core-(__VER__)\.tar\.gz
    }
    prog jupyter:snap = {
        version   = %{V_jupyter_snap}
        url       = http://download.openpkg.org/components/versioned/jupyter/
        regex     = jupyter-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n jupyter

%build

%install
    #   install package
    export HOME=`pwd`
    %{l_prefix}/bin/python -m pip \
        install \
        --root=$RPM_BUILD_ROOT \
        --prefix=%{l_prefix} \
        --isolated \
        --ignore-installed \
        --no-warn-script-location \
        *

    #   post-adjust installation
    (   cd $RPM_BUILD_ROOT%{l_prefix}

        #   remove problematic files
        rm -rf lib/python*/site-packages/nbconvert/preprocessors/tests
        rm -f  lib/python*/site-packages/setuptools/command/launcher\ manifest.xml
        rm -f  lib/python*/site-packages/setuptools/script\ \(dev\).tmpl
        rm -rf lib/python*/site-packages/nbclient/tests/files

        #   remove useless files
        rm -rf share/man
        rm -f bin/iptest bin/iptest2 bin/easy* pygmentize

        #   relocate scripts
        mkdir -p lib/jupyter
        mv lib/python/site-packages/* lib/jupyter/
        rm -rf lib/python

        #   relocate binaries
        mkdir -p libexec/jupyter
        mv bin/* libexec/jupyter/
        for exe in `cd libexec/jupyter && ls *`; do
            (   echo "#!/bin/sh"
                echo "PYTHONPATH=\"%{l_prefix}/lib/jupyter:%{l_prefix}/lib/python/site-packages\""
                echo "export PYTHONPATH"
                echo "exec %{l_prefix}/libexec/jupyter/$exe \${1+\"\$@\"}"
            ) >bin/$exe
            chmod a+x bin/$exe
        done
    ) || exit $?

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

