mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-16 01:21:07 +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
|
# 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
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import socket
|
import socket
|
||||||
from sys import argv, stderr
|
import sys
|
||||||
|
|
||||||
class UDTConfig(object):
|
class UDTConfig(object):
|
||||||
"""Ubuntu Dev Tools configuration file (devscripts config file) and
|
"""Ubuntu Dev Tools configuration file (devscripts config file) and
|
||||||
@ -40,9 +41,12 @@ 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(argv[0]).upper().replace('-', '_')
|
prefix = os.path.basename(sys.argv[0]).upper().replace('-', '_')
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
if not no_conf:
|
if not no_conf:
|
||||||
self.config = self.parse_devscripts_config()
|
self.config = self.parse_devscripts_config()
|
||||||
@ -60,9 +64,9 @@ 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:
|
||||||
print >> stderr, (
|
self.logger.warn(
|
||||||
"W: 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
|
||||||
@ -98,12 +102,13 @@ class UDTConfig(object):
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
if k in compat_keys:
|
if k in compat_keys:
|
||||||
r_prefix = self.prefix
|
replacements = self.prefix + '_' + key
|
||||||
if key in self.defaults:
|
if key in self.defaults:
|
||||||
r_prefix = 'UBUNTUTOOLS'
|
replacements += 'or UBUNTUTOOLS_' + key
|
||||||
print >> stderr, (
|
self.logger.warn(
|
||||||
'W: Deprecated configuration variable: %s. '
|
'Using deprecated configuration variable %s. '
|
||||||
'Replaced by %s_%s.') % (k, r_prefix, key)
|
'You should use %s.',
|
||||||
|
k, replacements)
|
||||||
return value
|
return value
|
||||||
return default
|
return default
|
||||||
|
|
||||||
@ -167,3 +172,9 @@ 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')
|
||||||
|
@ -14,17 +14,34 @@
|
|||||||
# 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.
|
||||||
|
|
||||||
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
|
import unittest2 as unittest
|
||||||
else:
|
else:
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
def discover():
|
def discover():
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
# import __main__ triggers code re-execution
|
# import __main__ triggers code re-execution
|
||||||
__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,14 +14,16 @@
|
|||||||
# 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
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from sys import stderr
|
|
||||||
|
|
||||||
import ubuntutools.config
|
import ubuntutools.config
|
||||||
from ubuntutools.config import UDTConfig, ubu_email
|
from ubuntutools.config import UDTConfig, ubu_email, setup_logging
|
||||||
from ubuntutools.test import unittest
|
from ubuntutools.test import unittest, LoggingCatcher
|
||||||
|
|
||||||
|
setup_logging()
|
||||||
|
|
||||||
config_files = {
|
config_files = {
|
||||||
'system': '',
|
'system': '',
|
||||||
@ -43,13 +45,21 @@ 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
|
||||||
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()
|
self.cleanEnvironment()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
del ubuntutools.config.open
|
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()
|
self.cleanEnvironment()
|
||||||
|
|
||||||
def cleanEnvironment(self):
|
def cleanEnvironment(self):
|
||||||
@ -86,10 +96,9 @@ REPEAT=yes
|
|||||||
'INHERIT': 'user',
|
'INHERIT': 'user',
|
||||||
'REPEAT': 'yes',
|
'REPEAT': 'yes',
|
||||||
})
|
})
|
||||||
errs = ubuntutools.config.stderr.getvalue().strip()
|
self.assertEqual(len(self.logs['warning']), 1)
|
||||||
ubuntutools.config.stderr = StringIO()
|
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||||
self.assertEqual(len(errs.splitlines()), 1)
|
r'Cannot parse.*\bCOMMAND_EXECUTION=a')
|
||||||
self.assertRegexpMatches(errs, r'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')
|
||||||
@ -125,11 +134,9 @@ 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')
|
||||||
errs = ubuntutools.config.stderr.getvalue().strip()
|
self.assertEqual(len(self.logs['warning']), 1)
|
||||||
ubuntutools.config.stderr = StringIO()
|
self.assertRegexpMatches(self.logs['warning'].pop(),
|
||||||
self.assertEqual(len(errs.splitlines()), 1)
|
r'deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b')
|
||||||
self.assertRegexpMatches(errs,
|
|
||||||
r'Deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b')
|
|
||||||
|
|
||||||
def test_boolean(self):
|
def test_boolean(self):
|
||||||
config_files['user'] = "TEST_BOOLEAN=yes"
|
config_files['user'] = "TEST_BOOLEAN=yes"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user