Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/Replicate.py @ 4688:177ad026d219
backward compatibility with old DicomModification serialization from Orthanc <= 1.6.1
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jun 2021 10:09:54 +0200 |
parents | 844ec5ecb6ef |
children | 7053502fbf97 |
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 | 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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
6 # Copyright (C) 2017-2021 Osimis S.A., Belgium |
1351
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 # |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # 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
|
12 # |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # General Public License for more details. |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 # |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 # 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
|
19 # 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
|
20 |
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 import base64 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 import httplib2 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 import json |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 import re |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 import sys |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 URL_REGEX = re.compile('(http|https)://((.+?):(.+?)@|)(.*)') |
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 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 if len(sys.argv) != 3: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 print(""" |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 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
|
34 server through their REST API. |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 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
|
37 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
|
38 """ % (sys.argv[0], sys.argv[0])) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 exit(-1) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
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 def CreateHeaders(parsedUrl): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 headers = { } |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 username = parsedUrl.group(3) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 password = parsedUrl.group(4) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 if username != None and password != None: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 # This is a custom reimplementation of the |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 # "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
|
51 # 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
|
52 # always work) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 # 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
|
54 |
844ec5ecb6ef
Fix "OrthancServer/Resources/Samples/Python/Replicate.py" for Python 3.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
55 # 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
|
56 # 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
|
57 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
|
58 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
|
59 headers['authorization'] = 'Basic ' + credentials_base64 |
1351
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 return headers |
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 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 def GetBaseUrl(parsedUrl): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 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
|
66 |
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 def DoGetString(url): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 global URL_REGEX |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 parsedUrl = URL_REGEX.match(url) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 headers = CreateHeaders(parsedUrl) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 h = httplib2.Http() |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 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
|
75 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 if resp.status == 200: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 return content |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 else: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 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
|
80 |
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 def DoPostDicom(url, body): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 global URL_REGEX |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 parsedUrl = URL_REGEX.match(url) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 headers = CreateHeaders(parsedUrl) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 headers['content-type'] = 'application/dicom' |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 h = httplib2.Http() |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST', |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 body = body, |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 headers = headers) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 if resp.status != 200: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 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
|
95 |
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 def _DecodeJson(s): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 if (sys.version_info >= (3, 0)): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 return json.loads(s.decode()) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 else: |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 return json.loads(s) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 |
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 def DoGetJson(url): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 return _DecodeJson(DoGetString(url)) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 |
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 SOURCE = sys.argv[1] |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 TARGET = sys.argv[2] |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 for study in DoGetJson('%s/studies' % SOURCE): |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 print('Sending study %s...' % study) |
fa09aa513fd4
script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 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
|
114 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
|
115 DoPostDicom('%s/instances' % TARGET, dicom) |