2019-10-07 06:52:00 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-10-08 09:39:27 +00:00
|
|
|
"""File registry to encapsulate file system related operations."""
|
|
|
|
|
2019-10-07 06:52:00 +00:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
|
2019-10-08 09:39:27 +00:00
|
|
|
import pathspec
|
|
|
|
|
2021-01-01 12:50:41 +00:00
|
|
|
from ansibledoctor.config import SingleConfig
|
|
|
|
from ansibledoctor.contstants import YAML_EXTENSIONS
|
|
|
|
from ansibledoctor.utils import SingleLog
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Registry:
|
2020-04-05 21:16:53 +00:00
|
|
|
"""Register all yaml files."""
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
_doc = {}
|
|
|
|
log = None
|
|
|
|
config = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._doc = []
|
|
|
|
self.config = SingleConfig()
|
2019-10-07 12:44:45 +00:00
|
|
|
self.log = SingleLog().logger
|
2019-10-07 06:52:00 +00: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
|
2023-02-12 11:57:57 +00:00
|
|
|
base_dir = self.config.base_dir
|
|
|
|
role_name = os.path.basename(base_dir)
|
2019-10-08 09:30:31 +00:00
|
|
|
excludes = self.config.config.get("exclude_files")
|
|
|
|
excludespec = pathspec.PathSpec.from_lines("gitwildmatch", excludes)
|
2019-10-07 06:52:00 +00:00
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
self.log.debug(f"Scan for files: {base_dir}")
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
for extension in extensions:
|
2023-02-12 11:57:57 +00:00
|
|
|
pattern = os.path.join(base_dir, "**/*." + extension)
|
2019-10-08 09:30:31 +00:00
|
|
|
for filename in glob.iglob(pattern, recursive=True):
|
|
|
|
if not excludespec.match_file(filename):
|
2020-04-05 21:16:53 +00:00
|
|
|
self.log.debug(
|
2023-07-19 08:16:46 +00:00
|
|
|
f"Adding file to '{role_name}': {os.path.relpath(filename, base_dir)}"
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
2019-10-07 06:52:00 +00:00
|
|
|
self._doc.append(filename)
|
2019-10-08 09:30:31 +00:00
|
|
|
else:
|
2023-02-12 11:57:57 +00:00
|
|
|
self.log.debug(f"Excluding file: {os.path.relpath(filename, base_dir)}")
|