-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrc
executable file
·194 lines (167 loc) · 4.64 KB
/
xrc
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# Copyright 2011 Christian Haselgrove
# Licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php
import sys
import os
import getopt
import getpass
import base64
import urlparse
import httplib
def report_error(msg):
sys.stderr.write('%s: %s\n' % (progname, msg))
sys.stderr.write('run %s with no arguments for usage\n' % progname)
return
def debug(msg):
if debug_flag:
print msg
return
progname = os.path.basename(sys.argv[0])
try:
host = os.environ['XNAT_URI']
except KeyError:
host = None
try:
user_name = os.environ['XNAT_USER']
except KeyError:
user_name = None
try:
password = os.environ['XNAT_PASSWORD']
except KeyError:
password = None
if len(sys.argv) == 1:
print
print 'usage: %s [options] <method> <request>' % progname
print
print 'call an XNAT REST service'
print
print 'options are:'
print
print ' -d -- debug'
print ' -n -- no authentication'
print ' -h <host>'
print ' -u <user name>'
print ' -p <password>'
print ' -b <request body file> (or "-" for stdin)'
print
print 'option values may be given by environment variables:'
print
if host is None:
print ' host: XNAT_URI'
else:
print ' host: XNAT_URI (set to %s)' % host
if user_name is None:
print ' user name: XNAT_USER'
else:
print ' user name: XNAT_USER (set to %s)' % user_name
if password is None:
print ' password: XNAT_PASSWORD'
else:
print ' password: XNAT_PASSWORD (currently set)'
print
print '%s will prompt for a missing user name, password, or host' % progname
print
print 'method must be GET, PUT, POST, or DELETE'
print
print 'cheat sheet (see also http://docs.xnat.org/XNAT+REST+API):'
print
print ' /data/JSESSION'
print
sys.exit(1)
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'h:u:p:db:n')
except getopt.error, data:
report_error(data)
sys.exit(1)
if len(args) < 2:
report_error('not enough non-option arguments')
sys.exit(1)
if len(args) > 2:
report_error('too many non-option arguments')
sys.exit(1)
(method, request) = args
if method not in ('GET', 'PUT', 'POST', 'DELETE'):
report_error('unknown method "%s"' % method)
sys.exit(1)
if not request.startswith('/'):
report_error('request must start with "/"')
sys.exit(1)
debug_flag = False
body_fname = None
auth_flag = True
for (option, value) in opts:
if option == '-h':
host = value
if option == '-u':
user_name = value
if option == '-p':
password = value
if option == '-d':
debug_flag = True
if option == '-b':
body_fname = value
if option == '-n':
auth_flag = False
if host is None:
sys.stdout.write('Host: ')
sys.stdout.flush()
host = sys.stdin.readline().strip()
if body_fname is None:
body = None
elif body_fname == '-':
body = sys.stdin.read()
else:
try:
body = open(body_fname).read()
except IOError, data:
sys.stderr.write('%s: %s\n' % (progname, data))
sys.exit(1)
host = host.rstrip('/')
url_parts = urlparse.urlsplit(host)
headers = {}
if auth_flag:
if user_name is None:
sys.stdout.write('User name: ')
sys.stdout.flush()
user_name = sys.stdin.readline().strip()
if password is None:
password = getpass.getpass()
auth = 'Basic %s' % base64.b64encode('%s:%s' % (user_name, password))
headers['Authorization'] = auth
else:
if url_parts.scheme == 'https':
hc = httplib.HTTPSConnection(url_parts.netloc)
else:
hc = httplib.HTTPConnection(url_parts.netloc)
hc.request('GET', url_parts.path + '/')
response = hc.getresponse()
set_cookie = response.getheader('Set-Cookie')
if set_cookie:
headers['Cookie'] = set_cookie.split(';')[0]
hc.close()
if url_parts.scheme == 'https':
hc = httplib.HTTPSConnection(url_parts.netloc)
else:
hc = httplib.HTTPConnection(url_parts.netloc)
try:
path = '%s%s' % (url_parts.path, request)
debug('scheme: %s' % url_parts.scheme)
debug('method: %s' % method)
debug('path: %s' % path)
for name in sorted(headers):
debug('header: %s: %s' % (name, headers[name]))
if body:
hc.request(method, path, body=body, headers=headers)
else:
hc.request(method, path, headers=headers)
response = hc.getresponse()
data = response.read()
finally:
hc.close()
print response.status, response.reason
for (name, value) in response.getheaders():
print '%s: %s' % (name, value)
print
print data
sys.exit(0)
# eof