0

i am trying to run a python code, which calls the check_output() function for maven commands.

The first 'mvn clean test' works fine, but then i got stuck in an Error for the second mvn command, where it says:

File ~\Documents\PowerTac\powertac\powertac-tools\python-scripts\test_Extractor.py:29 in <module>
    print(subprocess.check_output(['mvn', 'exec:exec', '-Dexec.args='+ args],

  File ~\Anaconda3\envs\env_cirebro\lib\subprocess.py:415 in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,

  File ~\Anaconda3\envs\env_cirebro\lib\subprocess.py:516 in run
    raise CalledProcessError(retcode, process.args,

CalledProcessError: Command '['mvn', 'exec:exec', '-Dexec.args="org.powertac.logtool.example.MktPriceStats D:/Powertac/finals-2021/3/finals_2021_3.tar.gz D:/Powertac/finals-2021/data/mktPr3.csv"']' returned non-zero exit status 255.

My code looks like this:

    import os, subprocess

processEnv = {'JAVA_HOME': os.environ.get('JAVA_HOME'),
              'Path' : os.environ.get('PATH')}

logtoolDir = 'C:/Users/phili/Documents/PowerTac/powertac/powertac-tools/logtool-examples/'
logtoolDir = logtoolDir.replace(os.sep, '/')


args = r'"org.powertac.logtool.example.MktPriceStats D:/Powertac/finals-2021/3/finals_2021_3.tar.gz D:/Powertac/finals-2021/data/mktPr3.csv"'
extractorClass = 'MktPriceStats'
tarPath ='D:/Powertac/finals-2021/3/finals_2021_3.tar.gz'
dataDir = 'D:/Powertac/finals-2021/data'
datafileName = 'mktPr3.csv'

args = ''.join(['"org.powertac.logtool.example.' + extractorClass, ' ',
                tarPath, ' ',
                dataDir + "/" + datafileName + '"'])
print('mvn', 'exec:exec', '-Dexec.args='+ args)
print(subprocess.check_output(['mvn', 'clean', 'test'],
                             shell = True, env = processEnv, cwd = logtoolDir))
print(subprocess.check_output(['mvn', 'exec:exec', '-Dexec.args='+ args],
                           shell = True, env = processEnv, cwd = logtoolDir))

I assume that somehow the 'shell=True' is hiding that the command is not correct.

When I type in the command-string that was produced by my code into the cmd, everthing works. Maybe even my env varibales are set wrong.

Does anyone have a suggestion how i can solve it?

Thanks.

  • Using `shell = True` is almost always a mistake when you're simply telling the shell to run a program without using any shell features (e.g., I/O redirection). Also, exit code 255 can occur for several reasons. See, https://tldp.org/LDP/abs/html/exitcodes.html. – Kurtis Rader Jan 27 '22 at 17:44
  • @KurtisRader The link is dubious for a couple of reasons. One, the ABS is always dubious; two, the shell here might not be Bash at all. Python uses `/bin/sh` to run subprocesses unless you say otherwise; this is now not a symlink to Bash even on many Linux systems. The OP seems to be on Windows anyway. – tripleee Jan 27 '22 at 18:18
  • Indeed, if they had run this on a non-Windows system, the `shell=True` would have meant they had run simply `mvn` with the other arguments spilling into the shell's argument list, where they would be ignored entirely. (I'm thinking Python should warn about this, but meh.) Though yes, the `shell=True` is almost certainly wrong here. See also [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Jan 27 '22 at 18:20
  • Yes, I'm using Windows. So my first assumption was right. When I set shell=False, I get a FileNotFoundError: [WinError 2]. I think python cannot find the executable, right? Now I tried to point directly to the path of the executable with shutil.which('mvn') and the FileNotFoundError is gone, but exit code 255 comes back. – pweiler Jan 28 '22 at 11:17

0 Answers0