fix handling of working dir and rename base_dir to role_dir

This commit is contained in:
Robert Kaussow 2019-10-09 23:19:57 +02:00
parent e5b64c094c
commit beb81f348f
4 changed files with 33 additions and 28 deletions

View File

@ -35,7 +35,7 @@ class AnsibleDoctor:
# TODO: add function to print to stdout instead of file
parser = argparse.ArgumentParser(
description="Generate documentation from annotated Ansible roles using templates")
parser.add_argument("base_dir", nargs="?", help="role directory, (default: current working dir)")
parser.add_argument("role_dir", nargs="?", help="role directory, (default: current working dir)")
parser.add_argument("-c", "--config", nargs="?", dest="config_file", help="location of configuration file")
parser.add_argument("-o", "--output", action="store", dest="output_dir", type=str,
help="output base dir")

View File

@ -35,9 +35,9 @@ class Config():
"env": "CONFIG_FILE",
"type": environs.Env().str
},
"base_dir": {
"role_dir": {
"default": "",
"env": "BASE_DIR",
"env": "ROLE_DIR",
"type": environs.Env().str
},
"dry_run": {
@ -131,7 +131,7 @@ class Config():
self._args = args
self._schema = None
self.config_file = default_config_file
self.base_dir = os.getcwd()
self.role_dir = os.getcwd()
self.config = None
self._set_config()
self.is_role = self._set_is_role() or False
@ -186,18 +186,19 @@ class Config():
# preset config file path
if envs.get("config_file"):
self.config_file = self._normalize_path(envs.get("config_file"))
if envs.get("base_dir"):
self.base_dir = self._normalize_path(envs.get("base_dir"))
if envs.get("role_dir"):
self.role_dir = self._normalize_path(envs.get("role_dir"))
if args.get("config_file"):
self.config_file = self._normalize_path(args.get("config_file"))
if args.get("base_dir"):
self.base_dir = self._normalize_path(args.get("base_dir"))
if args.get("role_dir"):
self.role_dir = self._normalize_path(args.get("role_dir"))
source_files = []
source_files.append(self.config_file)
source_files.append(os.path.join(self.base_dir, ".ansibledoctor"))
source_files.append(os.path.join(self.base_dir, ".ansibledoctor.yml"))
source_files.append(os.path.join(self.base_dir, ".ansibledoctor.yaml"))
source_files.append(os.path.join(os.getcwd(), ".ansibledoctor"))
source_files.append(os.path.join(os.getcwd(), ".ansibledoctor.yml"))
source_files.append(os.path.join(os.getcwd(), ".ansibledoctor.yaml"))
for config in source_files:
if config and os.path.exists(config):
@ -219,25 +220,29 @@ class Config():
if self._validate(args):
anyconfig.merge(defaults, args, ac_merge=anyconfig.MS_DICTS)
defaults["output_dir"] = self._normalize_path(defaults["output_dir"])
defaults["template_dir"] = self._normalize_path(defaults["template_dir"])
defaults["custom_header"] = self._normalize_path(defaults["custom_header"])
fix_files = ["output_dir", "template_dir", "custom_header"]
for file in fix_files:
if defaults[file] and defaults[file] != "":
defaults[file] = self._normalize_path(defaults[file])
if defaults.get("config_file"):
if "config_file" in defaults:
defaults.pop("config_file")
if defaults.get("base_dir"):
defaults.pop("base_dir")
if "role_dir" in defaults:
defaults.pop("role_dir")
defaults["logging"]["level"] = defaults["logging"]["level"].upper()
self.config = defaults
def _normalize_path(self, path):
if not os.path.isabs(path):
return os.path.abspath(os.path.expanduser(os.path.expandvars(path)))
base = os.path.join(os.getcwd(), path)
return os.path.abspath(os.path.expanduser(os.path.expandvars(base)))
else:
return path
def _set_is_role(self):
if os.path.isdir(os.path.join(self.base_dir, "tasks")):
if os.path.isdir(os.path.join(self.role_dir, "tasks")):
return True
def _validate(self, config):

View File

@ -41,10 +41,10 @@ class Generator:
:return: None
"""
base_dir = self.config.get_template()
role_dir = self.config.get_template()
for file in glob.iglob(base_dir + "/**/*." + self.extension, recursive=True):
relative_file = file[len(base_dir) + 1:]
for file in glob.iglob(role_dir + "/**/*." + self.extension, recursive=True):
relative_file = file[len(role_dir) + 1:]
if ntpath.basename(file)[:1] != "_":
self.logger.debug("Found template file: " + relative_file)
self.template_files.append(relative_file)

View File

@ -35,18 +35,18 @@ class Registry:
:return: None
"""
extensions = YAML_EXTENSIONS
base_dir = self.config.base_dir
role_name = os.path.basename(base_dir)
role_dir = self.config.role_dir
role_name = os.path.basename(role_dir)
excludes = self.config.config.get("exclude_files")
excludespec = pathspec.PathSpec.from_lines("gitwildmatch", excludes)
self.log.debug("Scan for files: " + base_dir)
self.log.debug("Scan for files: " + role_dir)
for extension in extensions:
pattern = os.path.join(base_dir, "**/*." + extension)
pattern = os.path.join(role_dir, "**/*." + extension)
for filename in glob.iglob(pattern, recursive=True):
if not excludespec.match_file(filename):
self.log.debug("Adding file to '{}': {}".format(role_name, os.path.relpath(filename, base_dir)))
self.log.debug("Adding file to '{}': {}".format(role_name, os.path.relpath(filename, role_dir)))
self._doc.append(filename)
else:
self.log.debug("Excluding file: {}".format(os.path.relpath(filename, base_dir)))
self.log.debug("Excluding file: {}".format(os.path.relpath(filename, role_dir)))