annotate Tests/Toolbox.py @ 9:3670535fbd4b

fix external thread
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 Jun 2015 13:49:12 +0200
parents 6d645b3011e1
children 5f73008bb873
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
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
24 import os
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
25 import re
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
26 import signal
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
27 import subprocess
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
28 import threading
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 import time
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
30 import zipfile
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
32 from PIL import Image
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
33 from urllib import urlencode
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
34
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
36 HERE = os.path.dirname(__file__)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
37
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 # http://stackoverflow.com/a/1313868/881731
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 from cStringIO import StringIO
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 from StringIO import StringIO
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
46 def DefineOrthanc(url = 'http://localhost:8042',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
47 username = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
48 password = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
49 aet = 'ORTHANC',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
50 dicomPort = 4242):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
51 m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
52 if m != None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
53 url = m.groups()[0] + '://' + m.groups()[3]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
54 username = m.groups()[1]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
55 password = m.groups()[2]
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 if not url.endswith('/'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 url += '/'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
60 return {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
61 'Url' : url,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
62 'Username' : username,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
63 'Password' : password,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
64 'DicomAet' : aet,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
65 'DicomPort' : dicomPort
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
66 }
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 def _SetupCredentials(orthanc, http):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
70 if (orthanc['Username'] != None and
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
71 orthanc['Password'] != None):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
72 http.add_credentials(orthanc['Username'], orthanc['Password'])
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 def DoGet(orthanc, uri, data = {}, body = None, headers = {}):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 d = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 if len(data.keys()) > 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 d = '?' + urlencode(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
83 resp, content = http.request(orthanc['Url'] + uri + d, 'GET', body = body,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 headers = headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 def _DoPutOrPost(orthanc, uri, method, data, contentType, headers):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if isinstance(data, str):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 body = data
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if len(contentType) != 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 headers['content-type'] = contentType
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 body = json.dumps(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 headers['content-type'] = 'application/json'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 headers['expect'] = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
107 resp, content = http.request(orthanc['Url'] + uri, method,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 body = body,
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 headers = headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 if not (resp.status in [ 200, 302 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 def DoDelete(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 http = httplib2.Http()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
122 resp, content = http.request(orthanc['Url'] + uri, 'DELETE')
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 try:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 return json.loads(content)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 except:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 return content
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 def DoPut(orthanc, uri, data = {}, contentType = ''):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 return DoPutOrPost(orthanc, uri, 'PUT', data, contentType)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 def UploadInstance(orthanc, filename):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
138 global HERE
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
139 p = os.path.join(HERE, '..', 'Database', filename)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 f = open(p, 'rb')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 d = f.read()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 f.close()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 return DoPost(orthanc, '/instances', d, 'application/dicom')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 def UploadFolder(orthanc, path):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
146 global HERE
6
6d645b3011e1 test_hierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4
diff changeset
147 p = os.path.join(HERE, '..', 'Database', path)
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
148 for i in os.listdir(p):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
149 try:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
150 UploadInstance(orthanc, os.path.join(path, i))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
151 except:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
152 pass
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 def DropOrthanc(orthanc):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 # Reset the Lua callbacks
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 DoDelete(orthanc, '/exports')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 for s in DoGet(orthanc, '/patients'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 DoDelete(orthanc, '/patients/%s' % s)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 def ComputeMD5(data):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 m = hashlib.md5()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 m.update(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 return m.hexdigest()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 def GetImage(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 # http://www.pythonware.com/library/pil/handbook/introduction.htm
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 data = DoGet(orthanc, uri)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 return Image.open(StringIO(data))
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 def GetArchive(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 # http://stackoverflow.com/a/1313868/881731
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 s = DoGet(orthanc, uri)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 return zipfile.ZipFile(StringIO(s), "r")
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
178 def IsDefinedInLua(orthanc, name):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 return (s.strip() != 'nil')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
182 def WaitEmpty(orthanc):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 while True:
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
184 if len(DoGet(orthanc, '/instances')) == 0:
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 return
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 time.sleep(0.1)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
188 def GetDockerHostAddress():
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
189 route = subprocess.check_output([ '/sbin/ip', 'route' ])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
190 m = re.search(r'default via ([0-9.]+)', route)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
191 if m == None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
192 return 'localhost'
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
193 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
194 return m.groups()[0]
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
195
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
196
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
197
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
198
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
199 class ExternalCommandThread:
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
200 @staticmethod
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
201 def ExternalCommandFunction(arg, stop_event, command, env):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
202 external = subprocess.Popen(command, env = env)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
203
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
204 while (not stop_event.is_set()):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
205 error = external.poll()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
206 if error != None:
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
207 # http://stackoverflow.com/a/1489838/881731
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
208 os._exit(-1)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
209 stop_event.wait(0.1)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
210
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
211 print 'Stopping the external command'
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
212 external.terminate()
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
213 external.communicate() # Wait for the command to stop
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
214
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
215 def __init__(self, command, env = None):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
216 self.thread_stop = threading.Event()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
217 self.thread = threading.Thread(target = self.ExternalCommandFunction,
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
218 args = (10, self.thread_stop, command, env))
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
219 #self.daemon = True
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
220 self.thread.start()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
221
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
222 def stop(self):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
223 self.thread_stop.set()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
224 self.thread.join()