Last active
August 29, 2015 14:05
-
-
Save imsardine/e9cea93bbe6d86d58642 to your computer and use it in GitHub Desktop.
To parameterize the level of fingerprint validation. So negative validity (fingerprint not known at server) may be treated as an error (ArtifactBroken).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, logging | |
from jenkinsapi.fingerprint import Fingerprint | |
from jenkinsapi.custom_exceptions import ArtifactBroken | |
__all__ = ['save_artifact', 'save_artifact_to_dir'] | |
_log = logging.getLogger(__name__) | |
def save_artifact(artifact, fspath, strict_validation=False): | |
if os.path.exists(fspath): | |
try: | |
_validate_local_copy(artifact, fspath, strict_validation) | |
except ArtifactBroken: | |
_log.warning('Local copy [%s] is invalid, remove it.', fspath) | |
os.remove(fspath) | |
return _validate_local_copy(artifact, artifact.save(fspath), strict_validation) | |
def save_artifact_to_dir(artifact, dirpath, strict_validation=False): | |
fspath = os.path.join(dirpath, artifact.filename) | |
return save_artifact(artifact, fspath, strict_validation) | |
def _validate_local_copy(artifact, fspath, strict): | |
local_md5 = artifact._md5sum(fspath) | |
baseurl = artifact.build.job.jenkins.baseurl | |
job_name = artifact.build.job.name | |
buildno = artifact.build.buildno | |
fp = Fingerprint(baseurl, local_md5, artifact.build.job.jenkins) | |
valid = fp.validate_for_build(os.path.basename(fspath), job_name, buildno) | |
if not valid or (fp.unknown and strict): # strict = 404 as invalid | |
raise ArtifactBroken("Artifact %s seems to be broken, check %s" % (local_md5, baseurl)) | |
return fspath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment