From: Salim Fadhley Date: Wed, 11 Jan 2012 15:30:36 +0000 (+0000) Subject: Add some simple searching examples. X-Git-Tag: v0.2.23~315 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c09b05ac47627d987b358152cf972369f0062c27;p=tools%2Fpython-jenkinsapi.git Add some simple searching examples. --- diff --git a/examples/fetch_artifact.py b/examples/fetch_artifact.py deleted file mode 100644 index 02154d1..0000000 --- a/examples/fetch_artifact.py +++ /dev/null @@ -1,71 +0,0 @@ -from pyhudson.hudson import hudson -from pyhudson.exceptions import ArtifactsMissing -from types import NoneType - - -def searchArtifacts( hudsonurl, jobid, artifact_ids=[], same_build=True, build_search_limit=None ): - """ - Search the entire history of a hudson job for a list of artifact names. If same_build - is true then ensure that all artifacts come from the same build of the job - """ - if len( artifact_ids ) == 0: - return [] - - assert same_build, "same_build==False not supported yet" - assert isinstance( build_search_limit, ( NoneType, int )) - - h = hudson( hudsonurl ) - j = h[ jobid ] - - build_ids = j.getBuildIds() - - firstBuildId = None - - for build_id in build_ids: - build = j.getBuild( build_id ) - - artifacts = build.getArtifactDict() - - if set( artifact_ids ).issubset( set( artifacts.keys() ) ): - return dict( ( a,artifacts[a] ) for a in artifact_ids ) - - missing_artifacts = set( artifact_ids ) - set( artifacts.keys() ) - - if build_search_limit == None: - raise ("Artifacts %s missing from %s #%i" % ( ", ".join( missing_artifacts ), jobid, build_id ) ) - else: - if firstBuildId: - if firstBuildId - build_id > build_search_limit: - raise ( "Artifacts %s missing from %s #%i after search of depth %i " - % ( ", ".join( missing_artifacts ), jobid, build_id, build_search_limit ) ) - else: - firstBuildId = build_id - - raise ArtifactsMissing( missing_artifacts ) - - -def searchForArtifactByRegExp( hudsonurl, jobid, artifactRegExp, same_build=True, build_search_limit=None ): - """ - Search the entire history of a hudson job for a list of artifact names. If same_build - is true then ensure that all artifacts come from the same build of the job - """ - - assert same_build, "same_build==False not supported yet" - - h = hudson( hudsonurl ) - j = h[ jobid ] - - build_ids = j.getBuildIds() - - for build_id in build_ids: - build = j.getBuild( build_id ) - - artifacts = build.getArtifactDict() - - for name, art in artifacts.iteritems(): - md_match = artifactRegExp.search( name ) - - if md_match: - return art - - return None \ No newline at end of file diff --git a/examples/search_artifact_by_regexp.py b/examples/search_artifact_by_regexp.py new file mode 100644 index 0000000..60195b5 --- /dev/null +++ b/examples/search_artifact_by_regexp.py @@ -0,0 +1,11 @@ +from jenkinsapi.api import search_artifact_by_regexp +import logging +import re + +jenkinsurl = "http://localhost:8080/jenkins" +jobid = "test1" +artifactRegExp = re.compile( "test1\.txt" ) # A file name I want. + +result = search_artifact_by_regexp( jenkinsurl, jobid, artifactRegExp ) + +print repr( result ) \ No newline at end of file diff --git a/examples/search_artifacts.py b/examples/search_artifacts.py new file mode 100644 index 0000000..a26de5d --- /dev/null +++ b/examples/search_artifacts.py @@ -0,0 +1,9 @@ +from jenkinsapi.api import search_artifacts + +jenkinsurl = "http://localhost:8080/jenkins" +jobid = "test1" +artifact_ids = [ "test1.txt", "test2.txt" ] # I need a build that contains all of these + +result = search_artifacts( jenkinsurl, jobid, artifact_ids ) + +print repr( result ) \ No newline at end of file