使用python3 subprocess.run()检测不到Asciidoctor-pdf属性

问题描述 投票:0回答:1

在我们部门,我们将文档格式转换为Asciidoc(tor)。出于自动化目的,我们希望使用从.yml文件中读取的属性/变量。

尝试对此属性进行子处理时会发生此问题。使用shell,效果很好。

asciidoctor-pdf -a ui_host=10.10.10.10 -a ui_port=10 -a ext_host=10.11.11.11 -a ext_port=11 userman_asciidoc.adoc

将variables.yml解析为python3脚本,格式化它们并将它们作为解压缩列表附加到subprocess.run()调用将返回有效的asciidoc-pdf。但是,不包括属性。

我认为这是一个子流程问题而我做错了。那么,subprocess.run()如何生成与写入命令行完全相同的输出?


variables.yml:

ui_host: 10.10.10.10
ui_port: 10
ext_host: 10.11.11.11
ext_port: 11

asciidoc_build.py:

import yaml
import subprocess
import argparse

parser = argparse.ArgumentParser(description="This Script builds the Asciidoc usermanual for TASTE-OS as a pdf. It can take variables as input, which yould be stored in a .yml file")
parser.add_argument("adoc_file", help="Path to the usermanual as Asciidoc (.adoc) file")
parser.add_argument("yaml_file", help="The path to the yaml file, which contains all needed variables for the TASTE-OS usermanual")

args = parser.parse_args()

with open(args.yaml_file, "r") as f:
    try:
        yaml_content = yaml.load(f)
    except yaml.YAMLError as exc:
        print(exc)

yaml_variables = []
for key, value in yaml_content.items():
    print(key, value)
    yaml_variables.append("-a " + key + "=" + str(value))

subprocess.run(["asciidoctor-pdf", *yaml_variables, args.adoc_file])

python python-3.x subprocess asciidoctor asciidoctor-pdf
1个回答
0
投票

-a参数和实际值需要在给予子进程的列表中分离。

for key, value in yaml_content.items():
    print(key, value)
    yaml_variables.append("-a")
    yaml_variables.append(key + "=" + str(value))

之前:[-a ui_host=10.10.10.10, -a ui_port=10, ...]

之后:[-a, ui_host=10.10.10.10, -a, ui_port=10, ...]

© www.soinside.com 2019 - 2024. All rights reserved.