没有项目描述
setup的Python项目详细描述
设置
gitignore.io用于setup.py
setupy是一个生成由composable组成的setup.py
文件的工具
功能和设置的层次。
使用
setup.py
文件可以通过以下两种方式之一从setupy.dev生成
- Web UI(启用或不启用JavaScript)
- API
Web上使用API的一些示例
# Get a base setup.py file that can be extended
curl https://setupy.dev/get
# Include the help text to make it easier to manually edit the output
curl https://setupy.dev/get?include_help=true
# Include a feature and setting
curl https://setupy.dev/get?features=merge&settings=add_upload_command
# Get a list of all the available features
curl https://setupy.dev/features/list
# Get a list of all available settings
curl https://setupy.dev/settings/list
在命令行上(如果安装了setupy
python -m setupy \
-s base add_upload_command add_long_description \
--include-setting "$(cat setupy.yaml)"
python -m setupy help
注意:传入设置时,顺序很重要
setupy生成什么
setupy的setup.py
文件是由setupy生成的,因此它是一个很好的示例
能产生什么。这是较早(较小)的版本:
# 1
import os
import sys
from os import path
from shutil import rmtree
from setuptools import Command, find_packages, setup
# 2
here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
VERSION = open("VERSION.txt").read()
def merge(*dicts):
r = {}
for d in dicts:
r.update(d)
return r
# 3
base = {
"name": "mypackage",
"version": "0.1.0",
"packages": find_packages(exclude=['contrib', 'docs', 'test'])
}
add_long_description = {
"long_description": long_description,
"long_description_content_type": "text/markdown"
}
setupy = {
"name": "setupy",
"version": VERSION,
"install_requires": ["isort>=4.3", "pyyaml>=3.13", "flask>=1.0.2"],
"extras_require": {
"dev": ["pytest", "pytest-cov"],
"neovim": ["neovim"]
}
}
# 4
setup(**merge(base, add_upload_command, add_long_description, setupy))
setupy生成的文件中有4个主要部分
- 进口
- 功能
- 设置
- 合并所有设置层并调用
设置 < > >
设置
让我们从头开始看看每个部分的动机。我们的最终目标是
设置
来自设置工具
(这是每个setup.py
文件的最终目标。设置命令 接受多个关键字参数。为了使事情更模块化,我们可以创建一个数字 将字典合并在一起,然后用
**
解包,并在结果上执行setup
。这是setupy的核心模型。设置用于生成最终合并的词典 并传递到
安装程序
。基本设置:base = { "name": "mypackage", "version": "0.1.0", "packages": find_packages(exclude=['contrib', 'docs', 'test']) }
由setupy提供,在上面的示例中,setupy和setupy等设置在设置文件中使用
setupy = { "name": "setupy", "version": VERSION, "install_requires": ["isort>=4.3", "pyyaml>=3.13", "flask>=1.0.2"], "extras_require": { "dev": ["pytest", "pytest-cov"], "neovim": ["neovim"] } }
在合并期间覆盖以前的设置。在合并的最终调用中
merge(base, add_upload_command, add_long_description, setupy)
参数列表中的早期词典的值将被后期词典重写。所以在这种情况下, 由
base
传入的默认名称将被设置
字典覆盖。你可以从setupy
并使用提供的任何功能或设置定义一个新字典,然后对其进行扩充 通过编辑生成的设置文件。也可以使用命令行提供自定义设置或功能 就像setupy生成自己的
setup.py
文件一样:python -m setupy \ -s base add_upload_command add_long_description \ --include-setting "$(cat setupy.yaml)"
设置将按照它们传入api的顺序相互覆盖(传递到merge),无论 在网络上或通过命令行。如果你想知道合并是如何工作的,那就没有秘密了。这是一个包含的功能!
def merge(*dicts): r = {} for d in dicts: r.update(d) return r
功能
为了支持
setup.py
文件的特殊功能(例如从version.txt
文件获取版本 或通过上传我们需要能够将python代码添加到我们的
setup.py
文件中。功能添加了此功能。 例如,setupy中的标准功能允许从version.txt中提取版本
VERSION = open("VERSION.txt").read()
以后的设置可以使用
版本
变量。为了避免手动跟踪所需的功能 要激活哪些设置,设置可以声明其功能依赖项:
0# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
添加设置时,将自动拉入设置所依赖的任何功能。功能也可能依赖于其他 功能。
注意:目前还没有很多围绕依赖关系图行为构建的验证逻辑。修长时要小心 依赖链。
进口
功能和设置可能需要导入python模块才能工作。这些依赖项也在yaml文件中声明。 从
上载功能中获取此示例:
1# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
要使此功能正常工作,需要四个导入。文件生成后,它们都将被拉入。很多功能和设置 最终可能依赖于相同的进口。为了防止文件顶部出现混乱的导入集合,所有 在编写最终的
setup.py
之前,导入通过isort运行。为什么设置y
每当我启动一个新的python项目时,我都会做一些事情,更新pipenv并启动一个新的shell,我会在git init中添加一个
.gitignore
来自gitignore.io的文件,并为我的项目文件(主模块, a\u main\u
,一些\u init.py
文件和测试/
主要。最后,我想创建一个
setup.py
文件,并可能将我的模块发布到pypi。为了做到这一点我 不可避免地要查找一个要复制的setup.py
文件,找出我想要的选项,然后花几分钟填写所有内容。我想要一个像gitignore.io这样的解决方案,它有一个易于使用的api,可以与
curl
很好地交互,并且足够模块化 与许多不同的项目一起工作。我希望此存储库中的功能和设置数量继续增长 setupy可以成为一个非常有用的工具,用于生成标准化和可定制的setup.py
文件。自托管
标准存储库可能不适合所有人。您或您的组织可能还希望在以下项目上使用setupy 您希望保留对生成的任何功能或设置的权限,而不使它们成为开放源代码。setupy可以自托管 并通过拉动和运行Docker容器指向一组自定义设置和功能:
2# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
定义自己的功能
功能位于环境变量setupy-features指向的目录中。这个文件有两种类型 目录
- 定义功能元数据的.yaml文件
- 定义功能代码的.py文件
让我们定义一个名为
long_description
的新功能。首先,我们创建元数据文件long_description.yaml
3# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
此功能只有一个从属导入。名称在这里很重要,它必须与文件名相同。接下来我们创建 python文件,包含将被拉入我们的
setup.py
文件的代码:
4# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
就这样。我们现在可以使用此自定义功能生成
setup.py
文件功能
.yaml
文件的完整模式如下:
5# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
列表中的每个条目
< h2>定义自己的设置依赖项。features
应该是现有功能的名称。所有功能必须有唯一的名称。设置位于环境变量setupy设置指向的目录中。此文件包含 看起来像:
6# Get a base setup.py file that can be extended curl https://setupy.dev/get # Include the help text to make it easier to manually edit the output curl https://setupy.dev/get?include_help=true # Include a feature and setting curl https://setupy.dev/get?features=merge&settings=add_upload_command # Get a list of all the available features curl https://setupy.dev/features/list # Get a list of all available settings curl https://setupy.dev/settings/list
dependencies对象与
features
中的dependencies对象具有相同的模式。请注意,设置
可能不依赖于设置。 这是一个有意的设计选择:- 使依赖链尽可能短且清晰
- 在构造要合并的词典的最终排序列表时,防止出现复杂且可能不可预知的排序行为 < > >
您可能会注意到
属性
对象中的\"
(转义引号)字符。因为属性可以引用变量和 文本字符串,当properties对象被转换为python字典时,必须有一种方法来区分它们 包含在setup.py
文件中。文字字符串必须用引号和转义引号括起来(如"\"text/markdown"
)。 变量不应包含在两个变量中(如long_description
)。此文件中的name属性应与文件名匹配(减去
.yaml
扩展名),并将用作要分配的变量 创建seutp.py
时的结果字典。因此,所有设置必须具有唯一的名称。推荐PyPI第三方库