请原谅我可能会问的一个小问题,但是:我如何运行由pybuilder发布的脚本?
我正试图跟踪官方的Pybuilder教程。
我已经完成了这些步骤,并成功地生成了一个项目
setup.py.tar.gz使用的pip install。这一切都很好,但我还是不知道实际的可运行的艺术品是什么?
target目录中包含的所有内容似乎与src目录中的内容+其他报告和可安装档案的内容大致相同。
教程本身在“添加一个可运行的脚本”的末尾,-section得出结论,“脚本被选中了”。好的,它被捡起来了,现在我该怎么运行它?本教程从来没有证明我们实际上可以打印字符串"Hello,World!“在屏幕上,尽管事实是,整个玩具项目正是这样做的。
MCVE
下面是一个Bash脚本,它使用Python源文件和构建脚本生成以下目录树:
projectRoot
├── build.py
└── src
└── main
├── python
│ └── pkgRoot
│ ├── __init__.py
│ ├── pkgA
│ │ ├── __init__.py
│ │ └── modA.py
│ └── pkgB
│ ├── __init__.py
│ └── modB.py
└── scripts
└── entryPointScript.py
7 directories, 7 files
================================================================================
projectRoot/build.py
--------------------------------------------------------------------------------
from pybuilder.core import use_plugin
use_plugin("python.core")
use_plugin("python.distutils")
default_task = "publish"
================================================================================
projectRoot/src/main/scripts/entryPointScript.py
--------------------------------------------------------------------------------
#!/usr/bin/env python
from pkgRoot.pkgB.modB import b
if __name__ == "__main__":
print(f"Hello, world! 42 * 42 - 42 = {b(42)}")
================================================================================
projectRoot/src/main/python/pkgRoot/pkgA/modA.py
--------------------------------------------------------------------------------
def a(n):
"""Computes square of a number."""
return n * n
================================================================================
projectRoot/src/main/python/pkgRoot/pkgB/modB.py
--------------------------------------------------------------------------------
from pkgRoot.pkgA.modA import a
def b(n):
"""Evaluates a boring quadratic polynomial."""
return a(n) - n生成示例项目的完整脚本(免责声明:按原样提供,修改文件和目录,自行执行):
#!/bin/bash
# Creates a very simple hello-world like project
# that can be build with PyBuilder, and describes
# the result.
# Uses BASH heredocs and `cut -d'|' -f2-` to strip
# margin from indented code.
# strict mode
set -eu
# set up directory tree for packages and scripts
ROOTPKG_PATH="projectRoot/src/main/python/pkgRoot"
SCRIPTS_PATH="projectRoot/src/main/scripts"
mkdir -p "$ROOTPKG_PATH/pkgA"
mkdir -p "$ROOTPKG_PATH/pkgB"
mkdir -p "$SCRIPTS_PATH"
# Touch bunch of `__init__.py` files
touch "$ROOTPKG_PATH/__init__.py"
touch "$ROOTPKG_PATH/pkgA/__init__.py"
touch "$ROOTPKG_PATH/pkgB/__init__.py"
# Create module `modA` in package `pkgA`
cut -d'|' -f2- <<__HEREDOC > "$ROOTPKG_PATH/pkgA/modA.py"
|def a(n):
| """Computes square of a number."""
| return n * n
|
__HEREDOC
# Create module `modB` in package `pkgB`
cut -d'|' -f2- <<__HEREDOC > "$ROOTPKG_PATH/pkgB/modB.py"
|from pkgRoot.pkgA.modA import a
|
|def b(n):
| """Evaluates a boring quadratic polynomial."""
| return a(n) - n
|
__HEREDOC
# Create a hello-world script in `scripts`:
cut -d'|' -f2- <<__HEREDOC > "$SCRIPTS_PATH/entryPointScript.py"
|#!/usr/bin/env python
|
|from pkgRoot.pkgB.modB import b
|
|if __name__ == "__main__":
| print(f"Hello, world! 42 * 42 - 42 = {b(42)}")
|
__HEREDOC
# Create a simple `build.py` build script for PyBuilder
cut -d'|' -f2- <<__HEREDOC > "projectRoot/build.py"
|from pybuilder.core import use_plugin
|
|use_plugin("python.core")
|use_plugin("python.distutils")
|
|default_task = "publish"
|
__HEREDOC
#################################################
# Directory tree construction finished, only #
# debug output below this box. #
#################################################
# show the layout of the generater result
tree "projectRoot"
# walk through each python file, show path and content
find "projectRoot" -name "*.py" -print0 | \
while IFS= read -r -d $'\0' pathToFile
do
if [ -s "$pathToFile" ]
then
printf "=%.0s" {1..80} # thick horizontal line
echo ""
echo "$pathToFile"
printf -- "-%.0s" {1..80}
echo ""
cat "$pathToFile"
fi
done我找到的运行新构建项目的最简单方法如下(从包含projectRoot的目录中使用):
virtualenv env
source env/bin/activate
cd projectRoot
pyb
cd target/dist/projectRoot-1.0.dev0/dist/
pip install projectRoot-1.0.dev0.tar.gz
entryPointScript.py这确实成功地运行了脚本及其对用户定义的包的所有依赖项,并打印:
Hello, world! 42 * 42 - 42 = 1722但整个过程似乎相当复杂。作为比较,在SBT中类似的情况下,我只会发布
run从shell发出的命令--这就是为什么上面的七步食谱在我看来有点可疑的原因。
是否有类似于pyb run或pyb exec插件的功能,但不要求我设置所有这些环境并安装任何东西?我要寻找的是SBT中的sbt run或Maven中的mvn exec:java的类似物,它将构建所有东西,设置所有类路径,然后使用main方法运行类,而不会在项目目录之外留下任何跟踪。
由于源代码和目标的输出本质上没有区别,所以我可能忽略了如何运行脚本的一些显而易见的方法。如果根本不需要PyBuilder本身,那也没关系:我只想在终端中打印Hello, world! 42 * 42 - 42 = 1722-string。
发布于 2018-04-09 20:39:26
显然,以下工作流程:
正是PyBuilder 在这次演讲中的创建者提出的。
注意到链接视频是从2014年开始的。如果有人能提出最近提供的更精简的解决方案,我当然会接受这一点。
发布于 2019-08-29 07:36:45
在build.py中创建任务
@task
def run(project):
path.append("src/main/python")
from test_pack import test_app
test_app.main()尝试:
pyb run
https://stackoverflow.com/questions/49737459
复制相似问题