annotate Resources/Samples/Python/Replicate.py @ 2058:43cd2ab060c7

cont OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 Jun 2016 08:20:23 +0200
parents 65b1ce7cb84f
children a3a65de1840f
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # 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
11 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # 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
13 # 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
14 # 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
15 # General Public License for more details.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 #
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # 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
18 # 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
19
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 import base64
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import httplib2
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import json
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import re
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import sys
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 URL_REGEX = re.compile('(http|https)://((.+?):(.+?)@|)(.*)')
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
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 if len(sys.argv) != 3:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 print("""
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 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
33 server through their REST API.
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 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
36 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
37 """ % (sys.argv[0], sys.argv[0]))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 exit(-1)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
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 def CreateHeaders(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 headers = { }
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 username = parsedUrl.group(3)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 password = parsedUrl.group(4)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 if username != None and password != None:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 # This is a custom reimplementation of the
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 # "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
50 # 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
51 # always work)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 # 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
53 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 return headers
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
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 def GetBaseUrl(parsedUrl):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 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
60
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 def DoGetString(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 headers = CreateHeaders(parsedUrl)
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 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 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
69
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if resp.status == 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 return content
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 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
74
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 def DoPostDicom(url, body):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 global URL_REGEX
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 parsedUrl = URL_REGEX.match(url)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 headers = CreateHeaders(parsedUrl)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 headers['content-type'] = 'application/dicom'
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 h = httplib2.Http()
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST',
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 body = body,
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 headers = headers)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 if resp.status != 200:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 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
89
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 def _DecodeJson(s):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if (sys.version_info >= (3, 0)):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 return json.loads(s.decode())
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 else:
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 return json.loads(s)
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 DoGetJson(url):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 return _DecodeJson(DoGetString(url))
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
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 SOURCE = sys.argv[1]
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 TARGET = sys.argv[2]
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 for study in DoGetJson('%s/studies' % SOURCE):
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 print('Sending study %s...' % study)
fa09aa513fd4 script to copy one server to another
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 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
108 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
109 DoPostDicom('%s/instances' % TARGET, dicom)