add ability to parse from a file object
Change-Id: I4f62fad4d2c0e361eddaed6d4db2faa655bd4b11
This commit is contained in:
parent
72c02d4106
commit
a6194c582e
@ -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
|
plugin, or include locally defined methods to deal with an
|
||||||
idiosyncratic build system.
|
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
|
XML Processing
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
@ -117,13 +117,14 @@ class YamlParser(object):
|
|||||||
self.data = {}
|
self.data = {}
|
||||||
self.jobs = []
|
self.jobs = []
|
||||||
|
|
||||||
def parse(self, fn):
|
def parse_fp(self, fp):
|
||||||
data = yaml.load(open(fn))
|
data = yaml.load(fp)
|
||||||
if data:
|
if data:
|
||||||
if not isinstance(data, list):
|
if not isinstance(data, list):
|
||||||
raise JenkinsJobsException(
|
raise JenkinsJobsException(
|
||||||
"The topmost collection in file '{fname}' must be a list,"
|
"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:
|
for item in data:
|
||||||
cls, dfn = item.items()[0]
|
cls, dfn = item.items()[0]
|
||||||
group = self.data.get(cls, {})
|
group = self.data.get(cls, {})
|
||||||
@ -141,6 +142,10 @@ class YamlParser(object):
|
|||||||
group[name] = dfn
|
group[name] = dfn
|
||||||
self.data[cls] = group
|
self.data[cls] = group
|
||||||
|
|
||||||
|
def parse(self, fn):
|
||||||
|
with open(fn) as fp:
|
||||||
|
self.parse_fp(fp)
|
||||||
|
|
||||||
def getJob(self, name):
|
def getJob(self, name):
|
||||||
job = self.data.get('job', {}).get(name, None)
|
job = self.data.get('job', {}).get(name, None)
|
||||||
if not job:
|
if not job:
|
||||||
@ -510,13 +515,19 @@ class Builder(object):
|
|||||||
self.ignore_cache = ignore_cache
|
self.ignore_cache = ignore_cache
|
||||||
|
|
||||||
def load_files(self, fn):
|
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):
|
if os.path.isdir(fn):
|
||||||
files_to_process = [os.path.join(fn, f)
|
files_to_process = [os.path.join(fn, f)
|
||||||
for f in os.listdir(fn)
|
for f in os.listdir(fn)
|
||||||
if (f.endswith('.yml') or f.endswith('.yaml'))]
|
if (f.endswith('.yml') or f.endswith('.yaml'))]
|
||||||
else:
|
else:
|
||||||
files_to_process = [fn]
|
files_to_process = [fn]
|
||||||
self.parser = YamlParser(self.global_config)
|
|
||||||
for in_file in files_to_process:
|
for in_file in files_to_process:
|
||||||
logger.debug("Parsing YAML file {0}".format(in_file))
|
logger.debug("Parsing YAML file {0}".format(in_file))
|
||||||
self.parser.parse(in_file)
|
self.parser.parse(in_file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user