请原谅我这个可能微不足道的问题,但是:如何运行 pybuilder 发布的脚本?
我正在尝试遵循官方Pybuilder 教程。
我已经完成了这些步骤并成功生成了一个项目
- 运行单元测试
- 计算覆盖率
- 生成
setup.py
- 生成一个
.tar.gz
可供pip install
.
这一切都很好,但我仍然看不到实际的可运行工件是什么?
目录中包含的所有内容target
似乎或多或少与src
目录中的内容+附加报告和可安装档案中的内容完全相同。
“添加可运行脚本”部分末尾的教程本身得出结论,“脚本已被拾取”。好的,它已被拾取,现在我该如何运行它?本教程绝不会证明我们可以实际打印字符串“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
来自 SBT-shell 的命令——这就是为什么上面的七步配方对我来说有点可疑。
有没有类似pyb run
或pyb exec
插件的东西可以做同样的事情,但不需要我设置所有这些环境并安装任何东西?我正在寻找的是sbt run
在 SBT 或mvn exec:java
Maven 中的类比,它将构建所有内容,设置所有类路径,然后使用该main
方法运行类,而不会在项目目录之外留下任何痕迹。
由于源代码和目标的输出之间基本上没有区别,我可能错过了一些明显的方式来运行脚本。如果PyBuilder
根本不需要它本身,那也很好:我想要的只是以某种方式Hello, world! 42 * 42 - 42 = 1722
在终端中打印 -string 。