import json
import re
import glob
-import warnings
import datetime
+import fcntl
+import pwd
+import grp
+
+from operator import attrgetter, itemgetter
# Third party modules
import six
import yaml
-import requests
+
+from six import reraise
# Own modules
from . import __version__
-from .common import pp, to_str
+from .common import pp, to_str, to_bytes
from .base_app import BaseHookError, BaseHookApp
if self.verbose > 2:
LOG.debug("Found modules:\n{}".format(pp(self.modules)))
+ self.write_cache_file()
+
print()
dt = datetime.datetime.now().isoformat(' ')
msg = "[{}]: Finished.".format(dt)
raise GetForgeModulesError(
"Did not found environments in {!r}.".format(self.puppet_root_env_dir))
+ # -------------------------------------------------------------------------
+ def write_cache_file(self):
+
+ output_file = os.path.join(self.data_dir, 'modules-info.yaml')
+ tmp_file = output_file + '.new'
+ fd = None
+
+ LOG.debug("Sorting found modules ...")
+
+ data = []
+ for full_name in self.modules.keys():
+ module_info = self.modules[full_name]
+ data.append(module_info.to_data())
+ data.sort(key=itemgetter('name', 'vendor'))
+ dump = yaml.dump(data, default_flow_style=False)
+
+ LOG.info("Trying to open {!r} exclusive ...".format(tmp_file))
+
+ try:
+ fd = os.open(tmp_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ except OSError as e:
+ msg = "Error on creating file {f!r}: {e}".format(f=tmp_file, e=e)
+ if e.errno == errno.EEXIST:
+ LOG.warn(msg)
+ return None
+ else:
+ error_tuple = sys.exc_info()
+ reraise(GetForgeModulesError, msg, error_tuple[2])
+
+ try:
+ os.write(fd, to_bytes('---\n'))
+ os.write(fd, to_bytes(dump))
+ finally:
+ os.close(fd)
+
+ if os.path.exists(output_file) and not os.geteuid():
+ fstat = os.stat(output_file)
+ user = fstat.st_uid
+ try:
+ user = pwd.getpwuid(fstat.st_uid).pw_name
+ except KeyError:
+ user = fstat.st_uid
+ group = fstat.st_gid
+ try:
+ group = grp.getgrgid(fstat.st_gid).gr_name
+ except KeyError:
+ group = fstat.st_gid
+ LOG.debug("Chowning {f!r} to {u}:{g} ...".format(
+ f=tmp_file, u=user, g=group))
+ os.chown(tmp_file, fstat.st_uid, fstat.st_gid)
+
+ LOG.info("Renaming {src!r} => {tgt!r}.".format(src=tmp_file, tgt=output_file))
+ os.rename(tmp_file, output_file)
+
# =============================================================================
if __name__ == "__main__":