/
setup.py
124 lines (114 loc) · 3.35 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
try:
# Python 2.5+
from hashlib import md5
except ImportError:
from md5 import new as md5
import models.core as core
def isJsFile(path):
"""
Check if a path is a directory or a Javascript file.
Parameters:
path - a file path.
Returns:
bool
"""
return os.path.splitext(path)[1] == '.js'
def walk(dir, op=None):
"""
Parse a directory for view files.
Parameters:
dir - the directory to walk
op - the function to use on each file node.
"""
files = os.listdir(dir)
for afile in files:
path = os.path.join(dir, afile)
if os.path.isdir(path):
walk(path, op)
elif os.path.isfile(path) and isJsFile(path):
if op:
op(path)
else:
print "dir: %s, file: %s" % (dir, path)
def collectDesignDocs(viewDir="views"):
"""
Load all CouchDB design documents, their views, validation,
and lucene documents into an array.
Parameters:
viewDir - the directory to walk.
Returns:
a dictionary of design documents.
"""
designDocs = {}
def collect(path):
dir, file = os.path.split(path)
dir, view = os.path.split(dir)
root, design = os.path.split(dir)
if view == "validation":
design = "validation"
designDocName = "_design/%s" % design
designDoc = designDocs.get(designDocName)
if not designDoc:
designDoc = {"_id": designDocName, "language":"javascript"}
if design != "validation":
key = (design == "lucene" and "fulltext") or "views"
if not designDoc.get(key):
designDoc[key] = {}
if not designDoc[key].get(view):
designDoc[key][view] = {}
fn = os.path.splitext(file)[0]
designDoc[key][view][fn] = open(path).read()
else:
view = os.path.splitext(file)[0]
designDoc[view] = open(path).read()
designDocs[designDocName] = designDoc
walk(viewDir, collect)
return designDocs
def md5hash(str):
m = md5()
m.update(str)
return m.hexdigest()
adminUser = {
"type": "user",
"userName": "shiftspace",
"password": md5hash("shiftspace")
}
adminDoc = {
"type": "system",
"ids": ["shiftspace"]
}
def loadDocs(db, createAdmin=True):
"""
Load all of the initial documents for the database.
Optional create admin user for debugging. Not recommended
for deployment.
Parameters:
db - the database to load the design documents into.
createAdmin - flag to create superuser. Defaults to True.
"""
docs = collectDesignDocs()
if createAdmin:
docs["admins"] = adminDoc
docs["shiftspace"] = adminUser
for k, v in docs.items():
print "Loading %s" % k
db[k] = v
print "Design documents loaded."
def init(dbname="shiftspace"):
"""
Initialize the shiftspace database. Defaults to
shiftspace for the database name.
Parameters:
dbname - the name of the database to use.
"""
server = core.server()
if not server.__contains__(dbname):
print "Creating database %s." % dbname
server.create(dbname)
else:
print "%s database already exists." % dbname
db = server[dbname]
loadDocs(db)
if __name__ == "__main__":
init()