ansible源码解读
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 pythonimport os import syssys.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-8from distutils.core import setupsetup(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做了很多针对性的功能增强,比如依赖检查,动态扩展等。
内容还有很多,后期用到再进行扩展
转载于:https://www.cnblogs.com/sunshine-1/p/7533614.html
ansible源码解读相关推荐
- Bert系列(二)——源码解读之模型主体
本篇文章主要是解读模型主体代码modeling.py.在阅读这篇文章之前希望读者们对bert的相关理论有一定的了解,尤其是transformer的结构原理,网上的资料很多,本文内容对原理部分就不做过多 ...
- Bert系列(三)——源码解读之Pre-train
https://www.jianshu.com/p/22e462f01d8c pre-train是迁移学习的基础,虽然Google已经发布了各种预训练好的模型,而且因为资源消耗巨大,自己再预训练也不现 ...
- linux下free源码,linux命令free源码解读:Procps free.c
linux命令free源码解读 linux命令free源码解读:Procps free.c 作者:isayme 发布时间:September 26, 2011 分类:Linux 我们讨论的是linux ...
- nodeJS之eventproxy源码解读
1.源码缩影 !(function (name, definition) { var hasDefine = typeof define === 'function', //检查上下文环境是否为AMD ...
- PyTorch 源码解读之即时编译篇
点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 作者丨OpenMMLab 来源丨https://zhuanlan.zhihu.com/ ...
- Alamofire源码解读系列(九)之响应封装(Response)
本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...
- Feflow 源码解读
Feflow 源码解读 Feflow(Front-end flow)是腾讯IVWEB团队的前端工程化解决方案,致力于改善多类型项目的开发流程中的规范和非业务相关的问题,可以让开发者将绝大部分精力集中在 ...
- spring-session源码解读 sesion
2019独角兽企业重金招聘Python工程师标准>>> spring-session源码解读 sesion 博客分类: java spring 摘要: session通用策略 Ses ...
- 前端日报-20160527-underscore 源码解读
underscore 源码解读 API文档浏览器 JavaScript 中加号操作符细节 抛弃 jQuery,拥抱原生 JS 从 0 开始学习 GitHub 系列之「加入 GitHub」 js实现克隆 ...
- php service locator,Yii源码解读-服务定位器(ServiceLocator)
SL的目的也是解耦,并且非常适合基于服务和组件的应用. Service Locator充当了一个运行时的链接器的角色,可以在运行时动态地修改一个类所要选用的服务, 而不必对类作任何的修改. 一个类可以 ...
最新文章
- maven不能加载ojdbc14.jar的解决方法
- UVA - 11572
- WPF自定义控件的三种方式
- IBM研究院计画5年改变人类生活创新预测
- android开发那些事儿(三)--manifest文件中uses-sdk和project中target详解
- java画一只小猫程序代码_android 汤姆猫动画源码(主要就是模仿汤姆猫点击小猫给反应)...
- MFC中编辑框edit的用法
- 渗透测试——XP工具练习
- vant-ui的list
- 敏感词库 包含中英文
- Nginx入门5:搭建静态资源服务器;(入门级演示,没多少内容;)
- 轻健身餐的市场前景如何?如何选择一个投资小、美食和健身餐清淡的品牌?
- Microsoft Project
- 漫谈一条SQL语句的一生
- 高通手机系统属性配置:system.prop----build.prop是所有安卓手机都有的一些配置信息。
- jconsole是否可以在生产环境使用_使用rmi jmx JCONSOLE远程监控JVM
- ROOT后安装Magisk,使用Magisk Hide 解决建设银行APP-一元流量不能用问题
- php storm大数据处理,如何在eclipse调试storm程序
- 【行研资料】2020中国汽车用户消费洞察白皮书(2021)——附下载
- Android应用后台保活
热门文章
- 二分搜索/查找(最大化or最小化问题)
- swagger 上传文件 参数_跟我一起学.NetCore之Swagger让前后端不再烦恼及界面自定义...
- 迁安职中计算机专业,迁安职业技术教育中心2021年招生简章
- java 锁优化_Java中锁优化
- pandas 数据分析 相关性_Pandas库学习笔记2-Pandas数据特征分析
- oracle11g是什么语言,oracle11g下载
- TSAP(7) : ARIMA模型
- XML转换为dataframe
- 翻译: 3.线性神经网络 概览 深入神经网络 pytorch
- 极客大学架构师训练营 系统架构 淘宝架构 第四次作业