This question from years ago does what I need:
How do I check out a file from perforce in python?
but is there a way to do this using the subprocess module? (which I understand is the preferred way)
I’ve looked through stackoverflow, the python docs, as well as many google searches trying to find a way to use the stdin to send the required input to the p4 process, but I’ve not been successful. I’ve been able to find plenty on capturing the output of a subprocess command, but have not been able to grok the input commands.
I’m pretty new to python in general, so I am likely missing something obvious, but I don’t know what I don’t know in this case.
This is the code I’ve come up with so far:
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\n")
tempIn.write("description: " + descr)
tempIn.close()
(out, err) = p.communicate()
print out
As I mentioned in my comment, use the Perforce Python API.
Regarding your code:
tempfile.TemporaryFile()isn’t usually appropriate for creating a file and then passing the contents off to something else. The temporary file is automatically deleted as soon as the file is closed. Often you need to close the file for writing before you can re-open it for reading, creating a catch-22 situation. (You can get around this withtempfile.NamedTemporaryFile(delete=False), but that’s still too round-about for this situation.)To use
communicate(), you need to pass subprocess.PIPE: