From d0a1355b0b48071bdc28734a1f0f7ad10d43ebdd Mon Sep 17 00:00:00 2001 From: Ramon van Alteren Date: Mon, 2 Jan 2012 17:30:56 +0100 Subject: [PATCH] Changed imports and classnames All imports are now non-relative using the packagename All classes are now named differently from their modules by capitalizing them --- pyjenkinsci/api.py | 84 ++++++++++++------------ pyjenkinsci/artifact.py | 10 +-- pyjenkinsci/build.py | 23 +++---- pyjenkinsci/fingerprint.py | 18 ++--- pyjenkinsci/jenkins.py | 22 +++---- pyjenkinsci/{jenkinsobject.py => jenkinsbase.py} | 12 ++-- pyjenkinsci/job.py | 12 ++-- pyjenkinsci/result.py | 2 +- pyjenkinsci/result_set.py | 14 ++-- pyjenkinsci/view.py | 12 ++-- 10 files changed, 103 insertions(+), 106 deletions(-) rename pyjenkinsci/{jenkinsobject.py => jenkinsbase.py} (80%) diff --git a/pyjenkinsci/api.py b/pyjenkinsci/api.py index a40006a..bd3dd61 100644 --- a/pyjenkinsci/api.py +++ b/pyjenkinsci/api.py @@ -1,7 +1,7 @@ -import artifact -import constants -import jenkins -from exceptions import ArtifactsMissing, TimeOut, BadURL +from pyjenkinsci.artifact import Artifact +from pyjenkinsci import constants +from pyjenkinsci.jenkins import Jenkins +from pyjenkinsci.exceptions import ArtifactsMissing, TimeOut, BadURL from urllib2 import urlparse import os @@ -18,34 +18,34 @@ def get_latest_test_results( jenkinsurl, jobname ): res = latestbuild.get_resultset() return res -def get_latest_build( jenkinsurl, jobname ): +def get_latest_build(jenkinsurl, jobname): """ A convenience function to fetch down the very latest test results from a jenkins job. """ - jenkinsci = jenkins( jenkinsurl ) - job = jenkinsci[ jobname ] + jenkinsci = Jenkins(jenkinsurl) + job = jenkinsci[jobname] return job.get_last_build() -def get_latest_complete_build( jenkinsurl, jobname ): +def get_latest_complete_build(jenkinsurl, jobname): """ A convenience function to fetch down the very latest test results from a jenkins job. """ - jenkinsci = jenkins( jenkinsurl ) - job = jenkinsci[ jobname ] + jenkinsci = Jenkins(jenkinsurl) + job = jenkinsci[jobname] return job.get_last_completed_build() def get_artifacts( jenkinsurl, jobid=None, build_no=None, proxyhost=None, proxyport=None, proxyuser=None, proxypass=None ): """ Find all the artifacts for the latest build of a job. """ - jenkinsci = jenkins( jenkinsurl, proxyhost, proxyport, proxyuser, proxypass ) - job = jenkinsci[ jobid ] + jenkinsci = Jenkins(jenkinsurl, proxyhost, proxyport, proxyuser, proxypass) + job = jenkinsci[jobid] if build_no: build = job.get_build( build_no ) else: build = job.get_last_good_build() - artifacts = dict( (artifact.filename, artifact) for artifact in build.get_artifacts() ) - log.info("Found %i artifacts in '%s'" % ( len(artifacts.keys() ), build_no ) ) + artifacts = dict((artifact.filename, artifact) for artifact in build.get_artifacts()) + log.info("Found %i artifacts in '%s'" % ( len(artifacts.keys() ), build_no )) return artifacts def search_artifacts(jenkinsurl, jobid, artifact_ids=None, same_build=True, build_search_limit=None): @@ -53,10 +53,10 @@ def search_artifacts(jenkinsurl, jobid, artifact_ids=None, same_build=True, buil Search the entire history of a jenkins 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 or artifact_ids is None: + if len(artifact_ids) == 0 or artifact_ids is None: return [] assert same_build, "same_build==False not supported yet" - jenkinsci = jenkins( jenkinsurl ) + jenkinsci = Jenkins( jenkinsurl ) job = jenkinsci[ jobid ] build_ids = job.get_build_ids() for build_id in build_ids: @@ -68,7 +68,7 @@ def search_artifacts(jenkinsurl, jobid, artifact_ids=None, same_build=True, buil log.debug("Artifacts %s missing from %s #%i" % ( ", ".join( missing_artifacts ), jobid, build_id )) raise ArtifactsMissing( missing_artifacts ) -def grab_artifact( jenkinsurl, jobid, artifactid, targetdir ): +def grab_artifact(jenkinsurl, jobid, artifactid, targetdir): """ Convenience method to find the latest good version of an artifact and save it to a target directory. Directory is made automatically if not exists. @@ -79,7 +79,7 @@ def grab_artifact( jenkinsurl, jobid, artifactid, targetdir ): os.makedirs( targetdir ) artifact.savetodir( targetdir) -def block_until_complete( jenkinsurl, jobs, maxwait=12000, interval=30, raise_on_timeout=True ): +def block_until_complete(jenkinsurl, jobs, maxwait=12000, interval=30, raise_on_timeout=True): """ Wait until all of the jobs in the list are complete. """ @@ -87,51 +87,51 @@ def block_until_complete( jenkinsurl, jobs, maxwait=12000, interval=30, raise_on assert maxwait > interval assert interval > 0 - obj_jenkins = jenkins( jenkinsurl ) - obj_jobs = [ obj_jenkins[ jid ] for jid in jobs ] - for time_left in xrange( maxwait, 0, -interval ): - still_running = [ j for j in obj_jobs if j.is_queued_or_running() ] + obj_jenkins = Jenkins(jenkinsurl) + obj_jobs = [obj_jenkins[jid] for jid in jobs] + for time_left in xrange(maxwait, 0, -interval): + still_running = [j for j in obj_jobs if j.is_queued_or_running()] if not still_running: return - str_still_running = ", ".join( '"%s"' % str(a) for a in still_running ) - log.warn( "Waiting for jobs %s to complete. Will wait another %is" % ( str_still_running, time_left ) ) - time.sleep( interval ) + str_still_running = ", ".join('"%s"' % str(a) for a in still_running) + log.warn( "Waiting for jobs %s to complete. Will wait another %is" % (str_still_running, time_left )) + time.sleep(interval) if raise_on_timeout: - raise TimeOut( "Waited too long for these jobs to complete: %s" % str_still_running ) + raise TimeOut("Waited too long for these jobs to complete: %s" % str_still_running) -def get_view_from_url( url ): +def get_view_from_url(url): """ Factory method """ matched = constants.RE_SPLIT_VIEW_URL.search(url) if not matched: - raise BadURL("Cannot parse URL %s" % url ) + raise BadURL("Cannot parse URL %s" % url) jenkinsurl, view_name = matched.groups() - jenkinsci = jenkins( jenkinsurl ) - return jenkinsci.get_view( view_name ) + jenkinsci = Jenkins(jenkinsurl) + return jenkinsci.get_view(view_name) -def install_artifacts( artifacts, dirstruct, installdir, basestaticurl ): +def install_artifacts(artifacts, dirstruct, installdir, basestaticurl): """ Install the artifacts. """ assert basestaticurl.endswith("/"), "Basestaticurl should end with /" installed = [] for reldir, artifactnames in dirstruct.items(): - destdir = os.path.join( installdir, reldir ) - if not os.path.exists( destdir ): - log.warn( "Making install directory %s" % destdir ) - os.makedirs( destdir ) + destdir = os.path.join(installdir, reldir) + if not os.path.exists(destdir): + log.warn("Making install directory %s" % destdir) + os.makedirs(destdir) else: - assert os.path.isdir( destdir ) + assert os.path.isdir(destdir) for artifactname in artifactnames: - destpath = os.path.abspath( os.path.join( destdir, artifactname ) ) + destpath = os.path.abspath(os.path.join( destdir, artifactname)) if artifactname in artifacts.keys(): # The artifact must be loaded from jenkins - theartifact = artifacts[ artifactname ] + theartifact = artifacts[artifactname] else: # It's probably a static file, we can get it from the static collection - staticurl = urlparse.urljoin( basestaticurl, artifactname ) - theartifact = artifact( artifactname, staticurl ) - theartifact.save( destpath ) - installed.append( destpath ) + staticurl = urlparse.urljoin(basestaticurl, artifactname) + theartifact = Artifact(artifactname, staticurl) + theartifact.save(destpath) + installed.append(destpath) return installed diff --git a/pyjenkinsci/artifact.py b/pyjenkinsci/artifact.py index a3b0193..c2143d8 100644 --- a/pyjenkinsci/artifact.py +++ b/pyjenkinsci/artifact.py @@ -5,15 +5,15 @@ import cStringIO import zipfile import cPickle import datetime -import config +from pyjenkinsci import config -from utils.retry import retry_function -from exceptions import ArtifactBroken -from utils.md5hash import new_digest +from pyjenkinsci.utils.retry import retry_function +from pyjenkinsci.exceptions import ArtifactBroken +from pyjenkinsci.utils.md5hash import new_digest log = logging.getLogger( __name__ ) -class artifact( object ): +class Artifact(object): @staticmethod def timedelta_to_seconds( td ): diff --git a/pyjenkinsci/build.py b/pyjenkinsci/build.py index 00fe594..31236f8 100644 --- a/pyjenkinsci/build.py +++ b/pyjenkinsci/build.py @@ -1,15 +1,16 @@ -import artifact -import config -import jenkinsobject -import time +from pyjenkinsci.artifact import Artifact +from pyjenkinsci import config +from pyjenkinsci.jenkinsbase import JenkinsBase +from pyjenkinsci.exceptions import NoResults, FailedNoResults +from pyjenkinsci.constants import STATUS_FAIL, STATUS_ABORTED, RESULTSTATUS_FAILURE +from pyjenkinsci.result_set import ResultSet + +from datetime import time import logging -from exceptions import NoResults, FailedNoResults -from constants import STATUS_FAIL, STATUS_ABORTED, RESULTSTATUS_FAILURE -import result_set log = logging.getLogger(__name__) -class build(jenkinsobject): +class Build(JenkinsBase): """ Represents a jenkins build, executed in context of a job. """ @@ -21,7 +22,7 @@ class build(jenkinsobject): assert type(buildno) == int self.buildno = buildno self.job = job - jenkinsobject.__init__( self, url ) + JenkinsBase.__init__( self, url ) def __str__(self): return self._data['fullDisplayName'] @@ -38,7 +39,7 @@ class build(jenkinsobject): def get_artifacts( self ): for afinfo in self._data["artifacts"]: url = "%sartifact/%s" % ( self.baseurl, afinfo["relativePath"] ) - af = artifact( afinfo["fileName"], url, self ) + af = Artifact( afinfo["fileName"], url, self ) yield af del af, url @@ -90,7 +91,7 @@ class build(jenkinsobject): raise FailedNoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) ) if self.get_actions()[ self.STR_TOTALCOUNT ] == 0: raise NoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) ) - obj_results = result_set( result_url, build=self ) + obj_results = ResultSet( result_url, build=self ) return obj_results def has_resultset(self): diff --git a/pyjenkinsci/fingerprint.py b/pyjenkinsci/fingerprint.py index 836dc29..f9c00d6 100644 --- a/pyjenkinsci/fingerprint.py +++ b/pyjenkinsci/fingerprint.py @@ -1,5 +1,5 @@ -import jenkinsobject -from exceptions import ArtifactBroken +from pyjenkinsci.jenkinsbase import JenkinsBase +from pyjenkinsci.exceptions import ArtifactBroken import urllib2 import re @@ -8,18 +8,18 @@ import logging log = logging.getLogger( __name__ ) -class fingerprint(jenkinsobject): +class Fingerprint(JenkinsBase): """ Represents a jenkins fingerprint on a single artifact file ?? """ RE_MD5 = re.compile("^([0-9a-z]{32})$") - def __init__( self, baseurl, id, jenkins_obj ): + def __init__(self, baseurl, id, jenkins_obj): logging.basicConfig() self.jenkins_obj = jenkins_obj assert self.RE_MD5.search( id ), "%s does not look like a valid id" % id url = "%s/fingerprint/%s/" % ( baseurl, id ) - jenkinsobject.__init__( self, url, poll=False ) + JenkinsBase.__init__( self, url, poll=False ) self.id = id def get_jenkins_obj(self): @@ -72,11 +72,3 @@ class fingerprint(jenkinsobject): """ self.poll() return self._data["original"]["name"], self._data["original"]["number"], self._data["fileName"] - - -if __name__ == "__main__": - ff = fingerprint( "http://localhost:8080/hudson/", "0f37cbb6545b8778bc0700d90be66bf3" ) - print repr(ff) - print ff.baseurl - print ff.valid() - print ff.get_info( ) diff --git a/pyjenkinsci/jenkins.py b/pyjenkinsci/jenkins.py index 4b83074..d5c23dd 100644 --- a/pyjenkinsci/jenkins.py +++ b/pyjenkinsci/jenkins.py @@ -1,15 +1,15 @@ -import fingerprint -import jenkinsobject -import job +from pyjenkinsci.jenkinsbase import JenkinsBase +from pyjenkinsci.fingerprint import Fingerprint +from pyjenkinsci.job import Job +from pyjenkinsci.view import View from exceptions import UnknownJob from utils.urlopener import mkurlopener import logging import time -import view log = logging.getLogger(__name__) -class jenkins(jenkinsobject): +class Jenkins(JenkinsBase): """ Represents a jenkins environment. """ @@ -18,7 +18,7 @@ class jenkins(jenkinsobject): self.proxyport = proxyport self.proxyuser = proxyuser self.proxypass = proxypass - jenkinsobject.__init__( self, baseurl ) + JenkinsBase.__init__( self, baseurl ) def get_proxy_auth(self): return (self.proxyhost, self.proxyport, self.proxyuser, self.proxypass) @@ -27,17 +27,17 @@ class jenkins(jenkinsobject): return mkurlopener(*self.get_proxy_auth()) def validate_fingerprint( self, id ): - obj_fingerprint = fingerprint(self.baseurl, id, jenkins_obj=self) + obj_fingerprint = Fingerprint(self.baseurl, id, jenkins_obj=self) obj_fingerprint.validate() log.info("Jenkins says %s is valid" % id) def get_artifact_data(self, id): - obj_fingerprint = fingerprint(self.baseurl, id, jenkins_obj=self) + obj_fingerprint = Fingerprint(self.baseurl, id, jenkins_obj=self) obj_fingerprint.validate() return obj_fingerprint.get_info() def validate_fingerprint_for_build(self, digest, filename, job, build ): - obj_fingerprint = fingerprint( self.baseurl, digest, jenkins_obj=self ) + obj_fingerprint = Fingerprint( self.baseurl, digest, jenkins_obj=self ) return obj_fingerprint.validate_for_build( filename, job, build ) def get_jenkins_obj(self): @@ -48,7 +48,7 @@ class jenkins(jenkinsobject): Fetch all the build-names on this Hudson server. """ for info in self._data["jobs"]: - yield info["name"], job( info["url"], info["name"], jenkins_obj=self) + yield info["name"], Job( info["url"], info["name"], jenkins_obj=self) def iteritems(self): return self.get_jobs() @@ -84,7 +84,7 @@ class jenkins(jenkinsobject): def get_view(self, str_view_name ): view_url = self.get_view_url( str_view_name ) view_api_url = self.python_api_url( view_url ) - return view(view_api_url , str_view_name, jenkins_obj=self) + return View(view_api_url , str_view_name, jenkins_obj=self) def __getitem__( self, buildname ): """ diff --git a/pyjenkinsci/jenkinsobject.py b/pyjenkinsci/jenkinsbase.py similarity index 80% rename from pyjenkinsci/jenkinsobject.py rename to pyjenkinsci/jenkinsbase.py index e97ef95..0d7be0b 100644 --- a/pyjenkinsci/jenkinsobject.py +++ b/pyjenkinsci/jenkinsbase.py @@ -1,12 +1,12 @@ import urllib2 import logging import pprint -import config -from utils.retry import retry_function +from pyjenkinsci import config +from pyjenkinsci.utils.retry import retry_function log = logging.getLogger( __name__ ) -class jenkinsobject( object ): +class JenkinsBase(object): """ This appears to be the base object that all other jenkins objects are inherited from """ @@ -43,6 +43,10 @@ class jenkinsobject( object ): url = self.python_api_url( self.baseurl ) return retry_function( self.RETRY_ATTEMPTS , self.get_data, url ) + def get_jenkins_obj(self): + """Not implemented, abstract method implemented by child classes""" + raise NotImplemented("Abstract method, implemented by child classes") + @classmethod def python_api_url( cls, url ): if url.endswith( config.JENKINS_API ): @@ -58,7 +62,7 @@ class jenkinsobject( object ): """ Find out how to connect, and then grab the data. """ - fn_urlopen = self.getHudsonObject().get_opener() + fn_urlopen = self.get_jenkins_obj().get_opener() try: stream = fn_urlopen( url ) result = eval( stream.read() ) diff --git a/pyjenkinsci/job.py b/pyjenkinsci/job.py index 9d6889b..05b39ae 100644 --- a/pyjenkinsci/job.py +++ b/pyjenkinsci/job.py @@ -1,15 +1,15 @@ import logging import urlparse import urllib2 -import time -import build -import jenkinsobject +from datetime import time +from pyjenkinsci.build import Build +from pyjenkinsci.jenkinsbase import JenkinsBase from exceptions import NoBuildData log = logging.getLogger(__name__) -class job(jenkinsobject): +class Job(JenkinsBase): """ Represents a jenkins job A job can hold N builds which are the actual execution environments @@ -17,7 +17,7 @@ class job(jenkinsobject): def __init__( self, url, name, jenkins_obj ): self.name = name self.jenkins = jenkins_obj - jenkinsobject.__init__( self, url ) + JenkinsBase.__init__( self, url ) def id( self ): return self._data["name"] @@ -156,7 +156,7 @@ class job(jenkinsobject): def get_build( self, buildnumber ): assert type(buildnumber) == int url = self.get_build_dict()[ buildnumber ] - return build( url, buildnumber, job=self ) + return Build( url, buildnumber, job=self ) def __getitem__( self, buildnumber ): return self.get_build(buildnumber) diff --git a/pyjenkinsci/result.py b/pyjenkinsci/result.py index 64bd90a..d02a01c 100644 --- a/pyjenkinsci/result.py +++ b/pyjenkinsci/result.py @@ -1,4 +1,4 @@ -class result( object ): +class Result(object): def __init__(self, **kwargs ): """ diff --git a/pyjenkinsci/result_set.py b/pyjenkinsci/result_set.py index 9e343ad..776ab5b 100644 --- a/pyjenkinsci/result_set.py +++ b/pyjenkinsci/result_set.py @@ -1,18 +1,18 @@ -import jenkinsobject -import result +from pyjenkinsci.jenkinsbase import JenkinsBase +from pyjenkinsci.result import Result -class result_set(jenkinsobject): +class ResultSet(JenkinsBase): """ Represents a result from a completed Hudson run. """ - def getHudsonObject(self): + def get_jenkins_obj(self): return self.build.job.get_jenkins_obj() def __init__(self, url, build ): """ """ self.build = build - jenkinsobject.__init__( self, url ) + JenkinsBase.__init__( self, url ) def __str__(self): return "Test Result for %s" % str( self.build ) @@ -26,13 +26,13 @@ class result_set(jenkinsobject): def iteritems(self): for suite in self._data.get("suites", [] ): for case in suite["cases"]: - R = result( **case ) + R = Result( **case ) yield R.id(), R for report_set in self._data.get( "childReports", [] ): for suite in report_set["result"]["suites"]: for case in suite["cases"]: - R = result( **case ) + R = Result( **case ) yield R.id(), R def __len__(self): diff --git a/pyjenkinsci/view.py b/pyjenkinsci/view.py index 145e71c..d245d62 100644 --- a/pyjenkinsci/view.py +++ b/pyjenkinsci/view.py @@ -1,12 +1,12 @@ -import jenkinsobject -import job +from pyjenkinsci.jenkinsbase import JenkinsBase +from pyjenkinsci.job import Job -class view(jenkinsobject): +class View(JenkinsBase): def __init__(self, url, name, jenkins_obj): self.name = name self.jenkins_obj = jenkins_obj - jenkinsobject.__init__(self, url) + JenkinsBase.__init__(self, url) def __str__(self): return self.name @@ -14,7 +14,7 @@ class view(jenkinsobject): def __getitem__(self, str_job_id ): assert isinstance( str_job_id, str ) api_url = self.python_api_url( self.get_job_url( str_job_id ) ) - return job( api_url, str_job_id, self.jenkins_obj ) + return Job( api_url, str_job_id, self.jenkins_obj ) def keys(self): return self.get_job_dict().keys() @@ -22,7 +22,7 @@ class view(jenkinsobject): def iteritems(self): for name, url in self.get_job_dict().iteritems(): api_url = self.python_api_url( url ) - yield name, job( api_url, name, self.jenkins_obj ) + yield name, Job( api_url, name, self.jenkins_obj ) def values(self): return [ a[1] for a in self.iteritems() ] -- 2.7.4