## START: Set by rpmautospec
## (rpmautospec version 0.3.5)
## RPMAUTOSPEC: autorelease, autochangelog
%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
    release_number = 1;
    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 pkgname aws-cli

# Use the same python for all distros
%global __python3 /usr/bin/python3.11
%global python3_pkgversion 3.11

Name:               awscli2
Version:            2.15.18
Release:            %autorelease

Summary:            Universal Command Line Environment for AWS, version 2
# all files are licensed under Apache-2.0, except:
# - awscli/topictags.py is MIT
# - awscli/botocore/vendored/six.py is MIT
License:            Apache-2.0 AND MIT
URL:                https://github.com/aws/aws-cli/tree/v2

Source0:            https://github.com/aws/aws-cli/archive/%{version}/%{pkgname}-%{version}.tar.gz

# adapt to whitespace formatting changes and removal of OrderedDict in ruamel-yaml
Patch0:             ruamel-yaml-0.17.32.patch
# fix Python 3.12 incompatibilities
#Patch1:             python312.patch
Patch1:             build-ac.index-in-tmp.diff
# fix incorrect assertions in TestKubeconfigLoader
Patch2:             assertions.patch

BuildArch:          noarch

BuildRequires:      python%{python3_pkgversion}-devel
BuildRequires:      pyproject-rpm-macros
%if 0%{?rhel} <= 9
BuildRequires:      python%{python3_pkgversion}-jsonschema
BuildRequires:      python%{python3_pkgversion}-pip
BuildRequires:      python%{python3_pkgversion}-tox
BuildRequires:      python%{python3_pkgversion}-pytest
BuildRequires:      python%{python3_pkgversion}-tomli
BuildRequires:      python%{python3_pkgversion}-packaging
BuildRequires:      python%{python3_pkgversion}-flit
BuildRequires:      python%{python3_pkgversion}-pyrsistent >= 0.14
BuildRequires:      python%{python3_pkgversion}-distro
BuildRequires:      python%{python3_pkgversion}-jmespath
BuildRequires:      python%{python3_pkgversion}-urllib3
BuildRequires:      python%{python3_pkgversion}-awscrt
BuildRequires:      python%{python3_pkgversion}-ruamel-yaml
BuildRequires:      python%{python3_pkgversion}-ruamel-yaml-clib
BuildRequires:      python%{python3_pkgversion}-docutils
BuildRequires:      python%{python3_pkgversion}-cryptography
BuildRequires:      python%{python3_pkgversion}-installer
BuildRequires:      python%{python3_pkgversion}-gpep517
%endif
BuildRequires:      procps-ng-devel

Recommends:         groff

Provides:           bundled(python3dist(botocore)) = 2.0.0
Provides:           bundled(python3dist(s3transfer)) = 0.5.1

Provides:           awscli = %{version}-%{release}
Conflicts:          awscli < 2.0

# provide an upgrade path from awscli-2 (Amazon Linux)
Provides:           awscli-2 = %{version}-%{release}
Obsoletes:          awscli-2 < %{version}-%{release}

# python-awscrt does not build on s390x
ExcludeArch:        s390x

%{?python3_provide:%python_provide python%{python3_pkgversion}-%{pkgname}}

%description
This package provides version 2 of the unified command line
interface to Amazon Web Services.


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

# fix permissions
find awscli/examples/ -type f -name '*.rst' -executable -exec chmod -x '{}' +

# remove version caps on dependencies
sed -i 's/,<=\?[^"]*"/"/' pyproject.toml

# use unittest.mock
find -type f -name '*.py' -exec sed \
    -e 's/^\( *\)import mock$/\1from unittest import mock/' \
    -e 's/^\( *\)from mock import mock/\1from unittest import mock/' \
    -e 's/^\( *\)from mock import/\1from unittest.mock import/' \
    -i '{}' +

# Fedora does not run coverage tests.
# mock is deprecated in Fedora. We use unittest.mock.
# pip-tools is not used directly by the unit tests.
# pytest-xdist is unwanted in RHEL.
sed \
    -e 's|==.*||' \
    -e '/coverage/d' \
    -e '/mock/d' \
    -e '/pip-tools/d' \
    -e '/pytest-cov/d' \
    %{?rhel:-e '/pytest-xdist/d'} \
    requirements-test.txt > _requirements-test.txt


%if 0%{?rhel} >= 10
%generate_buildrequires
%pyproject_buildrequires _requirements-test.txt
%endif

%build
%if 0%{?rhel} >= 10
%pyproject_wheel
%else
gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
%endif

%install
%if 0%{?rhel} >= 10
%pyproject_install
%pyproject_save_files awscli
%else
%{__python3} -m installer -d %{buildroot} .dist/*.whl
rm %{buildroot}%{_bindir}/aws.cmd
%endif

# remove unnecessary scripts
rm -vf %{buildroot}%{_bindir}/{aws_bash_completer,aws_zsh_completer.sh,aws.cmd}

# install shell completion
install -Dpm0644 bin/aws_bash_completer \
  %{buildroot}%{bash_completions_dir}/aws
install -Dpm0644 bin/aws_zsh_completer.sh \
  %{buildroot}%{zsh_completions_dir}/_awscli

%if 0%{?rhel} >= 10
%check
# it appears that some tests modify the environment and remove PYTHONPATH
# so it's not passed to botocore cmd-runner, inject it here
sed -i '/self.driver.start(env=env)/i \ \ \ \ \ \ \ \ env["PYTHONPATH"] = "%{buildroot}%{python3_sitelib}"' \
    tests/utils/botocore/__init__.py

export TESTS_REMOVE_REPO_ROOT_FROM_PATH=1 TZ=UTC
%if 0%{?rhel}
export OPENSSL_ENABLE_SHA1_SIGNATURES=yes
%endif
%pytest --verbose %{!?rhel:--numprocesses=auto --dist=loadfile --maxprocesses=4} tests/unit tests/functional

%files -f %{pyproject_files}
%else
%files 
%{python3_sitelib}/awscli
%{python3_sitelib}/awscli-%{version}.dist-info
%endif
%license LICENSE.txt
%doc README.rst
%{_bindir}/aws
%{_bindir}/aws_completer
%{bash_completions_dir}/aws
%{zsh_completions_dir}/_awscli


%changelog
* Thu Feb  8 2024 Raven <raven@sysadmins.ws< - 2.15.18-1
- update to 2.15.18

* Mon Jan  8 2024 Raven <raven@sysadmins.ws> - 2.15.8-1
- Initial package for el8/el9