# 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.2
%define binsuffix 3.2
%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.2
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.bz2
Source1: http://docs.python.org/py3k/archives/python-%{version}-docs-html.tar.bz2
Patch1: python-3.2b2-lib64.patch
BuildRoot: %{_tmppath}/%{name}-%{version}-root
BuildRequires: expat-devel
BuildRequires: db4-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
Requires: bzip2
Requires: gdbm
Requires: ncurses
Requires: openssl
Requires: readline
Requires: sqlite3
Requires: zlib
Packager: owa
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

%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}
Requires: tcl, tk, tix
Requires: libX11, libxcb, libXau, libXdmcp
BuildRequires: tcl, tk, 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}
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.
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


%build
./configure \
	--prefix=%{_prefix} \
	--libdir=%{_libdir} \
	--enable-shared \
	--enable-profiling \
	--enable-ipv6 \
	--with-fpectl \
	--with-wide-unicode \
	--with-doc-strings \
	--with-pymalloc \
	--with-signal-module \
	--with-system-ffi \
	--with-threads \
	--with-valgrind \
%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}u $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*

%files devel
%defattr(-,root,root)
%dir %{_includedir}/python%{libvers}u
%{_includedir}/python%{libvers}u/*.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 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

