From 1e8dd893edf4e616e98175f6e1355fbf96de1887 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Wed, 30 May 2012 09:15:05 -0700 Subject: [PATCH] Remove stray print statements. Add tox.ini with pep8 section. Fix pep8 errors. Change-Id: I6c171104359b16bcb130fdd56697d0663d8ec562 --- setup.py | 2 - tox.ini | 6 +++ zuul-server | 6 ++- zuul/launcher/jenkins.py | 60 ++++++++++++++++------------ zuul/lib/gerrit.py | 14 ++++--- zuul/model.py | 33 ++++++++++------ zuul/scheduler.py | 84 ++++++++++++++++++++-------------------- zuul/trigger/gerrit.py | 6 +-- 8 files changed, 120 insertions(+), 91 deletions(-) create mode 100644 tox.ini diff --git a/setup.py b/setup.py index ec8654d1d1..ac680d920d 100644 --- a/setup.py +++ b/setup.py @@ -14,9 +14,7 @@ # under the License. from setuptools import find_packages -from setuptools.command.sdist import sdist from setuptools import setup -import subprocess setup(name='zuul', version='1.0', diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000000..2ddaee1152 --- /dev/null +++ b/tox.ini @@ -0,0 +1,6 @@ +[tox] +envlist = pep8 + +[testenv:pep8] +deps = pep8 +commands = pep8 --repeat --show-source zuul zuul-server setup.py diff --git a/zuul-server b/zuul-server index f63ae6ad32..bb7a1e6ab8 100755 --- a/zuul-server +++ b/zuul-server @@ -23,14 +23,16 @@ import zuul.trigger.gerrit import logging.config + def parse_arguments(): parser = argparse.ArgumentParser(description='Project gating system.') parser.add_argument('-c', dest='config', help='specify the config file') return parser.parse_args() + def read_config(args): - config=ConfigParser.ConfigParser() + config = ConfigParser.ConfigParser() if args.config: locations = [args.config] else: @@ -42,6 +44,7 @@ def read_config(args): return config raise Exception("Unable to locate config file in %s" % locations) + def setup_logging(config): if config.has_option('zuul', 'log_config'): fp = os.path.expanduser(config.get('zuul', 'log_config')) @@ -51,6 +54,7 @@ def setup_logging(config): else: logging.basicConfig(level=logging.DEBUG) + def main(config): sched = zuul.scheduler.Scheduler(config) diff --git a/zuul/launcher/jenkins.py b/zuul/launcher/jenkins.py index 0cda714a7c..19012a8a77 100644 --- a/zuul/launcher/jenkins.py +++ b/zuul/launcher/jenkins.py @@ -28,6 +28,7 @@ import pprint from zuul.model import Build + class JenkinsCallback(threading.Thread): log = logging.getLogger("zuul.JenkinsCallback") @@ -61,7 +62,8 @@ class JenkinsCallback(threading.Thread): uuid = params.get('UUID') if (status and url and uuid and phase and phase == 'COMPLETED'): - self.jenkins.onBuildCompleted(uuid, status, url, number) + self.jenkins.onBuildCompleted(uuid, status, url, + number) if (phase and phase == 'STARTED'): self.jenkins.onBuildStarted(uuid, url, number) @@ -70,10 +72,11 @@ STOP_BUILD = 'job/%(name)s/%(number)s/stop' CANCEL_QUEUE = 'queue/item/%(number)s/cancelQueue' BUILD_INFO = 'job/%(name)s/%(number)s/api/json?depth=0' + class ExtendedJenkins(jenkins.Jenkins): def jenkins_open(self, req): ''' - Utility routine for opening an HTTP request to a Jenkins server. + Utility routine for opening an HTTP request to a Jenkins server. ''' try: if self.auth: @@ -93,7 +96,7 @@ class ExtendedJenkins(jenkins.Jenkins): @param number: Jenkins build number for the job @type number: int ''' - self.jenkins_open(urllib2.Request(self.server + STOP_BUILD%locals())) + self.jenkins_open(urllib2.Request(self.server + STOP_BUILD % locals())) def cancel_queue(self, number): ''' @@ -104,10 +107,10 @@ class ExtendedJenkins(jenkins.Jenkins): ''' # Jenkins returns a 302 from this URL, unless Referer is not set, # then you get a 404. - self.jenkins_open(urllib2.Request(self.server + CANCEL_QUEUE%locals(), + self.jenkins_open(urllib2.Request(self.server + + CANCEL_QUEUE % locals(), headers={'Referer': self.server})) - def get_build_info(self, name, number): ''' Get information for a build. @@ -118,7 +121,9 @@ class ExtendedJenkins(jenkins.Jenkins): @type number: int @return: dictionary ''' - return json.loads(self.jenkins_open(urllib2.Request(self.server + BUILD_INFO%locals()))) + return json.loads(self.jenkins_open(urllib2.Request( + self.server + BUILD_INFO % locals()))) + class Jenkins(object): log = logging.getLogger("zuul.Jenkins") @@ -132,27 +137,29 @@ class Jenkins(object): self.jenkins = ExtendedJenkins(server, user, apikey) self.callback_thread = JenkinsCallback(self) self.callback_thread.start() - - def launch(self, job, change, dependent_changes = []): - self.log.info("Launch job %s for change %s with dependent changes %s" % ( - job, change, dependent_changes)) + + def launch(self, job, change, dependent_changes=[]): + self.log.info("Launch job %s for change %s with dependent changes %s" % + (job, change, dependent_changes)) uuid = str(uuid1()) changes_str = '^'.join( - ['%s:%s:%s' % (c.project.name, c.branch, c.refspec) - for c in dependent_changes+[change]]) + ['%s:%s:%s' % (c.project.name, c.branch, c.refspec) + for c in dependent_changes + [change]]) params = dict(UUID=uuid, GERRIT_PROJECT=change.project.name, GERRIT_BRANCH=change.branch, GERRIT_CHANGES=changes_str) build = Build(job, uuid) self.builds[uuid] = build - # We can get the started notification on another thread before this is done - # so we add the build even before we trigger the job on Jenkins. We should - # be careful to clean it up if it doesn't actually kick off. + # We can get the started notification on another thread before + # this is done so we add the build even before we trigger the + # job on Jenkins. We should be careful to clean it up if it + # doesn't actually kick off. try: self.jenkins.build_job(job.name, parameters=params) except: - self.log.exception("Exception launching build %s for job %s for change %s:" % ( + self.log.exception( + "Exception launching build %s for job %s for change %s:" % ( build, job, change)) # Whoops. Remove that build we added. del self.builds[uuid] @@ -171,33 +178,36 @@ class Jenkins(object): self.log.debug("Looking for build %s in queue" % build) for item in self.jenkins.get_queue_info(): - if not item.has_key('actions'): + if 'actions' not in item: continue for action in item['actions']: - if not action.has_key('parameters'): + if 'parameters' not in action: continue parameters = action['parameters'] for param in parameters: - if (param['name'] == 'UUID' and build.uuid == param['value']): + if (param['name'] == 'UUID' and + build.uuid == param['value']): self.log.debug("Found queue item %s for build %s" % ( item['id'], build)) try: self.jenkins.cancel_queue(item['id']) - self.log.debug("Canceled queue item %s for build %s" % ( + self.log.debug( + "Canceled queue item %s for build %s" % ( item['id'], build)) return except: - self.log.exception("Exception canceling queue item %s for build %s" % ( - item['id'], build)) - + self.log.exception("Exception canceling queue \ +item %s for build %s" % (item['id'], build)) self.log.debug("Still unable to find build %s to cancel" % build) if build.number: self.log.debug("Build %s has just started" % build) self.jenkins.stop_build(build.job.name, build.number) self.log.debug("Canceled just running build %s" % build) else: - self.log.error("Build %s has not started but was not found in queue" % build) - + self.log.error( + "Build %s has not started but was not found in queue" % + build) + def onBuildCompleted(self, uuid, status, url, number): self.log.info("Build %s #%s complete, status %s" % ( uuid, number, status)) diff --git a/zuul/lib/gerrit.py b/zuul/lib/gerrit.py index 15c7c05834..a8838dd434 100644 --- a/zuul/lib/gerrit.py +++ b/zuul/lib/gerrit.py @@ -1,3 +1,4 @@ +# Copyright 2011 OpenStack, LLC. # Copyright 2012 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -24,6 +25,7 @@ import pprint # TODO: switch this to paramiko? + class GerritWatcher(threading.Thread): log = logging.getLogger("gerrit.GerritWatcher") @@ -45,7 +47,7 @@ class GerritWatcher(threading.Thread): cmd += ['-l', self.username, self.server, 'gerrit', 'stream-events'] self.proc = subprocess.Popen(cmd, - bufsize=1, + bufsize=1, stdin=None, stdout=subprocess.PIPE, stderr=None, @@ -67,7 +69,8 @@ class GerritWatcher(threading.Thread): def _read(self): l = self.proc.stdout.readline() data = json.loads(l) - self.log.debug("Received data from Gerrit event stream: \n%s" % pprint.pformat(data)) + self.log.debug("Received data from Gerrit event stream: \n%s" % + pprint.pformat(data)) self.gerrit.addEvent(data) def _listen(self): @@ -79,7 +82,7 @@ class GerritWatcher(threading.Thread): self._read() else: raise Exception("event on ssh connection") - + def _run(self): try: if not self.proc: @@ -94,6 +97,7 @@ class GerritWatcher(threading.Thread): while True: self._run() + class Gerrit(object): log = logging.getLogger("gerrit.Gerrit") @@ -122,7 +126,7 @@ class Gerrit(object): def review(self, project, change, message, action={}): cmd = 'gerrit review --project %s --message "%s"' % ( project, message) - for k,v in action.items(): + for k, v in action.items(): if v is True: cmd += ' --%s' % k else: @@ -151,7 +155,7 @@ class Gerrit(object): client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) - client.connect(self.hostname, + client.connect(self.hostname, username=self.username, port=29418) diff --git a/zuul/model.py b/zuul/model.py index aafcdd6430..2b7be6bd6d 100644 --- a/zuul/model.py +++ b/zuul/model.py @@ -14,6 +14,7 @@ import re + class ChangeQueue(object): def __init__(self, queue_name): self.name = '' @@ -51,6 +52,7 @@ class ChangeQueue(object): for project in other.projects: self.addProject(project) + class Job(object): def __init__(self, name): self.name = name @@ -64,6 +66,7 @@ class Job(object): def __repr__(self): return '' % (self.name) + class Build(object): def __init__(self, job, uuid): self.job = job @@ -75,6 +78,7 @@ class Build(object): def __repr__(self): return '' % (self.uuid, self.job.name) + class JobTree(object): """ A JobTree represents an instance of one Job, and holds JobTrees whose jobs should be run if that Job succeeds. A root node of a @@ -106,6 +110,7 @@ class JobTree(object): return ret return None + class Project(object): def __init__(self, name): self.name = name @@ -122,7 +127,7 @@ class Project(object): return self.job_trees[name] def hasQueue(self, name): - if self.job_trees.has_key(name): + if name in self.job_trees: return True return False @@ -135,6 +140,7 @@ class Project(object): return [] return tree.getJobs() + class Change(object): def __init__(self, queue_name, project, branch, number, patchset, refspec): self.queue_name = queue_name @@ -153,18 +159,18 @@ class Change(object): return '' % (id(self), self.number, self.patchset) def formatStatus(self, indent=0): - indent_str = ' '*indent + indent_str = ' ' * indent ret = '' ret += '%sProject %s change %s,%s\n' % (indent_str, self.project.name, - self.number, + self.number, self.patchset) for job in self.project.getJobs(self.queue_name): result = self.jobs.get(job.name) ret += '%s %s: %s\n' % (indent_str, job.name, result) if self.change_ahead: ret += '%sWaiting on:\n' % (indent_str) - ret += self.change_ahead.formatStatus(indent+2) + ret += self.change_ahead.formatStatus(indent + 2) return ret def formatReport(self): @@ -173,7 +179,7 @@ class Change(object): ret += 'Build successful\n\n' else: ret += 'Build failed\n\n' - + for job in self.project.getJobs(self.queue_name): result = self.jobs.get(job.name) url = self.job_urls.get(job.name, job.name) @@ -222,7 +228,7 @@ class Change(object): def areAllJobsComplete(self): tree = self.project.getJobTreeForQueue(self.queue_name) for job in tree.getJobs(): - if not self.jobs.has_key(job.name): + if not job.name in self.jobs: return False return True @@ -236,6 +242,7 @@ class Change(object): if self.change_behind: self.change_behind.change_ahead = None + class TriggerEvent(object): def __init__(self): self.data = None @@ -250,17 +257,19 @@ class TriggerEvent(object): def __str__(self): ret = '