1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
20 from os import rename, mkdir
21 from os.path import join
22 from zipfile import ZipFile, is_zipfile, BadZipfile
23 from httplib import responses
24 from requests import get, codes, RequestException, Timeout, TooManyRedirects, \
25 HTTPError, ConnectionError
27 from errors import PresentationError
28 from utils import execute_command
30 # Chunk size used for file download
33 # Separator used in file names
36 REGEX_RELEASE = re.compile(r'(\D*)(\d{4}|master)(\D*)')
39 def _download_file(url, file_name, log):
40 """Download a file with input data.
42 :param url: URL to the file to download.
43 :param file_name: Name of file to download.
44 :param log: List of log messages.
47 :type log: list of tuples (severity, msg)
48 :returns: True if the download was successful, otherwise False.
54 log.append(("INFO", " Connecting to '{0}' ...".format(url)))
55 response = get(url, stream=True)
56 code = response.status_code
57 log.append(("INFO", " {0}: {1}".format(code, responses[code])))
59 if code != codes["OK"]:
60 url = url.replace("_info", "")
61 log.append(("INFO", " Connecting to '{0}' ...".format(url)))
62 response = get(url, stream=True)
63 code = response.status_code
64 log.append(("INFO", " {0}: {1}".format(code, responses[code])))
65 if code != codes["OK"]:
66 return False, file_name
67 file_name = file_name.replace("_info", "")
69 log.append(("INFO", " Downloading the file '{0}' to '{1}' ...".
70 format(url, file_name)))
72 with open(file_name, "wb") as file_handle:
73 for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
75 file_handle.write(chunk)
77 except ConnectionError as err:
78 log.append(("ERROR", "Not possible to connect to '{0}'.".format(url)))
79 log.append(("DEBUG", repr(err)))
80 except HTTPError as err:
81 log.append(("ERROR", "Invalid HTTP response from '{0}'.".format(url)))
82 log.append(("DEBUG", repr(err)))
83 except TooManyRedirects as err:
84 log.append(("ERROR", "Request exceeded the configured number "
85 "of maximum re-directions."))
86 log.append(("DEBUG", repr(err)))
87 except Timeout as err:
88 log.append(("ERROR", "Request timed out."))
89 log.append(("DEBUG", repr(err)))
90 except RequestException as err:
91 log.append(("ERROR", "Unexpected HTTP request exception."))
92 log.append(("DEBUG", repr(err)))
93 except (IOError, ValueError, KeyError) as err:
94 log.append(("ERROR", "Download failed."))
95 log.append(("DEBUG", repr(err)))
97 log.append(("INFO", " Download finished."))
98 return success, file_name
101 def _unzip_file(spec, build, pid, log):
102 """Unzip downloaded source file.
104 :param spec: Specification read form the specification file.
105 :param build: Information about the build.
106 :param log: List of log messages.
107 :type spec: Specification
109 :type log: list of tuples (severity, msg)
110 :returns: True if the download was successful, otherwise False.
114 data_file = spec.input["extract"]
115 file_name = build["file-name"]
116 directory = spec.environment["paths"]["DIR[WORKING,DATA]"]
117 tmp_dir = join(directory, str(pid))
122 new_name = "{0}{1}{2}".format(file_name.rsplit('.')[-2],
124 data_file.split("/")[-1])
126 log.append(("INFO", " Unzipping: '{0}' from '{1}'.".
127 format(data_file, file_name)))
129 with ZipFile(file_name, 'r') as zip_file:
130 zip_file.extract(data_file, tmp_dir)
131 log.append(("INFO", " Renaming the file '{0}' to '{1}'".
132 format(join(tmp_dir, data_file), new_name)))
133 rename(join(tmp_dir, data_file), new_name)
134 build["file-name"] = new_name
136 except (BadZipfile, RuntimeError) as err:
137 log.append(("ERROR", "Failed to unzip the file '{0}': {1}.".
138 format(file_name, str(err))))
140 except OSError as err:
141 log.append(("ERROR", "Failed to rename the file '{0}': {1}.".
142 format(data_file, str(err))))
146 def download_and_unzip_data_file(spec, job, build, pid, log):
147 """Download and unzip a source file.
149 :param spec: Specification read form the specification file.
150 :param job: Name of the Jenkins job.
151 :param build: Information about the build.
152 :param pid: PID of the process executing this method.
153 :param log: List of log messages.
154 :type spec: Specification
158 :type log: list of tuples (severity, msg)
159 :returns: True if the download was successful, otherwise False.
163 if job.startswith("csit-"):
164 if spec.input["file-name"].endswith(".zip"):
165 url = spec.environment["urls"]["URL[JENKINS,CSIT]"]
166 elif spec.input["file-name"].endswith(".gz"):
167 url = spec.environment["urls"]["URL[NEXUS,LOG]"]
169 log.append(("ERROR", "Not supported file format."))
171 elif job.startswith("hc2vpp-"):
172 url = spec.environment["urls"]["URL[JENKINS,HC]"]
173 elif job.startswith("intel-dnv-"):
174 url = spec.environment["urls"]["URL[VIRL,DNV]"].format(release=job[-4:])
176 raise PresentationError("No url defined for the job '{}'.".format(job))
177 file_name = spec.input["file-name"]
178 full_name = spec.input["download-path"]. \
179 format(job=job, build=build["build"], filename=file_name)
180 if not job.startswith("intel-dnv-"):
181 url = "{0}/{1}".format(url, full_name)
182 new_name = join(spec.environment["paths"]["DIR[WORKING,DATA]"],
183 "{job}{sep}{build}{sep}{name}".
184 format(job=job, sep=SEPARATOR, build=build["build"],
187 # Download the file from the defined source (Jenkins, logs.fd.io):
188 success, downloaded_name = _download_file(url, new_name, log)
190 new_name = downloaded_name
192 if success and new_name.endswith(".zip"):
193 if not is_zipfile(new_name):
196 # If not successful, download from docs.fd.io:
198 log.append(("INFO", " Trying to download from https://docs.fd.io:"))
199 release = re.search(REGEX_RELEASE, job).group(2)
200 for rls in (release, "master"):
201 nexus_file_name = "{job}{sep}{build}{sep}{name}". \
202 format(job=job, sep=SEPARATOR, build=build["build"],
205 rls = "rls{0}".format(int(rls))
208 url = "{url}/{release}/{dir}/{file}". \
209 format(url=spec.environment["urls"]["URL[NEXUS]"],
211 dir=spec.environment["urls"]["DIR[NEXUS]"],
212 file=nexus_file_name)
213 success, new_name = _download_file(url, new_name, log)
218 build["file-name"] = new_name
222 if spec.input["file-name"].endswith(".gz"):
223 if "docs.fd.io" in url:
224 execute_command("gzip --decompress --keep --force {0}".
227 rename(new_name, new_name[:-3])
228 execute_command("gzip --keep {0}".format(new_name[:-3]))
229 build["file-name"] = new_name[:-3]
231 if new_name.endswith(".zip"):
232 if is_zipfile(new_name):
233 return _unzip_file(spec, build, pid, log)
236 "Zip file '{0}' is corrupted.".format(new_name)))