Skip to content

Instantly share code, notes, and snippets.

@pokstad
Created December 17, 2013 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pokstad/7998090 to your computer and use it in GitHub Desktop.
Save pokstad/7998090 to your computer and use it in GitHub Desktop.
Quick and dirty script for uploading the contents of a directory to a single CouchDB document
#!/usr/bin/python
'''
This script will upload the contents of a directory to a document in
CouchDB. It preserves all of the directory structures and is great
for uploading static websites to be served from CouchDB.
'''
import tkFileDialog
import couchdbkit
import os
import sys
SERVER = 'http://localhost:5984/'
def main():
print('Select directory for upload to CouchDB:')
dir_path = tkFileDialog.askdirectory()
# recursively collect all files (special thanks to http://mayankjohri.wordpress.com/2008/07/02/create-list-of-files-in-a-dir-tree/)
fileList = []
rootdir = dir_path
for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(os.path.join(root,file))
# time to create a couchdb doc
server = couchdbkit.Server(SERVER)
db = server.get_or_create_db('test')
doc = {}
db.save_doc(doc)
print(doc)
# attach all files as attachments to doc
root = os.path.split(dir_path)[1]
for filename in fileList:
with open(filename, 'r') as f:
couch_path = os.path.join(root,filename)
couch_path = couch_path.replace(dir_path, root)
couch_path = couch_path.replace("\\", "/")
db.put_attachment(doc, f, couch_path)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment