【发布时间】:2022-10-20 22:02:11
【问题描述】:
您好,我正在尝试将此代码传递给命令行,当我手动将其复制并粘贴到我的 cmd shell 时,一切正常,但是当我在脚本中使用确切的命令时,我的命令似乎中断了多个部分,我不知道发生了什么任何想法?
python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452/spec --distpath ./artifacts-repo/2022-10-09-174452/dist --workpath ./artifacts-repo/2022-10-09-174452/build --onefile ./codes/SayHello.py
当我将其复制并粘贴到我的 CMD 中时,上面的工作正常
bat "python -m PyInstaller --specpath ./artifacts-repo/${directoryName}/spec --distpath ./artifacts-repo/${directoryName}/dist --workpath ./artifacts-repo/${directoryName}/build --onefile ./codes/SayHello.py"
但是当我尝试通过我的管道脚本传递它时,它似乎缩小了!!!结果如下:
C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
[--add-data <SRC;DEST or SRC:DEST>]
[--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
[--hidden-import MODULENAME]
[--collect-submodules MODULENAME]
[--collect-data MODULENAME] [--collect-binaries MODULENAME]
[--collect-all MODULENAME] [--copy-metadata PACKAGENAME]
[--recursive-copy-metadata PACKAGENAME]
[--additional-hooks-dir HOOKSPATH]
[--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
[--key KEY] [--splash IMAGE_FILE]
[-d {all,imports,bootloader,noarchive}]
[--python-option PYTHON_OPTION] [-s] [--noupx]
[--upx-exclude FILE] [-c] [-w]
[-i <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">]
[--disable-windowed-traceback] [--version-file FILE]
[-m <FILE or XML>] [--no-embed-manifest] [-r RESOURCE]
[--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
[--win-no-prefer-redirects] [--argv-emulation]
[--osx-bundle-identifier BUNDLE_IDENTIFIER]
[--target-architecture ARCH] [--codesign-identity IDENTITY]
[--osx-entitlements-file FILENAME] [--runtime-tmpdir PATH]
[--bootloader-ignore-signals] [--distpath DIR]
[--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a]
[--clean] [--log-level LEVEL]
scriptname [scriptname ...]
pyinstaller: error: the following arguments are required: scriptname
C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/spec --distpath ./artifacts-repo/2022-10-09-174452
'/spec' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/dist --workpath ./artifacts-repo/2022-10-09-174452
'/dist' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Ata System\AppData\Local\Jenkins\.jenkins\workspace\Pipeline-01>/build --onefile ./codes/SayHello.py
'/build' is not recognized as an internal or external command,
operable program or batch file.
script returned exit code 1
查看它缩小为 4 个命令的命令:
> python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452
> /spec --distpath ./artifacts-repo/2022-10-09-174452 /dist --workpath
> ./artifacts-repo/2022-10-09-174452 /build --onefile
> ./codes/SayHello.py
【问题讨论】:
-
关于Naming Files, Paths, and Namespaces 的Microsoft 文档页面解释说Windows 上的目录分隔符是
\,而不是Linux/Mac 上的/。所以应该在所有带有/的参数字符串中使用两个反斜杠。第一个反斜杠被 Jenkins 解释为转义字符,以解释第二个反斜杠字面上写入由 Jenkins 创建的批处理文件以供执行。将文件扩展名.exe附加到python也很好,即使用python.exe。 -
${directoryName}是 Linux/Mac shell 解释器语法,用于引用名称为direcoryName的变量。处理批处理文件的 Windows 命令处理器cmd.exe不支持此语法。cmd.exe支持引用环境如果在命令行引用带有感叹号的环境变量之前显式启用延迟扩展,则使用语法%directoryName%或!directoryName!的变量。 -
看起来
${directoryName}在创建批处理文件和运行cmd.exe选项/c和临时创建的批处理文件之前已经被Java 扩展为解释管道脚本的行。问题显然是分配给变量directoryName的字符串值在末尾包含一个行结束字符,如回车和/或换行符,因此批处理文件中的命令行只是python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452。用于定义变量directoryName值的代码是主要原因。 -
是的@Mofi那是确切的问题
标签: batch-file jenkins pipeline declarative