from .module_info import ModuleInfo
-from .puppetfile import Puppetfile
+from .puppetfile import Puppetfile, PuppetfileError
LOG = logging.getLogger(__name__)
default_http_timeout = 30
max_http_timeout = 600
- re_comment = re.compile(r'^\s*#')
- re_comma_at_end = re.compile(r',\s*$')
-
open_args = {}
if six.PY3:
open_args = {
appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
initialized=True)
- if self.verbose > 0:
+ if self.verbose > 1:
LOG.debug("Got Puppetfile:\n{}".format(pp(pfile.as_dict())))
-
- puppetfile = os.path.join(self.puppet_root_env_dir, env, 'Puppetfile')
- if self.verbose > 1:
- LOG.debug("Searching {!r} ...".format(puppetfile))
- if not os.path.exists(puppetfile):
- LOG.warn("Could not find {!r}.".format(puppetfile))
- return
- if not os.access(puppetfile, os.R_OK):
- LOG.warn("No read access to {!r}.".format(puppetfile))
- return
-
- LOG.debug("Reading {!r} ...".format(puppetfile))
-
- with open(puppetfile, 'r', **self.open_args) as fh:
- if self.verbose > 3:
- LOG.debug("Class of opened file: {!r}".format(fh.__class__.__name__))
- prev_line = ''
- for line in fh.readlines():
-
- line = line.strip()
- if not line:
- continue
- if self.re_comment.match(line):
- continue
-
- if self.verbose > 3:
- LOG.debug("Read line {!r}...".format(line))
-
- prev_line += line
- if self.re_comma_at_end.search(line):
- continue
-
- if self.verbose > 3:
- LOG.debug("Evaluating line {!r}...".format(prev_line))
- module_info = ModuleInfo.init_from_puppetfile_line(
- appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
- line=prev_line, env=env)
- if module_info:
- full_name = module_info.full_name
- if full_name in self.modules:
- self.modules[full_name].merge_in(module_info)
- else:
- self.modules[full_name] = module_info
-
- prev_line = ''
-
- if prev_line:
- if self.verbose > 2:
- LOG.debug("Evaluating line {!r}...".format(prev_line))
- module_info = ModuleInfo.init_from_puppetfile_line(
- appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
- line=prev_line, env=env)
- if module_info:
- full_name = module_info.full_name
- if full_name in self.modules:
- self.modules[full_name].merge_in(module_info)
- else:
- self.modules[full_name] = module_info
+ try:
+ self.modules = pfile.read_modules()
+ except PuppetfileError as e:
+ LOG.warn(str(e))
+ self.modules = {}
+ else:
+ if self.verbose > 2:
+ LOG.debug("Successful read {!r}.".format(pfile.filename))
# -------------------------------------------------------------------------
def init_puppet_environments(self):
LOG.info("Renaming {src!r} => {tgt!r}.".format(src=tmp_file, tgt=output_file))
os.rename(tmp_file, output_file)
+
# =============================================================================
if __name__ == "__main__":
import grp
# Third party modules
+import six
# Own modules
from .common import pp, to_str, to_bool, is_sequence
from .obj import BaseObjectError
from .obj import BaseObject
-__version__ = '0.1.1'
+from .module_info import ModuleInfo
+
+__version__ = '0.2.1'
LOG = logging.getLogger(__name__)
default_environment = 'production'
default_env_root_dir = os.sep + os.path.join("etc", "puppetlabs", "code", "environments")
+ re_comment = re.compile(r'^\s*#')
+ re_comma_at_end = re.compile(r',\s*$')
+
+ open_args = {}
+ if six.PY3:
+ open_args = {
+ 'encoding': 'utf-8',
+ 'errors': 'surrogateescape',
+ }
+
# -------------------------------------------------------------------------
def __init__(
self, env_root_dir=None, environment=None,
"""A flag, whether the Puppetfile exists."""
return os.path.exists(self.filename)
+ # -------------------------------------------------------------------------
+ @property
+ def readable(self):
+ """A flag indicating, that the puppetfile is readable."""
+ if not self.exists:
+ return False
+
+ if not os.access(self.filename, os.R_OK):
+ return False
+
+ return True
+
# -------------------------------------------------------------------------
@property
def stat(self):
res['env_dir'] = self.env_dir
res['filename'] = self.filename
res['exists'] = self.exists
+ res['readable'] = self.readable
res['stat'] = self.stat
res['owner'] = self.owner
res['group'] = self.group
+ res['open_args'] = self.open_args
return res
+ # -------------------------------------------------------------------------
+ def read_modules(self):
+ """Reads the current Puppetfile and returns a hash of all
+ module information from this file with the full name of the modules as keys."""
+
+ if self.verbose > 1:
+ LOG.debug("Searching {!r} ...".format(self.filename))
+
+ if not self.exists:
+ msg = "Puppetfile {!r} does not exists.".format(self.filename)
+ raise PuppetfileError(msg)
+
+ if not self.readable:
+ msg = "Puppetfile {!r} is not readable.".format(self.filename)
+ raise PuppetfileError(msg)
+
+ modules = {}
+
+ LOG.debug("Reading {!r} ...".format(self.filename))
+
+ with open(self.filename, 'r', **self.open_args) as fh:
+
+ prev_line = ''
+ for line in fh.readlines():
+
+ line = line.strip()
+ if not line:
+ continue
+ if self.re_comment.match(line):
+ continue
+
+ if self.verbose > 3:
+ LOG.debug("Read line {!r}...".format(line))
+
+ prev_line += line
+ if self.re_comma_at_end.search(line):
+ continue
+
+ if self.verbose > 3:
+ LOG.debug("Evaluating line {!r}...".format(prev_line))
+ module_info = ModuleInfo.init_from_puppetfile_line(
+ appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
+ line=prev_line, env=self.environment)
+ if module_info:
+ full_name = module_info.full_name
+ if full_name in modules:
+ modules[full_name].merge_in(module_info)
+ else:
+ modules[full_name] = module_info
+
+ prev_line = ''
+
+ if prev_line:
+ if self.verbose > 2:
+ LOG.debug("Evaluating line {!r}...".format(prev_line))
+ module_info = ModuleInfo.init_from_puppetfile_line(
+ appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
+ line=prev_line, env=self.environment)
+ if module_info:
+ full_name = module_info.full_name
+ if full_name in self.modules:
+ modules[full_name].merge_in(module_info)
+ else:
+ modules[full_name] = module_info
+
+ if self.verbose > 1:
+ LOG.debug("Closing {!r} ...".format(self.filename))
+ return modules
# =============================================================================