mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 08:01:09 +00:00
Migrate to ubuntutools.logger
This commit is contained in:
parent
b94d134650
commit
8cee545568
@ -15,7 +15,6 @@
|
||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
# PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import os.path
|
||||
import pwd
|
||||
@ -24,6 +23,8 @@ import shlex
|
||||
import socket
|
||||
import sys
|
||||
|
||||
from ubuntutools.logger import Logger
|
||||
|
||||
class UDTConfig(object):
|
||||
"""Ubuntu Dev Tools configuration file (devscripts config file) and
|
||||
environment variable parsing.
|
||||
@ -41,9 +42,6 @@ class UDTConfig(object):
|
||||
config = {}
|
||||
|
||||
def __init__(self, no_conf=False, prefix=None):
|
||||
setup_logging()
|
||||
self.logger = logging.getLogger('config')
|
||||
|
||||
self.no_conf = no_conf
|
||||
if prefix is None:
|
||||
prefix = os.path.basename(sys.argv[0]).upper().replace('-', '_')
|
||||
@ -64,9 +62,8 @@ class UDTConfig(object):
|
||||
for line in f:
|
||||
parsed = shlex.split(line, comments=True)
|
||||
if len(parsed) > 1:
|
||||
self.logger.warn(
|
||||
'Cannot parse variable assignment in %s: %s',
|
||||
getattr(f, 'name', '<config>'), line)
|
||||
Logger.warn('Cannot parse variable assignment in %s: %s',
|
||||
getattr(f, 'name', '<config>'), line)
|
||||
if len(parsed) >= 1 and '=' in parsed[0]:
|
||||
key, value = parsed[0].split('=', 1)
|
||||
config[key] = value
|
||||
@ -105,7 +102,7 @@ class UDTConfig(object):
|
||||
replacements = self.prefix + '_' + key
|
||||
if key in self.defaults:
|
||||
replacements += 'or UBUNTUTOOLS_' + key
|
||||
self.logger.warn(
|
||||
Logger.warn(
|
||||
'Using deprecated configuration variable %s. '
|
||||
'You should use %s.',
|
||||
k, replacements)
|
||||
@ -172,9 +169,3 @@ def ubu_email(name=None, email=None, export=True):
|
||||
os.environ['DEBFULLNAME'] = name
|
||||
os.environ['DEBEMAIL'] = email
|
||||
return name, email
|
||||
|
||||
def setup_logging():
|
||||
"""Basic logging configuration
|
||||
This has no effect if logger is already configured."""
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(levelname)s (%(name)s) %(message)s')
|
||||
|
@ -24,36 +24,39 @@ class Logger(object):
|
||||
script_name = os.path.basename(sys.argv[0])
|
||||
verbose = False
|
||||
|
||||
stdout = sys.stdout
|
||||
stderr = sys.stderr
|
||||
|
||||
@classmethod
|
||||
def command(cls, cmd):
|
||||
if cls.verbose:
|
||||
for i in xrange(len(cmd)):
|
||||
if cmd[i].find(" ") >= 0:
|
||||
cmd[i] = '"' + cmd[i] + '"'
|
||||
print "%s: I: %s" % (cls.script_name, " ".join(cmd))
|
||||
print >> cls.stdout, "%s: I: %s" % (cls.script_name, " ".join(cmd))
|
||||
|
||||
@classmethod
|
||||
def debug(cls, message, *args):
|
||||
if cls.verbose:
|
||||
print "%s: D: %s" % (cls.script_name, message % args)
|
||||
print >> cls.stderr, "%s: D: %s" % (cls.script_name, message % args)
|
||||
|
||||
@classmethod
|
||||
def error(cls, message, *args):
|
||||
print >> sys.stderr, "%s: Error: %s" % (cls.script_name, message % args)
|
||||
print >> cls.stderr, "%s: Error: %s" % (cls.script_name, message % args)
|
||||
|
||||
@classmethod
|
||||
def warn(cls, message, *args):
|
||||
print >> sys.stderr, "%s: Warning: %s" % (cls.script_name,
|
||||
print >> cls.stderr, "%s: Warning: %s" % (cls.script_name,
|
||||
message % args)
|
||||
|
||||
@classmethod
|
||||
def info(cls, message, *args):
|
||||
if cls.verbose:
|
||||
print "%s: I: %s" % (cls.script_name, message % args)
|
||||
print >> cls.stdout, "%s: I: %s" % (cls.script_name, message % args)
|
||||
|
||||
@classmethod
|
||||
def normal(cls, message, *args):
|
||||
print "%s: %s" % (cls.script_name, message % args)
|
||||
print >> cls.stdout, "%s: %s" % (cls.script_name, message % args)
|
||||
|
||||
@classmethod
|
||||
def set_verbosity(cls, verbose):
|
||||
|
@ -14,7 +14,6 @@
|
||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
# PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
@ -28,20 +27,3 @@ def discover():
|
||||
__main__ = sys.modules['__main__']
|
||||
setupDir = os.path.abspath(os.path.dirname(__main__.__file__))
|
||||
return unittest.defaultTestLoader.discover(setupDir)
|
||||
|
||||
|
||||
class LoggingCatcher(logging.Handler):
|
||||
def __init__(self):
|
||||
logging.Handler.__init__(self)
|
||||
self.m = {'critical': [],
|
||||
'error': [],
|
||||
'warning': [],
|
||||
'info': [],
|
||||
'debug': [],
|
||||
'notset': []}
|
||||
|
||||
def emit(self, record):
|
||||
self.m[record.levelname.lower()].append(record.getMessage())
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.m[key]
|
||||
|
@ -14,16 +14,15 @@
|
||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
# PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
from StringIO import StringIO
|
||||
|
||||
import ubuntutools.config
|
||||
from ubuntutools.config import UDTConfig, ubu_email, setup_logging
|
||||
from ubuntutools.test import unittest, LoggingCatcher
|
||||
|
||||
setup_logging()
|
||||
from ubuntutools.config import UDTConfig, ubu_email
|
||||
from ubuntutools.logger import Logger
|
||||
from ubuntutools.test import unittest
|
||||
|
||||
config_files = {
|
||||
'system': '',
|
||||
@ -45,20 +44,18 @@ def fake_open(filename, mode='r'):
|
||||
class ConfigTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
ubuntutools.config.open = fake_open
|
||||
|
||||
self.logs = LoggingCatcher()
|
||||
self.logging_handler = logging.root.handlers[0]
|
||||
logging.root.removeHandler(self.logging_handler)
|
||||
logging.root.addHandler(self.logs)
|
||||
Logger.stdout = StringIO()
|
||||
Logger.stderr = StringIO()
|
||||
|
||||
self.cleanEnvironment()
|
||||
|
||||
def tearDown(self):
|
||||
del ubuntutools.config.open
|
||||
|
||||
logging.root.removeHandler(self.logs)
|
||||
logging.root.addHandler(self.logging_handler)
|
||||
self.assertTrue(all(len(x) == 0 for x in self.logs.m.itervalues()))
|
||||
self.assertEqual(Logger.stdout.getvalue(), '')
|
||||
self.assertEqual(Logger.stderr.getvalue(), '')
|
||||
Logger.stdout = sys.stdout
|
||||
Logger.stderr = sys.stderr
|
||||
|
||||
self.cleanEnvironment()
|
||||
|
||||
@ -96,9 +93,11 @@ REPEAT=yes
|
||||
'INHERIT': 'user',
|
||||
'REPEAT': 'yes',
|
||||
})
|
||||
self.assertEqual(len(self.logs['warning']), 1)
|
||||
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||
r'Cannot parse.*\bCOMMAND_EXECUTION=a')
|
||||
errs = Logger.stderr.getvalue().strip()
|
||||
Logger.stderr = StringIO()
|
||||
self.assertEqual(len(errs.splitlines()), 1)
|
||||
self.assertRegexpMatches(errs,
|
||||
r'Warning: Cannot parse.*\bCOMMAND_EXECUTION=a')
|
||||
|
||||
def get_value(self, *args, **kwargs):
|
||||
config = UDTConfig(prefix='TEST')
|
||||
@ -134,8 +133,10 @@ REPEAT=yes
|
||||
config_files['user'] = 'COMPATFOOBAR=bar'
|
||||
self.assertEqual(self.get_value('QUX', compat_keys=['COMPATFOOBAR']),
|
||||
'bar')
|
||||
self.assertEqual(len(self.logs['warning']), 1)
|
||||
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||
errs = Logger.stderr.getvalue().strip()
|
||||
Logger.stderr = StringIO()
|
||||
self.assertEqual(len(errs.splitlines()), 1)
|
||||
self.assertRegexpMatches(errs,
|
||||
r'deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b')
|
||||
|
||||
def test_boolean(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user