如何将stdin上的输入发送到Makefile中定义的python脚本? [英] How to send input on stdin to a python script defined inside a Makefile?

查看:80
本文介绍了如何将stdin上的输入发送到Makefile中定义的python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下问题的答案,在Makefile中嵌入Python以设置make变量有效!

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)'

如何通过管道将数据传输到python std中?例如:

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)'

将输出:

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)'

结果:(无新行)

相关问题:

  1. 可以吗?在Makefile中创建多行字符串变量
  2. https://unix.stackexchange.com/questions/222911/using -embedded-python-script-in-makefile
  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


新示例,没有在Python脚本中添加内容:


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}"

结果:

  1. 将文件制作为带有shebang的可执行脚本吗?

推荐答案

对我来说,规避所有命令行引用问题的最简单方法是使用GNUmake的$(file )函数将代码写入文件.您甚至可以在define d变量内使用#作为Python注释:

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)

这篇关于如何将stdin上的输入发送到Makefile中定义的python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