Subprocess module fails to run command

前端 未结 3 529
说谎
说谎 2021-01-13 23:25

I\'m trying to execute Google\'s cpplint.py on a group of my files and collect the results to one log file. However, I have not managed to beat the subprocess module. My cur

相关标签:
3条回答
  • 2021-01-14 00:18

    I did it by replacing the def main() with the following (I editet the errorfunction too to get a proper csv-file):

    errorlog = sys.stderr
    sys.stderr = open("errorlog.csv","w")
    sys.stderr.write("File;Line;Message;Category;Confidence\n")
    for filename in filenames:
      ProcessFile(filename, _cpplint_state.verbose_level)
    _cpplint_state.PrintErrorCounts()
    sys.exit(_cpplint_state.error_count > 0)
    sys.stdout = errorlog
    sys.stderr.close()
    
    0 讨论(0)
  • 2021-01-14 00:19

    I would request you to run this first

    pipe = subprocess.Popen([cmd, options],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout, stderr = pipe.communicate()
    

    You will get to know what exactly is the error behind it, since CalledProcessError is raised only if the exit code was non-zero.

    0 讨论(0)
  • 2021-01-14 00:22

    Looks like cpplint.py is simply exiting with a non-zero return code - which it might do, for instance, if it finds errors or "lint" in the source files it is checking.

    See the documentation for subprocess.check_output. Note that if the command executed returns a non-zero exit code then a subprocess.CalledProcessError is raised.

    You could work around it by watching for CalledProcessError, e.g.

    try:
        output = subprocess.check_output(cmd)
    except subprocess.CalledProcessError as e:
        # ack!  cpplint.py failed... report an error to the user?
    

    EDIT:

    The SyntaxError seems to be the key here, and is probably caused by C:\Python32\lib being in your PYTHONPATH (either explicitly, or, this could happen if it is your current working directory).

    The Python interpreter (since about 1.5.2-ish) automatically runs import site when started. So, when this is the case, and your script goes to execute:

    c:/Python27/python.exe C:/users/me/Documents/dev/cpplint.py ...
    

    then the Python 2.7 interpreter will find C:\Python32\lib\site.py first, and try to load that, instead of the one (presumably) at C:\Python27\lib\site.py. The issue is that Python 3's site.py contains syntax incompatible with Python 2, so the process launched by subprocess.check_output is failing with a SyntaxError before it even gets a chance to run cpplint, which propagates the CalledProcessError.

    Solution? Make sure Python2 gets a bonafide Python2 "PYTHONPATH", and likewise for Python3! In other words, make sure C:\Python32\lib is not in the PYTHONPATH search path when running the Python2 interpreter.

    One way to do this in your case is to set an explicit environment when launching the process, e.g.:

    python2_env = {"PYTHONPATH": "path/to/python2/stuff:..."}
    output = subprocess.check_output(cmd, env=python2_env)
    
    0 讨论(0)
提交回复
热议问题