1 |
1674
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# A DiGIR client
|
3 |
|
|
|
4 |
1678
|
aaronmk
|
import os
|
5 |
1674
|
aaronmk
|
import os.path
|
6 |
|
|
import sys
|
7 |
|
|
import urllib
|
8 |
|
|
import urllib2
|
9 |
|
|
|
10 |
|
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11 |
|
|
|
12 |
1678
|
aaronmk
|
import dates
|
13 |
1674
|
aaronmk
|
import opts
|
14 |
|
|
import streams
|
15 |
|
|
import util
|
16 |
|
|
|
17 |
|
|
# Config
|
18 |
|
|
timeout = 20 # sec
|
19 |
1679
|
aaronmk
|
default_schema = 'http://digir.net/schema/conceptual/darwin/full/2003/1.0/darwin2full.xsd'
|
20 |
1674
|
aaronmk
|
|
21 |
|
|
request_xml_template = '''\
|
22 |
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
23 |
|
|
<request
|
24 |
|
|
xmlns="http://digir.net/schema/protocol/2003/1.0"
|
25 |
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
26 |
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
27 |
|
|
xmlns:digir="http://digir.net/schema/protocol/2003/1.0"
|
28 |
|
|
xmlns:darwin="http://digir.net/schema/conceptual/darwin/2003/1.0"
|
29 |
|
|
xmlns:dwc="http://digir.net/schema/conceptual/darwin/2003/1.0"
|
30 |
|
|
xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0
|
31 |
|
|
http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd
|
32 |
|
|
http://digir.net/schema/conceptual/darwin/2003/1.0
|
33 |
|
|
http://digir.sourceforge.net/schema/conceptual/darwin/2003/1.0/darwin2.xsd">
|
34 |
|
|
<header>
|
35 |
|
|
<version>1.0</version>
|
36 |
1678
|
aaronmk
|
<sendTime>[time]</sendTime>
|
37 |
|
|
<source>[source]</source>
|
38 |
1674
|
aaronmk
|
<destination resource="[resource]">[url]</destination>
|
39 |
|
|
<type>search</type>
|
40 |
|
|
</header>
|
41 |
|
|
<search>
|
42 |
|
|
<filter>
|
43 |
1675
|
aaronmk
|
<equals>
|
44 |
1678
|
aaronmk
|
<darwin:Kingdom>plantae</darwin:Kingdom>
|
45 |
1675
|
aaronmk
|
</equals>
|
46 |
1674
|
aaronmk
|
</filter>
|
47 |
|
|
<records limit="[count]" start="[start]">
|
48 |
1679
|
aaronmk
|
<structure schemaLocation="[schema]"/>
|
49 |
1674
|
aaronmk
|
</records>
|
50 |
|
|
<count>true</count>
|
51 |
|
|
</search>
|
52 |
|
|
</request>
|
53 |
|
|
'''
|
54 |
|
|
|
55 |
1683
|
aaronmk
|
diags_start = '<diagnostics>'
|
56 |
|
|
diags_end = '</diagnostics>'
|
57 |
|
|
|
58 |
1685
|
aaronmk
|
class InputException(Exception): pass
|
59 |
|
|
|
60 |
1674
|
aaronmk
|
def main():
|
61 |
|
|
env_names = []
|
62 |
|
|
def usage_err():
|
63 |
|
|
raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
|
64 |
|
|
+sys.argv[0]+' 2>>log')
|
65 |
|
|
|
66 |
|
|
# Get config from env vars
|
67 |
|
|
url = opts.get_env_var('url', None, env_names)
|
68 |
|
|
resource = opts.get_env_var('resource', None, env_names)
|
69 |
1679
|
aaronmk
|
schema = opts.get_env_var('schema', default_schema, env_names)
|
70 |
1674
|
aaronmk
|
start = util.cast(int, opts.get_env_var('start', 0, env_names))
|
71 |
|
|
count = util.cast(int, opts.get_env_var('n', 1, env_names))
|
72 |
1678
|
aaronmk
|
debug = opts.env_flag('debug', False, env_names)
|
73 |
1674
|
aaronmk
|
if url == None or resource == None: usage_err()
|
74 |
|
|
|
75 |
1678
|
aaronmk
|
def clear_line(): sys.stderr.write('\n')
|
76 |
|
|
log_indent = 0
|
77 |
|
|
def log(msg, line_ending='\n'):
|
78 |
|
|
sys.stderr.write((' '*log_indent)+msg+line_ending)
|
79 |
1685
|
aaronmk
|
def debug_log(str_, label=None):
|
80 |
|
|
if debug:
|
81 |
|
|
if label != None: sys.stderr.write(label+':\n')
|
82 |
|
|
sys.stderr.write(str_+'\n')
|
83 |
1674
|
aaronmk
|
|
84 |
1678
|
aaronmk
|
self_dir = os.path.dirname(__file__)
|
85 |
|
|
source = os.popen(self_dir+"/local_ip").read().strip()
|
86 |
|
|
this_request_xml_template = (request_xml_template
|
87 |
|
|
.replace('[source]', source)
|
88 |
|
|
.replace('[url]', url)
|
89 |
|
|
.replace('[resource]', resource)
|
90 |
1679
|
aaronmk
|
.replace('[schema]', schema)
|
91 |
1678
|
aaronmk
|
.replace('[count]', str(count))
|
92 |
|
|
)
|
93 |
|
|
|
94 |
|
|
time = dates.strftime('%Y-%m-%d %H:%M:%S %Z', dates.now())
|
95 |
|
|
request_xml = (this_request_xml_template
|
96 |
|
|
.replace('[start]', str(start))
|
97 |
|
|
.replace('[time]', time)
|
98 |
|
|
)
|
99 |
1685
|
aaronmk
|
debug_log(request_xml, 'request')
|
100 |
1674
|
aaronmk
|
this_url = url+'?'+urllib.urlencode({'request': request_xml})
|
101 |
1683
|
aaronmk
|
diag_capture = streams.CaptureStream(streams.TimeoutInputStream(
|
102 |
|
|
urllib2.urlopen(this_url), timeout), diags_start, diags_end)
|
103 |
|
|
stream = streams.StreamIter(diag_capture)
|
104 |
1674
|
aaronmk
|
|
105 |
|
|
try:
|
106 |
|
|
# Copy lines
|
107 |
|
|
for line in stream:
|
108 |
|
|
sys.stdout.write(line)
|
109 |
|
|
finally: # still run if break is called
|
110 |
|
|
stream.close()
|
111 |
1683
|
aaronmk
|
|
112 |
1685
|
aaronmk
|
debug_log(diag_capture.match, 'diagnostics')
|
113 |
1674
|
aaronmk
|
|
114 |
|
|
main()
|