def dump_db(dburl, username=None, password=None, boundary=None): envelope = MIMEMultipart('mixed', boundary) db = Database(dburl) if username is not None and password is not None: db.resource.http.add_credentials(username, password) for docid in db: doc = db.get(docid, attachments=True) print >> sys.stderr, 'Dumping document %r' % doc.id attachments = doc.pop('_attachments', {}) part = MIMEBase('application', 'json') part.set_payload(json.dumps(doc, sort_keys=True, indent=2)) if attachments: inner = MIMEMultipart('mixed') inner.attach(part) for name, info in attachments.items(): content_type = info.get('content_type') if content_type is None: # CouchDB < 0.8 content_type = info.get('content-type') maintype, subtype = content_type.split('/', 1) subpart = MIMEBase(maintype, subtype) subpart['Content-ID'] = name subpart.set_payload(b64decode(info['data'])) inner.attach(subpart) part = inner part['Content-ID'] = doc.id part['ETag'] = doc.rev envelope.attach(part) return envelope.as_string()
def dump_db(dburl, username=None, password=None, boundary=None): envelope = MIMEMultipart("mixed", boundary) db = Database(dburl) if username is not None and password is not None: db.resource.http.add_credentials(username, password) for docid in db: doc = db.get(docid, attachments=True) print >> sys.stderr, "Dumping document %r" % doc.id attachments = doc.pop("_attachments", {}) part = MIMEBase("application", "json") part.set_payload(json.dumps(doc, sort_keys=True, indent=2)) if attachments: inner = MIMEMultipart("mixed") inner.attach(part) for name, info in attachments.items(): content_type = info.get("content_type") if content_type is None: # CouchDB < 0.8 content_type = info.get("content-type") maintype, subtype = content_type.split("/", 1) subpart = MIMEBase(maintype, subtype) subpart["Content-ID"] = name subpart.set_payload(b64decode(info["data"])) inner.attach(subpart) part = inner part["Content-ID"] = doc.id part["ETag"] = doc.rev envelope.attach(part) return envelope.as_string()
def load_db(fileobj, dburl, username=None, password=None, ignore_errors=False): db = Database(dburl) if username is not None and password is not None: db.resource.credentials = (username, password) hostname = Is_Server(dburl, db.name) for headers, is_multipart, payload in read_multipart(fileobj): docid = headers['content-id'] time.sleep(1) if '-db' in db.name: if 'credential' in docid or 'flow' in docid or 'setting' in docid or 'functions' in docid: docid = Rename_docid(docid, hostname) obj = db.get(docid) if obj == None: new_doc = {'_id': docid} db.save(new_doc) obj = db.get(docid) if is_multipart: # doc has attachments for headers, _, payload in payload: if 'content-id' not in headers: doc = json.decode(payload) doc['_attachments'] = {} else: doc['_attachments'][headers['content-id']] = { 'data': b64encode(payload).decode('ascii'), 'content_type': headers['content-type'], 'length': len(payload) } else: # no attachments, just the JSON doc = json.decode(payload) doc['_rev'] = obj['_rev'] doc['_id'] = obj['_id'] print('Loading document %r' % docid, file=sys.stderr) try: db[docid] = doc except Exception as e: if not ignore_errors: raise print('Error: %s' % e, file=sys.stderr)
def dump_db(dburl, username=None, password=None, boundary=None, output=sys.stdout): db = Database(dburl) if username is not None and password is not None: db.resource.credentials = username, password envelope = write_multipart(output, boundary=boundary) for docid in db: doc = db.get(docid, attachments=True) print('Dumping document %r' % doc.id, file=sys.stderr) attachments = doc.pop('_attachments', {}) jsondoc = json.encode(doc) if attachments: parts = envelope.open({ 'Content-ID': doc.id, 'ETag': '"%s"' % doc.rev }) parts.add('application/json', jsondoc) for name, info in list(attachments.items()): content_type = info.get('content_type') if content_type is None: # CouchDB < 0.8 content_type = info.get('content-type') parts.add(content_type, b64decode(info['data']), { 'Content-ID': name }) parts.close() else: envelope.add('application/json', jsondoc, { 'Content-ID': doc.id, 'ETag': '"%s"' % doc.rev }) envelope.close()
def dump_db(dburl, username=None, password=None, boundary=None, output=sys.stdout): db = Database(dburl) if username is not None and password is not None: db.resource.credentials = username, password envelope = write_multipart(output, boundary=boundary) for docid in db: doc = db.get(docid, attachments=True) print >> sys.stderr, 'Dumping document %r' % doc.id attachments = doc.pop('_attachments', {}) jsondoc = json.encode(doc) if attachments: parts = envelope.open({ 'Content-ID': doc.id, 'ETag': '"%s"' % doc.rev }) parts.add('application/json', jsondoc) for name, info in attachments.items(): content_type = info.get('content_type') if content_type is None: # CouchDB < 0.8 content_type = info.get('content-type') parts.add(content_type, b64decode(info['data']), { 'Content-ID': name }) parts.close() else: envelope.add('application/json', jsondoc, { 'Content-ID': doc.id, 'ETag': '"%s"' % doc.rev }) envelope.close()