File takahe.spec of Package takahe

#
# spec file for package python-takahe
#
# Copyright (c) 2022 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

%define appnameUpper Takahe
%define pythons python310
%define appdir %{_libexecdir}/%{name}
%define sitedir /srv/%{name}
%define appuser _%{name}
#global _default_patch_fuzz 1
Name:           takahe
Version:        0.6.1+git75.fa688a5
Release:        0
Group:          Development/Libraries/Python
Summary:        An ActivityPub/Fediverse server
License:        BSD-3-Clause
URL:            https://jointakahe.org
Source:         _service
Source1:        setup.py
Source2:        system-user-%{name}.conf
Source3:        %{name}.sysconfig
Source4:        %{name}.service
Source5:        %{name}-stator.service
Source6:        %{name}.target
Source7:        README.SUSE
Patch0:         takahe-settings.patch
BuildArch:      noarch
BuildRequires:  fdupes
BuildRequires:  python-rpm-macros
BuildRequires:  %{python_module setuptools}
BuildRequires:  %pythons
BuildRequires:  rsync
BuildRequires:  sysuser-tools
%sysusers_requires
# Requires based on requirements.txt:
Requires:       python3-bleach
Requires:       python3-blurhash-python
Requires:       python3-cryptography
Requires:       python3-dj-database-url
Requires:       python3-django-cache-url
Requires:       python3-django-cors-headers
Requires:       python3-django-htmx
Requires:       python3-django-ninja
Requires:       python3-django-oauth-toolkit
Requires:       python3-django-storages
Requires:       python3-django
Requires:       python3-email-validator
Requires:       python3-gunicorn
Requires:       python3-httpx
Requires:       python3-markdown-it-py
Requires:       python3-Pillow
Requires:       python3-psycopg2
Requires:       python3-pydantic
Requires:       python3-pyld
Requires:       python3-pylibmc
Requires:       python3-pymemcache
Requires:       python3-python-dotenv
Requires:       python3-redis
Requires:       python3-sentry-sdk
Requires:       python3-Sphinx
Requires:       python3-urlman
Requires:       python3-uvicorn
Requires:       python3-whitenoise
# Requires based on packagers choice:
Requires:       python3-base
Requires:       python3-cachetools
Requires:       python3-frozendict
Requires:       python3-lxml
# these have a lot of dependencies on its own - to-do: try to build a package with optional Google/Amazon functionality
Requires:       python3-google-cloud-storage
Requires:       python3-boto3

%description
An experimental Fediverse server for microblogging/"toots".

%prep
%autosetup -p1 -TDn %{_sourcedir}/%{name}-%{version}
cp %{SOURCE1} .
find . -type f -name '*.py' -execdir perl -i -pe 's/^from (activities|api|core|stator|users|mediaproxy|takahe)(\..*)? import/from takahe.\1\2 import/g;s/(?:takahe\.)?(takahe\.wsgi|users\.models|stator\.models|activities\.models|activities\.templatetags|core\.uploads)/takahe.\1/' {} +
find . -type f -name 'apps.py' -execdir sed -i -Ee 's/name = "(\w+)"/name = "takahe.\1"/' {} +
cp %{SOURCE7} .

%build
%python_build
%sysusers_generate_pre %{SOURCE2} ../%{name} system-user-%{name}.conf

%install
install -dm0755 %{buildroot}%{appdir} %{buildroot}%{_sbindir} %{buildroot}%{_sharedstatedir}/%{name} %{buildroot}%{_sysusersdir} %{buildroot}%{sitedir} %{buildroot}%{_fillupdir} %{buildroot}%{_unitdir} %{buildroot}%{_sysconfdir}/%{name}

install -m0644 %{SOURCE2} %{buildroot}%{_sysusersdir}
install -m0644 %{SOURCE3} %{buildroot}%{_fillupdir}/sysconfig.%{name}
install -m0644 %{SOURCE4} %{buildroot}%{_unitdir}
install -m0644 %{SOURCE5} %{buildroot}%{_unitdir}
install -m0644 %{SOURCE6} %{buildroot}%{_unitdir}

for dir in static templates
do
  rsync -r "$dir" '%{buildroot}%{sitedir}'
done

for zerodir in media static-collected
do
  install -dm0755 "%{buildroot}%{sitedir}/$zerodir"
done

tee %{buildroot}%{_sbindir}/%{name}-manage <<EOF
#!/bin/sh
if ! [ "\$(id -u)" = 0 ]
then
  echo 'Please execute this as root.'
  exit 1
fi
su -c "%{appdir}/manage.py \$@" -s '/bin/sh' %{appuser}
EOF

ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-stator

%python_install
%python_expand %fdupes %{buildroot}%{$python_sitelib}

mv %{buildroot}%{_bindir}/manage.py %{buildroot}%{appdir}

for file in settings urls
do
  %python_expand mv %{buildroot}%{$python_sitelib}/%{name}/%{name}/$file.py %{buildroot}%{_sysconfdir}/%{name}
  %python_expand ln -s %{_sysconfdir}/%{name}/$file.py %{buildroot}%{$python_sitelib}/%{name}/%{name}
done

%pre
/usr/sbin/sysusers2shadow system-user-takahe.conf <<"EOF" || [ -f /.buildenv ]
u _takahe - "Takahe" /var/lib/takahe -
EOF
%service_add_pre %{name}.target
%service_add_pre %{name}.service
%service_add_pre %{name}-stator.service

%post
%{fillup_only -n %{name}}
%service_add_post %{name}.target
%service_add_post %{name}.service
%service_add_post %{name}-stator.service

