- for job, builds in spec.builds.items():
- for build in builds:
- if job.startswith("csit-"):
- if spec.input["file-name"].endswith(".zip"):
- url = spec.environment["urls"]["URL[JENKINS,CSIT]"]
- elif spec.input["file-name"].endswith(".gz"):
- url = spec.environment["urls"]["URL[NEXUS,LOG]"]
- else:
- logging.error("Not supported file format.")
- continue
- elif job.startswith("hc2vpp-"):
- url = spec.environment["urls"]["URL[JENKINS,HC]"]
- else:
- raise PresentationError("No url defined for the job '{}'.".
- format(job))
- file_name = spec.input["file-name"]
- full_name = spec.input["download-path"].\
- format(job=job, build=build["build"], filename=file_name)
- url = "{0}/{1}".format(url, full_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 the file '{0}' to '{1}' ...".format(url, new_name))
-
- status = "failed"
- try:
- response = get(url, stream=True)
- code = response.status_code
-
- # temporary workaround, remove when output.log.xml is not needed
- if code != codes["OK"] and \
- spec.input["file-name"].endswith(".gz"):
- url = '.'.join(url.split('.')[:-1]) + ".log.gz"
- response = get(url, stream=True)
- code = response.status_code
-
- if code != codes["OK"]:
- logging.warning(
- "Jenkins: {0}: {1}.".format(code, responses[code]))
- logging.info("Trying to download from Nexus:")
- spec.set_input_state(job, build["build"], "not found")
- if code == codes["not_found"]:
- release = re.search(REGEX_RELEASE, job).group(2)
- nexus_file_name = "{job}{sep}{build}{sep}{name}".\
- format(job=job, sep=SEPARATOR, build=build["build"],
- name=file_name)
- try:
- release = "rls".format(int(release))
- except ValueError:
- pass
- url = "{url}/{release}/{dir}/{file}".\
- format(url=spec.environment["urls"]["URL[NEXUS]"],
- release=release,
- dir=spec.environment["urls"]["DIR[NEXUS]"],
- file=nexus_file_name)
- logging.info("Downloading the file '{0}' to '{1}' ...".
- format(url, new_name))
- response = get(url, stream=True)
- code = response.status_code
- if code != codes["OK"]:
- logging.error(
- "Nexus: {0}: {1}".format(code, responses[code]))
- spec.set_input_state(
- job, build["build"], "not found")
- continue
-
- file_handle = open(new_name, "wb")
- for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
- if chunk:
- file_handle.write(chunk)
- file_handle.close()
-
- if spec.input["file-name"].endswith(".zip"):
- expected_length = None
- try:
- expected_length = int(response.
- headers["Content-Length"])
- logging.debug(" Expected file size: {0}B".
- format(expected_length))
- except KeyError:
- logging.debug(" No information about expected size.")
-
- real_length = getsize(new_name)
- logging.debug(" Downloaded size: {0}B".format(real_length))
-
- if expected_length:
- if real_length == expected_length:
- status = "downloaded"
- logging.info("{0}: {1}".format(code,
- responses[code]))
- else:
- logging.error("The file size differs from the "
- "expected size.")
- else:
- status = "downloaded"
- logging.info("{0}: {1}".format(code, responses[code]))
-
- elif spec.input["file-name"].endswith(".gz"):
- rename(new_name, new_name[:-3])
- with open(new_name[:-3], 'r') as xml_file:
- with gzip.open(new_name, 'w') as gz_file:
- gz_file.write(xml_file.read())
- new_name = new_name[:-3]
- status = "downloaded"
- logging.info("{0}: {1}".format(code, responses[code]))
-
- except ConnectionError as err:
- logging.error("Not possible to connect to '{0}'.".format(url))
- logging.debug(err)
- except HTTPError as err:
- logging.error("Invalid HTTP response from '{0}'.".format(url))
- logging.debug(err)
- except TooManyRedirects as err:
- logging.error("Request exceeded the configured number "
- "of maximum re-directions.")
- logging.debug(err)
- except Timeout as err:
- logging.error("Request timed out.")
- logging.debug(err)
- except RequestException as err:
- logging.error("Unexpected HTTP request exception.")
- logging.debug(err)
- except (IOError, ValueError, KeyError) as err:
- logging.error("Download failed.")
- logging.debug("Reason: {0}".format(err))
-
- spec.set_input_state(job, build["build"], status)
- spec.set_input_file_name(job, build["build"], new_name)
-
- if status == "failed":
- logging.info("Removing the file '{0}'".format(new_name))
- try:
- remove(new_name)
- except OSError as err:
- logging.warning(str(err))
- spec.set_input_file_name(job, build["build"], None)
-
- unzip_files(spec)
-
-
-def unzip_files(spec):
- """Unzip downloaded zip files
+ success = False
+ try:
+ log.append(("INFO", " Connecting to '{0}' ...".format(url)))
+ response = get(url, stream=True)
+ code = response.status_code
+ log.append(("INFO", " {0}: {1}".format(code, responses[code])))
+
+ if code != codes["OK"]:
+ url = url.replace("_info", "")
+ log.append(("INFO", " Connecting to '{0}' ...".format(url)))
+ response = get(url, stream=True)
+ code = response.status_code
+ log.append(("INFO", " {0}: {1}".format(code, responses[code])))
+ if code != codes["OK"]:
+ return False, file_name
+ file_name = file_name.replace("_info", "")
+
+ log.append(("INFO", " Downloading the file '{0}' to '{1}' ...".
+ format(url, file_name)))
+
+ with open(file_name, "wb") as file_handle:
+ for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
+ if chunk:
+ file_handle.write(chunk)
+ success = True
+ except ConnectionError as err:
+ log.append(("ERROR", "Not possible to connect to '{0}'.".format(url)))
+ log.append(("DEBUG", repr(err)))
+ except HTTPError as err:
+ log.append(("ERROR", "Invalid HTTP response from '{0}'.".format(url)))
+ log.append(("DEBUG", repr(err)))
+ except TooManyRedirects as err:
+ log.append(("ERROR", "Request exceeded the configured number "
+ "of maximum re-directions."))
+ log.append(("DEBUG", repr(err)))
+ except Timeout as err:
+ log.append(("ERROR", "Request timed out."))
+ log.append(("DEBUG", repr(err)))
+ except RequestException as err:
+ log.append(("ERROR", "Unexpected HTTP request exception."))
+ log.append(("DEBUG", repr(err)))
+ except (IOError, ValueError, KeyError) as err:
+ log.append(("ERROR", "Download failed."))
+ log.append(("DEBUG", repr(err)))
+
+ log.append(("INFO", " Download finished."))
+ return success, file_name
+
+
+def _unzip_file(spec, build, pid, log):
+ """Unzip downloaded source file.
+
+ :param spec: Specification read form the specification file.
+ :param build: Information about the build.
+ :param log: List of log messages.
+ :type spec: Specification
+ :type build: dict
+ :type log: list of tuples (severity, msg)
+ :returns: True if the download was successful, otherwise False.
+ :rtype: bool
+ """