有没有办法使用python子进程创建新的P4更改列表? [英] Is there a way to create a new P4 changelist using python subprocess?

查看:0
本文介绍了有没有办法使用python子进程创建新的P4更改列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前的这个问题满足了我的需求:

How do I check out a file from perforce in python?

但是,有没有使用子流程模块来实现这一点的方法?(我知道这是首选方式)

我已经查看了stackoverflow、python文档以及许多Google搜索,试图找到一种方法来使用stdin将所需的输入发送到p4进程,但没有成功。我已经能够找到大量关于捕获子进程命令的输出的信息,但还无法理解输入命令。

总的来说,我对Python还很陌生,所以我可能遗漏了一些显而易见的东西,但在这种情况下,我不知道我不知道的是什么。

这是我到目前为止想出的代码:

descr = "this is a test description"
tempIn = tempfile.TemporaryFile()
tempOut = tempfile.TemporaryFile()
p = subprocess.Popen(["p4","change","-i"],stdout=tempOut, stdin=tempIn)
tempIn.write("change: New
")
tempIn.write("description: " + descr)
tempIn.close()

(out, err) = p.communicate()
print out

推荐答案

正如我在评论中提到的use the Perforce Python API

关于您的代码:

tempfile.TemporaryFile()通常不适合创建文件,然后将内容传递给其他内容。The temporary file is automatically deleted as soon as the file is closed.通常,您需要先关闭文件以进行写入,然后才能重新打开以进行读取,这就造成了一种进退两难的局面。(您可以使用tempfile.NamedTemporaryFile(delete=False)解决此问题,但对于这种情况来说,这仍然太迂回了。)

使用communicate()you need to pass subprocess.PIPE

descr = "this is a test description"
changespec = "change: New
description: " + descr

p = subprocess.Popen(["p4","change","-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

(out, err) = p.communicate(changespec)
print out

这篇关于有没有办法使用python子进程创建新的P4更改列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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