add ability to parse from a file object

Change-Id: I4f62fad4d2c0e361eddaed6d4db2faa655bd4b11
This commit is contained in:
Alexandre Conrad 2014-04-03 13:45:04 -07:00
parent 72c02d4106
commit a6194c582e
2 changed files with 23 additions and 4 deletions

View File

@ -8,6 +8,14 @@ attributes to existing components, a new module to support a Jenkins
plugin, or include locally defined methods to deal with an
idiosyncratic build system.
The Builder
-----------
The ``Builder`` class manages Jenkins jobs. It's responsible for
creating/deleting/updating jobs and can be called from your application. You
can pass it a filename or an open file-like object that represents your YAML
configuration. See the ``jenkins_jobs/builder.py`` file for more details.
XML Processing
--------------

View File

@ -117,13 +117,14 @@ class YamlParser(object):
self.data = {}
self.jobs = []
def parse(self, fn):
data = yaml.load(open(fn))
def parse_fp(self, fp):
data = yaml.load(fp)
if data:
if not isinstance(data, list):
raise JenkinsJobsException(
"The topmost collection in file '{fname}' must be a list,"
" not a {cls}".format(fname=fn, cls=type(data)))
" not a {cls}".format(fname=getattr(fp, 'name', fp),
cls=type(data)))
for item in data:
cls, dfn = item.items()[0]
group = self.data.get(cls, {})
@ -141,6 +142,10 @@ class YamlParser(object):
group[name] = dfn
self.data[cls] = group
def parse(self, fn):
with open(fn) as fp:
self.parse_fp(fp)
def getJob(self, name):
job = self.data.get('job', {}).get(name, None)
if not job:
@ -510,13 +515,19 @@ class Builder(object):
self.ignore_cache = ignore_cache
def load_files(self, fn):
self.parser = YamlParser(self.global_config)
if hasattr(fn, 'read'):
self.parser.parse_fp(fn)
return
if os.path.isdir(fn):
files_to_process = [os.path.join(fn, f)
for f in os.listdir(fn)
if (f.endswith('.yml') or f.endswith('.yaml'))]
else:
files_to_process = [fn]
self.parser = YamlParser(self.global_config)
for in_file in files_to_process:
logger.debug("Parsing YAML file {0}".format(in_file))
self.parser.parse(in_file)