如何使用python子进程在windows shell中包装(Vivado)CLI

2024-09-28 01:27:30 发布

您现在位置:Python中文网/ 问答频道 /正文

一些背景: Vivado有一个CLI,可以使用

C:/path_to_vivado/bin/vivado -mode tcl。你知道吗

它默默地调用几个.bat文件并设置几个环境变量,直到: CLI Vivado

因此,基本上在这一点上,您有一个tcl解释器,它提供vivado对象、命令、接口等

(例如lindex [get_parts] 0produce this

目标:我想用python包装它。我已经有一些工作,但我想要的东西更多,我不知道如何实施。你知道吗

我想要的是这样的东西

代码:

import subprocess
from subprocess import PIPE, STDOUT
shl_cmd = r"C:/Xilinx/Vivado/2019.1/bin/vivado -mode tcl"
proc = subprocess.Popen(shl_cmd, shell=True ,stdin = PIPE, stdout = PIPE,  stderr = STDOUT)

tcl_cmd =b'lindex [get_parts] 0\n'
proc.stdin.write(tcl_cmd)
proc.stdin.flush()

#I know this "read all" is blocking but for now it is fine
while(1):                
        out = p.read(1)
        print(out.decode(),end='')

所需的输出(如果我手动输入命令,而不是使用python,则基本上就是提示中显示的输出):

****** Vivado v2018.3 (64-bit)
  **** SW Build 2405991 on Thu Dec  6 23:38:27 MST 2018
  **** IP Build 2404404 on Fri Dec  7 01:43:56 MST 2018
    ** Copyright 1986-2018 Xilinx, Inc. All Rights Reserved.

Vivado% lindex [get_parts] 0
xc7vx485tffg1157-1
Vivado%

但我什么都没有(stdout是空的,程序块)。你知道吗

无论如何,如果在前面的代码中tcl\u cmd是tcl_cmd =b'puts [lindex [get_parts] 0]\n' 我有输出:

****** Vivado v2019.1 (64-bit)
  **** SW Build 2552052 on Fri May 24 14:49:42 MDT 2019
  **** IP Build 2548770 on Fri May 24 18:01:18 MDT 2019
    ** Copyright 1986-2019 Xilinx, Inc. All Rights Reserved.

xc7vx485tffg1157-1

注意:可能只有在其他内容出现之前,头才会被刷新到std输出,所以这就是为什么在put指令之前不显示头的原因。 在我用python听的地方,我可以阅读我用puts明确发送的内容,但我不知道为什么。你知道吗

更多实验:

实验1:

C:\Users\Corrado>vivado -mode tcl > filelog.txt
lindex [get_parts] 0
puts hello_world
exit

以及文件日志.txt内容是:

****** Vivado v2018.3 (64-bit)
  **** SW Build 2405991 on Thu Dec  6 23:38:27 MST 2018
  **** IP Build 2404404 on Fri Dec  7 01:43:56 MST 2018
    ** Copyright 1986-2018 Xilinx, Inc. All Rights Reserved.

Vivado% xc7vx485tffg1157-1
Vivado% hello_world
Vivado% exit
INFO: [Common 17-206] Exiting Vivado at Tue Nov  5 17:49:52 2019...

但对于代码:

proc = subprocess.Popen(r"C:/Xilinx/Vivado/2019.1/bin/vivado -mode tcl > filelog.txt",shell=True ,stdin = PIPE, stdout=PIPE, stderr=STDOUT )
proc.stdin.write(b'lindex [get_parts] 0\n')
proc.stdin.flush()
proc.stdin.write(b'puts hello\n')
proc.stdin.flush()
proc.stdin.write(b'exit\n')
proc.stdin.flush()

你知道吗日志文件.txt是:

****** Vivado v2019.1 (64-bit)
  **** SW Build 2552052 on Fri May 24 14:49:42 MDT 2019
  **** IP Build 2548770 on Fri May 24 18:01:18 MDT 2019
    ** Copyright 1986-2019 Xilinx, Inc. All Rights Reserved.

hello
exit
INFO: [Common 17-206] Exiting Vivado at Tue Nov  5 17:54:32 2019...

请注意,显示exit可能是因为它不是tcl命令,默认情况下,未知命令由将它们发送到shell的应用程序管理(我想我不太确定)

因此,shell调用和使用子进程似乎产生了不同的行为,即使我认为它们是相同的。

我也尝试过使用pexpect,但没有成功,结果几乎一样。你知道吗

我不太在乎显示Vivado%提示符(即使这会很好),但我希望在使用CLI时,即使在使用python的CLI时,也能在终端上打印出内容的精确副本。(例如,将其打印到内部GUI中)

抱歉,如果我已经有点冗长,但我想更清楚,我可以。你知道吗


Tags: buildcmdgetonstdinexitproctcl

热门问题