- if spec.is_debug:
- data_file = spec.debug["extract"]
- else:
- data_file = spec.input["extract"]
-
- for job, builds in spec.builds.items():
- for build in builds:
- if build["status"] == "failed" or build["status"] == "not found":
- continue
- try:
- status = "failed"
- directory = spec.environment["paths"]["DIR[WORKING,DATA]"]
- file_name = join(build["file-name"])
-
- if build["status"] == "downloaded":
- logging.info("Unziping: '{0}' from '{1}'.".
- format(data_file, file_name))
- new_name = "{0}{1}{2}".format(file_name.rsplit('.')[-2],
- SEPARATOR,
- data_file.split("/")[-1])
- try:
- if is_zipfile(file_name):
- with ZipFile(file_name, 'r') as zip_file:
- zip_file.extract(data_file, directory)
- logging.info("Moving {0} to {1} ...".
- format(join(directory, data_file),
- directory))
- move(join(directory, data_file), directory)
- logging.info("Renaming the file '{0}' to '{1}'".
- format(join(directory,
- data_file.split("/")[-1]),
- new_name))
- rename(join(directory, data_file.split("/")[-1]),
- new_name)
- spec.set_input_file_name(job, build["build"],
- new_name)
- status = "unzipped"
- spec.set_input_state(job, build["build"], status)
- except (BadZipfile, RuntimeError) as err:
- logging.error("Failed to unzip the file '{0}': {1}.".
- format(file_name, str(err)))
- except OSError as err:
- logging.error("Failed to rename the file '{0}': {1}.".
- format(data_file, str(err)))
- finally:
- if status == "failed":
- spec.set_input_file_name(job, build["build"], None)
- else:
- raise PresentationError("The file '{0}' does not exist or "
- "it is not a zip file".
- format(file_name))
+ file_name = build[u"file-name"]
+ data_file = "robot-plugin/output.xml"
+ directory = spec.environment[u"paths"][u"DIR[WORKING,DATA]"]
+ tmp_dir = join(directory, str(pid))
+ try:
+ mkdir(tmp_dir)
+ except OSError:
+ pass
+ new_name = \
+ f"{file_name.rsplit(u'.')[-2]}{SEPARATOR}{data_file.split(u'/')[-1]}"
+
+ logging.info(f" Unzipping: {data_file} from {file_name}.")
+ try:
+ with ZipFile(file_name, u'r') as zip_file:
+ zip_file.extract(data_file, tmp_dir)
+ logging.info(
+ f" Renaming the file {join(tmp_dir, data_file)} to {new_name}"
+ )
+ rename(join(tmp_dir, data_file), new_name)
+ build[u"file-name"] = new_name
+ return True
+ except (BadZipfile, RuntimeError) as err:
+ logging.error(f"Failed to unzip the file {file_name}: {repr(err)}.")
+ return False
+ except OSError as err:
+ logging.error(f"Failed to rename the file {data_file}: {repr(err)}.")
+ return False
+
+
+def _download_xml(source, job, build, w_dir, arch):
+ """
+
+ :param source:
+ :param job:
+ :param build:
+ :param w_dir: Path to working directory
+ :param arch:
+ :return:
+ """
+
+ file_name = source.get(u"file-name", u"")
+ new_name = join(
+ w_dir,
+ f"{job}{SEPARATOR}{build[u'build']}{SEPARATOR}{file_name}"
+ )
+ url = u"{0}/{1}".format(
+ source.get(u"url", u""),
+ source.get(u"path", u"").format(
+ job=job, build=build[u'build'], filename=file_name
+ )
+ )
+ logging.info(f" Trying to download {url}")
+ success, downloaded_name = _download_file(
+ url, new_name, arch=arch, verify=(u"nginx" not in url), repeat=3
+ )
+ return success, downloaded_name
+
+
+def _download_xml_docs(source, job, build, w_dir, arch):
+ """
+
+ :param source:
+ :param job:
+ :param build:
+ :param w_dir: Path to working directory
+ :param arch:
+ :return:
+ """
+
+ file_name = source.get(u"file-name", u"")
+ release = re.search(REGEX_RELEASE, job).group(2)
+ for rls in (release, u"master"):
+ try:
+ rls = f"rls{int(rls)}"
+ except ValueError:
+ pass # It is master
+ url = (
+ f"{source.get(u'url', u'')}/"
+ f"{rls}/"
+ f"{source.get(u'path', u'')}/"
+ f"{job}{SEPARATOR}{build[u'build']}{SEPARATOR}{file_name}"
+ )
+ new_name = join(
+ w_dir,
+ f"{job}{SEPARATOR}{build[u'build']}{SEPARATOR}{file_name}"
+ )
+
+ logging.info(f" Trying to download {url}")
+
+ success, downloaded_name = _download_file(url, new_name, arch=arch)
+ if success:
+ if file_name.endswith(u".gz"):
+ with gzip.open(downloaded_name[:-3], u"rb") as gzip_file:
+ file_content = gzip_file.read()
+ with open(downloaded_name[:-3], u"wb") as xml_file:
+ xml_file.write(file_content)
+ break
+
+ return success, downloaded_name
+
+
+def download_and_unzip_data_file(spec, job, build, pid):
+ """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.
+ :type spec: Specification
+ :type job: str
+ :type build: dict
+ :type pid: int
+ :returns: True if the download was successful, otherwise False.
+ :rtype: bool
+ """
+
+ download = {
+ "xml": _download_xml,
+ "xml-docs": _download_xml_docs
+ }
+
+ success = False
+ downloaded_name = u""
+ arch = bool(spec.environment.get(u"archive-inputs", True))
+
+ for source in spec.environment.get(u"data-sources", tuple()):
+ if not source.get(u"enabled", False):
+ continue
+ download_type = source.get(u"type", None)
+ if not download_type:
+ continue
+ success, downloaded_name = download[download_type](
+ source,
+ job,
+ build,
+ spec.environment[u"paths"][u"DIR[WORKING,DATA]"],
+ arch
+ )
+ if success:
+ source[u"successful-downloads"] += 1
+ build[u"source"] = source[u"type"]
+ break
+
+ # TODO: Remove when only .gz is used.
+ if success and downloaded_name.endswith(u".zip"):
+ if not is_zipfile(downloaded_name):
+ logging.error(f"Zip file {downloaded_name} is corrupted.")
+ success = False