26
26
import os
27
27
import json
28
28
import requests
29
+ import shutil
29
30
30
31
from DIRAC import S_OK , S_ERROR
31
32
from DIRAC .Core .Security import Locations
@@ -144,7 +145,7 @@ def _urlJoin(self, command):
144
145
"""
145
146
return os .path .join (self .base_url , command )
146
147
147
- def _request (self , method , query , params = None , data = None , headers = None , timeout = None ):
148
+ def _request (self , method , query , params = None , data = None , headers = None , timeout = None , stream = False ):
148
149
"""Perform a request and properly handle the results/exceptions.
149
150
150
151
:param str method: "post", "get", "put"
@@ -164,12 +165,7 @@ def _request(self, method, query, params=None, data=None, headers=None, timeout=
164
165
165
166
try :
166
167
response = self .session .request (
167
- method ,
168
- query ,
169
- headers = headers ,
170
- params = params ,
171
- data = data ,
172
- timeout = timeout ,
168
+ method , query , headers = headers , params = params , data = data , timeout = timeout , stream = stream
173
169
)
174
170
if not response .ok :
175
171
return S_ERROR (f"Response: { response .status_code } - { response .reason } " )
@@ -811,20 +807,21 @@ def getJobOutput(self, jobID, workingDirectory=None):
811
807
query = self ._urlJoin (os .path .join ("jobs" , job , "session" , remoteOutput ))
812
808
813
809
# Submit the GET request to retrieve outputs
814
- result = self ._request ("get" , query )
810
+ result = self ._request ("get" , query , stream = True )
815
811
if not result ["OK" ]:
816
812
self .log .error ("Error downloading" , f"{ remoteOutput } for { job } : { result ['Message' ]} " )
817
813
return S_ERROR (f"Error downloading { remoteOutput } for { jobID } " )
818
814
response = result ["Value" ]
819
- outputContent = response .text
815
+
816
+ localOutput = os .path .join (workingDirectory , remoteOutput )
817
+ with open (localOutput , "wb" ) as f :
818
+ shutil .copyfileobj (response .raw , f )
820
819
821
820
if remoteOutput == f"{ stamp } .out" :
822
- stdout = outputContent
823
- elif remoteOutput == f"{ stamp } .err" :
824
- stderr = outputContent
825
- else :
826
- localOutput = os .path .join (workingDirectory , remoteOutput )
827
- with open (localOutput , "w" ) as f :
828
- f .write (outputContent )
821
+ with open (localOutput ) as f :
822
+ stdout = f .read ()
823
+ if remoteOutput == f"{ stamp } .err" :
824
+ with open (localOutput ) as f :
825
+ stderr = f .read ()
829
826
830
827
return S_OK ((stdout , stderr ))
0 commit comments