+ directory = spec.environment["paths"]["DIR[WORKING,DATA]"]
+ tmp_dir = join(directory, str(pid))
+ try:
+ mkdir(tmp_dir)
+ except OSError:
+ pass
+ new_name = "{0}{1}{2}".format(file_name.rsplit('.')[-2],
+ SEPARATOR,
+ data_file.split("/")[-1])
+
+ log.append(("INFO", " Unzipping: '{0}' from '{1}'.".
+ format(data_file, file_name)))
+ try:
+ with ZipFile(file_name, 'r') as zip_file:
+ zip_file.extract(data_file, tmp_dir)
+ log.append(("INFO", " Renaming the file '{0}' to '{1}'".
+ format(join(tmp_dir, data_file), new_name)))
+ rename(join(tmp_dir, data_file), new_name)
+ build["file-name"] = new_name
+ return True
+ except (BadZipfile, RuntimeError) as err:
+ log.append(("ERROR", "Failed to unzip the file '{0}': {1}.".
+ format(file_name, str(err))))
+ return False
+ except OSError as err:
+ log.append(("ERROR", "Failed to rename the file '{0}': {1}.".
+ format(data_file, str(err))))
+ return False
+
+
+def download_and_unzip_data_file(spec, job, build, pid, log):
+ """Download and unzip a source file.
+
+ :param spec: Specification read form the specification file.
+ :param job: Name of the Jenkins job.
+ :param build: Information about the build.
+ :param pid: PID of the process executing this method.
+ :param log: List of log messages.
+ :type spec: Specification
+ :type job: str
+ :type build: dict
+ :type pid: int
+ :type log: list of tuples (severity, msg)
+ :returns: True if the download was successful, otherwise False.
+ :rtype: bool
+ """
+
+ # Try to download .gz from logs.fd.io
+
+ file_name = spec.input["file-name"]
+ url = "{0}/{1}".format(
+ spec.environment["urls"]["URL[NEXUS,LOG]"],
+ spec.input["download-path"].format(
+ job=job, build=build["build"], filename=file_name))
+ new_name = join(spec.environment["paths"]["DIR[WORKING,DATA]"],
+ "{job}{sep}{build}{sep}{name}".format(
+ job=job, sep=SEPARATOR, build=build["build"],
+ name=file_name))
+
+ logging.info("Trying to download {0}".format(url))
+
+ arch = True if spec.configuration.get("archive-inputs", True) else False
+ success, downloaded_name = _download_file(url, new_name, log, arch=arch)
+
+ if not success:
+
+ # Try to download .gz from docs.fd.io
+
+ file_name = spec.input["file-name"]
+ url = "{0}/{1}".format(
+ spec.environment["urls"]["URL[NEXUS,DOC]"],
+ spec.input["download-path"].format(
+ job=job, build=build["build"], filename=file_name))
+ new_name = join(spec.environment["paths"]["DIR[WORKING,DATA]"],
+ "{job}{sep}{build}{sep}{name}".format(
+ job=job, sep=SEPARATOR, build=build["build"],
+ name=file_name))
+
+ logging.info("Downloading {0}".format(url))
+
+ if spec.configuration.get("archive-inputs", True):
+ arch = True
+ success, downloaded_name = _download_file(url, new_name, log, arch=arch)
+
+ if not success:
+
+ # Try to download .zip from docs.fd.io
+
+ file_name = spec.input["zip-file-name"]
+ new_name = join(spec.environment["paths"]["DIR[WORKING,DATA]"],
+ "{job}{sep}{build}{sep}{name}".format(
+ job=job, sep=SEPARATOR, build=build["build"],
+ name=file_name))
+ release = re.search(REGEX_RELEASE, job).group(2)
+ for rls in (release, "master"):
+ nexus_file_name = "{job}{sep}{build}{sep}{name}". \
+ format(job=job, sep=SEPARATOR, build=build["build"],
+ name=file_name)