annotate OrthancServer/Resources/Samples/Python/Replicate.py @ 5823:a856763e5736 attach-custom-data

fix SDK for custom-data in attachments
author Alain Mazy <am@orthanc.team>
date Wed, 02 Oct 2024 09:39:58 +0200
parents f7adfb22e20e
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
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 # Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
7 # Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
8 # 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
9 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # 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
11 # 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
12 # 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
13 # 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
14 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # 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
16 # 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
17 # 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
18 # General Public License for more details.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # 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
21 # 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
22
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import base64
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import httplib2
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import json
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import re
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 import sys
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 URL_REGEX = re.compile('(http|https)://((.+?):(.+?)@|)(.*)')
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 if len(sys.argv) != 3:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 print("""
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 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
36 server through their REST API.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 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
39 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
40 """ % (sys.argv[0], sys.argv[0]))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 exit(-1)
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 def CreateHeaders(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 headers = { }
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 username = parsedUrl.group(3)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 password = parsedUrl.group(4)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 if username != None and password != None:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 # This is a custom reimplementation of the
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 # "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
53 # 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
54 # always work)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 # 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
56
844ec5ecb6ef Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
57 # 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
58 # 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
59 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
60 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
61 headers['authorization'] = 'Basic ' + credentials_base64
1351
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 return headers
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 def GetBaseUrl(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 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
68
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 def DoGetString(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 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
77
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 if resp.status == 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 return content
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 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
82
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 def DoPostDicom(url, body):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 headers['content-type'] = 'application/dicom'
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST',
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 body = body,
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 headers = headers)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 if resp.status != 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 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
97
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 def _DecodeJson(s):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 if (sys.version_info >= (3, 0)):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 return json.loads(s.decode())
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 return json.loads(s)
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 def DoGetJson(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 return _DecodeJson(DoGetString(url))
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 SOURCE = sys.argv[1]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 TARGET = sys.argv[2]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 for study in DoGetJson('%s/studies' % SOURCE):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 print('Sending study %s...' % study)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 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
116 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
117 DoPostDicom('%s/instances' % TARGET, dicom)