annotate Tests/Toolbox.py @ 220:7b1c976caa9b Orthanc-1.5.5

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 25 Feb 2019 10:57:28 +0100
parents af8e034f4262
children 0f03ee6ffa80
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
73
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
5 # Department, University Hospital of Liege, Belgium
195
af8e034f4262 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
6 # Copyright (C) 2017-2019 Osimis S.A., Belgium
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
7 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
8 # This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
9 # modify it under the terms of the GNU General Public License as
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
10 # published by the Free Software Foundation, either version 3 of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
11 # License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
12 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
13 # This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
16 # General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
17 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
18 # You should have received a copy of the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import hashlib
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import httplib2
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import json
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
25 import os
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
26 import re
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
27 import signal
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
28 import subprocess
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
29 import threading
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
30 import sys
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 import time
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
32 import zipfile
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
34 from PIL import Image
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
35
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
36 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
37 from urllib.parse import urlencode
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
38 from io import StringIO
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
39 from io import BytesIO
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
40
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
41 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
42 from urllib import urlencode
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
43
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
44 # http://stackoverflow.com/a/1313868/881731
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
45 try:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
46 from cStringIO import StringIO
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
47 except:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
48 from StringIO import StringIO
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
49
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
51 def _DecodeJson(s):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
52 t = s
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
53
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
54 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
55 try:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
56 t = s.decode()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
57 except:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
58 pass
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
59
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
60 try:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
61 return json.loads(t)
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
62 except:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
63 return t
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
66 def DefineOrthanc(server = 'localhost',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
67 restPort = 8042,
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
68 username = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
69 password = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
70 aet = 'ORTHANC',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
71 dicomPort = 4242):
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
72 #m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
73 #if m != None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
74 # url = m.groups()[0] + '://' + m.groups()[3]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
75 # username = m.groups()[1]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
76 # password = m.groups()[2]
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
78 #if not url.endswith('/'):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
79 # url += '/'
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
81 return {
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
82 'Server' : server,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
83 'Url' : 'http://%s:%d/' % (server, restPort),
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
84 'Username' : username,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
85 'Password' : password,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
86 'DicomAet' : aet,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
87 'DicomPort' : dicomPort
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
88 }
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
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 _SetupCredentials(orthanc, http):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
92 if (orthanc['Username'] != None and
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
93 orthanc['Password'] != None):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
94 http.add_credentials(orthanc['Username'], orthanc['Password'])
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
28
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
96 def DoGetRaw(orthanc, uri, data = {}, body = None, headers = {}):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 d = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 if len(data.keys()) > 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 d = '?' + urlencode(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 http = httplib2.Http()
21
2a29bcff60a7 tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 13
diff changeset
102 http.follow_redirects = False
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 _SetupCredentials(orthanc, http)
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 + d, 'GET', body = body,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 headers = headers)
28
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
107 return (resp, content)
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
108
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
109
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
110 def DoGet(orthanc, uri, data = {}, body = None, headers = {}):
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
111 (resp, content) = DoGetRaw(orthanc, uri, data = data, body = body, headers = headers)
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
112
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 else:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
116 return _DecodeJson(content)
0
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 _DoPutOrPost(orthanc, uri, method, data, contentType, headers):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 http = httplib2.Http()
21
2a29bcff60a7 tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 13
diff changeset
120 http.follow_redirects = False
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
123 if isinstance(data, (str, bytearray, bytes)):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 body = data
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if len(contentType) != 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 headers['content-type'] = contentType
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 body = json.dumps(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 headers['content-type'] = 'application/json'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 headers['expect'] = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
133 resp, content = http.request(orthanc['Url'] + uri, method,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 body = body,
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 headers = headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 if not (resp.status in [ 200, 302 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 else:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
139 return _DecodeJson(content)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 def DoDelete(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 http = httplib2.Http()
21
2a29bcff60a7 tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 13
diff changeset
143 http.follow_redirects = False
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
146 resp, content = http.request(orthanc['Url'] + uri, 'DELETE')
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 if not (resp.status in [ 200 ]):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 raise Exception(resp.status)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 else:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
150 return _DecodeJson(content)
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 DoPut(orthanc, uri, data = {}, contentType = ''):
10
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 9
diff changeset
153 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, {})
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
158 def GetDatabasePath(filename):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
159 return os.path.join(os.path.dirname(__file__), '..', 'Database', filename)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
160
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 def UploadInstance(orthanc, filename):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
162 with open(GetDatabasePath(filename), 'rb') as f:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
163 d = f.read()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
164
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 return DoPost(orthanc, '/instances', d, 'application/dicom')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 def UploadFolder(orthanc, path):
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
168 for i in os.listdir(GetDatabasePath(path)):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
169 try:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
170 UploadInstance(orthanc, os.path.join(path, i))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
171 except:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
172 pass
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 def DropOrthanc(orthanc):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 # Reset the Lua callbacks
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 DoDelete(orthanc, '/exports')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 for s in DoGet(orthanc, '/patients'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 DoDelete(orthanc, '/patients/%s' % s)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
174
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
183 def InstallLuaScriptFromPath(orthanc, path):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
184 with open(GetDatabasePath(path), 'r') as f:
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
185 InstallLuaScript(orthanc, f.read())
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
186
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
187 def InstallLuaScript(orthanc, script):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
188 DoPost(orthanc, '/tools/execute-script', script, 'application/lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
189
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
190 def UninstallLuaCallbacks(orthanc):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
191 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance() end', 'application/lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
192 InstallLuaScriptFromPath(orthanc, 'Lua/TransferSyntaxEnable.lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
193
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
194
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 def ComputeMD5(data):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 m = hashlib.md5()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 m.update(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 return m.hexdigest()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
59
84378ada15ab test_decode_brainix_as_jpeg
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 44
diff changeset
200 def GetImage(orthanc, uri, headers = {}):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 # http://www.pythonware.com/library/pil/handbook/introduction.htm
59
84378ada15ab test_decode_brainix_as_jpeg
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 44
diff changeset
202 data = DoGet(orthanc, uri, headers = headers)
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
203 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
204 return Image.open(BytesIO(data))
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
205 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
206 return Image.open(StringIO(data))
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 def GetArchive(orthanc, uri):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 # http://stackoverflow.com/a/1313868/881731
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 s = DoGet(orthanc, uri)
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
211
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
212 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
213 return zipfile.ZipFile(BytesIO(s), "r")
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
214 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
215 return zipfile.ZipFile(StringIO(s), "r")
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
192
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
217 def PostArchive(orthanc, uri, body):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
218 # http://stackoverflow.com/a/1313868/881731
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
219 s = DoPost(orthanc, uri, body)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
221 if (sys.version_info >= (3, 0)):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
222 return zipfile.ZipFile(BytesIO(s), "r")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
223 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
224 return zipfile.ZipFile(StringIO(s), "r")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
225
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
226 def IsDefinedInLua(orthanc, name):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 return (s.strip() != 'nil')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
230 def WaitEmpty(orthanc):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 while True:
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
232 if len(DoGet(orthanc, '/instances')) == 0:
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 return
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 time.sleep(0.1)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
137
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
236 def WaitJobDone(orthanc, job):
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
237 while True:
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
238 s = DoGet(orthanc, '/jobs/%s' % job) ['State']
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
239
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
240 if s == 'Success':
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
241 return True
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
242 elif s == 'Failure':
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
243 return False
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
244
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
245 time.sleep(0.1)
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
246
138
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
247 def MonitorJob(orthanc, func): # "func" is a lambda
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
248 a = set(DoGet(orthanc, '/jobs'))
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
249 func()
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
250 b = set(DoGet(orthanc, '/jobs'))
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
251
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
252 diff = list(b - a)
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
253 if len(diff) != 1:
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
254 print('No job was created!')
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
255 return False
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
256 else:
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
257 return WaitJobDone(orthanc, diff[0])
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
258
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
259 def MonitorJob2(orthanc, func): # "func" is a lambda
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
260 a = set(DoGet(orthanc, '/jobs'))
181
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
261 job = func()
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
262 b = set(DoGet(orthanc, '/jobs'))
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
263
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
264 diff = list(b - a)
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
265 if len(diff) != 1:
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
266 print('No job was created!')
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
267 return None
181
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
268 elif (not 'ID' in job or
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
269 diff[0] != job['ID']):
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
270 print('Mismatch in the job ID')
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
271 return None
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
272 elif WaitJobDone(orthanc, diff[0]):
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
273 return diff[0]
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
274 else:
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
275 print('Error while executing the job')
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
276 return None
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
277
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
278 def WaitAllNewJobsDone(orthanc, func): # "func" is a lambda
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
279 a = set(DoGet(orthanc, '/jobs'))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
280 func()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
281
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
282 first = True
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
283
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
284 while True:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
285 b = set(DoGet(orthanc, '/jobs'))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
286
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
287 diff = list(b - a)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
288 if len(diff) == 0:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
289 if first:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
290 raise Exception('No job was created')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
291 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
292 return # We're done
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
293 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
294 first = False
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
295
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
296 if WaitJobDone(orthanc, diff[0]):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
297 a.add(diff[0])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
298 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
299 raise Exception('Error while executing the job')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
300
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
301
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
302 def GetDockerHostAddress():
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
303 route = subprocess.check_output([ '/sbin/ip', 'route' ])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
304 m = re.search(r'default via ([0-9.]+)', route)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
305 if m == None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
306 return 'localhost'
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
307 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
308 return m.groups()[0]
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
309
44
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
310 def FindExecutable(name):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
311 p = os.path.join('/usr/local/bin', name)
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
312 if os.path.isfile(p):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
313 return p
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
314
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
315 p = os.path.join('/usr/local/sbin', name)
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
316 if os.path.isfile(p):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
317 return p
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
318
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
319 return name
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
320
173
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
321 def IsOrthancVersionAbove(orthanc, major, minor, revision):
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
322 v = DoGet(orthanc, '/system')['Version']
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
323
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
324 if v == 'mainline':
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
325 return True
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
326 else:
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
327 tmp = v.split('.')
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
328 a = int(tmp[0])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
329 b = int(tmp[1])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
330 c = int(tmp[2])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
331 return (a > major or
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
332 (a == major and b > minor) or
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
333 (a == major and b == minor and c >= revision))
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
334
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
335
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
336 class ExternalCommandThread:
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
337 @staticmethod
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
338 def ExternalCommandFunction(arg, stop_event, command, env):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
339 external = subprocess.Popen(command, env = env)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
340
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
341 while (not stop_event.is_set()):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
342 error = external.poll()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
343 if error != None:
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
344 # http://stackoverflow.com/a/1489838/881731
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
345 os._exit(-1)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
346 stop_event.wait(0.1)
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
347
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
348 print('Stopping the external command')
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
349 external.terminate()
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
350 external.communicate() # Wait for the command to stop
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
351
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
352 def __init__(self, command, env = None):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
353 self.thread_stop = threading.Event()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
354 self.thread = threading.Thread(target = self.ExternalCommandFunction,
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
355 args = (10, self.thread_stop, command, env))
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
356 #self.daemon = True
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
357 self.thread.start()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
358
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
359 def stop(self):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
360 self.thread_stop.set()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
361 self.thread.join()
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
362
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
363
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
364 def AssertAlmostEqualRecursive(self, a, b, places = 7):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
365 if type(a) is dict:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
366 self.assertTrue(type(b) is dict)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
367 self.assertEqual(a.keys(), b.keys())
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
368 for key, value in a.items():
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
369 AssertAlmostEqualRecursive(self, a[key], b[key], places)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
370
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
371 elif type(a) is list:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
372 self.assertTrue(type(b) is list)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
373 self.assertEqual(len(a), len(b))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
374 for i in range(len(a)):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
375 AssertAlmostEqualRecursive(self, a[i], b[i], places)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
376
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
377 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
378 self.assertAlmostEqual(a, b, places = places)