Ansible源碼獲取
Ansible Github:https://github.com/ansible
Ansible目錄結構
$ tree -L 2 ansible-2.0.0.0 ansible-2.0.0.0 |-- bin # 可執行程序存放目錄 | |-- ansible | |-- ansible-doc -> ansible | |-- ansible-galaxy -> ansible | |-- ansible-playbook -> ansible | |-- ansible-pull -> ansible | `-- ansible-vault -> ansible |-- CHANGELOG.md # 更新日志 |-- contrib | |-- inventory | `-- README.md |-- COPYING |-- docs # 文檔 | `-- man |-- examples # 主配置文件及hosts文件 | |-- ansible.cfg | `-- hosts |-- lib | |-- ansible | `-- ansible.egg-info |-- Makefile |-- MANIFEST.in |-- packaging | |-- arch | |-- debian | |-- gentoo | |-- macports | |-- port | `-- rpm |-- PKG-INFO |-- README.md |-- setup.cfg |-- setup.py `-- VERSION # 版本信息
setup.py解讀
#!/usr/bin/env python
import os
import sys
sys.path.insert(0, os.path.abspath('lib')) # 將lib目錄添加進環境變量 類似的方法sys.path.append() 兩者區別:追加和插入第一個位置
from ansible import __version__, __author__
try:
from setuptools import setup, find_packages
except ImportError:
print("Ansible now needs setuptools in order to build. Install it using"
" your package manager (usually python-setuptools) or via pip (pip"
" install setuptools).")
sys.exit(1)
setup(name='ansible',
version=__version__,
description='Radically simple IT automation',
author=__author__,
author_email='support@ansible.com',
url='http://ansible.com/',
license='GPLv3',
# Ansible will also make use of a system copy of python-six if installed but use a
# Bundled copy if it's not.
install_requires=['paramiko', 'jinja2', "PyYAML", 'setuptools', 'pycrypto >= 2.6'],
package_dir={ '': 'lib' },
packages=find_packages('lib'),
package_data={
'': ['module_utils/*.ps1', 'modules/core/windows/*.ps1', 'modules/extras/windows/*.ps1', 'galaxy/data/*'],
},
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'Intended Audience :: Information Technology',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Natural Language :: English',
'Operating System :: POSIX',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Topic :: System :: Installation/Setup',
'Topic :: System :: Systems Administration',
'Topic :: Utilities',
],
scripts=[
'bin/ansible',
'bin/ansible-playbook',
'bin/ansible-pull',
'bin/ansible-doc',
'bin/ansible-galaxy',
'bin/ansible-console',
'bin/ansible-vault',
],
data_files=[],
)
Python源碼包中的setup.py功能
setup.py功能:setup.py是python的一個項目發布管理工具。我們常常安裝別人的代碼也是借助setup.py
假設你要分發一個叫hello的模塊,文件名hello.py,那么setup.py內容如下
#!/usr/bin/env python
# coding: utf-8
from distutils.core import setup
setup(name='hello',
version="1.0",
py_modules=['hello'],
)
然后,運行python setup.py sdist為模塊創建一個源碼包
C:\Users\Administrator\PycharmProjects\untitled>python setup.py sdist running sdist running check 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 (using default file list) warning: sdist: standard file not found: should have one of README, README.txt writing manifest file 'MANIFEST' creating hello-1.0 copying files to hello-1.0... copying hello.py -> hello-1.0 copying setup.py -> hello-1.0 creating 'dist\hello-1.0.zip' and adding 'hello-1.0' to it adding 'hello-1.0\hello.py' adding 'hello-1.0\PKG-INFO' adding 'hello-1.0\setup.py' removing 'hello-1.0' (and everything under it)
在當前目錄下,會創建dist目錄,里面有個文件名為hello-1.0.zip,這個就是可以分發的包。使用者拿到這個包后,解壓,到hello-1.0目錄下執行:python setup.py install,那么,hello.py就會被拷貝到python類路徑下,可以被導入使用。
C:\Users\Administrator\PycharmProjects\untitled\dist\hello-1.0>python setup.py install running install running build running build_py creating build creating build\lib copying hello.py -> build\lib running install_lib copying build\lib\hello.py -> C:\Python27\Lib\site-packages byte-compiling C:\Python27\Lib\site-packages\hello.py to hello.pyc running install_egg_info Writing C:\Python27\Lib\site-packages\hello-1.0-py2.7.egg-info
對於Windows,可以執行python setup.py bdist_wininst生成一個exe文件;若要生成RPM包,執行python setup.py bdist_rpm,但系統必須有rpm命令的支持。可以運行下面的命令查看所有格式的支持:
C:\Users\Administrator\PycharmProjects\untitled\dist\hello-1.0>python setup.py bdist --help-formats List of available distribution formats: --formats=rpm RPM distribution --formats=gztar gzip'ed tar file --formats=bztar bzip2'ed tar file --formats=ztar compressed tar file --formats=tar tar file --formats=wininst Windows executable installer --formats=zip ZIP file --formats=msi Microsoft Installer
setup函數還有一些參數:
1、packages
告訴Distutils需要處理那些包(包含__init__.py的文件夾)
2、package_dir
告訴Distutils哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {'': 'lib'},表示“root package”中的模塊都在lib目錄中。
3、ext_modules
是一個包含Extension實例的列表,Extension的定義也有一些參數。
4、ext_package
定義extension的相對路徑
5、install_requires
定義依賴哪些模塊
6、provides
定義可以為哪些模塊提供依賴
7、scripts
指定python源碼文件,可以從命令行執行。在安裝時指定--install-script
8、package_data
通常包含與包實現相關的一些數據文件或類似於readme的文件。如果沒有提供模板,會被添加到MANIFEST文件中。
9、data_files
指定其他的一些文件(如配置文件)
setup(...,
data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
('config', ['cfg/data.cfg']),
('/etc/init.d', ['init-script'])]
)
規定了哪些文件被安裝到哪些目錄中。如果目錄名是相對路徑,則是相對於sys.prefix或sys.exec_prefix的路徑。如果沒有提供模板,會被添加到MANIFEST文件中。
執行sdist命令時,默認會打包哪些東西呢?
所有由py_modules或packages指定的源碼文件
所有由ext_modules或libraries指定的C源碼文件
由scripts指定的腳本文件
類似於test/test*.py的文件
README.txt或README,setup.py,setup.cfg
所有package_data或data_files指定的文件
還有一種方式是寫一個manifest template,名為MANIFEST.in,定義如何生成MANIFEST文件,內容就是需要包含在分發包中的文件。一個MANIFEST.in文件如下:
include *.txt recursive-include examples *.txt *.py prune examples/sample?/build
distutils模塊講解:https://docs.python.org/2/distutils/
相關知識擴展
setup.cfg
setup.cfg提供一種方式,可以讓包的開發者提供命令的默認選項,同時為用戶提供修改的機會。對setup.cfg的解析,是在setup.py之后,在命令行執行前。
setup.cfg文件的形式類似於
[command] option=value ...
其中,command是Distutils的命令參數,option是參數選項,可以通過python setup.py --help build_ext方式獲取。需要注意的是,比如一個選項是--foo-bar,在setup.cfg中必須改成foo_bar的格式
符合Distutils2的setup.cfg有些不同。包含一些sections:
1、global
定義Distutils2的全局選項,可能包含commands,compilers,setup_hook(定義腳本,在setup.cfg被讀取后執行,可以修改setup.cfg的配置)
2、metadata
3、files
packages_root:根目錄
packages
modules
scripts
extra_files
4、command sections
setuptools
上面的setup.py和setup.cfg都是遵循python標准庫中的Distutils,而setuptools工具針對Python官方的distutils做了很多針對性的功能增強,比如依賴檢查,動態擴展等。
內容還有很多,后期用到再進行擴展

