ansible-doctor/ansibledoctor/FileRegistry.py

59 lines
1.7 KiB
Python
Raw Normal View History

2019-10-07 08:52:00 +02:00
#!/usr/bin/env python3
2019-10-08 11:39:27 +02:00
"""File registry to encapsulate file system related operations."""
2019-10-07 08:52:00 +02:00
import glob
import os
2019-10-08 11:39:27 +02:00
import pathspec
2019-10-07 08:52:00 +02:00
from ansibledoctor.Config import SingleConfig
from ansibledoctor.Contstants import YAML_EXTENSIONS
from ansibledoctor.Utils import SingleLog
class Registry:
2020-04-05 23:16:53 +02:00
"""Register all yaml files."""
2019-10-07 08:52:00 +02:00
_doc = {}
log = None
config = None
def __init__(self):
self._doc = []
self.config = SingleConfig()
self.log = SingleLog().logger
2019-10-07 08:52:00 +02:00
self._scan_for_yamls()
def get_files(self):
return self._doc
def _scan_for_yamls(self):
"""
Search for the yaml files in each project/role root and append to the corresponding object.
:param base: directory in witch we are searching
:return: None
"""
extensions = YAML_EXTENSIONS
role_dir = self.config.role_dir
role_name = os.path.basename(role_dir)
2019-10-08 11:30:31 +02:00
excludes = self.config.config.get("exclude_files")
excludespec = pathspec.PathSpec.from_lines("gitwildmatch", excludes)
2019-10-07 08:52:00 +02:00
self.log.debug("Scan for files: " + role_dir)
2019-10-07 08:52:00 +02:00
for extension in extensions:
pattern = os.path.join(role_dir, "**/*." + extension)
2019-10-08 11:30:31 +02:00
for filename in glob.iglob(pattern, recursive=True):
if not excludespec.match_file(filename):
2020-04-05 23:16:53 +02:00
self.log.debug(
"Adding file to '{}': {}".format(
role_name, os.path.relpath(filename, role_dir)
)
)
2019-10-07 08:52:00 +02:00
self._doc.append(filename)
2019-10-08 11:30:31 +02:00
else:
2020-04-05 23:16:53 +02:00
self.log.debug(
"Excluding file: {}".format(os.path.relpath(filename, role_dir))
)