mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-14 00:21:08 +00:00
Migrate to logging
This commit is contained in:
parent
0184a10c21
commit
2c2aaf90b3
@ -15,13 +15,14 @@
|
||||
# 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
|
||||
import re
|
||||
import shlex
|
||||
import socket
|
||||
from sys import argv, stderr
|
||||
import sys
|
||||
|
||||
class UDTConfig(object):
|
||||
"""Ubuntu Dev Tools configuration file (devscripts config file) and
|
||||
@ -40,9 +41,12 @@ 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(argv[0]).upper().replace('-', '_')
|
||||
prefix = os.path.basename(sys.argv[0]).upper().replace('-', '_')
|
||||
self.prefix = prefix
|
||||
if not no_conf:
|
||||
self.config = self.parse_devscripts_config()
|
||||
@ -60,9 +64,9 @@ class UDTConfig(object):
|
||||
for line in f:
|
||||
parsed = shlex.split(line, comments=True)
|
||||
if len(parsed) > 1:
|
||||
print >> stderr, (
|
||||
"W: Cannot parse variable assignment in %s: %s"
|
||||
% (getattr(f, 'name', '<config>'), line))
|
||||
self.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
|
||||
@ -98,12 +102,13 @@ class UDTConfig(object):
|
||||
else:
|
||||
continue
|
||||
if k in compat_keys:
|
||||
r_prefix = self.prefix
|
||||
replacements = self.prefix + '_' + key
|
||||
if key in self.defaults:
|
||||
r_prefix = 'UBUNTUTOOLS'
|
||||
print >> stderr, (
|
||||
'W: Deprecated configuration variable: %s. '
|
||||
'Replaced by %s_%s.') % (k, r_prefix, key)
|
||||
replacements += 'or UBUNTUTOOLS_' + key
|
||||
self.logger.warn(
|
||||
'Using deprecated configuration variable %s. '
|
||||
'You should use %s.',
|
||||
k, replacements)
|
||||
return value
|
||||
return default
|
||||
|
||||
@ -167,3 +172,9 @@ 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')
|
||||
|
@ -14,17 +14,34 @@
|
||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
# PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from sys import version_info as _version_info
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
if _version_info < (2, 7):
|
||||
if sys.version_info < (2, 7):
|
||||
import unittest2 as unittest
|
||||
else:
|
||||
import unittest
|
||||
|
||||
def discover():
|
||||
import os
|
||||
import sys
|
||||
# import __main__ triggers code re-execution
|
||||
__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,14 +14,16 @@
|
||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
# PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import os.path
|
||||
from StringIO import StringIO
|
||||
from sys import stderr
|
||||
|
||||
import ubuntutools.config
|
||||
from ubuntutools.config import UDTConfig, ubu_email
|
||||
from ubuntutools.test import unittest
|
||||
from ubuntutools.config import UDTConfig, ubu_email, setup_logging
|
||||
from ubuntutools.test import unittest, LoggingCatcher
|
||||
|
||||
setup_logging()
|
||||
|
||||
config_files = {
|
||||
'system': '',
|
||||
@ -43,13 +45,21 @@ def fake_open(filename, mode='r'):
|
||||
class ConfigTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
ubuntutools.config.open = fake_open
|
||||
ubuntutools.config.stderr = StringIO()
|
||||
|
||||
self.logs = LoggingCatcher()
|
||||
self.logging_handler = logging.root.handlers[0]
|
||||
logging.root.removeHandler(self.logging_handler)
|
||||
logging.root.addHandler(self.logs)
|
||||
|
||||
self.cleanEnvironment()
|
||||
|
||||
def tearDown(self):
|
||||
del ubuntutools.config.open
|
||||
self.assertEqual(ubuntutools.config.stderr.getvalue(), '')
|
||||
ubuntutools.config.stderr = stderr
|
||||
|
||||
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.cleanEnvironment()
|
||||
|
||||
def cleanEnvironment(self):
|
||||
@ -86,10 +96,9 @@ REPEAT=yes
|
||||
'INHERIT': 'user',
|
||||
'REPEAT': 'yes',
|
||||
})
|
||||
errs = ubuntutools.config.stderr.getvalue().strip()
|
||||
ubuntutools.config.stderr = StringIO()
|
||||
self.assertEqual(len(errs.splitlines()), 1)
|
||||
self.assertRegexpMatches(errs, r'Cannot parse.*\bCOMMAND_EXECUTION=a')
|
||||
self.assertEqual(len(self.logs['warning']), 1)
|
||||
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||
r'Cannot parse.*\bCOMMAND_EXECUTION=a')
|
||||
|
||||
def get_value(self, *args, **kwargs):
|
||||
config = UDTConfig(prefix='TEST')
|
||||
@ -125,11 +134,9 @@ REPEAT=yes
|
||||
config_files['user'] = 'COMPATFOOBAR=bar'
|
||||
self.assertEqual(self.get_value('QUX', compat_keys=['COMPATFOOBAR']),
|
||||
'bar')
|
||||
errs = ubuntutools.config.stderr.getvalue().strip()
|
||||
ubuntutools.config.stderr = StringIO()
|
||||
self.assertEqual(len(errs.splitlines()), 1)
|
||||
self.assertRegexpMatches(errs,
|
||||
r'Deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b')
|
||||
self.assertEqual(len(self.logs['warning']), 1)
|
||||
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||
r'deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b')
|
||||
|
||||
def test_boolean(self):
|
||||
config_files['user'] = "TEST_BOOLEAN=yes"
|
||||
|
Loading…
x
Reference in New Issue
Block a user