From 38bf2470b8c74b090b45d4bf3f27aabe99647142 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Wed, 29 Apr 2020 09:10:46 -0700 Subject: [PATCH] Handle directories in zuul.d/ configs Prior to this change zuul-sphinx would error if there were directories in zuul.d that needed to be processed recursively. This is because it would try to open the directory as a normal file. Fix this by using os.walk instead of os.listdir to iterate through the contents of the zuul.d directory. Additionally filter for .yaml files only as Zuul does this as well. Change-Id: Id2d828d7842a86b7f89a2ce28a084e70f91d3f55 --- zuul_sphinx/zuul.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/zuul_sphinx/zuul.py b/zuul_sphinx/zuul.py index 43f0970..7962873 100644 --- a/zuul_sphinx/zuul.py +++ b/zuul_sphinx/zuul.py @@ -103,19 +103,22 @@ class ZuulDirective(Directive): def parse_zuul_d(self, path): layout = Layout() - for conf in os.listdir(path): - conf_path = os.path.join(path, conf) - with open(conf_path) as f: - data = yaml.load(f, Loader=ZuulSafeLoader) - if data is None: - raise SphinxError( - "File %s in Zuul dir is empty", conf_path) - for obj in data: - if 'job' in obj: - layout.jobs.append(obj['job']) - if 'project-template' in obj: - layout.project_templates.append( - ProjectTemplate(obj['project-template'])) + # zuul.d configs are recursively loaded in zuul so we recursively + # load them here too. + for (dirpath, dirnames, filenames) in os.walk(path): + for conf in filter(lambda x: x.endswith('.yaml'), filenames): + conf_path = os.path.join(dirpath, conf) + with open(conf_path) as f: + data = yaml.load(f, Loader=ZuulSafeLoader) + if data is None: + raise SphinxError( + "File %s in Zuul dir is empty", conf_path) + for obj in data: + if 'job' in obj: + layout.jobs.append(obj['job']) + if 'project-template' in obj: + layout.project_templates.append( + ProjectTemplate(obj['project-template'])) return layout def _parse_zuul_layout(self):