# Allow user site directory to be loaded by OctoPrint. This is needed because
# OctoPrint plugin management relies on "pip --user" command
%undefine _py3_shebang_s

Name:           octoprint
Version:        1.8.6
Release:        1%{?dist}
Summary:        The snappy web interface for your 3D printer

# OctoPrint is AGPL-3.0-only, see THIRDPARTYLICENSES.md for licenses for bundled
# dependencies
License:        AGPL-3.0-only AND Apache-2.0 AND BSD-3-Clause AND GPL-3.0-only AND MIT AND Unlicense
URL:            https://octoprint.org/
Source0:        https://github.com/OctoPrint/OctoPrint/archive/%{version}/%{name}-%{version}.tar.gz
Source1:        %{name}.service
Source2:        %{name}.sysconfig
Source3:        %{name}.sysusers
# Fix dependency versions
Patch0:         %{name}-1.8.6-dependencies.patch
# Disable build dependencies unavailable in Fedora and/or not required to run
# unit tests
Patch1:         %{name}-1.8.6-tests.patch
# Add support for Flask-Limiter >=3
Patch2:         %{name}-1.8.6-flask_limiter_3.patch

BuildRequires:  python3-devel
BuildRequires:  systemd-rpm-macros
# Needed by OctoPrint to provide plugin support
Requires:       %{py3_dist pip}
Suggests:       /usr/bin/ffmpeg
# Bundled dependencies in src/octoprint/vendor/. Most of them are forked from
# upstream
Provides:       bundled(python3dist(awesome-slugify)) = 1.6.5
Provides:       bundled(python3dist(flask-principal)) = 0.4.0
Provides:       bundled(python3dist(sockjs-tornado)) = 1.0.3
# Bundled JS dependencies in src/octoprint/static/js/lib/
Provides:       bundled(js-babel-polyfill) = 7.12.1
Provides:       bundled(js-bootstrap) = 2.3.2
Provides:       bundled(js-bootstrap-modal) = 2.2.5
Provides:       bundled(js-bootstrap-slider) = 2.0.0
Provides:       bundled(js-bootstrap-slider-knockout-binding)
Provides:       bundled(js-bootstrap-tabdrop)
Provides:       bundled(js-detectmobilebrowser)
Provides:       bundled(js-hls)
Provides:       bundled(js-jquery) = 3.5.1
Provides:       bundled(js-jquery-bootstrap-wizard) = 1.4.2
Provides:       bundled(js-jquery-file-upload)
Provides:       bundled(js-jquery-flot) = 0.8.3
Provides:       bundled(js-jquery-qrcode) = 0.18.0
Provides:       bundled(js-jquery-slimscroll) = 1.3.8
Provides:       bundled(js-jquery-ui) = 1.12.1
Provides:       bundled(js-knockout) = 3.5.1
Provides:       bundled(js-less) = 4.1.1
Provides:       bundled(js-lodash) = 3.10.1
Provides:       bundled(js-loglevel) = 1.7.0
Provides:       bundled(js-md5)
Provides:       bundled(js-modernizr) = 3.11.0
Provides:       bundled(js-moment) = 3.11.0
Provides:       bundled(js-pnotify) = 2.1.0
Provides:       bundled(js-pusher-color)
Provides:       bundled(js-sockjs-client) = 1.5.0
Provides:       bundled(js-sprintf) = 1.1.2
Provides:       bundled(js-ua-parser) = 0.7.28
BuildArch:      noarch

%description
OctoPrint provides a snappy web interface for controlling consumer 3D printers.


%prep
%autosetup -n OctoPrint-%{version} -p1

# Remove shebangs
pushd src/%{name}/
for i in __init__.py __main__.py plugins/softwareupdate/scripts/update-octoprint.py; do
   sed '1{\@^#!.*/bin/env python@d}' $i >$i.new &&
   touch -r $i $i.new &&
   mv $i.new $i
done
popd


%generate_buildrequires
%pyproject_buildrequires -r -x develop


%build
%{__python3} setup.py build_ext -i
%pyproject_wheel


%install
%pyproject_install
%pyproject_save_files %{name} %{name}_client %{name}_setuptools

# Workaround to manage lang files within %%{pyproject_files}
while read -r line; do
    [[ "$line" =~ /([^/]+)/LC_MESSAGES/messages.mo$ ]] || continue
    echo "%%lang(${BASH_REMATCH[1]}) $line" >>%{name}.lang
    sed -i "\|$line|d" %{pyproject_files}
done <%{pyproject_files}

install -Dpm 0644 %{SOURCE1} $RPM_BUILD_ROOT%{_unitdir}/%{name}.service
install -Dpm 0644 %{SOURCE2} $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/%{name}
install -dm 0755 $RPM_BUILD_ROOT%{_sharedstatedir}/%{name}/
install -Dpm 0644 %{SOURCE3} $RPM_BUILD_ROOT%{_sysusersdir}/%{name}.conf


%check
# - Don't use %%pytest macro here (pytest calls Python with -s option, which
#   prevents plugin installation tests with pip)
# - Deselect tests which may fail without pytest-doctest-custom (unavailable in
#   Fedora and disabled here; some doctest directives can randomly fail because
#   of non-exact matches in expected outputs, especially with sets results)
PYTHONPATH=$RPM_BUILD_ROOT%{python3_sitelib} %{python3} -m pytest \
    --deselect=src/octoprint/util/comm.py::octoprint.util.comm._normalize_command_handler_result \
    --deselect=tests/filemanager/test_localstorage.py::LocalStorageTest::test_list \
    --deselect=tests/filemanager/test_localstorage.py::LocalStorageTest::test_list_without_recursive


%pre
%sysusers_create_compat %{SOURCE3}


%post
%systemd_post %{name}.service


%preun
%systemd_preun %{name}.service


%postun
%systemd_postun_with_restart %{name}.service


%files -f %{pyproject_files} -f %{name}.lang
%doc AUTHORS.md CHANGELOG.md README.md
%license LICENSE.txt THIRDPARTYLICENSES.md
%{_bindir}/%{name}
%{_unitdir}/%{name}.service
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%attr(-,%{name},%{name}) %dir %{_sharedstatedir}/%{name}/
%{_sysusersdir}/%{name}.conf
# Exclude useless .po files
%exclude %{python3_sitelib}/%{name}/translations/*/LC_MESSAGES/messages.po


%changelog
* Sun Jan 15 2023 Mohamed El Morabity <melmorabity@fedoraproject.org> - 1.8.6-1
- Initial RPM release