ansible-doctor/ansibledoctor/FileRegistry.py

63 lines
1.7 KiB
Python
Raw Normal View History

2019-10-07 08:52:00 +02:00
#!/usr/bin/env python3
import glob
import os
import sys
from ansibledoctor.Config import SingleConfig
from ansibledoctor.Contstants import YAML_EXTENSIONS
from ansibledoctor.Utils import SingleLog
class Registry:
_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
base_dir = os.getcwd()
2019-10-07 08:52:00 +02:00
self.log.debug("Scan for files: " + base_dir)
for extension in extensions:
for filename in glob.iglob(base_dir + "/**/*." + extension, recursive=True):
if self._is_excluded_yaml_file(filename, base_dir):
self.log.debug("Excluding: " + filename)
2019-10-07 08:52:00 +02:00
else:
self.log.debug("Adding to role:" + base_dir + " => " + filename)
2019-10-07 08:52:00 +02:00
self._doc.append(filename)
# TODO: not working...
def _is_excluded_yaml_file(self, file, base_dir):
2019-10-07 08:52:00 +02:00
"""
Sub method for handling file exclusions based on the full path starts with.
:param file:
:param role_base_dir:
:return:
"""
excluded = self.config.config.get("exclude_files")
2019-10-07 08:52:00 +02:00
is_filtered = False
for excluded_dir in excluded:
if file.startswith(base_dir + "/" + excluded_dir):
is_filtered = True
return is_filtered