Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adicionada geração de arquivos tar e escrita em disco no local solici…

…tado
  • Loading branch information...
commit 733742c430cedd30502d283c08f7bb1a99094a4c 1 parent b31822c
@gustavofonseca gustavofonseca authored
Showing with 60 additions and 17 deletions.
  1. +12 −1 README.txt
  2. +33 −14 delorean/domain.py
  3. +15 −2 delorean/tests.py
View
13 README.txt
@@ -30,5 +30,16 @@ O arquivo terá a forma::
!vYYQ!...conteúdo da tag YYQ.............
...
+
FAQ:
- * A codificação do arquivo ID deve ser ASCII, conforme consta na documentação da Bireme?
+ * A codificação do arquivo ID deve ser ASCII, conforme consta na
+ documentação da Bireme?
+
+ **R:** Não, é possível que seja CP-1252.
+
+ * Existe uma maneira de delegarmos a criação do mfn para o CISIS?
+
+ **R:** Sim. Pode-se utilizar a sintaxe ``!ID 0`` para todos os registros
+ e no utilitário id2i utilizar a opção ``app``::
+
+ id2i arq.id create/app=i
View
47 delorean/domain.py
@@ -5,38 +5,57 @@
import os
import zipfile
import datetime
-
+import tarfile
+import StringIO
+import tempfile
class Bundle(object):
def __init__(self, *args, **kwargs):
+ """
+ Accepts an arbitrary number of logical name - data pairs::
+
+ b = Bundle(('arq1', 'arq1 content as str'))
+ """
+
# dependencie injection
if 'zip_lib' not in kwargs:
zip_lib = zipfile
if 'datetime_lib' not in kwargs:
datetime_lib = datetime
- self._data = args
+ self._data = dict(args)
self._zip_lib = zip_lib
self._datetime_lib = datetime_lib
- def deploy(self, target, extracted_filename=None):
- if not extracted_filename:
- # use the target name
- extracted_filename = os.path.split(os.path.splitext(target)[-2])[-1]
+ def _tar(self):
+ """
+ Generate a tarball containing the data passed at init time.
+
+ Returns a file handler.
+ """
+ tmp = tempfile.NamedTemporaryFile()
+ out = tarfile.open(tmp.name, 'w')
+
+ try:
+ for name, data in self._data.items():
+ info = tarfile.TarInfo(name)
+ info.size = len(data)
+ out.addfile(info, StringIO.StringIO(data))
+ finally:
+ out.close()
+
+ tmp.seek(0)
+ return tmp
- # zip metadata
- zi = self._zip_lib.ZipInfo(extracted_filename)
- zi.date_time = datetime.datetime.timetuple(
- self._datetime_lib.datetime.now())
- zi.external_attr = 0755 << 16L
+ def deploy(self, target):
+ data = self._tar()
base_path = os.path.split(os.path.splitext(target)[-2])[0]
if not os.path.exists(base_path):
os.makedirs(base_path, 0755)
- with self._zip_lib.ZipFile(target, 'w') as f:
- for data in self._data:
- f.writestr(zi, data)
+ with open(target, 'w') as f:
+ f.write(data.read())
class Transformer(object):
View
17 delorean/tests.py
@@ -171,7 +171,8 @@ def add_index(data_list):
u'!ID 0\n!v100!Revista Brasileira\n!ID 1\n!v100!Revista Mexicana')
class BundleTests(unittest.TestCase):
- basic_data = [u'arq A', u'arq B']
+ basic_data = [(u'arq_a', u'Arq A content'),
+ (u'arq_b', u'Arq B content')]
def setUp(self):
self.config = testing.setUp()
@@ -188,8 +189,20 @@ def test_instantiation(self):
p = self._makeOne(*self.basic_data)
self.assertTrue(isinstance(p, Bundle))
+ def test_generate_tarball(self):
+ import tarfile
+ data_as_dict = dict(self.basic_data)
+ p = self._makeOne(*self.basic_data)
+ tar_handler = p._tar()
+ self.assertTrue(hasattr(tar_handler, 'read'))
+ self.assertTrue(hasattr(tar_handler, 'name'))
+
+ t = tarfile.open(tar_handler.name, 'r')
+ for member in t.getmembers():
+ self.assertTrue(member.name in data_as_dict)
+
def test_zip_data(self):
p = self._makeOne(*self.basic_data)
- p.deploy('/tmp/files/zippedfile.zip')
+ p.deploy('/tmp/files/zippedfile.tar')
Please sign in to comment.
Something went wrong with that request. Please try again.