Kokesh
Kokesh

Reputation: 3263

How to check in makefile if python script outputs OK

I have a Python script, which checks my language translation files (in CSV format), if all the lines contain translations for all languages included in the CSV's first, header line. Script lists files/lines with missing translations. If no problem is found, it outputs OK. My question is following: how do I call the script from within the makefile and check, if the output was OK? If something else than OK was printed out by the script, I want the makefile to stop.

Any ideas?

Upvotes: 0

Views: 1424

Answers (3)

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25533

If modifying the Python script so that it would indicate its result using an exit code is not possible, you can implement it in Make as follows (assuming you work on *nix):

check-i18n:
    @if [ `python your_script.py` = 'OK' ]; \
        then echo "good enough"; \
        else echo "too bad"; exit 1; \
    fi

Another option, less readable, but cross-platform (so it will work on Windows as well):

# String equality check.
eq = $(findstring $1,$(findstring $2,$1))

check-i18n:
    @$(if $(call eq,OK,$(shell python your_script.py)), \
        echo "good enough", \
        echo "too bad"; exit 1)

Upvotes: 1

mgilson
mgilson

Reputation: 310147

make checks output status, not text which is output. The simplest solution is to use sys.exit(1) in your python script if it doesn't check out OK.

for example:

targetfile: dependsonfile
      python pythonscript.py -o targetfile dependsonfile

Of course the actual syntax will depend critically on how pythonscript.py is meant to be called.

If your pythonscript just does a check, you can accomplish that as follows:

makethis: didcheck
    echo "made it" > makethis   

didcheck:  #dependencies here
    python -c 'import sys; sys.exit(1)'
    touch didcheck

then you call make as make makethis.

Upvotes: 3

Inbar Rose
Inbar Rose

Reputation: 43487

as my answer to this question

import os
import copy
import subprocess

def command(command):
    env = copy.deepcopy(os.environ)
    proc = subprocess.Popen(command,
                shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result = proc.stdout.read()
    return result

ret = command(YOUR COMMAND HERE)

if ret == "OK":
    print "yay"
else:
    print "oh no!: %s" % ret

if you showed your code i could better implement my answer into it.

Upvotes: 0

Related Questions