/usr/lib/python2.7/dist-packages/instant/output.py is in python-instant 1.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | """This module contains internal logging utilities."""
# Copyright (C) 2008 Martin Sandve Alnes
# Copyright (C) 2014 Jan Blechta
#
# This file is part of Instant.
#
# Instant is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Instant 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Instant. If not, see <http://www.gnu.org/licenses/>.
#
# Alternatively, Instant may be distributed under the terms of the BSD license.
import logging, os, platform, sys
# Logging wrappers
_log = logging.getLogger("instant")
_loghandler = logging.StreamHandler()
_log.addHandler(_loghandler)
#_log.setLevel(logging.WARNING)
_log.setLevel(logging.INFO)
#_log.setLevel(logging.DEBUG)
# Choose method for calling external programs. use subprocess by
# deafult, and os.system on Windows
_default_call_method = 'SUBPROCESS'
if 'Windows' in platform.system() or 'CYGWIN' in platform.system():
_default_call_method = 'OS_SYSTEM'
_call_method = os.environ.get("INSTANT_SYSTEM_CALL_METHOD", \
_default_call_method)
_log.debug('Using call method: %s'%_call_method)
def get_log_handler():
return _loghandler
def get_logger():
return _log
def set_log_handler(handler):
global _loghandler
_log.removeHandler(_loghandler)
_loghandler = handler
_log.addHandler(_loghandler)
def set_logging_level(level):
import inspect
frame = inspect.currentframe().f_back
instant_warning("set_logging_level is deprecated but was called "\
"from %s, at line %d. Use set_log_level instead." % \
(inspect.getfile(frame), frame.f_lineno))
set_log_level(level)
def set_log_level(level):
if isinstance(level, str):
level = level.upper()
assert level in ("INFO", "WARNING", "ERROR", "DEBUG")
level = getattr(logging, level)
else:
assert isinstance(level, int)
_log.setLevel(level)
# Aliases for calling log consistently:
def instant_debug(*message):
_log.debug(*message)
def instant_info(*message):
_log.info(*message)
def instant_warning(*message):
_log.warning(*message)
def instant_error(*message):
_log.error(*message)
text = message[0] % message[1:]
raise RuntimeError(text)
def instant_assert(condition, *message):
if not condition:
_log.error(*message)
text = message[0] % message[1:]
raise AssertionError(text)
# Utility functions for file handling:
def write_file(filename, text):
"Write text to a file and close it."
try:
f = open(filename, "w")
f.write(text)
f.close()
except IOError as e:
instant_error("Can't open '%s': %s" % (filename, e))
if _call_method == 'SUBPROCESS':
from subprocess import Popen, PIPE, STDOUT
def get_status_output(cmd, input=None, cwd=None, env=None):
"Replacement for commands.getstatusoutput which does not work on Windows."
if isinstance(cmd, str):
cmd = cmd.strip().split()
instant_debug("Running: " + str(cmd))
# NOTE: This is not OFED-fork-safe! Check subprocess.py,
# http://bugs.python.org/issue1336#msg146685
# OFED-fork-safety means that parent should not
# touch anything between fork() and exec(),
# which is not met in subprocess module. See
# https://www.open-mpi.org/faq/?category=openfabrics#ofa-fork
# http://www.openfabrics.org/downloads/OFED/release_notes/OFED_3.12_rc1_release_notes#3.03
pipe = Popen(cmd, shell=False, cwd=cwd, env=env, stdout=PIPE, stderr=STDOUT)
(output, errout) = pipe.communicate(input=input)
assert not errout
status = pipe.returncode
output = output.decode('utf-8') if sys.version_info[0] > 2 else output
return (status, output)
elif _call_method == 'OS_SYSTEM':
import tempfile
from .paths import get_default_error_dir
def get_status_output(cmd, input=None, cwd=None, env=None):
# We don't need function with such a generality.
# We only need output and return code.
if not isinstance(cmd, str) or input is not None or \
cwd is not None or env is not None:
raise NotImplementedError(
'This implementation (%s) of get_status_output does'
' not accept \'input\', \'cwd\' and \'env\' kwargs.'
%_call_method)
f = tempfile.NamedTemporaryFile(dir=get_default_error_dir(),
delete=True)
# Execute cmd with redirection
cmd += ' > ' + f.name + ' 2>&1'
instant_debug("Running: " + str(cmd))
# NOTE: Possibly OFED-fork-safe, tests needed!
status = os.system(cmd)
output = f.read()
f.close()
output = output.decode('utf-8') if sys.version_info[0] > 2 else output
return (status, output)
elif _call_method == 'COMMANDS':
import commands
def get_status_output(*args, **kwargs):
status, output = commands.getstatusoutput(*args, **kwargs)
output = output.decode('utf-8') if sys.version_info[0] > 2 else output
return status, output
else:
instant_error('Incomprehensible environment variable'
' INSTANT_SYSTEM_CALL_METHOD=%s'%_call_method)
|