+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