我正在尝试获取setup.py
要与之配合使用的软件包的文件sdist
。该setup.py
文件的相关部分是:
from setuptools.command.test import test
[...]
class Tox(test):
"as described in
http://tox.readthedocs.org/en/latest/example/basic.html?highlight=setuptools#integration-with-setuptools-distribute-test-commands"
[...]
def run_tests(self):
if self.distribution.install_requires:
self.distribution.fetch_build_eggs(
self.distribution.install_requires)
if self.distribution.tox_requires:
self.distribution.fetch_build_eggs(self.distribution.tox_requires)
# import here, cause outside the eggs aren't loaded
import tox
import shlex
args = self.tox_args
if args:
args = shlex.split(self.tox_args)
else:
args = ""
errno = tox.cmdline(args=args)
sys.exit(errno)
entry_points ={}
distutils_ext = {'distutils.setup_keywords': [
"tox_requires = setuptools.dist:check_requirements", ]
}
entry_points.update(distutils_ext)
setup(
install_requires=['six', 'numpy', 'matplotlib', 'scipy', 'astropy>=1',
'Pillow', ],
cmdclass={
'test': PyTest, # this is to run python setup.py test
'tox': Tox,
},
# list of packages and data
packages=find_packages(),
# tests
tests_require=['pytest', 'pytest-cov'],
tox_requires=['tox'],
# other keywords, mostly metadata
)
如果我跑步python setup.py sdist
,开始时会收到警告:
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'tox_requires'
warnings.warn(msg)
但是sdist可以正常工作,它会创建一个tar.gz文件,可以用来安装我的软件包。
但是,如果我第二次运行它,它将以(这是Pillow构建的开始)开始:
warning: no previously-included files found matching '.editorconfig'
Building using 4 processes
_imaging.c: In function ‘getink’:
并开始将所有必需的软件包构建到.eggs目录中。
如果删除* egg-info目录,则可以重新运行该命令。如果我注释掉该tox_requires=[...]
行,则可以根据需要多次构建sdist。
现在,根据setuptools文档,上面的命令应该是运行将新参数添加到setup函数的正确方法。
按照副标题,问题不仅存在,sdist
而且可能是由于我对安装工具和需求的工作方式不了解。
如果我python setup.py tox
在没有安装Tox的地方运行,那么在安装一些测试包后,不应安装(即pytest
和pytest-cov
):
追溯(最近一次通话最近):[...]文件“ /usr/lib/python2.7/dist-packages/setuptools/command/test.py”,第127行,with_project_on_sys_path func()文件“ setup.py如果self.distribution.tox_requires是run_tests中第65行,则为:AttributeError:分发实例没有属性'tox_requires'
[更新]这tox_requires
也使安装过程中的点子非常混乱。如果已注释掉,我可以安装该软件包而不会出现任何问题;否则,它将开始编译软件包的源,并且系统地失败,因为numpy
在构建类似的东西时找不到scipy
如何获得setuptools以识别和正确使用tox_requires
?
解决此问题后,我认为我可以摆脱这里的虚假安装,在实现Tox
类方面做得更好,也许可以覆盖更多的东西,test
也可以直接从中派生Command
下述完整的(有效的)解决方案由8个文件(包括short README.rst
)组成,共有43行代码。这比原始问题中的代码少。
尽管很短,但它以非常方便的方式支持许多开发和测试方案。
无论如何,它不能完全回答您的问题,但是我敢肯定,它可以满足其背后的要求。
setup.py
从技术上讲,可以将test
包括tox
自动化在内的命令放入您的setup.py
,但是结果可能非常混乱且难以理解。
可以用简单得多的方式实现相同的结果:
对于开发人员,假设:
git
tox
安装到系统中对于包用户:
(可选)如果您希望用户通过单个命令测试程序包并将测试报告保存在中央服务器中:
devpi-server
并授予您的用户访问权限$ pip install devpi
该解决方案基于以下工具和软件包:
pbr
:简化包创建,包括 通过git标签进行版本控制,以及创建git commit消息AUTHORS
和ChangeLog
从git commit消息创建。pytest
:出色的测试框架,但可以使用任何其他框架代替它。tox
:出色的构建和测试自动化工具。coverage
:衡量测试覆盖率的工具(工作起来比之前更简单pytest-cov
)您也可以选择使用:
devpi-server
:具有密码保护访问权限的私人PyPi服务器。允许简单测试并提供测试报告收集。devpi
:类似于点子的工具。除安装外,还支持运行tox
定义的测试(安装,运行测试,按步发布报告)。创建新的项目目录并初始化git:
$ mkdir francesco
$ cd francesco
$ git init
在这里,我们创建了单个模块francesco
,但是对于更多的模块或包,同样如此。
francesco.py
def main():
print("Hi, it is me, Francesco, keeping things simple.")
requirements.txt
创建软件包列表以实际安装软件包:
six
test_requirements.txt
定义测试所需的软件包:
pytest
coverage
tests/test_it.py
启动测试套件:
from francesco import main
def test_this():
main()
print("All seems fine to me")
assert True
setup.py
您是否梦想过愚蠢的简单setup.py
?它去了:
from setuptools import setup
setup(setup_requires=["pbr"], pbr=True)
setup.cfg
元数据属于配置文件:
[metadata]
name = francesco
author = Francesco Montesano
author-email = [email protected]
summary = Nice and simply installed python module supporting testing in different pythons
description-file = README.rst
[files]
modules=francesco
[entry_points]
console_scripts =
francesco = francesco:main
tox.ini
要配置tox自动构建和测试:
[tox]
envlist = py27, py34
[testenv]
commands =
coverage run --source francesco -m pytest -sv tests
coverage report
coverage html
deps =
-rtest_requirements.txt
README.rst
永远不要忘记README.rst
:
===========================================
Complex package with 3 line long `setup.py`
===========================================
Can we keep`setup.py` simple and still support automated testing?
...
tox
:在所有受支持的python版本中构建sdist并运行测试在项目目录根目录下,只需运行单个命令tox
:
$ tox
GLOB sdist-make: /home/javl/sandbox/setuppy/setup.py
py27 inst-nodeps: /home/javl/sandbox/setuppy/.tox/dist/francesco-0.0.0.zip
py27 runtests: PYTHONHASHSEED='2409409075'
py27 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /home/javl/sandbox/setuppy/.tox/py27/bin/python2.7
cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile:
collecting ... collected 1 items
tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED
=========================== 1 passed in 0.01 seconds ===========================
py27 runtests: commands[1] | coverage report
Name Stmts Miss Cover
----------------------------------
francesco.py 2 0 100%
py27 runtests: commands[2] | coverage html
py34 inst-nodeps: /home/javl/sandbox/setuppy/.tox/dist/francesco-0.0.0.zip
py34 runtests: PYTHONHASHSEED='2409409075'
py34 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux -- Python 3.4.2, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /home/javl/sandbox/setuppy/.tox/py34/bin/python3.4
cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile:
collecting ... collected 1 items
tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED
=========================== 1 passed in 0.01 seconds ===========================
py34 runtests: commands[1] | coverage report
Name Stmts Miss Cover
----------------------------------
francesco.py 2 0 100%
py34 runtests: commands[2] | coverage html
___________________________________ summary ____________________________________
py27: commands succeeded
py34: commands succeeded
congratulations :)
ls .tox/dist
francesco-0.0.0.zip
$ source .tox/py27/bin/activate
(py27) $ py.test -sv tests
==============================================================================================
test session starts
===============================================================================================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
-- /home/javl/sandbox/setuppy/.tox/py27/bin/python2.7 cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile: collected 1 items
tests/test_it.py::test_this Hi, it is me, Francesco, keeping things
simple. All seems fine to me PASSED
============================================================================================
1 passed in 0.01 seconds
============================================================================================
(py27)$ coverage run --source francesco -m pytest -sv tests
.....
(py27)$ coverage report
Name Stmts Miss Cover
----------------------------------
francesco.py 2 0 100%
(py27)$ coverage html
(py27)$ firefox htmlcov/index.html
devpi-server
这里没有介绍devpi-server的安装,但是安装非常简单,特别是如果仅将其安装到本地计算机以进行个人测试时。
确保已提交所有源代码。
评估版本标签:
$ git tag -a 0.1
确保您已停用virtualenv(否则与tox冲突):
(py27)$ deactivate
运行tox
:
$ tox
.....
...it builds as usual, may fail, if you have forgotten to commit some changes or files...
查找软件包新版本的sdist:
$ ls .tox/dist/francesco-0.1.0.
.tox/dist/francesco-0.1.0.zip
大功告成 您可以像往常一样将软件包的新测试版本分发给用户。
假设执行以下步骤,您已经devpi-server
安装并运行。
$ devpi login javl
...enter your password...
$ devpi upload .tox/dist/francesco-0.1.0.zip
(如果激活,请禁用virtualenv):
$ cd /tmp
$ mkdir testing
$ cd testing
$ devpi test francesco
received http://localhost:3141/javl/dev/+f/4f7/c13fee84bb7c8/francesco-0.1.0.zip
unpacking /tmp/devpi-test6/downloads/francesco-0.1.0.zip to /tmp/devpi-test6/zip
/tmp/devpi-test6/zip/francesco-0.1.0$ tox --installpkg /tmp/devpi-test6/downloads/francesco-0.1.0.zip -i ALL=http://localhost:3141/javl/dev/+simple/ --recreate --result-json /tmp/devpi-test6/zip/toxreport.json
-c /tmp/devpi-test6/zip/francesco-0.1.0/tox.ini
py27 create: /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py27
py27 installdeps: -rtest_requirements.txt
py27 inst: /tmp/devpi-test6/downloads/francesco-0.1.0.zip
py27 installed: coverage==4.0.3,francesco==0.1.0,py==1.4.31,pytest==2.8.7,six==1.10.0,wheel==0.24.0
py27 runtests: PYTHONHASHSEED='3916044270'
py27 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py27/bin/python2.7
cachedir: .cache
rootdir: /tmp/devpi-test6/zip/francesco-0.1.0, inifile:
collecting ... collected 1 items
tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED
=========================== 1 passed in 0.01 seconds ===========================
py27 runtests: commands[1] | coverage report
Name Stmts Miss Cover
----------------------------------
francesco.py 2 0 100%
py27 runtests: commands[2] | coverage html
py34 create: /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py34
py34 installdeps: -rtest_requirements.txt
py34 inst: /tmp/devpi-test6/downloads/francesco-0.1.0.zip
py34 installed: coverage==4.0.3,francesco==0.1.0,py==1.4.31,pytest==2.8.7,six==1.10.0,wheel==0.24.0
py34 runtests: PYTHONHASHSEED='3916044270'
py34 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux -- Python 3.4.2, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py34/bin/python3.4
cachedir: .cache
rootdir: /tmp/devpi-test6/zip/francesco-0.1.0, inifile:
collecting ... collected 1 items
tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED
=========================== 1 passed in 0.01 seconds ===========================
py34 runtests: commands[1] | coverage report
Name Stmts Miss Cover
----------------------------------
francesco.py 2 0 100%
py34 runtests: commands[2] | coverage html
____________________________________________________________________________________________________ summary _____________________________________________________________________________________________________
py27: commands succeeded
py34: commands succeeded
congratulations :)
wrote json report at: /tmp/devpi-test6/zip/toxreport.json
posting tox result data to http://localhost:3141/javl/dev/+f/4f7/c13fee84bb7c8/francesco-0.1.0.zip
successfully posted tox result data
您可以在Web浏览器中检查测试结果:
$ firefox http://localhost:3141
然后搜索“ francesco”软件包,单击软件包名称,在表列中找到“毒素结果”,单击此处以显示环境设置和测试结果。
假设您devpi-server
正在运行,并且您的用户可以访问它。
用户应devpi
安装以下命令:
$ pip install devpi
(请注意,该工具未从安装任何工具devpi-server
)
帮助您的用户获得访问权限devpi-server
(此处不覆盖)。
然后,用户只需运行测试:
$ devpi test francesco
运行测试后(它会自动使用tox
,但用户不必关心它),您将在devpi Web界面上与之前找到的位置相同的位置找到测试结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句