Move 'update' yaml parsing to JenkinsJobs.execute.

1 Remove YamlParser from Builder.
 2 ...
 3 Rule the world.

Change-Id: Id465078da2a7016fd59407516fe124602abd016c
This commit is contained in:
Wayne Warren 2015-12-31 18:11:12 -08:00
parent 535acbc671
commit 3ce6618381
2 changed files with 47 additions and 26 deletions
jenkins_jobs

@ -245,8 +245,6 @@ class Builder(object):
def delete_old_managed(self, keep=None):
jobs = self.jenkins.get_jobs()
deleted_jobs = 0
if keep is None:
keep = [job.name for job in self.parser.xml_jobs]
for job in jobs:
if job['name'] not in keep:
if self.jenkins.is_managed(job['name']):
@ -296,24 +294,14 @@ class Builder(object):
logger.debug("'{0}' has not changed".format(job.name))
return changed
def update_jobs(self, input_fn, jobs_glob=None, output=None,
n_workers=None):
def update_jobs(self, xml_jobs, output=None, n_workers=None):
orig = time.time()
self.parser = YamlParser(self.jjb_config, self.plugins_list)
self.parser.load_files(input_fn)
logger.info("Number of jobs generated: %d", len(xml_jobs))
xml_jobs.sort(key=operator.attrgetter('name'))
self.parser.expandYaml(jobs_glob)
self.parser.generateXML()
step = time.time()
logging.debug('%d XML files generated in %ss',
len(self.parser.jobs), str(step - orig))
logger.info("Number of jobs generated: %d", len(self.parser.xml_jobs))
self.parser.xml_jobs.sort(key=operator.attrgetter('name'))
if (output and not hasattr(output, 'write')
and not os.path.isdir(output)):
if (output and not hasattr(output, 'write') and
not os.path.isdir(output)):
logger.info("Creating directory %s" % output)
try:
os.makedirs(output)
@ -326,7 +314,7 @@ class Builder(object):
if hasattr(output, 'write'):
output = utils.wrap_stream(output)
for job in self.parser.xml_jobs:
for job in xml_jobs:
if hasattr(output, 'write'):
# `output` is a file-like object
logger.info("Job name: %s", job.name)
@ -346,13 +334,13 @@ class Builder(object):
logger.debug("Writing XML to '{0}'".format(output_fn))
with io.open(output_fn, 'w', encoding='utf-8') as f:
f.write(job.output().decode('utf-8'))
return self.parser.xml_jobs, len(self.parser.xml_jobs)
return xml_jobs, len(xml_jobs)
# Filter out the jobs that did not change
logging.debug('Filtering %d jobs for changed jobs',
len(self.parser.xml_jobs))
len(xml_jobs))
step = time.time()
jobs = [job for job in self.parser.xml_jobs
jobs = [job for job in xml_jobs
if self.changed(job)]
logging.debug("Filtered for changed jobs in %ss",
(time.time() - step))

@ -18,10 +18,12 @@ import os
import logging
import platform
import sys
import time
import yaml
from jenkins_jobs.builder import Builder
from jenkins_jobs.parser import YamlParser
from jenkins_jobs.cli.parser import create_parser
from jenkins_jobs.config import JJBConfig
from jenkins_jobs import utils
@ -133,6 +135,7 @@ class JenkinsJobs(object):
if options.command == 'delete':
for job in options.name:
builder.delete_job(job, options.path)
elif options.command == 'delete-all':
if not utils.confirm(
'Sure you want to delete *ALL* jobs from Jenkins '
@ -142,6 +145,7 @@ class JenkinsJobs(object):
logger.info("Deleting all jobs")
builder.delete_all_jobs()
elif options.command == 'update':
if options.n_workers < 0:
self.parser.error(
@ -149,16 +153,45 @@ class JenkinsJobs(object):
logger.info("Updating jobs in {0} ({1})".format(
options.path, options.names))
orig = time.time()
# Generate XML
parser = YamlParser(self.jjb_config, builder.plugins_list)
parser.load_files(options.path)
parser.expandYaml(options.names)
parser.generateXML()
jobs = parser.jobs
step = time.time()
logging.debug('%d XML files generated in %ss',
len(jobs), str(step - orig))
jobs, num_updated_jobs = builder.update_jobs(
options.path, options.names,
parser.xml_jobs,
n_workers=options.n_workers)
logger.info("Number of jobs updated: %d", num_updated_jobs)
if options.delete_old:
num_deleted_jobs = builder.delete_old_managed()
logger.info("Number of jobs deleted: %d", num_deleted_jobs)
n = builder.delete_old_managed(keep=parser.xml_jobs)
logger.info("Number of jobs deleted: %d", n)
elif options.command == 'test':
builder.update_jobs(options.path, options.name,
output=options.output_dir,
logger.info("Updating jobs in {0} ({1})".format(
options.path, options.name))
orig = time.time()
# Generate XML
parser = YamlParser(self.jjb_config, builder.plugins_list)
parser.load_files(options.path)
parser.expandYaml(options.name)
parser.generateXML()
jobs = parser.jobs
step = time.time()
logging.debug('%d XML files generated in %ss',
len(jobs), str(step - orig))
builder.update_jobs(parser.xml_jobs, output=options.output_dir,
n_workers=1)