summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Hutchings <ben.hutchings@codethink.co.uk>2020-05-04 19:56:24 +0100
committerBen Hutchings <ben.hutchings@codethink.co.uk>2020-05-29 20:35:30 +0100
commiteebf790e2909ff39974e0db42fdfa9a98dbecbcf (patch)
tree5a999f6ea5ae4652bcad33ace2a95f50770632b3
parent4792912cecf52d63fdcc89e6f852e3a32f37c49c (diff)
downloadlorry-eebf790e2909ff39974e0db42fdfa9a98dbecbcf.tar.gz
lorry.{g,}zip-importer: Read and write in binary mode
gzip and zip files are not in text format, and the file contents that we send to git may not be either. So: * Open gzip files in binary mode. * Open the pipe to 'git fast-import' in binary mode. This requires using subprocess.Popen instead of os.popen. * Change printlines functions to encode text in UTF-8. * Use printlines functions in the remaining places where we need to write text.
-rwxr-xr-xlorry.gzip-importer22
-rwxr-xr-xlorry.zip-importer22
2 files changed, 24 insertions, 20 deletions
diff --git a/lorry.gzip-importer b/lorry.gzip-importer
index 8d482b7..d3d78b4 100755
--- a/lorry.gzip-importer
+++ b/lorry.gzip-importer
@@ -3,9 +3,9 @@
# gzip archive frontend for git-fast-import
from gzip import GzipFile
-import os
import os.path
import struct
+import subprocess
import sys
@@ -17,7 +17,7 @@ committer_email = 'lorry-gzip-importer@lorry'
# The size of a gzip file is stored in the last 4 bytes
def uncompressedsize(filename):
- with open(filename) as f:
+ with open(filename, 'rb') as f:
f.seek(-4, 2)
return struct.unpack('I', f.read(4))[0]
@@ -25,7 +25,7 @@ def uncompressedsize(filename):
def export(zipfile, fast_import):
def printlines(list):
for str in list:
- fast_import.write(str + "\n")
+ fast_import.write(str.encode('utf-8') + b"\n")
# Gzip does have an encoded mtime, however Python's GzipFile
# just ignores it, so we just yank the mtime of the zip file itself.
@@ -34,7 +34,7 @@ def export(zipfile, fast_import):
zip = GzipFile(zipfile, 'rb')
printlines(('blob', 'mark :1', 'data ' + str(file_size)))
- fast_import.write(zip.read() + "\n")
+ fast_import.write(zip.read() + b"\n")
committer = committer_name + ' <' + committer_email + '> %d +0000' % \
mtime
@@ -46,7 +46,7 @@ def export(zipfile, fast_import):
last_dot = zipfile_basename[:-1].rfind('.')
unzipped_file = zipfile_basename[:last_dot]
- fast_import.write('M 100644 :1 ' + unzipped_file + '\n');
+ printlines(('M 100644 :1 ' + unzipped_file,));
zipname, _ = os.path.splitext(zipfile_basename)
printlines(('', 'tag ' + zipname, \
@@ -59,11 +59,13 @@ def main():
print('usage:', sys.argv[0], '<gzipfile>...')
sys.exit(1)
- fast_import = os.popen('git fast-import --quiet', 'w')
- for zipfile in sys.argv[1:]:
- export(zipfile, fast_import)
- if fast_import.close():
- sys.exit(1)
+ with subprocess.Popen('git fast-import --quiet', shell=True,
+ stdin=subprocess.PIPE) as import_proc:
+ for zipfile in sys.argv[1:]:
+ export(zipfile, import_proc.stdin)
+ import_proc.stdin.close()
+ if import_proc.wait() != 0:
+ sys.exit(1)
main()
diff --git a/lorry.zip-importer b/lorry.zip-importer
index 7eca8f6..10b3b2b 100755
--- a/lorry.zip-importer
+++ b/lorry.zip-importer
@@ -10,8 +10,8 @@
## python import-zips.py *.zip
## git log --stat import-zips
-import os
import os.path
+import subprocess
import sys
import time
from zipfile import ZipFile
@@ -26,7 +26,7 @@ committer_email = 'lorry-zip-importer@lorry'
def export(zipfile, fast_import):
def printlines(list):
for str in list:
- fast_import.write(str + "\n")
+ fast_import.write(str.encode('utf-8') + b"\n")
commit_time = 0
next_mark = 1
@@ -53,7 +53,7 @@ def export(zipfile, fast_import):
printlines(('blob', 'mark ' + mark[name], \
'data ' + str(info.file_size)))
- fast_import.write(zip.read(name) + "\n")
+ fast_import.write(zip.read(name) + b"\n")
committer = committer_name + ' <' + committer_email + '> %d +0000' % \
time.mktime(commit_time + (0, 0, 0))
@@ -64,8 +64,8 @@ def export(zipfile, fast_import):
'', 'deleteall'))
for name in mark.keys():
- fast_import.write('M 100644 ' + mark[name] + ' ' +
- name[len(common_prefix):] + "\n")
+ printlines(('M 100644 ' + mark[name] + ' ' +
+ name[len(common_prefix):],))
zipname, _ = os.path.splitext(zipfile_basename)
@@ -79,11 +79,13 @@ def main():
print('usage:', sys.argv[0], '<zipfile>...')
sys.exit(1)
- fast_import = os.popen('git fast-import --quiet', 'w')
- for zipfile in sys.argv[1:]:
- export(zipfile, fast_import)
- if fast_import.close():
- sys.exit(1)
+ with subprocess.Popen('git fast-import --quiet', shell=True,
+ stdin=subprocess.PIPE) as import_proc:
+ for zipfile in sys.argv[1:]:
+ export(zipfile, import_proc.stdin)
+ import_proc.stdin.close()
+ if import_proc.wait() != 0:
+ sys.exit(1)
main()