summaryrefslogtreecommitdiff
path: root/tests/archive.py
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2016-11-13 16:52:38 -0800
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2016-11-13 16:52:38 -0800
commit5ca7e7cffc53b2378d3d9eb5168458935ca21627 (patch)
tree701bb640236cf08a5bd9c9c23c6a5bcbfe3f723e /tests/archive.py
parentc5223be4341ec8c679dad64c8c7bd3ce315d06cc (diff)
Rewrite archive script into Python
Perl version needed Archive::Zip that for some reason is not installed on WSL by default. Use this as an opportunity to remove the last Perl script.
Diffstat (limited to 'tests/archive.py')
-rw-r--r--tests/archive.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/archive.py b/tests/archive.py
new file mode 100644
index 0000000..a0967fc
--- /dev/null
+++ b/tests/archive.py
@@ -0,0 +1,46 @@
+import os.path
+import sys
+import tarfile
+import zipfile
+import StringIO
+
+def read_file(path, use_crlf):
+ with open(path, 'rb') as file:
+ data = file.read()
+
+ if '\0' not in data:
+ data = data.replace('\r', '')
+ if use_crlf:
+ data = data.replace('\n', '\r\n')
+
+ return data
+
+def write_zip(target, arcprefix, sources):
+ with zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as archive:
+ for source in sorted(sources):
+ data = read_file(source, use_crlf = True)
+ path = os.path.join(arcprefix, source)
+ archive.writestr(path, data)
+
+def write_tar(target, arcprefix, sources, compression):
+ with tarfile.open(target, 'w:' + compression) as archive:
+ for source in sorted(sources):
+ data = read_file(source, use_crlf = False)
+ path = os.path.join(arcprefix, source)
+ info = tarfile.TarInfo(path)
+ info.size = len(data)
+ archive.addfile(info, StringIO.StringIO(data))
+
+if len(sys.argv) < 4:
+ raise RuntimeError('Usage: python archive.py <target> <archive prefix> <source files>')
+
+target = sys.argv[1]
+arcprefix = sys.argv[2]
+sources = sys.argv[3:]
+
+if target.endswith('.zip'):
+ write_zip(target, arcprefix, sources)
+elif target.endswith('.tar.gz') or target.endswith('.tar.bz2'):
+ write_tar(target, arcprefix, sources, compression = os.path.splitext(target)[1][1:])
+else:
+ raise NotImplementedError('File type not supported: ' + target) \ No newline at end of file