Add some simple searching examples.
authorSalim Fadhley <sal@stodge.org>
Wed, 11 Jan 2012 15:30:36 +0000 (15:30 +0000)
committerSalim Fadhley <sal@stodge.org>
Wed, 11 Jan 2012 15:30:36 +0000 (15:30 +0000)
examples/fetch_artifact.py [deleted file]
examples/search_artifact_by_regexp.py [new file with mode: 0644]
examples/search_artifacts.py [new file with mode: 0644]

diff --git a/examples/fetch_artifact.py b/examples/fetch_artifact.py
deleted file mode 100644 (file)
index 02154d1..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-from pyhudson.hudson import hudson\r
-from pyhudson.exceptions import ArtifactsMissing\r
-from types import NoneType\r
-\r
-\r
-def searchArtifacts( hudsonurl, jobid, artifact_ids=[], same_build=True, build_search_limit=None ):\r
-    """\r
-    Search the entire history of a hudson job for a list of artifact names. If same_build\r
-    is true then ensure that all artifacts come from the same build of the job\r
-    """\r
-    if len( artifact_ids ) == 0:\r
-        return []\r
-    \r
-    assert same_build, "same_build==False not supported yet"\r
-    assert isinstance( build_search_limit, ( NoneType, int ))\r
-    \r
-    h = hudson( hudsonurl )        \r
-    j = h[ jobid ] \r
-    \r
-    build_ids = j.getBuildIds()\r
-    \r
-    firstBuildId = None\r
-    \r
-    for build_id in build_ids:\r
-        build = j.getBuild( build_id )\r
-        \r
-        artifacts = build.getArtifactDict()\r
-        \r
-        if set( artifact_ids ).issubset( set( artifacts.keys() ) ):\r
-            return dict( ( a,artifacts[a] ) for a in artifact_ids )\r
-    \r
-        missing_artifacts =  set( artifact_ids ) - set( artifacts.keys() )\r
-        \r
-        if build_search_limit == None:\r
-            raise ("Artifacts %s missing from %s #%i" % ( ", ".join( missing_artifacts ), jobid, build_id ) )\r
-        else:\r
-            if firstBuildId:\r
-                if firstBuildId - build_id > build_search_limit:\r
-                    raise ( "Artifacts %s missing from %s #%i after search of depth %i " \r
-                            % ( ", ".join( missing_artifacts ), jobid, build_id, build_search_limit ) )\r
-            else:\r
-                firstBuildId = build_id\r
-                \r
-    raise ArtifactsMissing( missing_artifacts )\r
-\r
-\r
-def searchForArtifactByRegExp( hudsonurl, jobid, artifactRegExp, same_build=True, build_search_limit=None ): \r
-    """\r
-    Search the entire history of a hudson job for a list of artifact names. If same_build\r
-    is true then ensure that all artifacts come from the same build of the job\r
-    """\r
-    \r
-    assert same_build, "same_build==False not supported yet"\r
-    \r
-    h = hudson( hudsonurl )\r
-    j = h[ jobid ] \r
-    \r
-    build_ids = j.getBuildIds()\r
-    \r
-    for build_id in build_ids:\r
-        build = j.getBuild( build_id )\r
-        \r
-        artifacts = build.getArtifactDict()\r
-        \r
-        for name, art in artifacts.iteritems():\r
-            md_match = artifactRegExp.search( name )\r
-            \r
-            if md_match:\r
-                return art\r
-        \r
-    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 (file)
index 0000000..60195b5
--- /dev/null
@@ -0,0 +1,11 @@
+from jenkinsapi.api import search_artifact_by_regexp\r
+import logging\r
+import re\r
+\r
+jenkinsurl = "http://localhost:8080/jenkins"\r
+jobid = "test1"\r
+artifactRegExp = re.compile( "test1\.txt" ) # A file name I want.\r
+\r
+result = search_artifact_by_regexp( jenkinsurl, jobid, artifactRegExp )\r
+\r
+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 (file)
index 0000000..a26de5d
--- /dev/null
@@ -0,0 +1,9 @@
+from jenkinsapi.api import search_artifacts\r
+\r
+jenkinsurl = "http://localhost:8080/jenkins"\r
+jobid = "test1"\r
+artifact_ids = [ "test1.txt", "test2.txt" ] # I need a build that contains all of these\r
+\r
+result = search_artifacts( jenkinsurl, jobid, artifact_ids )\r
+\r
+print repr( result )
\ No newline at end of file