2013-11-25 23:27:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2013 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2013-11-26 00:01:30 +00:00
|
|
|
import os
|
2013-11-25 23:27:08 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class Message:
|
2013-11-25 23:27:08 +00:00
|
|
|
"""Support messaging between types
|
|
|
|
|
|
|
|
"""
|
2013-11-26 00:01:30 +00:00
|
|
|
def __init__(self, prefix, messages):
|
2013-11-25 23:27:08 +00:00
|
|
|
self.prefix = prefix
|
2013-11-26 00:01:30 +00:00
|
|
|
self.global_messages = messages
|
2013-11-25 23:27:08 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
in_fd, self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in')
|
|
|
|
out_fd, self.messages_out = tempfile.mkstemp(
|
|
|
|
suffix='.cdist_message_out')
|
2013-11-26 00:01:30 +00:00
|
|
|
|
2014-12-09 18:54:49 +00:00
|
|
|
os.close(in_fd)
|
|
|
|
os.close(out_fd)
|
2013-11-25 23:27:08 +00:00
|
|
|
|
2014-12-09 18:54:49 +00:00
|
|
|
self._copy_messages()
|
2013-11-25 23:27:08 +00:00
|
|
|
|
|
|
|
@property
|
2013-11-26 00:01:30 +00:00
|
|
|
def env(self):
|
2013-11-25 23:27:08 +00:00
|
|
|
env = {}
|
2016-07-05 18:44:24 +00:00
|
|
|
env['__messages_in'] = self.messages_in
|
2013-11-25 23:27:08 +00:00
|
|
|
env['__messages_out'] = self.messages_out
|
|
|
|
|
|
|
|
return env
|
|
|
|
|
2013-11-26 00:01:30 +00:00
|
|
|
def _copy_messages(self):
|
|
|
|
"""Copy global contents into our copy"""
|
|
|
|
shutil.copyfile(self.global_messages, self.messages_in)
|
|
|
|
|
2013-11-25 23:27:08 +00:00
|
|
|
def _cleanup(self):
|
2013-12-03 13:36:38 +00:00
|
|
|
"""remove temporary files"""
|
2013-11-26 00:01:30 +00:00
|
|
|
if os.path.exists(self.messages_in):
|
|
|
|
os.remove(self.messages_in)
|
|
|
|
if os.path.exists(self.messages_out):
|
|
|
|
os.remove(self.messages_out)
|
2013-11-25 23:27:08 +00:00
|
|
|
|
|
|
|
def _merge_messages(self):
|
2013-12-03 13:36:38 +00:00
|
|
|
"""merge newly written lines into global file"""
|
2013-11-26 00:01:30 +00:00
|
|
|
with open(self.messages_out) as fd:
|
2013-11-25 23:27:08 +00:00
|
|
|
content = fd.readlines()
|
|
|
|
|
|
|
|
with open(self.global_messages, 'a') as fd:
|
|
|
|
for line in content:
|
2021-03-30 05:56:38 +00:00
|
|
|
fd.write("{}:{}".format(self.prefix, line))
|
2013-11-25 23:27:08 +00:00
|
|
|
|
|
|
|
def merge_messages(self):
|
|
|
|
self._merge_messages()
|
|
|
|
self._cleanup()
|