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