Code Cleanup, introduce docs_path and use it for create-docs

This commit is contained in:
Stefan Reimer 2020-06-19 17:54:48 +01:00
parent d4b6837260
commit 478f8a4bfa
3 changed files with 8 additions and 38 deletions

View File

@ -1,7 +1,6 @@
import pathlib
import logging
from .utils import ensure_dir
from .stackgroup import StackGroup
from .jinja import read_config_file
from .exceptions import InvalidProjectDir
@ -18,7 +17,8 @@ class CloudBender(object):
self.ctx = {
"config_path": self.root.joinpath("config"),
"template_path": self.root.joinpath("cloudformation"),
"parameter_path": self.root.joinpath("parameters"),
"hooks_path": self.root.joinpath("hooks"),
"docs_path": self.root.joinpath("docs"),
"artifact_paths": [self.root.joinpath("artifacts")]
}
@ -35,7 +35,7 @@ class CloudBender(object):
# Make sure all paths are abs
for k, v in self.ctx.items():
if k in ['config_path', 'template_path', 'parameter_path', 'artifact_paths']:
if k in ['config_path', 'template_path', 'hooks_path', 'docs_path', 'artifact_paths']:
if isinstance(v, list):
new_list = []
for p in v:
@ -50,9 +50,6 @@ class CloudBender(object):
if not v.is_absolute():
self.ctx[k] = self.root.joinpath(v)
if k in ['template_path', 'parameter_path']:
ensure_dir(self.ctx[k])
self.sg = StackGroup(self.ctx['config_path'], self.ctx)
self.sg.read_config()

View File

@ -2,7 +2,6 @@ import os
import re
import hashlib
import oyaml as yaml
import json
import time
import pathlib
import pprint
@ -12,7 +11,7 @@ from dateutil.tz import tzutc
from botocore.exceptions import ClientError
from .utils import dict_merge, search_refs
from .utils import dict_merge, search_refs, ensure_dir
from .connection import BotoConnection
from .jinja import JinjaEnv, read_config_file
from . import __version__
@ -230,7 +229,7 @@ class Stack(object):
def write_template_file(self):
if self.cfn_template:
yaml_file = os.path.join(self.ctx['template_path'], self.rel_path, self.stackname + ".yaml")
self._ensure_dirs('template_path')
ensure_dir(os.path.join(self.ctx['template_path'], self.rel_path))
with open(yaml_file, 'w') as yaml_contents:
yaml_contents.write(self.cfn_template)
logger.info('Wrote %s to %s', self.template, yaml_file)
@ -348,7 +347,8 @@ class Stack(object):
if 'Outputs' in self.cfn_data:
data['outputs'] = self.cfn_data['Outputs']
doc_file = os.path.join(self.ctx['template_path'], self.rel_path, self.stackname.upper() + ".md")
doc_file = os.path.join(self.ctx['docs_path'], self.rel_path, self.stackname.upper() + ".md")
ensure_dir(os.path.join(self.ctx['docs_path'], self.rel_path))
with open(doc_file, 'w') as doc_contents:
doc_contents.write(template.render(**data))
@ -401,28 +401,6 @@ class Stack(object):
if _errors:
raise ParameterNotFound('Cannot find value for parameters: {0}'.format(_errors))
def write_parameter_file(self):
parameter_file = os.path.join(self.ctx['parameter_path'], self.rel_path, self.stackname + ".yaml")
# Render parameters as json for AWS CFN
self._ensure_dirs('parameter_path')
with open(parameter_file, 'w') as parameter_contents:
parameter_contents.write(json.dumps(self.cfn_parameters, indent=2, separators=(',', ': '), sort_keys=True))
logger.info('Wrote json parameters for %s to %s', self.stackname, parameter_file)
if not self.cfn_parameters:
# Make sure there are no parameters from previous runs
if os.path.isfile(parameter_file):
os.remove(parameter_file)
def delete_parameter_file(self):
parameter_file = os.path.join(self.ctx['parameter_path'], self.rel_path, self.stackname + ".yaml")
try:
os.remove(parameter_file)
logger.debug('Deleted parameter %s.', parameter_file)
except OSError:
pass
def create(self):
"""Creates a stack """
@ -613,11 +591,6 @@ class Stack(object):
]))
self.most_recent_event_datetime = event["Timestamp"]
def _ensure_dirs(self, path):
# Ensure output dirs exist
if not os.path.exists(os.path.join(self.ctx[path], self.rel_path)):
os.makedirs(os.path.join(self.ctx[path], self.rel_path))
# stackoutput inspection
def _inspect_stacks(self, conglomerate):
# Get all stacks of the conglomertate

View File

@ -30,7 +30,7 @@ def ensure_dir(path):
"""Creates dir if it does not already exist."""
if not os.path.exists(path):
os.makedirs(path)
logger.info('Created directory: %s', path)
logger.debug('Created directory: %s', path)
def setup_logging(debug):