annotate Resources/Samples/Python/Replicate.py @ 3994:aba6b6b490ea

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Jun 2020 17:20:56 +0200
parents 94f4a18a79cc
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
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
6 # Copyright (C) 2017-2020 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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 return headers
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 def GetBaseUrl(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 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
61
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 def DoGetString(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 headers = CreateHeaders(parsedUrl)
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 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 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
70
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if resp.status == 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 return content
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 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
75
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 def DoPostDicom(url, body):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 headers['content-type'] = 'application/dicom'
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 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST',
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 body = body,
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 headers = headers)
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 if resp.status != 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 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
90
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 def _DecodeJson(s):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (sys.version_info >= (3, 0)):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 return json.loads(s.decode())
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return json.loads(s)
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 DoGetJson(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 return _DecodeJson(DoGetString(url))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
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 SOURCE = sys.argv[1]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 TARGET = sys.argv[2]
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 for study in DoGetJson('%s/studies' % SOURCE):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 print('Sending study %s...' % study)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 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
109 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
110 DoPostDicom('%s/instances' % TARGET, dicom)