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

前端 未结 1 1270
刺人心
刺人心 2021-01-15 20:50

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

define NEWLINE


endef

define PYTHON_SCRIPT_CODE
import sys
print(\         


        
1条回答
  •  悲&欢浪女
    2021-01-15 21:28

    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)
    

    0 讨论(0)
提交回复
热议问题