[英]Create different distribution types with setup.py
Given the following (demonstration) project layout: 给出以下(演示)项目布局:
MyProject/
README
LICENSE
setup.py
myproject/
... # packages
extrastuff/
... # some extra data
How (and where) do I declare different distribution types? 我如何(以及在何处)声明不同的分布类型? Especially I need these two options: 特别是我需要这两个选项:
A distribution containing only the source 仅包含源的分发
A distribution containing the source and all data files under (extrastuff) 包含源和所有数据文件的分发(extrastuff)
Ideally, how do I declare the upper two configuration whereas the second one depends on the first one? 理想情况下,如何声明上面两个配置,而第二个配置取决于第一个配置?
I've implemented something like this before ... the sdist
command can be extended to handle additional command line arguments and to manipulate the data files based on these. 我之前实现了类似的东西......可以扩展sdist
命令来处理额外的命令行参数并根据这些参数来操作数据文件。 If you run python setup.py sdist --help
, it'll include your custom command line arguments in the help, which is nice. 如果你运行python setup.py sdist --help
,它会在帮助中包含你的自定义命令行参数,这很好。 Use the following recipe: 使用以下配方:
from distutils import log
from distutils.core import setup
from distutils.command.sdist import sdist
class CustomSdist(sdist):
user_options = [
('packaging=', None, "Some option to indicate what should be packaged")
] + sdist.user_options
def __init__(self, *args, **kwargs):
sdist.__init__(self, *args, **kwargs)
self.packaging = "default value for this option"
def get_file_list(self):
log.info("Chosen packaging option: {self.packaging}".format(self=self))
# Change the data_files list here based on the packaging option
self.distribution.data_files = list(
('folder', ['file1', 'file2'])
)
sdist.get_file_list(self)
if __name__ == "__main__":
setup(
name = "name",
version = "version",
author = "author",
author_email = "author_email",
url = "url",
py_modules = [
# ...
],
packages = [
# ...
],
# data_files = default data files for commands other than sdist if you wish
cmdclass={
'sdist': CustomSdist
}
)
You could extend setup.py to additionally include some custom command line parsing. 您可以扩展setup.py以另外包含一些自定义命令行解析。 You could then catch a custom argument and strip it out so that it won't affect setuptools
. 然后,您可以捕获自定义参数并将其删除,以便它不会影响setuptools
。
You can access the command line argument in sys.argv
. 您可以访问sys.argv
的命令行参数。 As for modifying the call to setuptools.setup()
, I recommend creating a dictionary of arguments to pass, modifying the dictionary based on the command line arguments, and then calling setup()
using the **dict
notation, like so: 至于修改对setuptools.setup()
的调用,我建议创建一个要传递的参数字典,根据命令行参数修改字典,然后使用**dict
表示法调用setup()
,如下所示:
from setuptools import setup
import sys
basic = {'name': 'my program'}
extra = {'bonus': 'content'}
if '--extras' in sys.argv:
basic.update(extra)
sys.argv.remove('--extras')
setup(**basic)
For more thorough command line parsing you could also use the getopt
module , or the newer argparse
module if you're only targeting Python 2.7 and higher. 对于更彻底的命令行解析,如果您只针对Python 2.7及更高版本,也可以使用getopt
模块或更新的argparse
模块 。
EDIT : I also found a section in the distutils documentation titled Creating a new Distutils command . 编辑 :我还在distutils文档中找到了一个标题为Creating a new Distutils命令的部分 。 That may also be a helpful resource. 这也可能是一个有用的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.