forked from mastbaum/philadelphia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegret
executable file
·180 lines (155 loc) · 6.32 KB
/
egret
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# egret, http://github.com/mastbaum/egret
# Andy Mastbaum ([email protected]), 2011
import os
import sys
import re
import json
import httplib
import mimetypes
import base64
def help():
print \
'''Usage:
egret create
- Create directory structure for a new couch app
egret push db_url [--include-dotfiles]
- push couch app in cwd to the couchdb server. files beginning with '.' are
ignored unless the --include-dotfiles option is specified.
egret pushdata db_url file
- upload documents to the couchdb server. file must contain a list of
dictionaries, e.g. [{"_id": "foo", "bar": "baz"}, {...}]
(caveat: this uses the couchdb bulk document api, so all features apply
including not overwriting without the right _rev.)
egret createdb db_url
- create a database on the couchdb server. this is done automatically by
push and pushdata if necessary
In all cases, the format of db_url is (bracketed means optional):
[http(s)://][user:password@]server/dbname
'''
def create():
'''create the basic directory structure for a new couch app'''
for d in ['views', 'shows', 'updates', 'lists', 'attachments']:
os.mkdir(d)
with open('settings.json', 'w') as f:
f.write(json.dumps({'name': 'Project name', 'description': 'Description', 'designname': 'designname', 'language': 'javascript'}))
def make_connection(db_url):
'''make an http(s) connection based on a url string. includes basic
authentication.
'''
match = re.match(r'((?P<protocol>.+):\/\/)?((?P<user>.+):(?P<pw>.+)?@)?(?P<url>.+)', db_url)
if not match:
print 'Error in db string'
sys.exit(1)
host, dbpath = match.group('url').split('/', 1)
if match.group('protocol') == 'https':
conn = httplib.HTTPSConnection(host)
else:
conn = httplib.HTTPConnection(host)
headers = {'Content-type': 'application/json'}
if match.group('user'):
auth_string = base64.encodestring('%s:%s' % (match.group('user'), match.group('pw')))[:-1]
headers['Authorization'] = 'Basic %s' % auth_string
return conn, dbpath, headers
def createdb(db_url):
'''create a new database on a couchdb server with url db_url'''
conn, dbpath, headers = make_connection(db_url)
conn.request('PUT', '/%s/' % dbpath, '', headers)
response = conn.getresponse()
print 'Server response:', response.status, response.reason
def pushdata(db_url, path):
'''push json documents listed in file at path to db at db_url. uses
couchdb bulk docs api.
'''
with open(path, 'r') as f:
docs = {'docs': json.load(f)}
conn, dbpath, headers = make_connection(db_url)
conn.request('GET', '/%s' % dbpath, headers=headers)
response = conn.getresponse()
if response.status == 404:
conn.request('PUT', '/%s/' % dbpath, '', headers)
conn, dbpath, headers = make_connection(db_url)
conn.request('POST', '/%s/_bulk_docs' % dbpath, json.dumps(docs), headers)
response = conn.getresponse()
print 'Server response:', response.status, response.reason
def push(db_url, include_dotfiles=False):
'''push project to db at db_url, including design doc and any
attachments. include_dotfiles controls whether we try to push files
starting with a dot character, often swap files that don't base64
encode properly.
'''
with open('settings.json') as f:
doc = json.load(f)
doc['_id'] = '_design/' + doc['designname']
for d in ['views', 'shows', 'updates', 'lists', 'filters']:
if not os.path.exists(d):
continue
doc[d] = {}
for root, dirs, files in os.walk(d):
for name in files:
if not include_dotfiles and name[0] == '.':
continue
file_name = os.path.join(root, name)
path = file_name.split(os.path.sep, 1)[1].split(os.path.sep)
if len(path) == 1:
fn_name = os.path.splitext(name)[0]
with open(file_name, 'r') as f:
doc[d][fn_name] = f.read()
elif len(path) == 2:
key_name = path[0]
fn_name = os.path.splitext(path[1])[0]
with open(file_name, 'r') as f:
if key_name in doc[d]:
doc[d][key_name][fn_name] = f.read()
else:
doc[d][key_name] = {fn_name: f.read()}
else:
print 'directory recursion depth (2) exceeded'
sys.exit(1)
doc['_attachments'] = {}
for root, dirs, files in os.walk('attachments'):
for name in files:
file_name = os.path.join(root, name)
mime = mimetypes.guess_type(file_name)[0]
if mime is None:
mime = 'application/octet-stream'
with open(file_name, 'r') as f:
doc['_attachments'][file_name.split(os.path.sep, 1)[1]] = {'content_type': mime, 'data': f.read().encode('base64')}
conn, dbpath, headers = make_connection(db_url)
conn.request('GET', '/%s/%s' % (dbpath, doc['_id']), headers=headers)
response = conn.getresponse()
if response.status == 200:
doc['_rev'] = json.loads(response.read())['_rev']
elif response.status == 404:
conn.request('PUT', '/%s/' % dbpath, '', headers)
conn, dbpath, headers = make_connection(db_url)
conn.request('PUT', '/%s/%s' % (dbpath, doc['_id']), json.dumps(doc), headers)
response = conn.getresponse()
print 'Server response:', response.status, response.reason
if __name__ == '__main__':
if len(sys.argv) < 2:
help()
sys.exit(1)
if sys.argv[1] == 'create':
create()
elif sys.argv[1] == 'createdb':
if len(sys.argv) != 3:
help()
sys.exit(1)
createdb(sys.argv[2])
elif sys.argv[1] == 'push':
if len(sys.argv) < 3 or len(sys.argv) > 4:
help()
sys.exit(1)
if len(sys.argv) == 4 and sys.argv[3] == '--include-dotfiles':
push(sys.argv[2], True)
else:
push(sys.argv[2])
elif sys.argv[1] == 'pushdata':
if len(sys.argv) != 4:
help()
sys.exit(1)
pushdata(*sys.argv[2:])
else:
help()
sys.exit(1)