- 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))
+ 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_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
+ """
+
+ success = False
+
+ file_name = spec.input[u"file-name"]
+ new_name = join(
+ spec.environment[u"paths"][u"DIR[WORKING,DATA]"],
+ f"{job}{SEPARATOR}{build[u'build']}{SEPARATOR}{file_name}"
+ )
+ arch = bool(spec.configuration.get(u"archive-inputs", True))
+ downloaded_name = u""
+
+ # Try to download .gz from s3_storage
+ for path in spec.input[u'download-path']:
+ url = u"{0}/{1}".format(
+ spec.environment[u'urls'][u'URL[S3_STORAGE,LOG]'],
+ path.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=False, repeat=3
+ )
+ if success:
+ break