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.
21 from os import rename, mkdir
22 from os.path import join
23 from zipfile import ZipFile, is_zipfile, BadZipfile
24 from httplib import responses
25 from requests import get, codes, RequestException, Timeout, TooManyRedirects, \
26 HTTPError, ConnectionError
28 from errors import PresentationError
29 from utils import execute_command
31 # Chunk size used for file download
34 # Separator used in file names
37 REGEX_RELEASE = re.compile(r'(\D*)(\d{4}|master)(\D*)')
40 def _download_file(url, file_name, log):
41 """Download a file with input data.
43 :param url: URL to the file to download.
44 :param file_name: Name of file to download.
45 :param log: List of log messages.
48 :type log: list of tuples (severity, msg)
49 :returns: True if the download was successful, otherwise False.
55 log.append(("INFO", " Connecting to '{0}' ...".format(url)))
57 response = get(url, stream=True)
58 code = response.status_code
60 log.append(("INFO", " {0}: {1}".format(code, responses[code])))
62 if code != codes["OK"]:
65 log.append(("INFO", " Downloading the file '{0}' to '{1}' ...".
66 format(url, file_name)))
68 file_handle = open(file_name, "wb")
69 for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
71 file_handle.write(chunk)
74 except ConnectionError as err:
75 log.append(("ERROR", "Not possible to connect to '{0}'.".format(url)))
76 log.append(("DEBUG", str(err)))
77 except HTTPError as err:
78 log.append(("ERROR", "Invalid HTTP response from '{0}'.".format(url)))
79 log.append(("DEBUG", str(err)))
80 except TooManyRedirects as err:
81 log.append(("ERROR", "Request exceeded the configured number "
82 "of maximum re-directions."))
83 log.append(("DEBUG", str(err)))
84 except Timeout as err:
85 log.append(("ERROR", "Request timed out."))
86 log.append(("DEBUG", str(err)))
87 except RequestException as err:
88 log.append(("ERROR", "Unexpected HTTP request exception."))
89 log.append(("DEBUG", str(err)))
90 except (IOError, ValueError, KeyError) as err:
91 log.append(("ERROR", "Download failed."))
92 log.append(("DEBUG", str(err)))
94 log.append(("INFO", " Download finished."))
98 def _unzip_file(spec, build, pid, log):
99 """Unzip downloaded source file.
101 :param spec: Specification read form the specification file.
102 :param build: Information about the build.
103 :param log: List of log messages.
104 :type spec: Specification
106 :type log: list of tuples (severity, msg)
107 :returns: True if the download was successful, otherwise False.
111 data_file = spec.input["extract"]
112 file_name = build["file-name"]
113 directory = spec.environment["paths"]["DIR[WORKING,DATA]"]
114 tmp_dir = join(directory, str(pid))
119 new_name = "{0}{1}{2}".format(file_name.rsplit('.')[-2],
121 data_file.split("/")[-1])
123 log.append(("INFO", " Unzipping: '{0}' from '{1}'.".
124 format(data_file, file_name)))
126 with ZipFile(file_name, 'r') as zip_file:
127 zip_file.extract(data_file, tmp_dir)
128 log.append(("INFO", " Renaming the file '{0}' to '{1}'".
129 format(join(tmp_dir, data_file), new_name)))
130 rename(join(tmp_dir, data_file), new_name)
131 build["file-name"] = new_name
133 except (BadZipfile, RuntimeError) as err:
134 log.append(("ERROR", "Failed to unzip the file '{0}': {1}.".
135 format(file_name, str(err))))
137 except OSError as err:
138 log.append(("ERROR", "Failed to rename the file '{0}': {1}.".
139 format(data_file, str(err))))
143 def download_and_unzip_data_file(spec, job, build, pid, log):
144 """Download and unzip a source file.
146 :param spec: Specification read form the specification file.
147 :param job: Name of the Jenkins job.
148 :param build: Information about the build.
149 :param pid: PID of the process executing this method.
150 :param log: List of log messages.
151 :type spec: Specification
155 :type log: list of tuples (severity, msg)
156 :returns: True if the download was successful, otherwise False.
160 if job.startswith("csit-"):
161 if spec.input["file-name"].endswith(".zip"):
162 url = spec.environment["urls"]["URL[JENKINS,CSIT]"]
163 elif spec.input["file-name"].endswith(".gz"):
164 url = spec.environment["urls"]["URL[NEXUS,LOG]"]
166 log.append(("ERROR", "Not supported file format."))
168 elif job.startswith("hc2vpp-"):
169 url = spec.environment["urls"]["URL[JENKINS,HC]"]
170 elif job.startswith("intel-dnv-"):
171 url = spec.environment["urls"]["URL[VIRL,DNV]"]
173 raise PresentationError("No url defined for the job '{}'.".
175 file_name = spec.input["file-name"]
176 full_name = spec.input["download-path"]. \
177 format(job=job, build=build["build"], filename=file_name)
178 if not job.startswith("intel-dnv-"):
179 url = "{0}/{1}".format(url, full_name)
180 new_name = join(spec.environment["paths"]["DIR[WORKING,DATA]"],
181 "{job}{sep}{build}{sep}{name}".
182 format(job=job, sep=SEPARATOR, build=build["build"],
185 logging.info(new_name)
187 # Download the file from the defined source (Jenkins, logs.fd.io):
188 success = _download_file(url, new_name, log)
190 logging.info("{}: {}".format(url, success))
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 = _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)))