File python-story.spec of Package python-story

#
# spec file for package python-story
#
# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany.
#
# 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/
#

%bcond_without check

%define use_nuitka3_standalone 1

%{?!python_module:%define python_module() python-%{**} python3-%{**}}
%define skip_python2 1
Name:           python-story
Version:        0.20.4
# Exports in check section can be removed in next release
Release:        0
Summary:        Storyscript Cloud CLI
License:        Apache-2.0
Group:          Development/Languages/Python
URL:            https://docs.storyscript.io/cli
Source:         https://files.pythonhosted.org/packages/source/s/story/story-%{version}.tar.gz
BuildRequires:  %{python_module setuptools_scm}
BuildRequires:  %{python_module setuptools}
BuildRequires:  clang
BuildRequires:  fdupes
BuildRequires:  python-rpm-macros
Requires:       python-PyYAML >= 4.2
Requires:       python-appdirs
Requires:       python-blindspin
Requires:       python-click >= 7.0
Requires:       python-click-completion >= 0.5.1
Requires:       python-click-help-colors >= 0.5
Requires:       python-click-spinner >= 0.1.8
Requires:       python-emoji >= 0.5.0
Requires:       python-pytz >= 2018.5
Requires:       python-raven >= 6.9.0
Requires:       python-requests >= 2.20.0
Requires:       python-semver
Requires:       python-storyscript >= 0.20.1
Requires:       python-texttable >= 1.4.0
Requires:       python-websockets >= 7.0
Requires:       python-xerox
Requires:       python3-story-stories = %{version}
#BuildArch:      noarch
# SECTION test requirements
BuildRequires:  %{python_module PyYAML >= 4.2}
BuildRequires:  %{python_module appdirs}
BuildRequires:  %{python_module blindspin}
BuildRequires:  %{python_module click >= 7.0}
BuildRequires:  %{python_module click-completion >= 0.5.1}
BuildRequires:  %{python_module click-help-colors >= 0.5}
BuildRequires:  %{python_module click-spinner >= 0.1.8}
BuildRequires:  %{python_module delegator.py}
BuildRequires:  %{python_module emoji >= 0.5.0}
BuildRequires:  %{python_module pytest-asyncio}
BuildRequires:  %{python_module pytest-mock}
BuildRequires:  %{python_module pytest-runner}
BuildRequires:  %{python_module pytest}
BuildRequires:  %{python_module pytz >= 2018.5}
BuildRequires:  %{python_module raven >= 6.9.0}
BuildRequires:  %{python_module requests >= 2.20.0}
BuildRequires:  %{python_module semver}
BuildRequires:  %{python_module storyscript >= 0.23.4}
BuildRequires:  %{python_module texttable >= 1.4.0}
BuildRequires:  %{python_module websockets >= 7.0}
BuildRequires:  %{python_module xerox}
#BuildRequires:  gdb
BuildRequires:  zlib-devel
BuildRequires:  zlib-devel-static
BuildRequires:  libxcrypt-devel
BuildRequires:  libxcrypt-devel-static
#BuildRequires:  libopenssl-1_1-devel
BuildRequires:  libopenssl-1_1-devel-static
BuildRequires:  libffi-devel
BuildRequires:  libbz2-devel
BuildRequires:  uuid-devel
BuildRequires:  xz-devel
BuildRequires:  xz-static-devel

# /SECTION
%python_subpackages

%description
Storyscript Cloud CLI module.

%package -n story-cli-static
Summary:        Storyscript Cloud CLI binary
Group:          Development/Tools/Other

%description -n story-cli-static
Storyscript Cloud CLI binary.

%if 0
%package stories
Summary:        Stories for Storyscript Cloud CLI
Group:          Development/Tools/Other

%description stories
Stories for Storyscript Cloud CLI.
%endif

%prep
%setup -q -n story-%{version}
#sed -i '/asyncio/d' setup.py
sed -i 's/==/>=/g;s/~=/>=/g' setup.py

# Nuitka uses the standard __main__.py for its entry point
cp story/main.py story/__main__.py
sed -i 's/^from \. /from story /;s/from \./from story./' ./story/__main__.py

python3 -c 'import _ctypes'
python3 -c 'import _ctypes; print(_ctypes.__file__)' ||:

python3 -c 'import sys; print(sys.builtin_module_names)'

%build
%if 0%{?have_nuitka3} && ! 0%{?skip_nuitka3}
# Show that most of the packages are already pre-compiled, and the resources of them are prominent here for debugging
%python_expand find %{$python_sitelib} -not -name __pycache__ -and -not -name '*.pyi' | sort
%endif

export CFLAGS="%{optflags}"
# Ideally standalone build shouldnt need to have 'story' built and installed already, but I havent worked that out yet
# `install` fails without `build`, so need to build here.
#%%python_build

%install
#%%python_install

# build standalone
%if 0%{?use_nuitka3_standalone}

