1. 程式人生 > >如何在Linux系統下把一個python模組轉換成一個rpm安裝包

如何在Linux系統下把一個python模組轉換成一個rpm安裝包

這樣做的目的一方面是解決那些不能訪問外網的主機安裝這些python模組的問題,另一方面是可以提高特定python模組的部署效率。
下文是根據使用需求,將一個python2.7使用的mysql-python模組製作為一個rpm安裝包,作為示例。類似的步驟可以在變換fpm選項引數後,用於滿足更多的rpm包製作需求。 我們先來看下為python2.7安裝一個mysql-python模組使其支援MySQLdb的常規方法:
yum -y install mysql-devel python27-devel
yum -y install python27-pip
pip2.7 install MySQL-python
對於原始碼編譯安裝的mysql,還需手動建立一個下面的軟連結,不然python2.7在import MySQLdb時會報錯:
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

上面的安裝過程中會有很多的依賴包問題,對於不能訪問外網的主機,達到這個安裝的目的是一個很大的挑戰。即便是那些可以訪問外網yum安裝源的主機,因為要經過多個步驟的下載、安裝過程,也略顯繁瑣。

下面是我們使用轉換後的rpm包,一個命令,不需要連網,立即達到相同的配置效果:

rpm -ivh  python-mysql-python-1.2.5-1.el6.x86_64.rpm
以下是製作這樣一個rpm包的全部步驟:   yum -y install ruby-devel gcc curl libyaml-devel   yum -y install mysql-devel python27-devel python27-setuptools
  command curl -sSL https://rvm.io/mpapis.asc | sudo gpg2 --import -   curl -L get.rvm.io | bash -s stable   source /etc/profile.d/rvm.sh   rvm requirements   rvm install 1.9.3   rvm use 1.9.3 --default   rvm rubygems current   gem install fpm   fpm --debug -s python -t rpm --python-bin python2.7 --python-install-lib /usr/lib/python2.7/site-packages --rpm-dist 'el6' mysql-python
詳情說明: 1、安裝rvm工具 Ruby enVironment Manager 一個Ruby的環境管理器,詳細介紹參見:https://github.com/rvm/rvm 上文步驟中,先安裝了一個rvm管理器,然後使用rvm安裝了Ruby 1.9.3,並作為預設版本使用。 2、安裝RubyGems打包工具
rvm rubygems current
switches version of rubygems for the current ruby,RubyGems(簡稱 gems)是一個用於對 Ruby元件進行打包的 Ruby 打包系統。
3、安裝fpm打包工具
gem install fpm
FPM功能簡單說就是將一種型別的包轉換成另一種型別。 FPM的github:https://github.com/jordansissel/fpm 支援的源型別包:
  • dir: 將目錄打包成所需要的型別,可以用於原始碼編譯安裝的軟體包
  • rpm: 對rpm進行轉換
  • gem: 對rubygem包進行轉換
  • python: 將Python模組打包成相應的型別
4、生成需要的rpm安裝包
fpm --debug -s python -t rpm --python-bin python2.7 --python-install-lib /usr/lib/python2.7/site-packages --rpm-dist 'el6' mysql-python
Usage:     fpm [OPTIONS] [ARGS] ... FPM引數:     [ARGS] ...                    Inputs to the source package type. For the 'dir' type, this is the files and directories you want to include in the package. For others, like 'gem', it specifies the packages to download and use as the gem input 。輸入源包型別。 對於“dir”型別,這是要包含在檔案包中的檔案和目錄。 對於其他情況,如“gem”,需要指定要下載和使用gem輸入的軟體包。例如上面命令列最後一個引數,即“mysql-python”模組的名稱。 FPM常用選項:
1 2 3 4 5 6 7 8 9 10 11 12 -s:指定源型別 -t:指定目標型別,即想要製作為什麼包 -n:指定包的名字 -v:指定包的版本號 -C:指定打包的相對路徑 -d:指定依賴於哪些包 -f:第二次包時目錄下如果有同名安裝包存在,則覆蓋它 -p:輸出的安裝包的目錄,不想放在當前目錄下就需要指定 --post-install:軟體包安裝完成之後所要執行的指令碼;同--offer-install --pre-install:軟體包安裝完成之前所要執行的指令碼;同--before-install --post-uninstall:軟體包解除安裝完成之後所要執行的指令碼;同--offer-remove --pre-uninstall:軟體包解除安裝完成之前所要執行的指令碼;同—before-remove --python-bin PYTHON_EXECUTABLE (python only) The path to the python executable you wish to run. (default: "python") --python-install-lib LIB_PATH (python only) The path to where python libs should be installed to --rpm-dist DIST-TAG           (rpm only) Set the rpm distribution.