import os import logging logger = logging.getLogger('Caldoz') from App import config as zconfig C = zconfig.getConfiguration().product_config.get('caldoz', {}) _marker = object() STORE = C.get('store_location', _marker) if STORE == _marker: logger.exception('Caldoz is not configured properly. See installation docs') raise 'Caldoz is not configured properly. See installation docs' if not os.path.isdir(STORE): try: os.mkdir(STORE) logger.info('Created store directory: '+STORE) except OSError: logger.exception('Store does not exist!') raise 'Caldoz is not configured properly. Store does not exist!' if not os.access(STORE, os.W_OK): logger.exception('Store is not writable!') raise 'Caldoz is not configured properly. Store is not writable!' def storeZip(pid, file): """ stores a zip """ p = os.path.join(STORE, pid) if not os.path.isdir(p): os.mkdir(p) fzip = os.path.join(p, 'original.zip') f = open(fzip, 'wb') f.write(file.read()) f.close() def getZipLocation(pid): return os.path.join(STORE, pid, 'original.zip') def storeContent(pid, fid, content): """ stores a packagefile content """ p = os.path.join(STORE, pid) if not os.path.isdir(p): os.mkdir(p) c = os.path.join(p, 'content') if not os.path.isdir(c): os.mkdir(c) f = open(os.path.join(c, fid), 'wb') f.write(content) f.close() def getContent(pid, fid): """ retrieve a packagefile content """ p = os.path.join(STORE, pid) if not os.path.isdir(p): os.mkdir(p) c = os.path.join(p, 'content') if not os.path.isdir(c): os.mkdir(c) f = open(os.path.join(c, fid), 'rb') content = f.read() f.close() return content def clearContent(pid): """ remove all files from package/content directory """ p = os.path.join(STORE, pid) if not os.path.isdir(p): os.mkdir(p) # it is propably clean already! return c = os.path.join(p, 'content') if not os.path.isdir(c): os.mkdir(c) # it is propably clean already! return for x in os.listdir(c): os.remove(os.path.join(c, x))