%{python_expand # export PYTHONPATH=%{buildroot}%{$python_sitelib}

cp story/__main__.py cli.py

echo '' >> cli.py
echo 'import storyhub' >> cli.py
echo 'import story.commands.apps' >> cli.py
echo 'import story.commands.template' >> cli.py

mv story .story
export CC=clang
export CXX=clang++
export LDFLAGS="-z nodeflib"
# top level module and 'main' are excluded
$python -m nuitka --explain-imports --standalone --recurse-all --recurse-stdlib --python-flag=no_site --show-progress --show-scons --unstripped --include-plugin-files /usr/lib/python3.7/site-packages/raven/processors.py --verbose cli.py

%if 0
\
  --include-module story.api \
  --include-module story.awesome \
  --include-module story.cli \
  --include-module story.commands \
  --include-module story.commands.apps \
  --include-module story.commands.completion \
  --include-module story.commands.config \
  --include-module story.commands.deploy \
  --include-module story.commands.feedback \
  --include-module story.commands.login \
  --include-module story.commands.logout \
  --include-module story.commands.logs \
  --include-module story.commands.maintenance \
  --include-module story.commands.releases \
  --include-module story.commands.repl \
  --include-module story.commands.repl.Repl \
  --include-module story.commands.repl.Scope \
  --include-module story.commands.run \
  --include-module story.commands.status \
  --include-module story.commands.template \
  --include-module story.commands.test \
  --include-module story.commands.update \
  --include-module story.ensure \
  --include-module story.environment \
  --include-module story.helpers \
  --include-module story.helpers.aliases \
  --include-module story.helpers.datetime \
  --include-module story.helpers.didyoumean \
  --include-module story.options \
  --include-module story.storage \
  --include-module story.support \
  --include-module story.utils \
  --include-module story.version \
  cli.py
%endif

mv .story story

output_dir=./cli.dist/

find $output_dir

ls $output_dir

file $output_dir/cli
ls -al $output_dir/cli

mkdir -p %{buildroot}%{_libdir}/storyscript/cli
mv $output_dir/* %{buildroot}%{_libdir}/storyscript/cli

#cp -rp %{$python_sitelib}/storyhub* %{buildroot}%{_libdir}/storyscript/cli

mkdir -p %{buildroot}%{_bindir}
(cd %{buildroot}/%{_bindir} && ln -fs ../../%{_libdir}/storyscript/cli/cli story)

LD_LIBRARY_PATH=%{buildroot}%{_libdir}/storyscript/cli ldd %{buildroot}%{_bindir}/story

mkdir -p %{buildroot}/etc/ld.so.conf.d/
#echo %{_libdir}/storyscript/cli > %{buildroot}/etc/ld.so.conf.d/storyscript-cli.conf

# Horrible hack around for story.so
# /home/abuild/rpmbuild/BUILDROOT/python-story-0.20.4-40.1.i386/usr/lib/python3.7/site-packages/story/commands/template.py:26: in get_stories
# FileNotFoundError: [Errno 2] No such file or directory: '/home/abuild/rpmbuild/BUILDROOT/python-story-0.20.4-40.1.i386/usr/lib/python3.7/site-packages/stories'
# or when standalone cli
# ["FileNotFoundError: [Errno 2] No such file or directory: '/home/abuild/rpmbuild/BUILDROOT/python-story-0.20.4-0.i386/usr/lib/storyscript/cli/story/stories'", '  File "cli/cli.py", line 10, in <module>'

mkdir -p %{buildroot}%{_libdir}/storyscript/cli/story
#cp -r %{buildroot}%{$python_sitelib}/story/stories/ %{buildroot}%{$python_sitelib}
cp -r ./story/stories/ %{buildroot}%{_libdir}/storyscript/cli/story
cp -r ./story/stories/ %{buildroot}%{_libdir}/storyscript/cli/
# check it is there
ls %{buildroot}%{_libdir}/storyscript/cli/story/stories/http.story
# end of hack

# core dumped
#LD_LIBRARY_PATH=%{buildroot}%{_libdir}/storyscript/cli %{buildroot}%{_bindir}/story --help

#LD_LIBRARY_PATH=%{buildroot}%{_libdir}/storyscript/cli %{buildroot}%{_bindir}/story apps ||:

}

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

%endif
# endif standalone install

%if 0%{?use_nuitka3_standalone}

%post
/sbin/ldconfig

%postun
/sbin/ldconfig

%endif

%if %{with check}
%check
export LANG=en_US.UTF-8

# This was necessary, but maybe --ignore tests/integration/commands/apps.py has meant it isnt getting coverage
#export PATH=%{buildroot}%{_bindir}:$PATH

export PYTHONPATH=%{buildroot}%{_libdir}/storyscript/cli/

mv story .story
#%%python_expand py.test-%{$python_bin_suffix} --ignore tests/integration/commands/apps.py -k 'not test_apps_create and not api_throws_an_exception2'
mv .story story
%endif

%files %{python_files}
%doc CHANGELOG README.md
%license LICENSE

%if %{use_nuitka3_standalone}
%files -n story-cli-static
%doc CHANGELOG README.md
%license LICENSE
%{_bindir}/story
%dir %{_libdir}/storyscript/
%{_libdir}/storyscript/cli/
#%%config /etc/ld.so.conf.d/storyscript-cli.conf
%endif

%changelog