annotate OrthancServer/Resources/Samples/Python/Replicate.py @ 5628:6045c696e86b default

upgraded to boost 1.85.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 16 May 2024 21:33:19 +0200
parents 48b8dae6dc77
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1351
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
6 # Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
7 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # This program is free software: you can redistribute it and/or
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # modify it under the terms of the GNU General Public License as
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # published by the Free Software Foundation, either version 3 of the
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # License, or (at your option) any later version.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # This program is distributed in the hope that it will be useful, but
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # General Public License for more details.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # You should have received a copy of the GNU General Public License
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import base64
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import httplib2
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import json
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import re
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import sys
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 URL_REGEX = re.compile('(http|https)://((.+?):(.+?)@|)(.*)')
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 if len(sys.argv) != 3:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 print("""
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 Script to copy the content of one Orthanc server to another Orthanc
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 server through their REST API.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 Usage: %s [SourceURI] [TargetURI]
2032
65b1ce7cb84f Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
38 For instance: %s http://orthanc:password@127.0.0.1:8042/ http://127.0.0.1:8043/
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 """ % (sys.argv[0], sys.argv[0]))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 exit(-1)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 def CreateHeaders(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 headers = { }
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 username = parsedUrl.group(3)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 password = parsedUrl.group(4)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 if username != None and password != None:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 # This is a custom reimplementation of the
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 # "Http.add_credentials()" method for Basic HTTP Access
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 # Authentication (for some weird reason, this method does not
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 # always work)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 # http://en.wikipedia.org/wiki/Basic_access_authentication
4625
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
55
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
56 # The ".encode()" and ".decode()" below fix the following issue:
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
57 # https://groups.google.com/g/orthanc-users/c/KZ_iYOet5IQ/m/AUQWiSiAAQAJ
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
58 credentials = (username + ':' + password)
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
59 credentials_base64 = base64.b64encode(credentials.encode("ascii")).decode("ascii")
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
60 headers['authorization'] = 'Basic ' + credentials_base64
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 return headers
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 def GetBaseUrl(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 return '%s://%s' % (parsedUrl.group(1), parsedUrl.group(5))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 def DoGetString(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 resp, content = h.request(GetBaseUrl(parsedUrl), 'GET', headers = headers)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 if resp.status == 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return content
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 raise Exception('Unable to contact Orthanc at: ' + url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 def DoPostDicom(url, body):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 headers['content-type'] = 'application/dicom'
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST',
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 body = body,
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 headers = headers)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 if resp.status != 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 raise Exception('Unable to contact Orthanc at: ' + url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 def _DecodeJson(s):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if (sys.version_info >= (3, 0)):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 return json.loads(s.decode())
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 return json.loads(s)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 def DoGetJson(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 return _DecodeJson(DoGetString(url))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 SOURCE = sys.argv[1]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 TARGET = sys.argv[2]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 for study in DoGetJson('%s/studies' % SOURCE):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 print('Sending study %s...' % study)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 for instance in DoGetJson('%s/studies/%s/instances' % (SOURCE, study)):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 dicom = DoGetString('%s/instances/%s/file' % (SOURCE, instance['ID']))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 DoPostDicom('%s/instances' % TARGET, dicom)