0

Hello I am trying to pass this code to the command line everything works fine when I copy and paste it into my cmd shell by hand but when I use the exact command in my script it seems my command broke to multiple parts I don't know what's happening any idea?

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

above works fine when i copy and paste it in my CMD

bat "python -m PyInstaller --specpath ./artifacts-repo/${directoryName}/spec --distpath ./artifacts-repo/${directoryName}/dist --workpath ./artifacts-repo/${directoryName}/build --onefile ./codes/SayHello.py"

but when I try to pass that trough my pipline script it seems to shrinks!!! the result is bellow:

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

Look the command it shrinked to 4 commands:

> 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
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
  • 2
    The Microsoft documentation page about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) explains that the directory separator on Windows is ``\`` and not `/` as on Linux/Mac. So there should be used everywhere two backslashes in all argument strings with `/`. The first backslash is interpreted by Jenkins as escape character to interpret the second backslash literally written into the batch file created by Jenkins for execution. It would be also good to append to `python` also the file extension `.exe`, i.e. use `python.exe`. – Mofi Oct 09 '22 at 14:39
  • 2
    `${directoryName}` is Linux/Mac shell interpreter syntax to reference a variable with name `direcoryName`. The Windows Command Processor `cmd.exe` processing a batch file does not support this syntax. `cmd.exe` supports referencing __environment__ variables with the syntax `%directoryName%` or with `!directoryName!` if delayed expansion is explicitly enabled before the command line referencing environment variables with exclamation marks. – Mofi Oct 09 '22 at 14:42
  • 3
    It looks like `${directoryName}` is already expanded by Java on interpreting the lines of the Pipeline script before creating the batch file and running `cmd.exe` with option `/c` and the temporarily created batch file. The problem is obviously that the string value assigned to the variable `directoryName` contains at end a line ending character like carriage return and/or line-feed and therefore the command line in the batch file is just `python -m PyInstaller --specpath ./artifacts-repo/2022-10-09-174452`. The code used to define the value of the variable `directoryName` is the main cause. – Mofi Oct 09 '22 at 14:47
  • yes @Mofi That was the exact problem – Mohammad Taghadosi Oct 09 '22 at 14:55

1 Answers1

0

Pipeline script the commands and variables not a problem (for jenkins)

BUT THE PROBLEM WAS: (OH MY GOD I am FILLING SO SILLY NOW -_- ) a couple of line upper i used a command for getting the date and time prefixes for making a directory and i echoed it like bellow:

directoryName = bat(returnStdout: true,script: '@echo %date:~-4,4%-%date:~-10,2%%date:~7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%')

the echo command add a \n after the command that was why my command split! it can be fix by using a .trim() after the command like below

directoryName = bat(returnStdout: true,script: '@echo %date:~-4,4%-%date:~-10,2%%date:~7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%').trim()
  • I recommend to read the answers on [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351) or [Why does %date% produce a different result in batch file executed as scheduled task?](https://stackoverflow.com/a/44670322/3074564) I suggest to use the robocopy solution or the powershell command line instead of the dynamic variables `DATE` and `TIME` of `cmd.exe`. – Mofi Oct 09 '22 at 15:01