在每個openstack項目中,都有setup.py和setup.cfg這兩個文件。那setup.py和setup.cfg是什麼?如何使用?以keystone項目爲例,進行實踐和解析。
簡單的說,setup.py和setup.cfg是控制python項目打包、安裝的工具類。
Setup.py
我們從一個簡單的例子開始。假設要分發一個叫foo的模塊,文件名foo.py,那麼setup.py的內容如下:
fromdistutils.core import setup setup(name='foo',version='1.0',py_modules=['foo'],) |
然後,運行pythonsetup.py sdist爲模塊創建一個源碼包。
stack@liujunpeng-Inspur-Computer:~/python_setup$python setup.py sdist runningsdist runningcheck warning:check: missing required meta-data: url
warning:check: missing meta-data: either (author and author_email) or(maintainer and maintainer_email) must be supplied
warning:sdist: manifest template 'MANIFEST.in' does not exist (usingdefault file list)
warning:sdist: standard file not found: should have one of README,README.txt
writingmanifest file 'MANIFEST' creatingfoo-1.0 makinghard links in foo-1.0... hardlinking foo.py -> foo-1.0 hardlinking setup.py -> foo-1.0 Creatingtar archive removing'foo-1.0' (and everything under it) |
在當前目錄下,會創建dist目錄,裏面有個文件名爲foo-1.0.tar.gz,這個就是可以分發的包。使用者,把該包解壓,然後執行pythonsetup.pyinstall命令,就把該包安裝到了pyhon的類庫下面,一把會安裝在/usr/local/lib/python2.7/dist-packages/目錄下面。
Setup.py還支持將項目打包成可執行文件。如windows下的exe,執行pythonsetup.py bdist_wininst;Rehdat系列操作系統下面,打包成rpm包,執行命令pythonsetup.py bdist_rpm。使用pythonsetup.py bdist –help-fromats命令可以查看支持的打包格式。
執行sdist命令時,默認會打包那些東西呢?
1、所有由py_modules或packages指定的源碼文件
2、所有ext_modules或libraries指定的C源碼文件
3、由script指定的腳本文件
4、類似於test/test*.py的文件
5、README.txt或者README,setup.py,setup.cfg
6、所有package_data
以上參數都是setup函數支持的參數。
通過MANIFEST模版MANIFEST.in文件也可以定義分發包中需要包含的文件。Keystone項目中MANIFEST.in文件內容如下:
includeAUTHORS includebabel.cfg includeChangeLog includeCONTRIBUTING.txt includeLICENSE includeHACKING.rst includeREADME.rst includeopenstack-common.conf includerun_tests.py includerun_tests.sh includesetup.cfg includesetup.py includeTODO includetox.ini includeetc/* includehttpd/* graftbin graftdoc graftkeystone/tests grafttools graftexamples recursive-includekeystone *.json *.xml *.cfg *.pem README *.po *.pot *.sql global-exclude*.pyc *.sdx *.log *.db *.swp keystone/tests/tmp/* |
Setup.cfg
setup.cfg提供setup.py的默認參數,同時易於修改。Setup.py先解析setup.cfg文件,然後執行相關命令。在setup.cfg支持setctions:
1、global定義Distutils2的全局選項,可能包含commands、compilers,setup_hook(定義腳本,在setup.cfg被讀取後執行,可以修改setup.cfg的配置)
2、metadata
3、files。Package_root、packages、modules、scripts、extra_files
4、commandsections
keystone項目中的setup.cfg文件部分內容如下:
[metadata] name= keystone version= 2015.2 summary= OpenStack Identity description-file= README.rst author= OpenStack author-email= [email protected] home-page= http://www.openstack.org/ classifier= Environment:: OpenStack IntendedAudience :: Information Technology IntendedAudience :: System Administrators License:: OSI Approved :: Apache Software License OperatingSystem :: POSIX :: Linux ProgrammingLanguage :: Python ProgrammingLanguage :: Python :: 2 ProgrammingLanguage :: Python :: 2.7
[files] packages= keystone
[global] setup-hooks= pbr.hooks.setup_hook
[egg_info] tag_build= tag_date= 0 tag_svn_revision= 0
[build_sphinx] all_files= 1 build-dir= doc/build source-dir= doc/source
[compile_catalog] directory= keystone/locale domain= keystone |
PBR
pbr是setuptools的輔助工具,最初爲openstack開發,基於d2to1。Pbr會讀取和過濾setup.cfg中的內容,然後將解析後的數據提供給setup.py作爲參數,包括以下功能:
1、從gti中獲取Version,AUTHORS和ChangeLog信息
2、SphinxAutodoc。pbr會掃描project,找到所有模塊,生成stubfiles
3、Requirements。讀取requirements.txt文件,生成setup函數需要依賴包
4、long_description。從README.rst、README.txt或者READMEfile中生成long_description參數
那如何是使用pbr呢,其實很簡單,keystone中是這樣使用的。
setuptools.setup( setup_requires=['pbr'], pbr=True) |
Babel
openstack項目中是通過Babel庫實現國際化的。Babel提供了對distutils或setuptools的支持,包括一些命令。
1、compile_catalog。編譯目錄,從PO文件中提取message,並編譯進二進制的MO文件。
$./setup.py
compile_catalog --directory foobar/locale --locale pt_BR
2、extract_messages,從源碼文件中提取本地化字符串,生成PO模板
$./setup.py
extract_messages --output-file foobar/locale/messages.pot
3、update_catalog
基於PO模板文件更新已存在的翻譯目錄
總結
主要介紹了setup.py、setup.cfg、順帶說明了PBR和Babel對setup.py的支持和如何使用。
致謝
整理這個知識時,參加了網絡上不少博客文章,比如chris的博客http://blog.sina.com.cn/s/blog_4951301d0101etvj.html。感謝無私貢獻的博主。