Skip to content
Snippets Groups Projects
Commit b3e244a8 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Allow specifying modules directly on the command line.

parent 0be243cb
No related branches found
No related tags found
No related merge requests found
"""New, better, entry point.""" """New, better, entry point."""
import argparse import argparse
import pathlib
from .cache import Cache from .cache import Cache
from .gather import get_modules from .gather import get_module, get_modules, ModuleEntry
from .output import setup_index, setup_module from .output import setup_index, setup_module
parser = argparse.ArgumentParser(
prog='puppet-doc configure',
description='Sets up puppet doc')
parser.add_argument('--env', action='store') def __main() -> None:
parser = argparse.ArgumentParser(
prog='puppet-doc configure',
description='Sets up puppet doc')
args = parser.parse_args() parser.add_argument('--env', action='store')
parser.add_argument('modules', nargs='*', type=pathlib.Path)
env = args.env or '/etc/puppetlabs/code/modules' args = parser.parse_args()
env = args.env or '/etc/puppetlabs/code/modules'
def __main() -> None:
cache = Cache('/home/hugo/.cache/puppet-doc') cache = Cache('/home/hugo/.cache/puppet-doc')
modules = get_modules(cache, env)
modules: list[ModuleEntry]
if args.modules != []:
modules = [get_module(cache, mod)
for mod in args.modules]
else:
modules = get_modules(cache, env)
setup_index('output', modules) setup_index('output', modules)
for module in modules: for module in modules:
......
...@@ -76,9 +76,32 @@ def get_puppet_strings(cache: Cache, path: str) -> bytes: ...@@ -76,9 +76,32 @@ def get_puppet_strings(cache: Cache, path: str) -> bytes:
# pass # pass
def get_module(cache: Cache,
path: str) -> ModuleEntry:
"""
Return the metadata of a given module.
:param cache:
Cache objcet for modules, see python module configuration.
:param path:
Path of the given module.
"""
name = os.path.basename(path)
strings_data = get_puppet_strings(cache, path)
try:
with open(os.path.join(path, 'metadata.json')) as f:
metadata = json.load(f)
except FileNotFoundError:
metadata = {}
return ModuleEntry(name, path, strings_data, metadata)
def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]: def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]:
""" """
Enumerate modules in directory. Return all modules present in a given directory.
The directory should be the modules subdirectory of an environment, The directory should be the modules subdirectory of an environment,
e.g. /etc/puppetlabs/code/environments/production/modules. e.g. /etc/puppetlabs/code/environments/production/modules.
...@@ -88,16 +111,8 @@ def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]: ...@@ -88,16 +111,8 @@ def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]:
for entry in sorted(list(os.scandir(dir)), key=lambda d: d.name): for entry in sorted(list(os.scandir(dir)), key=lambda d: d.name):
# TODO Logging # TODO Logging
# print('- entry', entry, file=sys.stderr) # print('- entry', entry, file=sys.stderr)
name = entry.name
path = os.path.join(dir, entry) path = os.path.join(dir, entry)
strings_data = get_puppet_strings(cache, path)
try:
with open(os.path.join(path, 'metadata.json')) as f:
metadata = json.load(f)
except FileNotFoundError:
metadata = {}
modules.append(ModuleEntry(name, path, strings_data, metadata)) modules.append(get_module(cache, path))
return modules return modules
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment