diff --git a/git-p4 b/git-p4 index 29a52e2..6a51f96 100755 --- a/git-p4 +++ b/git-p4 @@ -8,7 +8,7 @@ # License: MIT # -import optparse, sys, os, marshal, subprocess, shlex +import optparse, sys, os, marshal, subprocess, shlex, stat import tempfile, os.path, time, platform import re import cStringIO @@ -780,10 +780,22 @@ class P4FileReader: if header['type'].startswith('utf16'): # Don't even try to convert utf16. Ask p4 to write the file directly. - tmpFile = tempfile.NamedTemporaryFile() - P4Helper().p4_system("print -o \"%s\" \"%s\"" % (tmpFile.name, escapeStringP4(header['depotFile']))) - text = open(tmpFile.name).read() - tmpFile.close() + + # Create temporary file and disable automatic removal to be able to close it. + # Closing is necessary because the p4 need to write some data into this file + # but Windows-based systems can not open an already opened temporary file + # created by NamedTemporaryFile function. + tmpFile = tempfile.NamedTemporaryFile(delete=False) + try: + tmpFile.close() + P4Helper().p4_system("print -o \"%s\" \"%s\"" % (tmpFile.name, escapeStringP4(header['depotFile']))) + text = open(tmpFile.name).read() + finally: + # The temporary file may become read-only after `p4 print`, + # but os.remove requires write access. + if not os.access(tmpFile.name, os.W_OK): + os.chmod(tmpFile.name, stat.S_IWUSR) + os.remove(tmpFile.name) else: text = textBuffer.getvalue() textBuffer.close()