# if you wanna test, add " --define '_run_test 1' " option to rpmbuild command
%define run_test %{?_run_test:1}%{!?_run_test:0}
%define libvers 3.4
%define binsuffix 3.4
%define python_lib %{_libdir}/python%{libvers}
%define config_htmldir /var/www/html/python
Summary: An interpreted, interactive, object-oriented programming language.
Summary(ja): オブジェクト指向 Python3 インタプリタ
Name: python3
Version: 3.4.4
Release: 1%{?_dist_release}
License: PSF
Group: Development/Languages
URL: http://www.python.org/download/releases/%{version}
Source0: http://www.python.org/ftp/python/%{version}/Python-%{version}.tar.xz
Source1: https://docs.python.org/3.4/archives/python-%{version}-docs-html.tar.bz2
Patch1: python-3.4.2-lib64.patch
Patch2: python3-3.4.4-revert-makefile.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-root
BuildRequires: expat-devel
BuildRequires: libdb-devel
BuildRequires: gdbm-devel
BuildRequires: sqlite3-devel
BuildRequires: gzip tar gcc-c++ pkgconfig sed findutils
BuildRequires: bzip2-devel
BuildRequires: glibc-devel
BuildRequires: ncurses-devel
BuildRequires: openssl-devel
BuildRequires: readline-devel
BuildRequires: zlib-devel
BuildRequires: libffi-devel
BuildRequires: valgrind-devel
Requires: bzip2
Requires: gdbm
Requires: ncurses
Requires: openssl
Requires: readline
Requires: sqlite3
Requires: zlib

Packager: owa, Takemikaduchi
Vendor: Project Vine
Distribution: Vine Linux

%description
Python is an interpreted, interactive, object-oriented programming
language.  It incorporates modules, exceptions, dynamic typing, very high
level dynamic data types, and classes. Python combines remarkable power
with very clear syntax. It has interfaces to many system calls and
libraries, as well as to various window systems, and is extensible in C or
C++. It is also usable as an extension language for applications that need
a programmable interface.  Finally, Python is portable: it runs on many
brands of UNIX, on PCs under Windows, MS-DOS, and OS/2, and on the
Mac.

%package devel
Summary: The libraries and header files needed for Python development.
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}

%description devel
The Python programming language's interpreter can be extended with
dynamically loaded extensions and can be embedded in other programs.
This package contains the header files and libraries needed to do
these types of tasks.

Install python-devel if you want to develop Python extensions.  The
python package will also need to be installed.  You'll probably also
want to install the python-docs package, which contains Python
documentation.

%package tkinter
Summary: A graphical user interface for the Python scripting language.
Group: Development/Languages
Requires: %{name} = %{version}-%{release}
Requires: tcl, tk, tix
Requires: libX11, libxcb, libXau, libXdmcp
BuildRequires: tcl-devel, tk-devel, tix
BuildRequires: libX11-devel, libxcb-devel, libXau-devel, libXdmcp-devel

%description tkinter
The Tkinter (Tk interface) program is an graphical user interface for
the Python scripting language.

You should install the tkinter package if you'd like to use a graphical
user interface for Python programming.

%package tools
Summary: A collection of development tools included with Python.
Group: Development/Tools
Requires: %{name} = %{version}-%{release}
Requires: %{name}-tkinter = %{version}

%description tools
The Python package includes several development tools that are used
to build python programs.

%package docs
Summary: Documentation for the Python programming language.
Summary(ja): Python プログラミング言語のドキュメント
Group: Documentation

%description docs
The python-docs package contains documentation on the Python
programming language and interpreter.  The documentation is provided
in ASCII text files and in LaTeX source files.

Install the python-docs package if you'd like to use the documentation
for the Python language.


%prep
%setup -q -n Python-%{version}
%if %{_lib} == lib64
%patch1 -p1 -b .lib64
%endif
%patch2 -p1 -b .libpl


%build
./configure \
	--prefix=%{_prefix} \
	--libdir=%{_libdir} \
	--enable-shared \
	--enable-ipv6 \
	--with-fpectl \
	--with-doc-strings \
	--with-pymalloc \
	--with-signal-module \
	--with-system-ffi \
	--with-threads \
	--with-valgrind \
	--without-ensurepip \
%ifarch x86_64
	--with-universal-archs=64-bit \
%endif
	CXX=g++;

make %{?_smp_mflags}

%if %{run_test}
make test
%endif


