Commits

Ronny Pfannschmidt committed a7f2bb2

add external zip unpack so i can use blueprint

Comments (0)

Files changed (1)

couchdb_compose/externals.py

+from __future__ import print_function
+import io
 import restkit
+import zipfile
 
 README="""\
 This Directory is used to cache couchdb composer externals,
 
 def download_external(external, path):
     url = external['url']
+    print(' * download', url)
 
-    resp = restkit.request(url)
-    path.join(url.split('/')[-1]).write(resp.body_string())
+    resp = restkit.request(url, follow_redirect=True)
+
+    path.join(url.split('/')[-1]).write(resp.body_string(), 'wb')
     path.ensure('.completed')
 
 
 def add_external(composer, external, path):
     filename = external['url'].split('/')[-1]
     attachment = external.get('attachment')
+    unpack = external.get('unpack')
     content = path.join(filename).read()
-    if attachment:
+    
+    if unpack:
+        fp = io.BytesIO(content)
+        zipfp = zipfile.ZipFile(fp)
+        names = zipfp.namelist()
+        for item in unpack:
+            sourcename = item['from']
+            #XXX hack for zipfile prefix
+            for name in names:
+                if name.endswith('/' + sourcename):
+                    content = zipfp.read(name)
+                    break
+            else:
+                print(' !', sourcename, 'not found')
+            info = composer.add_attachment(item['to'], content)
+            info.update(
+                url = external.get('url'),
+                name = external.get('name'),
+            )
+
+
+    elif attachment:
         info = composer.add_attachment(attachment, content)
         info.update(
             url = external.get('url'),