FANCOMI Ad-Tech Blog

株式会社ファンコミュニケーションズ nend・新規事業のエンジニア・技術ブログ

Numpy1.7.1のRPM作成

こんにちは、s_mamedaifukuです。 こしあん派です。

先日のk_oomoriさんの記事の話を実装するにあたりNumpyを利用することになりました。Numpyは数理計算を行うためのPython拡張モジュールです。 バージョン1.7.1を使うことになったのですが、 CentOS5用RPMパッケージは配布されていなかったので作成することにしました。

RPMをビルドする環境を整える

RPMをビルドするために必要なプログラムが含まれるrpm-build、 CentOS用マクロが定義されたbuildsys-macrosをyumでインストールします。 紛らわしいことに、コマンド名はrpmbuildなのにパッケージ名はrpm-buildです。 [shell] $ sudo yum install rpm-build buildsys-macros [/shell]   個人的に定義したいマクロを記述して~/.rpmmacrosに保存します。 [text] %topdir /home/s_mamedaifuku/rpmbuild %build_name_fmt %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}%{dist}.%%{ARCH}.rpm [/text] %topdirはホームディレクトリ下でRPMをビルドするために設定しています。 本当は~/rpmbuildとしたかったのですが、 エラーになってしまうので仕方なく/homeからの記述となりました。 %build_name_fmtは出来上がるRPMファイル名にディストリビューション名を入れるために追加しています。%distは/etc/rpm/macros.distで定義されており、 最初にインストールしたbuildsys-macrosパッケージに入っていたものです。   ビルド時に使用するディレクトリを作成します。 [shell] $ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} [/shell]   以上でビルド環境が整いました。

Numpyのビルド

バージョン1.7.1のソースコードを取得し、 SOURCESディレクトリに配置します。 [text] $ wget -L -P ~/rpmbuild/SOURCES/ http://sourceforge.net/projects/numpy/files/NumPy/1.7.1/numpy-1.7.1.tar.gz/download [/text]   Numpyの公式サイトからたどり着いたopensuseリポジトリからsrc.rpmファイルを取得しSPECファイルを取り出します。 [shell] $ wget -P ~/rpmbuild/SRPMS/ http://download.opensuse.org/repositories/science:/ScientificLinux/CentOS_5/src/python-numpy-1.6.1-15.1.src.rpm $ sudo rpm -ivh ~/rpmbuild/SRPMS/python-numpy-1.6.1-15.1.src.rpm [/shell] ~/rpmbuild/SPECSディレクトリにpython-numpy.specが配置されます。 もちろん、このSPECファイルは1.6.1用のものなのでこのままでは使えません。

SPECファイルを1.7.1用に書き換えます。 書き換えると言っても、Versionを1.7.1にすればそのまま流用できてしまいます。 SUSEFedora用の記述がありましたが、 使うことはまずないので削除してしまいました。 [text] Name: python-numpy Version: 1.7.1 Release: 1 Url: http://sourceforge.net/projects/numpy Summary: NumPy array processing for numbers, strings, records and objects License: BSD-3-Clause Group: Development/Libraries/Python Source: numpy-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-build BuildRequires: python-devel BuildRequires: gcc-gfortran Provides: numpy = %{version}

%{!?python_sitearch: %global python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)")}

%description NumPy is a general-purpose array-processing package

%package devel Summary: Development files for numpy applications Group: Development/Libraries/Python Requires: %{name} = %{version} Requires: blas Requires: lapack Requires: python-devel Requires: gcc-gfortran

%description devel This package contains files for developing applications using numpy.

%prep %setup -q -n numpy-%{version} %build CFLAGS="%{optflags} -fno-strict-aliasing" python setup.py build

%install python setup.py install --root="%{buildroot}" --prefix="%{_prefix}" rm -rf %{buildroot}%{python_sitearch}/numpy/{,core,distutils,f2py,fft,ma,matrixlib,oldnumeric,polynomial,random,testing}/tests

%files %defattr(-,root,root) %doc .txt %{_bindir}/ %{python_sitearch}/ %exclude %{python_sitearch}/numpy///.c %exclude %{python_sitearch}/numpy//.h %exclude %{python_sitearch}/numpy///.h %exclude %{python_sitearch}/numpy////*.h %exclude %{python_sitearch}/numpy/core/lib/libnpymath.a

%files devel %defattr(-,root,root) %{python_sitearch}/numpy///.c %{python_sitearch}/numpy//.h %{python_sitearch}/numpy///.h %{python_sitearch}/numpy////.h %{python_sitearch}/numpy/core/lib/libnpymath.a [/text]   最後にビルドする時に必要なpython-devel、gcc-gfortranをインストールします。 Numpyは奥底のところでfortranを呼び出しているそうです。 [shell] $ sudo yum install python-devel gcc-gfortran [/shell]   rpmbuildを実行すれば出来上がりです。 [shell] $ rpmbuild -bb ~/rpmbuild/SPECS/python-numpy.spec ... Wrote: /home/s_mamedaifuku/rpmbuild/RPMS/x86_64/python-numpy-1.7.1-1.el5.centos.x86_64.rpm ... [/shell]   無事完成したRPMは 自前で用意しているyumリポジトリに配置して使っています。

最後に

インストールできたらテストをしておきましょう。 テストにはpython-noseが必要です。

[shell] $ sudo yum install python-nose $ python -c 'import numpy; numpy.test();' [/shell] 問題なければ「OK」と表示されます。

めでたし、めでたし。