%install
[ -d "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT
echo '[install_scripts]' >setup.cfg
echo 'install_dir='"${RPM_BUILD_ROOT}%{_bindir}" >> setup.cfg

mkdir -p $RPM_BUILD_ROOT%{python_lib}/lib-dynload
make DESTDIR=$RPM_BUILD_ROOT LIBDIR=%{_libdir} install

# if --with-wide-unicode at configure
ln -s python%{libvers}m $RPM_BUILD_ROOT%{_includedir}/python%{libvers}

# replace path in pydoc
if [ ! -z "%{binsuffix}" ]
then
   (
      cd $RPM_BUILD_ROOT%{_bindir}
      mv pydoc%{libvers} pydoc.old
      sed 's|#!.*|#!%{_bindir}/env python'%{libvers}'|' \
            pydoc.old > pydoc%{libvers}
      chmod 755 pydoc%{libvers}
      rm -f pydoc.old
   )
fi

#  Tools
echo '#!%{_bindir}/env python%{binsuffix}' > ${RPM_BUILD_ROOT}%{_bindir}/idle%{binsuffix}
echo 'import os, sys' >> ${RPM_BUILD_ROOT}%{_bindir}/idle%{binsuffix}
echo 'os.execvp("%{_bindir}/python%{binsuffix}", ["%{_bindir}/python%{binsuffix}", "%{python_lib}/idlelib/idle.py"] + sys.argv[1:])' >> ${RPM_BUILD_ROOT}%{_bindir}/idle%{binsuffix}
echo 'print "Failed to exec Idle"' >> ${RPM_BUILD_ROOT}%{_bindir}/idle%{binsuffix}
echo 'sys.exit(1)' >> ${RPM_BUILD_ROOT}%{_bindir}/idle%{binsuffix}
chmod 755 $RPM_BUILD_ROOT%{_bindir}/idle%{binsuffix}
cp -a Tools $RPM_BUILD_ROOT%{python_lib}

# make file lists
rm -f mainpkg.files
find "$RPM_BUILD_ROOT"%{python_lib} -type f |
	sed "s|^${RPM_BUILD_ROOT}|/|" |
	grep -v -e '/python%{libvers}/config$' -e '_tkinter.so$' >> mainpkg.files

rm -f $RPM_BUILD_ROOT%{_prefix}/bin/2to3
find "$RPM_BUILD_ROOT""%{_prefix}"/bin -type f -o -type l |
	sed "s|^${RPM_BUILD_ROOT}|/|" |
	grep -v -e '/bin/2to3%{binsuffix}$' |
	grep -v -e '/bin/pydoc%{binsuffix}$' |
	grep -v -e '/bin/smtpd.py%{binsuffix}$' |
	grep -v -e '/bin/idle%{binsuffix}$' >> mainpkg.files


rm -f tools.files
find "$RPM_BUILD_ROOT"%{python_lib}/idlelib \
      "$RPM_BUILD_ROOT"%{python_lib}/Tools -type f |
      sed "s|^${RPM_BUILD_ROOT}|/|" > tools.files
echo %{_bindir}/2to3-%{binsuffix} >> tools.files
echo %{_bindir}/pydoc%{binsuffix} >> tools.files
##echo %{_bindir}/smtpd.py%{binsuffix} >> tools.files
echo %{_bindir}/idle%{binsuffix} >> tools.files

# docs
mkdir -p "$RPM_BUILD_ROOT"%{config_htmldir}
(
   cd "$RPM_BUILD_ROOT"%{config_htmldir}
   bunzip2 < %{SOURCE1} | tar x
)

#  fix the #! line in installed files
find "$RPM_BUILD_ROOT" -type f -print0 |
      xargs -0 grep -l /usr/local/bin/python | while read file
do
   FIXFILE="$file"
   sed 's|^#!.*python|#!%{_bindir}/env python'"%{binsuffix}"'|' \
         "$FIXFILE" >/tmp/fix-python-path.$$
   cat /tmp/fix-python-path.$$ > "$FIXFILE"
   rm -f /tmp/fix-python-path.$$
done

#  check to see if there are any straggling #! lines
find "$RPM_BUILD_ROOT" -type f | xargs egrep -n '^#! */usr/local/bin/python' \
      | grep ':1:#!' >/tmp/python-rpm-files.$$ || true
if [ -s /tmp/python-rpm-files.$$ ]
then
   echo '*****************************************************'
   cat /tmp/python-rpm-files.$$
   cat <<@EOF
   *****************************************************
     There are still files referencing /usr/local/bin/python in the
     install directory.  They are listed above.  Please fix the .spec
     file and try again.  If you are an end-user, you probably want
     to report this to jafo-rpms@tummy.com as well.
   *****************************************************
@EOF
   rm -f /tmp/python-rpm-files.$$
   exit 1
fi
rm -f /tmp/python-rpm-files.$$


%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig
%clean
rm -fr $RPM_BUILD_ROOT

%files -f mainpkg.files
%defattr(-, root, root)
%doc LICENSE
%doc Misc/ACKS Misc/HISTORY Misc/NEWS Misc/Porting Misc/README
%attr(755,root,root) %dir %{python_lib}/
%{_libdir}/libpython*
%{_mandir}/man1/python%{libvers}.1*
%{_mandir}/man1/%{name}.1*

%files devel
%defattr(-,root,root)
%dir %{_includedir}/python%{libvers}m
%{_includedir}/python%{libvers}m/*.h
%{_includedir}/python%{libvers}
%{_libdir}/pkgconfig/*.pc

%files -f tools.files tools
%defattr(-,root,root)

%files tkinter
%defattr(-,root,root)
%{python_lib}/tkinter
%{python_lib}/lib-dynload/_tkinter.*.so*

%files docs
%defattr(-,root,root)
%{config_htmldir}/*


%changelog
* Sat Feb 13 2016 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.4.4-1
- new upstream release
- add Patch2 (python3-3.4.4-revert-makefile.patch)

* Sun Apr 12 2015 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.4.3-1
- new upstream release

* Sun Mar 22 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.4.2-2
- rebuilt with readline 6.3
- changed BuildRequires: tcl-devel, tk-devel instead of tcl, tk respectively
- changed BuildRequires: libdb-devel instead of db4-devel

* Sun Jan 11 2015 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.4.2-1
- new upstream release
- update Patch1 (python-3.4.2-lib64.patch)

* Sun Sep 07 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3.3-3
- remove configure option "--enable-profiling"

* Sun Jul 06 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3.3-2
- rebuild with libffi-3.0.13

* Sun Nov 24 2013 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3.3-1
- new upstream release

* Sat Oct 26 2013 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3.2-1
- updated python to 3.3.2

* Sun Jan  6 2013 IWAI, Masaharu <iwai@alib.jp> 3.3.0-2
- build with Tcl/Tk 8.5.7-1

* Wed Oct 24 2012 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3.0-1
- updated python to 3.3.0
- update Patch1 (python-3.3.0-lib64.patch)
- add BuildRequires: valgrind-devel

* Sat Apr 16 2011 Shu KONNO <owa@bg.wakwak.com> 3.2-1
- updated python to 3.2
- updated python-*-docs-html to 3.2
- dropt all patchs
- added python-3.2b2-lib64.patch
- referred to the review Python-3.2/Misc/RPM/python-3.2.spec

* Fri Jan 14 2011 Shu KONNO <owa@bg.wakwak.com> 3.1.3-2
- rebuilt with openssl-1.0.0c

* Mon Nov 29 2010 Shu KONNO <owa@bg.wakwak.com> 3.1.3-1
- updated python to 3.1.3
- added python-3.1.3-locale.patch
- dropt python-3.1.2-lib64.patch
- added python-3.1.3-lib64-*.patchs
  python-3.1.3-lib64-Makefile.patch
  python-3.1.3-lib64-setup.patch
  python-3.1.3-lib64-site.patch
  python-3.1.3-lib64-install.patch
  python-3.1.3-lib64-sysconfig.patch
  python-3.1.3-lib64-getpath.patch
  python-3.1.3-lib64-test_install.patch
- added run_test macro to run unit test
- added BR: libffi

* Sun Sep 26 2010 Shu KONNO <owa@bg.wakwak.com> 3.1.2-4
- rebuilt with rpm-4.8.1 for pkg-config

* Sun Jun 20 2010 Shu KONNO <owa@bg.wakwak.com> 3.1.2-3
- spec in utf-8

* Sun Jun 20 2010 Shu KONNO <owa@bg.wakwak.com> 3.1.2-2
- added missings in BuildRequires, Requires

* Fri Jun 18 2010 Shu KONNO <owa@bg.wakwak.com> 3.1.2-1
- initial build for VineSeed