Created
December 17, 2013 00:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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