2020-11-26 01:08:07 -08:00
|
|
|
import io
|
2016-11-13 16:52:38 -08:00
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import tarfile
|
2016-11-27 12:09:12 -08:00
|
|
|
import time
|
2016-11-13 16:52:38 -08:00
|
|
|
import zipfile
|
|
|
|
|
|
|
|
def read_file(path, use_crlf):
|
|
|
|
with open(path, 'rb') as file:
|
|
|
|
data = file.read()
|
|
|
|
|
2020-11-26 01:08:07 -08:00
|
|
|
if b'\0' not in data:
|
|
|
|
data = data.replace(b'\r', b'')
|
2016-11-13 16:52:38 -08:00
|
|
|
if use_crlf:
|
2020-11-26 01:08:07 -08:00
|
|
|
data = data.replace(b'\n', b'\r\n')
|
2016-11-13 16:52:38 -08:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2016-11-28 23:24:27 -08:00
|
|
|
def write_zip(target, arcprefix, timestamp, sources):
|
|
|
|
with zipfile.ZipFile(target, 'w') as archive:
|
2016-11-13 16:52:38 -08:00
|
|
|
for source in sorted(sources):
|
|
|
|
data = read_file(source, use_crlf = True)
|
|
|
|
path = os.path.join(arcprefix, source)
|
2016-11-28 23:24:27 -08:00
|
|
|
info = zipfile.ZipInfo(path)
|
|
|
|
info.date_time = time.localtime(timestamp)
|
|
|
|
info.compress_type = zipfile.ZIP_DEFLATED
|
2020-11-26 01:08:07 -08:00
|
|
|
info.external_attr = 0o644 << 16
|
2016-11-28 23:24:27 -08:00
|
|
|
archive.writestr(info, data)
|
2016-11-13 16:52:38 -08:00
|
|
|
|
2016-11-28 23:24:27 -08:00
|
|
|
def write_tar(target, arcprefix, timestamp, sources, compression):
|
2016-11-13 16:52:38 -08:00
|
|
|
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)
|
2016-11-28 23:24:27 -08:00
|
|
|
info.mtime = timestamp
|
2020-11-26 01:08:07 -08:00
|
|
|
archive.addfile(info, io.BytesIO(data))
|
2016-11-13 16:52:38 -08:00
|
|
|
|
2016-11-28 23:24:27 -08:00
|
|
|
if len(sys.argv) < 5:
|
|
|
|
raise RuntimeError('Usage: python archive.py <target> <archive prefix> <timestamp> <source files>')
|
2016-11-13 16:52:38 -08:00
|
|
|
|
2016-11-28 23:24:27 -08:00
|
|
|
target, arcprefix, timestamp = sys.argv[1:4]
|
|
|
|
sources = sys.argv[4:]
|
|
|
|
|
|
|
|
# tarfile._Stream._init_write_gz always writes current time to gzip header
|
|
|
|
time.time = lambda: timestamp
|
2016-11-13 16:52:38 -08:00
|
|
|
|
|
|
|
if target.endswith('.zip'):
|
2016-11-28 23:24:27 -08:00
|
|
|
write_zip(target, arcprefix, int(timestamp), sources)
|
2016-11-13 16:52:38 -08:00
|
|
|
elif target.endswith('.tar.gz') or target.endswith('.tar.bz2'):
|
2016-11-28 23:24:27 -08:00
|
|
|
write_tar(target, arcprefix, int(timestamp), sources, compression = os.path.splitext(target)[1][1:])
|
2016-11-13 16:52:38 -08:00
|
|
|
else:
|
2016-11-27 12:09:12 -08:00
|
|
|
raise NotImplementedError('File type not supported: ' + target)
|