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做了很多针对性的功能增强,比如依赖检查,动态扩展等。

内容还有很多,后期用到再进行扩展

ansible源码解读的更多相关文章

  1. SDWebImage源码解读之SDWebImageDownloaderOperation

    第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...

  2. SDWebImage源码解读 之 NSData+ImageContentType

    第一篇 前言 从今天开始,我将开启一段源码解读的旅途了.在这里先暂时不透露具体解读的源码到底是哪些?因为也可能随着解读的进行会更改计划.但能够肯定的是,这一系列之中肯定会有Swift版本的代码. 说说 ...

  3. SDWebImage源码解读 之 UIImage+GIF

    第二篇 前言 本篇是和GIF相关的一个UIImage的分类.主要提供了三个方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name ----- 根据名 ...

  4. SDWebImage源码解读 之 SDWebImageCompat

    第三篇 前言 本篇主要解读SDWebImage的配置文件.正如compat的定义,该配置文件主要是兼容Apple的其他设备.也许我们真实的开发平台只有一个,但考虑各个平台的兼容性,对于框架有着很重要的 ...

  5. SDWebImage源码解读_之SDWebImageDecoder

    第四篇 前言 首先,我们要弄明白一个问题? 为什么要对UIImage进行解码呢?难道不能直接使用吗? 其实不解码也是可以使用的,假如说我们通过imageNamed:来加载image,系统默认会在主线程 ...

  6. SDWebImage源码解读之SDWebImageCache(上)

    第五篇 前言 本篇主要讲解图片缓存类的知识,虽然只涉及了图片方面的缓存的设计,但思想同样适用于别的方面的设计.在架构上来说,缓存算是存储设计的一部分.我们把各种不同的存储内容按照功能进行切割后,图片缓 ...

  7. SDWebImage源码解读之SDWebImageCache(下)

    第六篇 前言 我们在SDWebImageCache(上)中了解了这个缓存类大概的功能是什么?那么接下来就要看看这些功能是如何实现的? 再次强调,不管是图片的缓存还是其他各种不同形式的缓存,在原理上都极 ...

  8. AFNetworking 3.0 源码解读 总结(干货)(下)

    承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...

  9. AFNetworking 3.0 源码解读 总结(干货)(上)

    养成记笔记的习惯,对于一个软件工程师来说,我觉得很重要.记得在知乎上看到过一个问题,说是人类最大的缺点是什么?我个人觉得记忆算是一个缺点.它就像时间一样,会自己消散. 前言 终于写完了 AFNetwo ...

随机推荐

  1. MT【125】四点共圆

    (2017湖南省高中数学竞赛16题) \(AB\)是椭圆\(mx^2+ny^2=1(m>0,n>0,m\ne n)\)的斜率为 1 的弦.\(AB\)的垂直平分线与椭圆交于两点\(CD\) ...

  2. MT【111】画图估计

    评:此类方程是超越方程,一般情况下无法解出具体的解,常见手段:1.画图  2.猜根.此处可以取特殊值a=2.5,b=3.5,容易知道此时$x=2.5\in(2,3)$

  3. MT【99】2005联赛二试题我的一行解法

    为表示尊敬先展示参考答案:参考答案其实很好的体现了当年出题人陶平生的想法,就是利用已知形式联想到三角里的射影定理,从而写出余弦定理形式,利用三角解题,如下: 这里展示以下几年前做这题时我的解法: $\ ...

  4. BZOJ 2242 [SDOI2011]计算器 | BSGS

    insert的时候忘了取模了-- #include <cstdio> #include <cmath> #include <cstring> #include &l ...

  5. PostgreSQL(一)教程 -----高级特性

    一.视图 假设天气记录和城市为止的组合列表对我们的应用有用,但我们又不想每次需要使用它时都敲入整个查询.我们可以在该查询上创建一个视图,这会给该查询一个名字,我们可以像使用一个普通表一样来使用它: C ...

  6. Go(02)windows环境搭建和vscode配置

    之前讲述过linux环境下Go语言开发环境搭建,这次简述下windows的搭建以及vscode配置 windows环境搭建 同样去https://studygolang.com/dl下载windows ...

  7. 论攻击Web应用的常见技术

    攻击目标: 应用HTTP协议的服务器和客户端.以及运行在服务器上的Web应用等. 攻击基础: HTTP是一种通用的单纯协议机制.在Web应用中,从浏览器那接受到的HTTP请求的全部内容,都可以在客户端 ...

  8. python---django中form组件(2)自定制属性以及表单的各种验证,以及数据源的实时更新,以及和数据库关联使用ModelForm和元类

    自定义属性以及各种验证 分析widget: class TestForm(forms.Form): user = fields.CharField( required = True, widget = ...

  9. bzoj千题计划283:bzoj4516: [Sdoi2016]生成魔咒(后缀数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=4516 考虑在后面新加一个字母产生的影响 假设是第i个 如果不考虑重复,那么会增加i个不同的字符串 考 ...

  10. Excel VBA保护工作表

    '设定可编辑区域 ActiveSheet.Protection.AllowEditRanges.Add Title:="区域1", Range:=Range("E5:H1 ...