git-p4: use subprocess in p4CmdList
This allows bidirectional piping - useful for "-x -" to avoid commandline arguments - and is a step toward bypassing the shell. Signed-off-by: Scott Lamb <slamb@slamb.org> Signed-off-by: Simon Hausmann <simon@lst.de>
Scott Lamb committed
Jul 15, 2007 at 20:58 UTC
9f90c7335e223c26d19f3c01a6d89e6c0cd8b827
1 file changed
+18
-5
contrib/fast-import/git-p4
+18
-5
@@ -63,21 +63,34 @@ def system(cmd):
63
if os.system(cmd) != 0:
64
die("command failed: %s" % cmd)
65
66
-def p4CmdList(cmd):
66
+def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
67
cmd = "p4 -G %s" % cmd
68
if verbose:
69
sys.stderr.write("Opening pipe: %s\n" % cmd)
70
- pipe = os.popen(cmd, "rb")
70
+
71
+ # Use a temporary file to avoid deadlocks without
72
+ # subprocess.communicate(), which would put another copy
73
+ # of stdout into memory.
74
+ stdin_file = None
75
+ if stdin is not None:
76
+ stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
77
+ stdin_file.write(stdin)
78
+ stdin_file.flush()
79
+ stdin_file.seek(0)
80
+
81
+ p4 = subprocess.Popen(cmd, shell=True,
82
+ stdin=stdin_file,
83
+ stdout=subprocess.PIPE)
84
85
result = []
86
try:
87
while True:
75
- entry = marshal.load(pipe)
88
+ entry = marshal.load(p4.stdout)
89
result.append(entry)
90
except EOFError:
91
pass
79
- exitCode = pipe.close()
80
- if exitCode != None:
92
+ exitCode = p4.wait()
93
+ if exitCode != 0:
94
entry = {}
95
entry["p4ExitCode"] = exitCode
96
result.append(entry)