annotate Tests/Toolbox.py @ 735:be8f174d3c9d find-refactoring tip

tools/find: Limit and Since are now forbidden when filtering on DICOM tags that are not stored in DB
author Alain Mazy <am@orthanc.team>
date Thu, 24 Oct 2024 15:08:59 +0200
parents 0322fda1834e
children d2ac1c6588db
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
649
5d7b6e43ab7d updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 640
diff changeset
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium
5d7b6e43ab7d updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 640
diff changeset
7 # Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
640
9f8276ac1cdd update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 543
diff changeset
8 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
9 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
10 # This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
11 # modify it under the terms of the GNU General Public License as
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
12 # published by the Free Software Foundation, either version 3 of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
13 # License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
14 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
15 # This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
18 # General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
19 #
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
20 # You should have received a copy of the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import hashlib
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import httplib2
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import json
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
27 import os
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
28 import re
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
29 import signal
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
30 import subprocess
291
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
31 import tempfile
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
32 import threading
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
33 import sys
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 import time
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
35 import zipfile
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
337
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
37 from xml.dom import minidom
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
38
304
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
39 from PIL import Image, ImageChops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
40 import math
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
41 import operator
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
42
83
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 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
45 from urllib.parse import urlencode
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
46 from io import StringIO
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
47 from io import BytesIO
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
48 from urllib.parse import unquote
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
49
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
50 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
51 from urllib import urlencode
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
52 from urlparse import unquote
83
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 # http://stackoverflow.com/a/1313868/881731
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 from cStringIO import StringIO
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 from StringIO import StringIO
3
2dbba2e6aa4b reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
59
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
60
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 391
diff changeset
61 def DecodeJson(s):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
62 t = s
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
63
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
64 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
65 try:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
66 t = s.decode()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
67 except:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
68 pass
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
69
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
70 try:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
71 return json.loads(t)
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
72 except:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
73 return t
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
76 def DefineOrthanc(server = 'localhost',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
77 restPort = 8042,
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
78 username = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
79 password = None,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
80 aet = 'ORTHANC',
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
81 dicomPort = 4242):
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
82 #m = re.match(r'(http|https)://([^:]+):([^@]+)@([^@]+)', url)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
83 #if m != None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
84 # url = m.groups()[0] + '://' + m.groups()[3]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
85 # username = m.groups()[1]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
86 # password = m.groups()[2]
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
88 #if not url.endswith('/'):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
89 # url += '/'
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
91 return {
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
92 'Server' : server,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
93 'Url' : 'http://%s:%d/' % (server, restPort),
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
94 'Username' : username,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
95 'Password' : password,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
96 'DicomAet' : aet,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
97 'DicomPort' : dicomPort
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
98 }
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 def _SetupCredentials(orthanc, http):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
102 if (orthanc['Username'] != None and
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
103 orthanc['Password'] != None):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
104 http.add_credentials(orthanc['Username'], orthanc['Password'])
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
28
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
106 def DoGetRaw(orthanc, uri, data = {}, body = None, headers = {}):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 d = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if len(data.keys()) > 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 d = '?' + urlencode(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 http = httplib2.Http()
21
2a29bcff60a7 tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 13
diff changeset
112 http.follow_redirects = False
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
115 resp, content = http.request(orthanc['Url'] + uri + d, 'GET', body = body,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 headers = headers)
28
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
117 return (resp, content)
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
118
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
119
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
120 def DoGet(orthanc, uri, data = {}, body = None, headers = {}):
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
121 (resp, content) = DoGetRaw(orthanc, uri, data = data, body = body, headers = headers)
156c7ae164b5 new test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 21
diff changeset
122
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 if not (resp.status in [ 200 ]):
239
8980bd19e31d dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
124 raise Exception(resp.status, resp)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 else:
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 391
diff changeset
126 return DecodeJson(content)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 def _DoPutOrPost(orthanc, uri, method, data, contentType, headers):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 http = httplib2.Http()
21
2a29bcff60a7 tests of image decoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 13
diff changeset
130 http.follow_redirects = False
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 _SetupCredentials(orthanc, http)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
133 if isinstance(data, (str, bytearray, bytes)):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 body = data
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 if len(contentType) != 0:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 headers['content-type'] = contentType
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 else:
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 body = json.dumps(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 headers['content-type'] = 'application/json'
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 headers['expect'] = ''
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
143 resp, content = http.request(orthanc['Url'] + uri, method,
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 body = body,
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 headers = headers)
391
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
146 return (resp, content)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
147
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
148 def DoDeleteRaw(orthanc, uri, headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
149 http = httplib2.Http()
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
150 http.follow_redirects = False
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
151 _SetupCredentials(orthanc, http)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
152
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
153 resp, content = http.request(orthanc['Url'] + uri, 'DELETE', headers = headers)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
154 return (resp, content)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
155
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
156 def DoDelete(orthanc, uri, headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
157 (resp, content) = DoDeleteRaw(orthanc, uri, headers)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
158 if not (resp.status in [ 200 ]):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
159 raise Exception(resp.status, resp)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
160 else:
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 391
diff changeset
161 return DecodeJson(content)
391
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
162
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
163 def DoPutRaw(orthanc, uri, data = {}, contentType = '', headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
164 return _DoPutOrPost(orthanc, uri, 'PUT', data, contentType, headers)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
165
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
166 def DoPut(orthanc, uri, data = {}, contentType = '', headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
167 (resp, content) = DoPutRaw(orthanc, uri, data, contentType, headers)
342
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
168 if not (resp.status in [ 200, 201, 302 ]):
239
8980bd19e31d dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
169 raise Exception(resp.status, resp)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 else:
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 391
diff changeset
171 return DecodeJson(content)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
391
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
173 def DoPostRaw(orthanc, uri, data = {}, contentType = '', headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
174 return _DoPutOrPost(orthanc, uri, 'POST', data, contentType, headers)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
175
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
176 def DoPost(orthanc, uri, data = {}, contentType = '', headers = {}):
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
177 (resp, content) = DoPostRaw(orthanc, uri, data, contentType, headers)
227d9a932467 testing revisions in metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 363
diff changeset
178 if not (resp.status in [ 200, 201, 302 ]):
239
8980bd19e31d dicomweb: test_allowed_methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 222
diff changeset
179 raise Exception(resp.status, resp)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 else:
398
9528e2a03d3c adapt DICOMweb tests following fix of issue 196 (STOW-RS: Should return 200 only when successfully stored all instances)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 391
diff changeset
181 return DecodeJson(content)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
183 def GetDatabasePath(filename):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
184 return os.path.join(os.path.dirname(__file__), '..', 'Database', filename)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
185
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 def UploadInstance(orthanc, filename):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
187 with open(GetDatabasePath(filename), 'rb') as f:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
188 d = f.read()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
189
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 return DoPost(orthanc, '/instances', d, 'application/dicom')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 def UploadFolder(orthanc, path):
13
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
193 for i in os.listdir(GetDatabasePath(path)):
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
194 try:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
195 UploadInstance(orthanc, os.path.join(path, i))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
196 except:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
197 pass
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 def DropOrthanc(orthanc):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 # Reset the Lua callbacks
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance(instanceId, tags, metadata) end', 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 DoDelete(orthanc, '/exports')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 for s in DoGet(orthanc, '/patients'):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 DoDelete(orthanc, '/patients/%s' % s)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
174
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
208 def InstallLuaScriptFromPath(orthanc, path):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
209 with open(GetDatabasePath(path), 'r') as f:
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
210 InstallLuaScript(orthanc, f.read())
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
211
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
212 def InstallLuaScript(orthanc, script):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
213 DoPost(orthanc, '/tools/execute-script', script, 'application/lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
214
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
215 def UninstallLuaCallbacks(orthanc):
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
216 DoPost(orthanc, '/tools/execute-script', 'function OnStoredInstance() end', 'application/lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
217 InstallLuaScriptFromPath(orthanc, 'Lua/TransferSyntaxEnable.lua')
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
218
d468cbe1b161 added tests for IncomingWorklistRequestFilter
am@osimis.io
parents: 173
diff changeset
219
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 def ComputeMD5(data):
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 m = hashlib.md5()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 m.update(data)
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 return m.hexdigest()
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
249
24e5c8ca9440 DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 239
diff changeset
225 def UncompressImage(data):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
226 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
227 return Image.open(BytesIO(data))
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
228 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
229 return Image.open(StringIO(data))
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
249
24e5c8ca9440 DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 239
diff changeset
231 def GetImage(orthanc, uri, headers = {}):
24e5c8ca9440 DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 239
diff changeset
232 # http://www.pythonware.com/library/pil/handbook/introduction.htm
24e5c8ca9440 DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 239
diff changeset
233 return UncompressImage(DoGet(orthanc, uri, headers = headers))
24e5c8ca9440 DICOMweb: test_rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 239
diff changeset
234
404
931be0125954 Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
235 def ParseArchive(s):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 # http://stackoverflow.com/a/1313868/881731
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
237 if (sys.version_info >= (3, 0)):
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
238 return zipfile.ZipFile(BytesIO(s), "r")
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
239 else:
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
240 return zipfile.ZipFile(StringIO(s), "r")
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
404
931be0125954 Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
242 def GetArchive(orthanc, uri):
931be0125954 Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
243 return ParseArchive(DoGet(orthanc, uri))
931be0125954 Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
244
192
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
245 def PostArchive(orthanc, uri, body):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
246 # http://stackoverflow.com/a/1313868/881731
404
931be0125954 Tests/CheckZipStreams.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
247 return ParseArchive(DoPost(orthanc, uri, body))
192
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
248
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
249 def IsDefinedInLua(orthanc, name):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 s = DoPost(orthanc, '/tools/execute-script', 'print(type(%s))' % name, 'application/lua')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 return (s.strip() != 'nil')
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
253 def WaitEmpty(orthanc):
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 while True:
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
255 if len(DoGet(orthanc, '/instances')) == 0:
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 return
342
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
257 time.sleep(0.01)
0
cc43b57242a4 toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
137
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
259 def WaitJobDone(orthanc, job):
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
260 while True:
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
261 s = DoGet(orthanc, '/jobs/%s' % job) ['State']
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
262
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
263 if s == 'Success':
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
264 return True
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
265 elif s == 'Failure':
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
266 return False
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
267
342
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
268 time.sleep(0.01)
137
412d5f70447e testing asynchronous c-move
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 130
diff changeset
269
138
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
270 def MonitorJob(orthanc, func): # "func" is a lambda
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
271 a = set(DoGet(orthanc, '/jobs'))
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
272 func()
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
273 b = set(DoGet(orthanc, '/jobs'))
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
274
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
275 diff = list(b - a)
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
276 if len(diff) != 1:
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
277 print('No job was created!')
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
278 return False
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
279 else:
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
280 return WaitJobDone(orthanc, diff[0])
0682740fcfcb cleanup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 137
diff changeset
281
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
282 def MonitorJob2(orthanc, func): # "func" is a lambda
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
283 a = set(DoGet(orthanc, '/jobs'))
181
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
284 job = func()
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
285 b = set(DoGet(orthanc, '/jobs'))
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
286
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
287 diff = list(b - a)
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
288 if len(diff) != 1:
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
289 print('No job was created!')
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
290 return None
181
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
291 elif (not 'ID' in job or
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
292 diff[0] != job['ID']):
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
293 print('Mismatch in the job ID')
911070f790e3 more checks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
294 return None
179
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
295 elif WaitJobDone(orthanc, diff[0]):
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
296 return diff[0]
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
297 else:
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
298 print('Error while executing the job')
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
299 return None
8a2dd77d4035 testing split/merge
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 174
diff changeset
300
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
301 def WaitAllNewJobsDone(orthanc, func): # "func" is a lambda
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
302 a = set(DoGet(orthanc, '/jobs'))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
303 func()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
304
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
305 first = True
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
306
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
307 while True:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
308 b = set(DoGet(orthanc, '/jobs'))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
309
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
310 diff = list(b - a)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
311 if len(diff) == 0:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
312 if first:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
313 raise Exception('No job was created')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
314 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
315 return # We're done
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
316 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
317 first = False
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
318
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
319 if WaitJobDone(orthanc, diff[0]):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
320 a.add(diff[0])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
321 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
322 raise Exception('Error while executing the job')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
323
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
324
1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
325 def GetDockerHostAddress():
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
326 route = subprocess.check_output([ '/sbin/ip', 'route' ])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
327 m = re.search(r'default via ([0-9.]+)', route)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
328 if m == None:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
329 return 'localhost'
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
330 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
331 return m.groups()[0]
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
332
44
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
333 def FindExecutable(name):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
334 p = os.path.join('/usr/local/bin', name)
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
335 if os.path.isfile(p):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
336 return p
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
337
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
338 p = os.path.join('/usr/local/sbin', name)
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
339 if os.path.isfile(p):
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
340 return p
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
341
ffa542cce638 Toolbox.FindExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 28
diff changeset
342 return name
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
343
173
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
344 def IsOrthancVersionAbove(orthanc, major, minor, revision):
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
345 v = DoGet(orthanc, '/system')['Version']
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
346
703
8b255268d79b support mainline-xxx versions
Alain Mazy <am@orthanc.team>
parents: 693
diff changeset
347 if v.startswith('mainline'):
173
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
348 return True
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
349 else:
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
350 tmp = v.split('.')
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
351 a = int(tmp[0])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
352 b = int(tmp[1])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
353 c = int(tmp[2])
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
354 return (a > major or
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
355 (a == major and b > minor) or
ed3db6386587 testing overwrite
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 138
diff changeset
356 (a == major and b == minor and c >= revision))
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
357
693
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
358
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
359 def HasExtendedFind(orthanc):
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
360 v = DoGet(orthanc, '/system')
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
361
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
362 if 'Capabilities' in v and 'HasExtendedFind' in v['Capabilities']:
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
363 return v['Capabilities']['HasExtendedFind']
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
364 return False
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
365
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
366
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
367 def HasExtendedChanges(orthanc):
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
368 v = DoGet(orthanc, '/system')
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
369
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
370 if 'Capabilities' in v and 'HasExtendedChanges' in v['Capabilities']:
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
371 return v['Capabilities']['HasExtendedChanges']
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
372 return False
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
373
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
374
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
375 def GetStorageAccessesCount(orthanc):
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
376 mm = DoGetRaw(orthanc, "/tools/metrics-prometheus")[1]
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
377
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
378 if (sys.version_info >= (3, 0)):
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
379 try:
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
380 mm = mm.decode()
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
381 except:
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
382 pass
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
383
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
384 mm = [x.split(" ") for x in mm.split("\n")]
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
385
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
386 count = 0
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
387 for m in mm:
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
388 if m[0] == 'orthanc_storage_cache_hit_count':
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
389 count += int(m[1])
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
390 if m[0] == 'orthanc_storage_cache_miss_count':
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
391 count += int(m[1])
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
392
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
393 # print("storage access count = %s" % count)
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
394 return count
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
395
4567c3947f8a more requested tags tests
Alain Mazy <am@orthanc.team>
parents: 649
diff changeset
396
532
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
397 def IsPluginVersionAbove(orthanc, plugin, major, minor, revision):
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
398 v = DoGet(orthanc, '/plugins/%s' % plugin)['Version']
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
399
711
Alain Mazy <am@orthanc.team>
parents: 703
diff changeset
400 if v.startswith('mainline'):
532
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
401 return True
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
402 else:
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
403 tmp = v.split('.')
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
404 if len(tmp) >= 3:
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
405 a = int(tmp[0])
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
406 b = int(tmp[1])
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
407 c = int(tmp[2])
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
408 return (a > major or
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
409 (a == major and b > minor) or
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
410 (a == major and b == minor and c >= revision))
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
411 elif len(tmp) >= 2:
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
412 a = int(tmp[0])
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
413 b = int(tmp[1])
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
414 return (a > major or
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
415 (a == major and b > minor))
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
416 else:
f96c023ce523 dicom-web X-Forwarded headers
Alain Mazy <am@osimis.io>
parents: 511
diff changeset
417 return False
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
418
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
419 class ExternalCommandThread:
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
420 @staticmethod
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
421 def ExternalCommandFunction(arg, stop_event, command, env):
318
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
422 with open(os.devnull, 'w') as devnull:
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
423 external = subprocess.Popen(command, env = env, stderr = devnull)
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
424
318
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
425 while (not stop_event.is_set()):
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
426 error = external.poll()
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
427 if error != None:
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
428 # http://stackoverflow.com/a/1489838/881731
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
429 os._exit(-1)
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
430 stop_event.wait(0.1)
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
431
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
432 print('Stopping the external command')
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
433 external.terminate()
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
434 external.communicate() # Wait for the command to stop
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
435
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
436 def __init__(self, command, env = None):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
437 self.thread_stop = threading.Event()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
438 self.thread = threading.Thread(target = self.ExternalCommandFunction,
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
439 args = (10, self.thread_stop, command, env))
9
3670535fbd4b fix external thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
440 #self.daemon = True
4
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
441 self.thread.start()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
442
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
443 def stop(self):
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
444 self.thread_stop.set()
292a46fe374c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3
diff changeset
445 self.thread.join()
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
446
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
447
222
0f03ee6ffa80 DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 220
diff changeset
448 def AssertAlmostEqualRecursive(self, a, b, places = 7, ignoreKeys = []):
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
449 if type(a) is dict:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
450 self.assertTrue(type(b) is dict)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
451 self.assertEqual(a.keys(), b.keys())
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
452 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
453 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
454 AssertAlmostEqualRecursive(self, a[key], b[key], places)
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
455
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
456 elif type(a) is list:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
457 self.assertTrue(type(b) is list)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
458 self.assertEqual(len(a), len(b))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
459 for i in range(len(a)):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
460 AssertAlmostEqualRecursive(self, a[i], b[i], places)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
461
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
462 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 195
diff changeset
463 self.assertAlmostEqual(a, b, places = places)
291
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
464
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
465
543
Alain Mazy <am@osimis.io>
parents: 542
diff changeset
466 def GetTransferSyntax(dicom, encoding='utf-8'):
291
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
467 with tempfile.NamedTemporaryFile(delete = True) as f:
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
468 f.write(dicom)
cfa785074c64 test_modify_transcode
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 260
diff changeset
469 f.flush()
318
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
470
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
471 with open(os.devnull, 'w') as devnull:
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
472 data = subprocess.check_output([ FindExecutable('dcm2xml'), f.name ],
bac7cc80f240 dicomweb: test_wado_transcoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 304
diff changeset
473 stderr = devnull)
543
Alain Mazy <am@osimis.io>
parents: 542
diff changeset
474 return re.search('<data-set xfer="(.*?)"', data.decode(encoding)).group(1)
304
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
475
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
476
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
477 def HasGdcmPlugin(orthanc):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
478 plugins = DoGet(orthanc, '/plugins')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
479 return ('gdcm' in plugins)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
480
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
481
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
482 def _GetMaxImageDifference(im1, im2):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
483 h = ImageChops.difference(im1, im2).histogram()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
484
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
485 if len(h) < 256:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
486 raise Exception()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
487
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
488 i = len(h) - 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
489 while h[i] == 0:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
490 i -= 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
491
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
492 return i
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
493
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
494
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
495 def GetMaxImageDifference(im1, im2):
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
496 if im1.mode != im2.mode:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
497 raise Exception('Incompatible image modes')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
498
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
499 if im1.mode == 'RGB':
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
500 red1, green1, blue1 = im1.split()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
501 red2, green2, blue2 = im2.split()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
502 return max([ _GetMaxImageDifference(red1, red2),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
503 _GetMaxImageDifference(green1, green2),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
504 _GetMaxImageDifference(blue1, blue2) ])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
505 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 291
diff changeset
506 return _GetMaxImageDifference(im1, im2)
337
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
507
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
508
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
509 def DoPropFind(orthanc, uri, depth):
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
510 http = httplib2.Http()
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
511 http.follow_redirects = False
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
512 _SetupCredentials(orthanc, http)
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
513
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
514 resp, content = http.request(orthanc['Url'] + uri, 'PROPFIND', headers = { 'Depth' : str(depth) })
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
515
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
516 if not (resp.status in [ 207 ]):
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
517 raise Exception(resp.status, resp)
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
518 else:
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
519 xml = minidom.parseString(content)
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
520
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
521 if (xml.documentElement.nodeName != 'D:multistatus' or
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
522 xml.documentElement.attributes['xmlns:D'].value != 'DAV:'):
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
523 raise Exception()
337
ec13ace43bde trying webdav tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 318
diff changeset
524
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
525 result = {}
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
526
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
527 for i in xml.documentElement.childNodes:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
528 if i.nodeType == minidom.Node.ELEMENT_NODE:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
529 if i.nodeName != 'D:response':
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
530 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
531 href = None
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
532 prop = None
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
533 for j in i.childNodes:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
534 if j.nodeType == minidom.Node.ELEMENT_NODE:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
535 if j.nodeName == 'D:href':
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
536 if href == None:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
537 href = unquote(j.firstChild.nodeValue)
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
538 else:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
539 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
540 elif j.nodeName == 'D:propstat':
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
541 for k in j.childNodes:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
542 if k.nodeName == 'D:status':
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
543 if k.firstChild.nodeValue != 'HTTP/1.1 200 OK':
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
544 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
545 elif k.nodeType == minidom.Node.ELEMENT_NODE:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
546 if (k.nodeName != 'D:prop' or
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
547 prop != None):
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
548 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
549 prop = k
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
550 else:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
551 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
552 if href == None or prop == None:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
553 raise Exception()
342
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
554
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
555 info = {}
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
556
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
557 for j in prop.childNodes:
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
558 if j.nodeType == minidom.Node.ELEMENT_NODE:
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
559 if j.nodeName == 'D:displayname':
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
560 info['displayname'] = j.firstChild.nodeValue if j.firstChild != None else ''
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
561 elif j.nodeName == 'D:creationdate':
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
562 info['creationdate'] = j.firstChild.nodeValue
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
563 elif j.nodeName == 'D:getlastmodified':
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
564 info['lastmodified'] = j.firstChild.nodeValue
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
565 elif j.nodeName == 'D:resourcetype':
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
566 k = j.getElementsByTagName('D:collection')
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
567 info['folder'] = (len(k) == 1)
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
568
bf8369ea3ff1 more tests of webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 341
diff changeset
569 result[href] = info
341
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
570 elif i.nodeType != minidom.Node.TEXT_NODE:
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
571 raise Exception()
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
572
66a36befb208 extending Toolbox.DoPropFind()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 337
diff changeset
573 return result