From 86ed1cf3b90aa7713558f2d7466ae214f42aa684 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 27 Nov 2016 17:55:41 -0800 Subject: scripts: Move archive.py from tests --- Makefile | 2 +- scripts/archive.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/archive.py | 48 ------------------------------------------------ 3 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 scripts/archive.py delete mode 100644 tests/archive.py diff --git a/Makefile b/Makefile index 17b8b66..84290c7 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ docs: docs/quickstart.html docs/manual.html build/pugixml-%: .FORCE | $(RELEASE) @mkdir -p $(BUILD) - python tests/archive.py $@ pugixml-$(VERSION) $| + python scripts/archive.py $@ pugixml-$(VERSION) $| $(EXECUTABLE): $(OBJECTS) $(CXX) $(OBJECTS) $(LDFLAGS) -o $@ diff --git a/scripts/archive.py b/scripts/archive.py new file mode 100644 index 0000000..ac62dc2 --- /dev/null +++ b/scripts/archive.py @@ -0,0 +1,48 @@ +import os.path +import sys +import tarfile +import time +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) + info.mtime = time.time() + archive.addfile(info, StringIO.StringIO(data)) + +if len(sys.argv) < 4: + raise RuntimeError('Usage: python archive.py ') + +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) diff --git a/tests/archive.py b/tests/archive.py deleted file mode 100644 index ac62dc2..0000000 --- a/tests/archive.py +++ /dev/null @@ -1,48 +0,0 @@ -import os.path -import sys -import tarfile -import time -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) - info.mtime = time.time() - archive.addfile(info, StringIO.StringIO(data)) - -if len(sys.argv) < 4: - raise RuntimeError('Usage: python archive.py ') - -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) -- cgit v1.2.3