1919import json
2020import shutil
2121from requests .exceptions import HTTPError
22+ from etos_client .lib .graphql import request_artifacts
2223
2324_LOGGER = logging .getLogger (__name__ )
2425
@@ -75,16 +76,23 @@ def all_logs(self):
7576 for log in self ._logs (finished ):
7677 yield log
7778
78- def _get_path (self , _ ):
79- """Path to store log in.
80-
81- :param log_name: Log name to check.
82- :type log_name: str
83- :return: Path where to store log.
84- :rtype: str
85- """
86- # TODO: Detect artifact logs for artifact_dir.
87- return self .report_dir
79+ @property
80+ def all_artifacts (self ):
81+ """Iterate over all artifacts for the executed test suite."""
82+ for artifact_created in request_artifacts (
83+ self .etos , self .events .get ("activityId" )
84+ ):
85+ for _ , location in self .etos .utils .search (artifact_created , "uri" ):
86+ suite_name = ""
87+ for link in artifact_created .get ("links" , []):
88+ for _ , name in self .etos .utils .search (
89+ link .get ("links" , {}), "name"
90+ ):
91+ suite_name = name # There should be exactly one!
92+ for _ , name in self .etos .utils .search (
93+ artifact_created .get ("data" , {}), "name"
94+ ):
95+ yield f"{ suite_name } _{ name } " , f"{ location } /{ name } "
8896
8997 def _iut_data (self , environment ):
9098 """Get IUT data from Environment URI.
@@ -106,26 +114,55 @@ def iuts(self):
106114 if environment .get ("data" , {}).get ("name" , "" ).startswith ("IUT Data" ):
107115 yield self ._iut_data (environment .get ("data" ))
108116
117+ def _download (self , name , uri , directory , spinner ):
118+ """Download a file and and write to disk.
119+
120+ :param name: Name of resulting file.
121+ :type name: str
122+ :param uri: URI from where the file can be downloaded.
123+ :type uri: str
124+ :param directory: Into which directory to write the downloaded file.
125+ :type directory: str
126+ :param spinner: Spinner text item.
127+ :type spinner: :obj:`Spinner`
128+ """
129+ index = 0
130+ download_name = name
131+ while os .path .exists (os .path .join (directory , download_name )):
132+ index += 1
133+ download_name = f"{ index } _{ name } "
134+ spinner .text = "Downloading {}" .format (download_name )
135+ generator = self .etos .http .wait_for_request (uri , as_json = False , stream = True )
136+ try :
137+ for response in generator :
138+ with open (os .path .join (directory , download_name ), "wb+" ) as report :
139+ for chunk in response :
140+ report .write (chunk )
141+ break
142+ return True
143+ except (ConnectionError , HTTPError ) as error :
144+ spinner .warn ("Failed in downloading '{}'." .format (download_name ))
145+ spinner .warn (str (error ))
146+ return False
147+
109148 def download_logs (self , spinner ):
110149 """Download all logs to report and artifact directories."""
111150 nbr_of_logs_downloaded = 0
112151 incomplete = False
113152
114153 for name , uri in self .all_logs :
115- path = self ._get_path (name )
116- spinner . text = "Downloading {}" . format ( name )
117- generator = self . etos . http . wait_for_request ( uri , as_json = False , stream = True )
118- try :
119- for response in generator :
120- with open ( os . path . join ( path , name ), "wb+" ) as report :
121- for chunk in response :
122- report . write ( chunk )
123- nbr_of_logs_downloaded += 1
124- break
125- except ( ConnectionError , HTTPError ) as error :
154+ result = self ._download (name , uri , self . report_dir , spinner )
155+ if result :
156+ nbr_of_logs_downloaded += 1
157+ else :
158+ incomplete = True
159+
160+ for name , uri in self . all_artifacts :
161+ result = self . _download ( name , uri , self . artifact_dir , spinner )
162+ if result :
163+ nbr_of_logs_downloaded += 1
164+ else :
126165 incomplete = True
127- spinner .warn ("Failed in downloading '{}'." .format (name ))
128- spinner .warn (str (error ))
129166
130167 for index , iut in enumerate (self .iuts ):
131168 if iut is None :
@@ -135,7 +172,7 @@ def download_logs(self, spinner):
135172 filename = "IUT_{}.json" .format (index )
136173 with open (os .path .join (self .artifact_dir , filename ), "w+" ) as report :
137174 json .dump (iut , report )
138- except : # pylint:disable=bare -except
175+ except Exception as error : # pylint:disable=broad -except
139176 spinner .warn ("Failed in downloading '{}'." .format (filename ))
140177 spinner .warn (str (error ))
141178 incomplete = True
0 commit comments