Skip to content

Commit 1b270ef

Browse files
committed
git-p4: auto-delete named temporary file
Take new approach using the NamedTemporaryFile() file-like object as input to the ZipFile() which auto-deletes after implicit close leaving with scope. Original code produced double-open problems on Windows platform from using already open NamedTemporaryFile() generated filename instead of object. Thanks to Andrey for patiently suggesting several iterations on this change for avoiding exceptions! Also print error details after resulting IOError to make debugging cause of exception less mysterious when it has nothing to do with "git version recent enough." Signed-off-by: Philip.McGraw <[email protected]>
1 parent 1feeaaf commit 1b270ef

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

git-p4.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,13 +1160,11 @@ def exceedsLargeFileThreshold(self, relPath, contents):
11601160
if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
11611161
return False
11621162
contentTempFile = self.generateTempFile(contents)
1163-
compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1164-
zf = zipfile.ZipFile(compressedContentFile.name, mode='w')
1165-
zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1166-
zf.close()
1167-
compressedContentsSize = zf.infolist()[0].compress_size
1163+
compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1164+
with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1165+
zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1166+
compressedContentsSize = zf.infolist()[0].compress_size
11681167
os.remove(contentTempFile)
1169-
os.remove(compressedContentFile.name)
11701168
if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
11711169
return True
11721170
return False
@@ -3514,8 +3512,9 @@ def importHeadRevision(self, revision):
35143512
self.updateOptionDict(details)
35153513
try:
35163514
self.commit(details, self.extractFilesFromCommit(details), self.branch)
3517-
except IOError:
3515+
except IOError as err:
35183516
print("IO error with git fast-import. Is your git version recent enough?")
3517+
print("IO error details: {}".format(err))
35193518
print(self.gitError.read())
35203519

35213520
def openStreams(self):

0 commit comments

Comments
 (0)