Skip tarball and just gzip the archive file.

It's just a single file, so it makes sense to just gzip it.

Change-Id: I0103269a9e365fdbcb30c1389cf6f3eabfce1a23
This commit is contained in:
Sandy Walsh 2015-04-15 13:39:03 -07:00
parent 7a600792db
commit 8fc4b88519
4 changed files with 30 additions and 32 deletions

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = shoebox name = shoebox
version = 0.6 version = 0.7
author = Dark Secret Software Inc. author = Dark Secret Software Inc.
author-email = admin@darksecretsoftware.com author-email = admin@darksecretsoftware.com
summary = data archiving library summary = data archiving library

View File

@ -15,10 +15,10 @@
import datetime import datetime
import fnmatch import fnmatch
import gzip
import hashlib import hashlib
import os import os
import os.path import os.path
import tarfile
import notification_utils import notification_utils
@ -144,16 +144,16 @@ class WritingRollManager(RollManager):
class WritingJSONRollManager(object): class WritingJSONRollManager(object):
"""No archiver. No roll checker. Just write 1 file line per json payload. """No archiver. No roll checker. Just write 1 file line per json payload.
Once the file gets big enough, .tar.gz the file and move Once the file gets big enough, gzip the file and move
into the destination_directory. into the destination_directory.
Expects an external tool like rsync to move the file. Expects an external tool like rsync to move the file.
A SHA-256 of the payload may be included in the tarball filename.""" A SHA-256 of the payload may be included in the archive filename."""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.filename_template = args[0] self.filename_template = args[0]
self.directory = kwargs.get('directory', '.') self.directory = kwargs.get('directory', '.')
self.destination_directory = kwargs.get('destination_directory', '.') self.destination_directory = kwargs.get('destination_directory', '.')
self.roll_size_mb = int(kwargs.get('roll_size_mb', 1000)) self.roll_size_mb = int(kwargs.get('roll_size_mb', 1000))
minutes = kwargs.get('roll_minutes', 15) minutes = kwargs.get('roll_minutes', 60)
self.roll_after = datetime.timedelta(minutes=minutes) self.roll_after = datetime.timedelta(minutes=minutes)
# Look in the working directory for any files. Move them to the # Look in the working directory for any files. Move them to the
@ -205,17 +205,16 @@ class WritingJSONRollManager(object):
f.close() f.close()
return sha256.hexdigest() return sha256.hexdigest()
def _tar_working_file(self, filename): def _gzip_working_file(self, filename):
# tar all the files in working directory into an archive # gzip the working file in the destination_directory.
# in destination_directory.
crc = self._get_file_sha(filename) crc = self._get_file_sha(filename)
# No contextmgr for tarfile in 2.6 :( fn = self._make_filename(crc, self.destination_directory) + ".gz"
fn = self._make_filename(crc, self.destination_directory) + ".tar.gz"
tar = tarfile.open(fn, "w:gz") with open(filename, 'r') as file_in:
just_name = os.path.basename(filename) file_out = gzip.open(fn, 'wb')
tar.add(filename, arcname=just_name) file_out.writelines(file_in)
tar.close() file_out.close()
def _clean_working_directory(self, filename): def _clean_working_directory(self, filename):
os.remove(filename) os.remove(filename)
@ -223,7 +222,7 @@ class WritingJSONRollManager(object):
def _do_roll(self, filename): def _do_roll(self, filename):
self.close() self.close()
self._tar_working_file(filename) self._gzip_working_file(filename)
self._clean_working_directory(filename) self._clean_working_directory(filename)
def write(self, metadata, json_payload): def write(self, metadata, json_payload):

View File

@ -1,10 +1,10 @@
import datetime import datetime
import gzip
import hashlib import hashlib
import json import json
import mock import mock
import os import os
import shutil import shutil
import tarfile
import unittest import unittest
import notification_utils import notification_utils
@ -59,20 +59,14 @@ class TestDirectory(unittest.TestCase):
if os.path.isfile(full): if os.path.isfile(full):
self.fail("Working directory not empty.") self.fail("Working directory not empty.")
# Extract the tarballs ... # Read the gzip files ...
total = 0 total = 0
for f in os.listdir(DESTDIR): for f in os.listdir(DESTDIR):
tar = tarfile.open(os.path.join(DESTDIR, f), "r:gz") archive = gzip.open(os.path.join(DESTDIR, f), 'rb')
names = tar.getnames() file_content = archive.read().split('\n')
tar.extractall(path=EXTRACTDIR) archive.close()
tar.close()
for item in names: num = len(file_content) - 1
full = os.path.join(EXTRACTDIR, item)
num = 0
with open(full, "r") as handle:
for line in handle:
num += 1
total += num total += num
print "In %s: %d of %d Remaining: %d" % (f, num, actual, print "In %s: %d of %d Remaining: %d" % (f, num, actual,
actual - total) actual - total)

View File

@ -144,15 +144,20 @@ class TestJSONRollManager(unittest.TestCase):
@mock.patch( @mock.patch(
"shoebox.roll_manager.WritingJSONRollManager._archive_working_files") "shoebox.roll_manager.WritingJSONRollManager._archive_working_files")
def test_tar_working_file(self, awf): def test_gzip_working_file(self, awf):
rm = roll_manager.WritingJSONRollManager("template.foo") rm = roll_manager.WritingJSONRollManager("template.foo")
with mock.patch.object(rm, "_get_file_sha") as gfs: with mock.patch.object(rm, "_get_file_sha") as gfs:
gfs.return_value = "aabbcc" gfs.return_value = "aabbcc"
with mock.patch.object(roll_manager.tarfile, 'open') as tar:
tar.return_value = mock.MagicMock() open_name = '%s.open' % roll_manager.__name__
rm._tar_working_file("foo") with mock.patch(open_name, create=True) as mock_open:
self.assertTrue(tar.called) handle = mock.MagicMock()
mock_open.return_value = handle
with mock.patch.object(roll_manager.gzip, 'open') as gzip:
gzip.return_value = mock.MagicMock()
rm._gzip_working_file("foo")
self.assertTrue(gzip.called)
@mock.patch( @mock.patch(
"shoebox.roll_manager.WritingJSONRollManager._archive_working_files") "shoebox.roll_manager.WritingJSONRollManager._archive_working_files")