Compare commits

...

1 commit

Author SHA1 Message Date
Darko Poljak
151aa80ebd Regain Python 3.2 support 2020-06-11 09:37:28 +02:00
11 changed files with 89 additions and 24 deletions

View file

@ -129,7 +129,8 @@ class Config(object):
@staticmethod @staticmethod
def hosts(source): def hosts(source):
try: try:
yield from cdist.hostsource.HostSource(source)() for x in cdist.hostsource.HostSource(source)():
yield x
except (IOError, OSError, UnicodeError) as e: except (IOError, OSError, UnicodeError) as e:
raise cdist.Error( raise cdist.Error(
"Error reading hosts from \'{}\': {}".format( "Error reading hosts from \'{}\': {}".format(

View file

@ -28,6 +28,11 @@ import cdist.core
from cdist.util import fsproperty from cdist.util import fsproperty
# FileNotFoundError is added in 3.3.
if not hasattr(__builtins__, 'FileNotFoundError'):
FileNotFoundError = (OSError, IOError, )
class IllegalObjectIdError(cdist.Error): class IllegalObjectIdError(cdist.Error):
def __init__(self, object_id, message=None): def __init__(self, object_id, message=None):
self.object_id = object_id self.object_id = object_id

View file

@ -26,6 +26,11 @@ import cdist.core
import logging import logging
# FileNotFoundError is added in 3.3.
if not hasattr(__builtins__, 'FileNotFoundError'):
FileNotFoundError = (OSError, IOError, )
class InvalidTypeError(cdist.Error): class InvalidTypeError(cdist.Error):
def __init__(self, name, type_path, type_absolute_path): def __init__(self, name, type_path, type_absolute_path):
self.name = name self.name = name

View file

@ -26,6 +26,12 @@ import os
import cdist import cdist
from . import util from . import util
# FileNotFoundError is added in 3.3.
if not hasattr(__builtins__, 'FileNotFoundError'):
FileNotFoundError = (OSError, IOError, )
''' '''
common: common:
runs only locally, does not need remote runs only locally, does not need remote

View file

@ -32,6 +32,11 @@ from cdist import flock
from cdist.core.manifest import Manifest from cdist.core.manifest import Manifest
# FileNotFoundError is added in 3.3.
if not hasattr(__builtins__, 'FileNotFoundError'):
FileNotFoundError = (OSError, IOError, )
class MissingRequiredEnvironmentVariableError(cdist.Error): class MissingRequiredEnvironmentVariableError(cdist.Error):
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name

View file

@ -191,9 +191,14 @@ class Local(object):
close_stdout = False close_stdout = False
close_stderr = False close_stderr = False
stderr_special_devnull = False
stdout_special_devnull = False
if quiet: if quiet:
stderr = subprocess.DEVNULL stderr, close_stderr = util._get_devnull()
stdout = subprocess.DEVNULL stderr_special_devnull = not close_stderr
stdout, close_stdout = util._get_devnull()
stdout_special_devnull = not close_stdout
elif do_save_output: elif do_save_output:
if not return_output and stdout is None: if not return_output and stdout is None:
stdout = util.get_std_fd(self.stdout_base_path, 'local') stdout = util.get_std_fd(self.stdout_base_path, 'local')
@ -228,7 +233,9 @@ class Local(object):
output = None output = None
if do_save_output: if do_save_output:
if not stderr_special_devnull:
util.log_std_fd(self.log, command, stderr, 'Local stderr') util.log_std_fd(self.log, command, stderr, 'Local stderr')
if not stdout_special_devnull:
util.log_std_fd(self.log, command, stdout, 'Local stdout') util.log_std_fd(self.log, command, stdout, 'Local stdout')
return output return output
except (OSError, subprocess.CalledProcessError) as error: except (OSError, subprocess.CalledProcessError) as error:
@ -237,8 +244,14 @@ class Local(object):
if message_prefix: if message_prefix:
message.merge_messages() message.merge_messages()
if close_stdout: if close_stdout:
if isinstance(stdout, int):
os.close(stdout)
else:
stdout.close() stdout.close()
if close_stderr: if close_stderr:
if isinstance(stderr, int):
os.close(stderr)
else:
stderr.close() stderr.close()
def run_script(self, script, env=None, return_output=False, def run_script(self, script, env=None, return_output=False,

View file

@ -24,12 +24,10 @@ import os
import glob import glob
import subprocess import subprocess
import logging import logging
import multiprocessing
import cdist import cdist
import cdist.exec.util as util import cdist.exec.util as util
import cdist.util.ipaddr as ipaddr import cdist.util.ipaddr as ipaddr
from cdist.mputil import mp_pool_run
def _wrap_addr(addr): def _wrap_addr(addr):
@ -298,10 +296,11 @@ class Remote(object):
os_environ['__target_fqdn'] = self.target_host[2] os_environ['__target_fqdn'] = self.target_host[2]
self.log.trace("Remote run: %s", command) self.log.trace("Remote run: %s", command)
special_devnull = False
try: try:
if self.quiet_mode: if self.quiet_mode:
stderr = subprocess.DEVNULL stderr, close_stderr = util._get_devnull()
close_stderr = False special_devnull = not close_stderr
if return_output: if return_output:
output = subprocess.check_output(command, env=os_environ, output = subprocess.check_output(command, env=os_environ,
stderr=stderr).decode() stderr=stderr).decode()
@ -311,6 +310,7 @@ class Remote(object):
output = None output = None
if self.save_output_streams: if self.save_output_streams:
if not special_devnull:
util.log_std_fd(self.log, command, stderr, 'Remote stderr') util.log_std_fd(self.log, command, stderr, 'Remote stderr')
util.log_std_fd(self.log, command, stdout, 'Remote stdout') util.log_std_fd(self.log, command, stdout, 'Remote stdout')
@ -323,4 +323,7 @@ class Remote(object):
if close_stdout: if close_stdout:
stdout.close() stdout.close()
if close_stderr: if close_stderr:
if isinstance(stderr, int):
os.close(stderr)
else:
stderr.close() stderr.close()

View file

@ -172,11 +172,19 @@ def get_std_fd(base_path, name):
return stdfd return stdfd
def log_std_fd(log, command, stdfd, prefix): # subprocess.DEVNULL is added in 3.3.
if hasattr(subprocess, 'DEVNULL'):
def log_std_fd(log, command, stdfd, prefix):
if stdfd is not None and stdfd != subprocess.DEVNULL: if stdfd is not None and stdfd != subprocess.DEVNULL:
stdfd.seek(0, 0) stdfd.seek(0, 0)
log.trace("Command: {}; {}: {}".format( log.trace("Command: {}; {}: {}".format(command, prefix,
command, prefix, stdfd.read().decode())) stdfd.read().decode()))
else:
def log_std_fd(log, command, stdfd, prefix):
if stdfd is not None and not isinstance(stdfd, int):
stdfd.seek(0, 0)
log.trace("Command: {}; {}: {}".format(command, prefix,
stdfd.read().decode()))
def dist_conf_dir(): def dist_conf_dir():
@ -210,3 +218,11 @@ def resolve_conf_dirs_from_config_and_args(args):
cfg = cdist.configuration.Configuration(args) cfg = cdist.configuration.Configuration(args)
configuration = cfg.get_config(section='GLOBAL') configuration = cfg.get_config(section='GLOBAL')
return resolve_conf_dirs(configuration, args.conf_dir) return resolve_conf_dirs(configuration, args.conf_dir)
# subprocess.DEVNULL is added in 3.3.
def _get_devnull():
if hasattr(subprocess, 'DEVNULL'):
return (subprocess.DEVNULL, False)
else:
return (os.open(os.devnull, os.O_RDWR), True)

View file

@ -24,6 +24,11 @@ import logging
import os import os
# FileNotFoundError is added in 3.3.
if not hasattr(__builtins__, 'FileNotFoundError'):
FileNotFoundError = (OSError, IOError, )
log = logging.getLogger('cdist-flock') log = logging.getLogger('cdist-flock')

View file

@ -68,9 +68,12 @@ class HostSource(object):
return return
if isinstance(self.source, str): if isinstance(self.source, str):
yield from self._hosts_from_file() for x in self._hosts_from_file():
yield x
else: else:
yield from self._hosts_from_sequence() for x in self._hosts_from_sequence():
yield x
def __call__(self): def __call__(self):
yield from self.hosts() for x in self.hosts():
yield x

View file

@ -43,18 +43,21 @@ def scan_preos_dir_plugins(dir):
module_name = fname module_name = fname
try: try:
module = __import__(module_name) module = __import__(module_name)
yield from preos_plugin(module) for x in preos_plugin(module):
yield x
clsmembers = inspect.getmembers(module, inspect.isclass) clsmembers = inspect.getmembers(module, inspect.isclass)
for cm in clsmembers: for cm in clsmembers:
c = cm[1] c = cm[1]
yield from preos_plugin(c) for x in preos_plugin(c):
yield x
except ImportError as e: except ImportError as e:
log.warning("Cannot import '{}': {}".format(module_name, e)) log.warning("Cannot import '{}': {}".format(module_name, e))
def find_preos_plugins(): def find_preos_plugins():
for dir in _PLUGINS_PATH: for dir in _PLUGINS_PATH:
yield from scan_preos_dir_plugins(dir) for x in scan_preos_dir_plugins(dir):
yield x
def find_preoses(): def find_preoses():