annotate Tests/Toolbox.py @ 3:2dbba2e6aa4b

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 Jun 2015 10:03:49 +0200
parents Toolbox.py@08dadea8f40a
children 292a46fe374c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
5 # Department, University Hospital of Liege, Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
6 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
7 # This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
8 # modify it under the terms of the GNU General Public License as
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
9 # published by the Free Software Foundation, either version 3 of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
10 # License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
11 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
12 # This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
15 # General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
16 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
17 # You should have received a copy of the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 import hashlib
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import httplib2
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import json
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import os.path
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
25 import re
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
26 import subprocess
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import time
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
28 import zipfile
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
30 from PIL import Image
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
31 from urllib import urlencode
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
32
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
34 HERE = os.path.dirname(__file__)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
35
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 # http://stackoverflow.com/a/1313868/881731
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 from cStringIO import StringIO
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 from StringIO import StringIO
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
44 def DefineOrthanc(url = 'http://localhost:8042',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
45 username = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
46 password = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
47 aet = 'ORTHANC',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
48 dicomPort = 4242):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
49 m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
50 if m != None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
51 url = m.groups()[0] + '://' + m.groups()[3]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
52 username = m.groups()[1]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
53 password = m.groups()[2]
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if not url.endswith('/'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 url += '/'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
58 return {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
59 'Url' : url,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
60 'Username' : username,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
61 'Password' : password,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
62 'DicomAet' : aet,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
63 'DicomPort' : dicomPort
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
64 }
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 def _SetupCredentials(orthanc, http):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
68 if (orthanc['Username'] != None and
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
69 orthanc['Password'] != None):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
70 http.add_credentials(orthanc['Username'], orthanc['Password'])
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 def DoGet(orthanc, uri, data = {}, body = None, headers = {}):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 d = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if len(data.keys()) > 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 d = '?' + urlencode(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
81 resp, content = http.request(orthanc['Url'] + uri + d, 'GET', body = body,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 headers = headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 def _DoPutOrPost(orthanc, uri, method, data, contentType, headers):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 if isinstance(data, str):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 body = data
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if len(contentType) != 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 headers['content-type'] = contentType
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 body = json.dumps(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 headers['content-type'] = 'application/json'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 headers['expect'] = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
105 resp, content = http.request(orthanc['Url'] + uri, method,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 body = body,
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 headers = headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if not (resp.status in [ 200, 302 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 def DoDelete(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
120 resp, content = http.request(orthanc['Url'] + uri, 'DELETE')
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 def DoPut(orthanc, uri, data = {}, contentType = ''):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 return DoPutOrPost(orthanc, uri, 'PUT', data, contentType)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 def UploadInstance(orthanc, filename):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
136 global HERE
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
137 p = os.path.join(HERE, '..', 'Database', filename)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 f = open(p, 'rb')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 d = f.read()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 f.close()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 return DoPost(orthanc, '/instances', d, 'application/dicom')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 def UploadFolder(orthanc, path):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
144 global HERE
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
145 p = os.path.join(HERE, 'Database', path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
146 for i in os.listdir(p):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
147 try:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
148 UploadInstance(orthanc, os.path.join(path, i))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
149 except:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
150 pass
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 def DropOrthanc(orthanc):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 # Reset the Lua callbacks
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 DoDelete(orthanc, '/exports')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 for s in DoGet(orthanc, '/patients'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 DoDelete(orthanc, '/patients/%s' % s)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 def ComputeMD5(data):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 m = hashlib.md5()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 m.update(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 return m.hexdigest()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 def GetImage(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 # http://www.pythonware.com/library/pil/handbook/introduction.htm
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 data = DoGet(orthanc, uri)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 return Image.open(StringIO(data))
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 def GetArchive(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 # http://stackoverflow.com/a/1313868/881731
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 s = DoGet(orthanc, uri)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 return zipfile.ZipFile(StringIO(s), "r")
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
176 def IsDefinedInLua(orthanc, name):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 return (s.strip() != 'nil')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
180 def WaitEmpty(orthanc):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 while True:
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
182 if len(DoGet(orthanc, '/instances')) == 0:
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 time.sleep(0.1)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
186 def GetDockerHostAddress():
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
187 route = subprocess.check_output([ '/sbin/ip', 'route' ])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
188 m = re.search(r'default via ([0-9.]+)', route)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
189 if m == None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
190 return 'localhost'
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
191 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
192 return m.groups()[0]