Evandro Coan
Evandro Coan

Reputation: 9418

How to send input on stdin to a python script defined inside a Makefile?

Given this question answer, Embed Python in Makefile to set make variables which works!

define NEWLINE


endef

define PYTHON_SCRIPT_CODE
import sys
print("hi")
endef

SDK_PATH := $(shell echo \
    '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \
    sed 's/@NEWLINE@/\n/g' | python -)

default:
    @echo 'SDK Path Detected: $(SDK_PATH)'

How to pipe data to python std in? For example:

define PYTHON_SCRIPT_CODE
import sys
print("hi")
print(sys.stdin.read())
endef

# pseudocode
SDK_PATH := $(shell bash --version | PYTHON_SCRIPT_CODE)
default:
    @echo 'SDK Path Detected: $(SDK_PATH)'

Would output:

SDK Path Detected: hi
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

For now, I am doing this:

define NEWLINE


endef

BASH_VERSION := $(shell bash --version)
define PYTHON_SCRIPT_CODE
import sys
print("Hi")
print("${BASH_VERSION}")
endef

SDK_PATH := $(shell echo \
    '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \
    sed 's/@NEWLINE@/\n/g' | python -)

default:
    @echo 'SDK Path Detected: $(SDK_PATH)'

Results: (No new lines)

enter image description here


Related questions:

  1. Is it possible to create a multi-line string variable in a Makefile
  2. https://unix.stackexchange.com/questions/222911/using-embedded-python-script-in-makefile

New example, without piping things into the Python Script:

#!/usr/bin/make -f
ECHOCMD:=/bin/echo -e
SHELL := /bin/bash

define NEWLINE


endef

VERSION := $(shell bash --version)

# With this, you cannot use single quotes inside your python code
define PYTHON_VERSION_CODE
import re, sys;
program_version = """${VERSION}"""
match = re.search("Copyright[^\d]+(\d+)", program_version);

if match:
    if int( match.group(1) ) >= 2018:
        sys.stdout.write("1")
    else:
        sys.stdout.write( match.group(1) )
else:
    sys.stdout.write("0")
endef

# Due to this, you cannot use single quotes inside your python code
PYTHON_SCRIPT_RESULTS := $(shell echo \
    '$(subst $(NEWLINE),@NEWLINE@,${PYTHON_VERSION_CODE})' | \
    sed 's/@NEWLINE@/\n/g' | python -)

all:
    printf 'Results: %s\n' "${PYTHON_SCRIPT_RESULTS}"

Results:

enter image description here

  1. Makefile as an executable script with shebang?

Upvotes: 1

Views: 1289

Answers (1)

Vroomfondel
Vroomfondel

Reputation: 2898

For me the easiest way to circumvent all the command line quoting problems is to write the code to a file with GNUmake's $(file ) function. You can even use # as Python comment inside of defined variables:

VERSION := $(shell bash --version)

# With this, you can use any form of quotes inside your python code
define PYTHON_VERSION_CODE :=
# -*- coding: iso-8859-15 -*-
import re, sys;
program_version = "${VERSION}"
match = re.search("Copyright[^\d]+(\d+)", program_version);

if match:
    if int( match.group(1) ) >= 2018:
        sys.stdout.write("1")
    else:
        sys.stdout.write( "match:" + match.group(1) )
else:
    sys.stdout.write("0")
endef


$(file > test.py,$(PYTHON_VERSION_CODE))

PYTHON_SCRIPT_RESULTS := $(shell python test.py)

.PHONY: all
all:
    @echo $(PYTHON_SCRIPT_RESULTS)

Upvotes: 1

Related Questions