Commits

Ronny Pfannschmidt committed b822b12

initial externals support

Comments (0)

Files changed (4)

 .*\.egg-info
+.cache

couchdb_compose/attachments.py

 
 def add_attachments(composer):
     #XXX: renames
-    attachments = composer.config.get('attachments')
+    attachments = composer.getlist('attachments')
     for attachment in attachments:
         if isinstance(attachment, str):
             for path in composer.path.visit(attachment):

couchdb_compose/externals.py

+import restkit
+
+README="""\
+This Directory is used to cache couchdb composer externals,
+it can be deleted to refresh the cache
+"""
+
+def make_cache(path):
+    cache = path.ensure('.couchdb-compose-external-cache', dir=1)
+    cache.join('README.txt').write(README)
+
+    return cache
+
+
+def download_external(external, path):
+    url = external['url']
+
+    resp = restkit.request(url)
+    path.join(url.split('/')[-1]).write(resp.body_string())
+    path.ensure('.completed')
+
+
+def maybe_download_external(external, path):
+    if not path.join('.completed').check():
+        download_external(external, path)
+
+
+def add_external(composer, external, path):
+    pass
 
 
 def include_externals(composer):
     externals = composer.getlist('externals')
+    if not externals:
+        return
 
+    cache = make_cache(composer.path)
+
+
+    for external in externals:
+        path = cache.ensure(external['name'], dir=1)
+        maybe_download_external(external, cache)
+        add_external(composer, external, path)
+

testing/test_externals.py

+import pytest
+import mock
+import restkit
+from couchdb_compose import externals
+
+def test_make_cache(tmpdir):
+    cache = externals.make_cache(tmpdir)
+
+    assert cache.check(dir=1)
+    assert cache.dirpath() == tmpdir
+    assert cache.join('README.txt').check()
+
+
+def test_download_external(tmpdir, monkeypatch):
+    underscore = {
+        'name': 'underscore',
+        'url': 'http://underscorejs.org/underscore.js',
+    }
+
+    externals.maybe_download_external(underscore, tmpdir)
+    assert tmpdir.join('underscore.js').check()
+    assert tmpdir.join('.completed').check()
+
+    monkeypatch.setattr(restkit,
+                        'request',
+                        mock.Mock(side_effect=RuntimeError()))
+
+    externals.maybe_download_external(underscore, tmpdir)
+