if [ $1 -eq 1 ]
then
  if grep -q 'TAKAHE_SECRET_KEY=__REPLACE_ME__' %{_sysconfdir}/sysconfig/%{name}
  then
    echo 'Generating secret ...'
    SECRET="$(openssl rand -base64 32)"
    sed -i -Ee "s|(TAKAHE_SECRET_KEY)=__REPLACE_ME__|\1=$SECRET|" %{_sysconfdir}/sysconfig/%{name}
  fi
  echo 'Welcome to Takahe!'
  echo 'If this is your first time installing this package, please read the bundled documentation in %{_docdir}/%{name}/README.SUSE before proceeding.'
fi

%preun
%service_del_preun %{name}.target
%service_del_preun %{name}.service
%service_del_preun %{name}-stator.service

%postun
%service_del_postun %{name}.target
%service_del_postun %{name}.service
%service_del_postun %{name}-stator.service

%files
%license LICENSE
%doc README.md README.SUSE
%{_sysusersdir}/system-user-%{name}.conf
%{_fillupdir}/sysconfig.%{name}
%{_unitdir}/%{name}.target
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}-stator.service
%{_sbindir}/rc%{name}
%{_sbindir}/rc%{name}-stator
%attr (755,-,-) %{_sbindir}/%{name}-manage
%dir %{appdir}
%dir %{sitedir}
%{sitedir}/static
%{sitedir}/templates
%dir %attr (755,%{appuser},%{appuser}) %{sitedir}/media
%dir %attr (755,%{appuser},%{appuser}) %{sitedir}/static-collected
%dir %attr (755,%{appuser},%{appuser}) %{_sharedstatedir}/%{name}
%dir %{python_sitelib}/%{name}
%dir %{python_sitelib}/%{name}/activities/migrations
%dir %{python_sitelib}/%{name}/activities/models
%dir %{python_sitelib}/%{name}/activities/templatetags
%dir %{python_sitelib}/%{name}/activities/services
%dir %{python_sitelib}/%{name}/activities/views
%dir %{python_sitelib}/%{name}/activities/views/admin
%dir %{python_sitelib}/%{name}/api
%dir %{python_sitelib}/%{name}/api/migrations
%dir %{python_sitelib}/%{name}/api/models
%dir %{python_sitelib}/%{name}/api/views
%dir %{python_sitelib}/%{name}/core/migrations
%dir %{python_sitelib}/%{name}/core/models
%dir %{python_sitelib}/%{name}/core/templatetags
%dir %{python_sitelib}/%{name}/stator
%dir %{python_sitelib}/%{name}/stator/management
%dir %{python_sitelib}/%{name}/stator/management/commands
%dir %{python_sitelib}/%{name}/stator/migrations
%dir %{python_sitelib}/%{name}/users
%dir %{python_sitelib}/%{name}/users/migrations
%dir %{python_sitelib}/%{name}/users/models
%dir %{python_sitelib}/%{name}/users/services
%dir %{python_sitelib}/%{name}/users/views
%dir %{python_sitelib}/%{name}/users/views/admin
%dir %{python_sitelib}/%{name}/users/views/settings
%dir %{python_sitelib}/%{name}/activities
%dir %{python_sitelib}/%{name}/core
%dir %{python_sitelib}/%{name}/mediaproxy
%dir %{python_sitelib}/%{name}/%{name}
%{appdir}/manage.py
%{python_sitelib}/%{name}/activities/*.py
%{python_sitelib}/%{name}/activities/migrations/*.py
%{python_sitelib}/%{name}/activities/models/*.py
%{python_sitelib}/%{name}/activities/templatetags/*.py
%{python_sitelib}/%{name}/activities/services/*.py
%{python_sitelib}/%{name}/activities/views/*.py
%{python_sitelib}/%{name}/activities/views/admin/*.py
%{python_sitelib}/%{name}/api/*.py
%{python_sitelib}/%{name}/api/migrations/*.py
%{python_sitelib}/%{name}/api/models/*.py
%{python_sitelib}/%{name}/api/views/*.py
%{python_sitelib}/%{name}/core/*.py
%{python_sitelib}/%{name}/core/migrations/*.py
%{python_sitelib}/%{name}/core/models/*.py
%{python_sitelib}/%{name}/core/templatetags/*.py
%{python_sitelib}/%{name}/stator/*.py
%{python_sitelib}/%{name}/stator/management/*.py
%{python_sitelib}/%{name}/stator/management/commands/*.py
%{python_sitelib}/%{name}/stator/migrations/*.py
%{python_sitelib}/%{name}/users/*.py
%{python_sitelib}/%{name}/users/migrations/*.py
%{python_sitelib}/%{name}/users/models/*.py
%{python_sitelib}/%{name}/users/services/*.py
%{python_sitelib}/%{name}/users/views/*.py
%{python_sitelib}/%{name}/users/views/admin/*.py
%{python_sitelib}/%{name}/users/views/settings/*.py
%{python_sitelib}/%{name}/mediaproxy/*.py
%{python_sitelib}/%{name}/%{name}/{__init__,asgi,wsgi}.py
%dir %attr (755,root,%{appuser}) %{_sysconfdir}/%{name}
%config(noreplace) %attr(644,root,%{appuser}) %{_sysconfdir}/%{name}/{settings,urls}.py
%{python_sitelib}/%{name}/%{name}/{settings,urls}.py
%{python_sitelib}/%{appnameUpper}-*.egg-info
%pycache_only %{python_sitelib}/%{name}/*/__pycache__/
%pycache_only %{python_sitelib}/%{name}/*/*/__pycache__/
%pycache_only %{python_sitelib}/%{name}/*/*/*/__pycache__/

%changelog

openSUSE Build Service is sponsored by