annotate Tests/Toolbox.py @ 291:cfa785074c64

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