Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 95 lines (76 sloc) 2.896 kb
ef0440b @rnewson Python variant of couchdb external hook.
authored
1 #!/usr/bin/python
2
416a059 @davisp Refactored the external hook a bit.
davisp authored
3 import httplib
4 import optparse as op
5 import sys
6 import traceback
9e37cdb @rnewson url escape query parameters.
authored
7 import urllib
7217c19 @rnewson simplify Python script.
authored
8
ef0440b @rnewson Python variant of couchdb external hook.
authored
9 try:
10 import json
11 except:
12 import simplejson as json
13
416a059 @davisp Refactored the external hook a bit.
davisp authored
14 __usage__ = "%prog [OPTIONS]"
15
16 def options():
17 return [
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
18 op.make_option('--remote-host', dest='remote_host',
19 default="localhost",
b9dc7a9 @rnewson fix external hook and external documentation.
authored
20 help="Hostname of the couchdb-lucene server. [%default]"),
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
21 op.make_option('--remote-port', dest='remote_port', type='int',
416a059 @davisp Refactored the external hook a bit.
davisp authored
22 default=5985,
b9dc7a9 @rnewson fix external hook and external documentation.
authored
23 help="Port of the couchdb-lucene server. [%default]"),
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
24 op.make_option('--local-host', dest='local_host',
25 default="localhost",
26 help="Hostname of this couchdb instance. [%default]"),
27 op.make_option('--local-port', dest='local_port', type='int',
28 default=5984,
29 help="Port of this couchdb instance. [%default]"),
416a059 @davisp Refactored the external hook a bit.
davisp authored
30 ]
31
32 def main():
33 parser = op.OptionParser(usage=__usage__, option_list=options())
34 opts, args = parser.parse_args()
35
36 if len(args):
37 parser.error("Unrecognized arguments: %s" % ' '.join(args))
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
38 res = httplib.HTTPConnection(opts.remote_host, opts.remote_port)
416a059 @davisp Refactored the external hook a bit.
davisp authored
39 for req in requests():
40 try:
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
41 resp = respond(res, req, opts.local_host, opts.local_port)
416a059 @davisp Refactored the external hook a bit.
davisp authored
42 except Exception, e:
43 body = traceback.format_exc()
44 resp = mkresp(500, body, {"Content-Type": "text/plain"})
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
45 res = httplib.HTTPConnection(opts.remote_host, opts.remote_port)
416a059 @davisp Refactored the external hook a bit.
davisp authored
46
47 sys.stdout.write(json.dumps(resp))
48 sys.stdout.write("\n")
49 sys.stdout.flush()
50
ef0440b @rnewson Python variant of couchdb external hook.
authored
51 def requests():
52 line = sys.stdin.readline()
53 while line:
54 yield json.loads(line)
55 line = sys.stdin.readline()
56
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
57 def respond(res, req, host, port):
416a059 @davisp Refactored the external hook a bit.
davisp authored
58 path = req.get("path", [])
989c04a @rnewson make Python external hook script work for cache hits too.
authored
59
416a059 @davisp Refactored the external hook a bit.
davisp authored
60 if len(path) != 4:
61 body = "\n".join([
62 "Invalid path: %s" % '/'.join([''] + path),
bfea51b @rnewson clarify error message
authored
63 "Paths should be: /db_name/_fti/ddocid/index_name?q=...",
64 "'ddocid' is from the '_design/ddocid' that defines index_name"
416a059 @davisp Refactored the external hook a bit.
davisp authored
65 ])
66 return mkresp(400, body, {"Content-Type": "text/plain"})
ef0440b @rnewson Python variant of couchdb external hook.
authored
67
cbfe065 @rnewson remove registry and indexkey, change output paths
authored
68 path = '/'.join(['', 'search', host, str(port), path[0], path[2], path[3]])
416a059 @davisp Refactored the external hook a bit.
davisp authored
69 path = '?'.join([path, urllib.urlencode(req["query"])])
24c528e @rnewson allow -u/--url= override for couchdb-lucene location.
authored
70
989c04a @rnewson make Python external hook script work for cache hits too.
authored
71 req_headers = {}
416a059 @davisp Refactored the external hook a bit.
davisp authored
72 for h in req.get("headers", []):
73 if h.lower() in ["accept", "if-none-match"]:
989c04a @rnewson make Python external hook script work for cache hits too.
authored
74 req_headers[h] = req["headers"][h]
416a059 @davisp Refactored the external hook a bit.
davisp authored
75
989c04a @rnewson make Python external hook script work for cache hits too.
authored
76 res.request("GET", path, headers=req_headers)
416a059 @davisp Refactored the external hook a bit.
davisp authored
77 resp = res.getresponse()
989c04a @rnewson make Python external hook script work for cache hits too.
authored
78
79 resp_headers = {}
80 for h, v in resp.getheaders():
81 if h.lower() in ["content-type", "etag"]:
82 resp_headers[h] = resp.getheader(h, [])
83
84 return mkresp(resp.status, resp.read(), resp_headers)
416a059 @davisp Refactored the external hook a bit.
davisp authored
85
86 def mkresp(code, body, headers=None):
87 ret = {"code": code, "body": body}
88 if headers is not None:
89 ret["headers"] = headers
90 return ret
ef0440b @rnewson Python variant of couchdb external hook.
authored
91
92 if __name__ == "__main__":
93 main()
416a059 @davisp Refactored the external hook a bit.
davisp authored
94
Something went wrong with that request. Please try again.