--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2018 by Frank Brehm, Publicies Pixelpark GmbH, Berlin
+@summary: A module for encapsulating all information about a Puppetfile
+ of a r10k environment
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import logging
+import os
+import re
+import copy
+import warnings
+import time
+import datetime
+import pwd
+import grp
+
+# Third party modules
+
+# Own modules
+from .common import pp, to_str, to_bool, is_sequence
+
+from .obj import BaseObjectError
+from .obj import BaseObject
+
+__version__ = '0.1.1'
+
+LOG = logging.getLogger(__name__)
+
+
+# =============================================================================
+class PuppetfileError(BaseObjectError):
+
+ pass
+
+
+
+# =============================================================================
+class Puppetfile(BaseObject):
+ """Class for encapsulating a Puppetfile of a r10k environment."""
+
+ default_environment = 'production'
+ default_env_root_dir = os.sep + os.path.join("etc", "puppetlabs", "code", "environments")
+
+ # -------------------------------------------------------------------------
+ def __init__(
+ self, env_root_dir=None, environment=None,
+ appname=None, verbose=0, version=__version__, base_dir=None, initialized=None):
+
+ self._env_root_dir = self.default_env_root_dir
+ self._environment = self.default_environment
+ self._stat = None
+
+ super(Puppetfile, self).__init__(
+ appname=appname, verbose=verbose, version=version,
+ base_dir=base_dir, initialized=False,
+ )
+
+ if initialized is not None:
+ self.initialized = initialized
+
+ # -------------------------------------------------------------------------
+ @property
+ def env_root_dir(self):
+ """The root directory of all puppet environments."""
+ return self._env_root_dir
+
+ @env_root_dir.setter
+ def env_root_dir(self, value):
+ if value is None:
+ msg = "The root directory of all puppet environments may not be None."
+ raise TypeError(msg)
+
+ val = str(value)
+ if not os.path.isabs(val):
+ msg = (
+ "Path {!r} for the root directory of all puppet environments must "
+ "be an absolute path.").format(value)
+ raise ValueError(msg)
+ self._env_root_dir = val
+
+ # -------------------------------------------------------------------------
+ @property
+ def environment(self):
+ """The r10k environment of this Puppetfile."""
+ return self._environment
+
+ @environment.setter
+ def environment(self, value):
+ if value is None:
+ msg = "The Puppet environment may not be None."
+ raise TypeError(msg)
+
+ val = str(value).strip()
+ if val == '':
+ msg = "The Puppet environment may not be empty."
+ raise ValueError(msg)
+
+ self._environment = val
+
+ # -------------------------------------------------------------------------
+ @property
+ def env_dir(self):
+ """The directory containing the r10k environmenmt."""
+ edir = os.path.normpath(os.path.join(self.env_root_dir, self.environment))
+ if os.path.exists(edir):
+ return os.path.abspath(edir)
+ return edir
+
+ # -------------------------------------------------------------------------
+ @property
+ def filename(self):
+ """The filename of the Puppetfile."""
+ return os.path.join(self.env_dir, 'Puppetfile')
+
+ # -------------------------------------------------------------------------
+ def get_file_stat(self, forced=False):
+
+ if self._stat is not None and not forced:
+ return
+
+ if self.verbose > 2:
+ LOG.debug("Getting file status of {!r}.".format(self.filename))
+ if not self.exists:
+ self._stat = None
+ return
+
+ self._stat = os.stat(self.filename)
+
+ # -------------------------------------------------------------------------
+ @property
+ def exists(self):
+ """A flag, whether the Puppetfile exists."""
+ return os.path.exists(self.filename)
+
+ # -------------------------------------------------------------------------
+ @property
+ def stat(self):
+ """The file status of the Puppetfile."""
+ self.get_file_stat()
+ return self._stat
+
+ # -------------------------------------------------------------------------
+ @property
+ def owner(self):
+ """The owner of the Puppetfile as a string."""
+ if self.stat is None:
+ return None
+
+ owner_name = None
+ try:
+ owner_name = pwd.getpwuid(self.stat.st_uid).pw_name
+ except KeyError:
+ owner_name = "{}".format(self.stat.st_uid)
+ return owner_name
+
+ # -------------------------------------------------------------------------
+ @property
+ def group(self):
+ """The owning group of the Puppetfile as a string."""
+ if self.stat is None:
+ return None
+
+ group_name = None
+ try:
+ group_name = grp.getgrgid(self.stat.st_gid).gr_name
+ except KeyError:
+ group_name = "{}".format(self.stat.st_gid)
+ return group_name
+
+ # -------------------------------------------------------------------------
+ def as_dict(self, short=True):
+ """
+ Transforms the elements of the object into a dict
+
+ @return: structure as dict
+ @rtype: dict
+ """
+
+ res = super(Puppetfile, self).as_dict(short=short)
+
+ res['default_environment'] = self.default_environment
+ res['default_env_root_dir'] = self.default_env_root_dir
+ res['env_root_dir'] = self.env_root_dir
+ res['environment'] = self.environment
+ res['env_dir'] = self.env_dir
+ res['filename'] = self.filename
+ res['exists'] = self.exists
+ res['stat'] = self.stat
+ res['owner'] = self.owner
+ res['group'] = self.group
+
+ return res
+
+
+
+# =============================================================================
+
+if __name__ == "__main__":
+
+ pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list