Commit defdcec1 authored by Jim Fulton's avatar Jim Fulton

Added logging support.

parent eb311a70
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
$Id$ $Id$
""" """
import logging
import md5 import md5
import os import os
import pprint import pprint
...@@ -64,15 +65,17 @@ class Buildout(dict): ...@@ -64,15 +65,17 @@ class Buildout(dict):
super(Buildout, self).__init__() super(Buildout, self).__init__()
# default options # default options
data = dict(buildout={'directory': os.path.dirname(config_file), data = dict(buildout={
'eggs-directory': 'eggs', 'directory': os.path.dirname(config_file),
'bin-directory': 'bin', 'eggs-directory': 'eggs',
'parts-directory': 'parts', 'bin-directory': 'bin',
'installed': '.installed.cfg', 'parts-directory': 'parts',
'python': 'buildout', 'installed': '.installed.cfg',
'executable': sys.executable, 'python': 'buildout',
}, 'executable': sys.executable,
) 'log-level': 'WARNING',
'log-format': '%(name)s: %(message)s',
})
# load user defaults, which override defaults # load user defaults, which override defaults
if 'HOME' in os.environ: if 'HOME' in os.environ:
...@@ -116,8 +119,6 @@ class Buildout(dict): ...@@ -116,8 +119,6 @@ class Buildout(dict):
for name in ('bin', 'parts', 'eggs'): for name in ('bin', 'parts', 'eggs'):
d = self._buildout_path(options[name+'-directory']) d = self._buildout_path(options[name+'-directory'])
options[name+'-directory'] = d options[name+'-directory'] = d
if not os.path.exists(d):
os.mkdir(d)
options['installed'] = os.path.join(options['directory'], options['installed'] = os.path.join(options['directory'],
options['installed']) options['installed'])
...@@ -163,6 +164,15 @@ class Buildout(dict): ...@@ -163,6 +164,15 @@ class Buildout(dict):
return os.path.join(self._buildout_dir, *names) return os.path.join(self._buildout_dir, *names)
def install(self, install_parts): def install(self, install_parts):
# Create buildout directories
for name in ('bin', 'parts', 'eggs'):
d = self['buildout'][name+'-directory']
if not os.path.exists(d):
self._logger.info('Creating directory %s', d)
os.mkdir(d)
# Build develop eggs
self._develop() self._develop()
# load installed data # load installed data
...@@ -181,7 +191,7 @@ class Buildout(dict): ...@@ -181,7 +191,7 @@ class Buildout(dict):
if install_parts: if install_parts:
extra = [p for p in install_parts if p not in conf_parts] extra = [p for p in install_parts if p not in conf_parts]
if extra: if extra:
error('Invalid install parts:', *extra) self._error('Invalid install parts:', *extra)
uninstall_missing = False uninstall_missing = False
else: else:
install_parts = conf_parts install_parts = conf_parts
...@@ -206,12 +216,14 @@ class Buildout(dict): ...@@ -206,12 +216,14 @@ class Buildout(dict):
continue continue
# ununstall part # ununstall part
self._logger.info('Uninstalling %s', part)
self._uninstall( self._uninstall(
installed_part_options[part]['__buildout_installed__']) installed_part_options[part]['__buildout_installed__'])
installed_parts = [p for p in installed_parts if p != part] installed_parts = [p for p in installed_parts if p != part]
# install new parts # install new parts
for part in install_parts: for part in install_parts:
self._logger.info('Installing %s', part)
installed_part_options[part] = self[part].copy() installed_part_options[part] = self[part].copy()
del self[part]['__buildout_signature__'] del self[part]['__buildout_signature__']
installed_files = recipes[part].install() or () installed_files = recipes[part].install() or ()
...@@ -241,7 +253,7 @@ class Buildout(dict): ...@@ -241,7 +253,7 @@ class Buildout(dict):
setup = self._buildout_path(setup) setup = self._buildout_path(setup)
if os.path.isdir(setup): if os.path.isdir(setup):
setup = os.path.join(setup, 'setup.py') setup = os.path.join(setup, 'setup.py')
self._logger.info("Running %s -q develop ...", setup)
os.chdir(os.path.dirname(setup)) os.chdir(os.path.dirname(setup))
os.spawnle( os.spawnle(
os.P_WAIT, sys.executable, sys.executable, os.P_WAIT, sys.executable, sys.executable,
...@@ -348,6 +360,33 @@ class Buildout(dict): ...@@ -348,6 +360,33 @@ class Buildout(dict):
print >>f print >>f
_save_options(part, installed_options[part], f) _save_options(part, installed_options[part], f)
f.close() f.close()
def _error(self, message, *args, **kw):
self._logger.error(message, *args, **kw)
sys.exit(1)
def _setup_logging(self):
root_logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(self['buildout']['log-format']))
root_logger.addHandler(handler)
self._logger = logging.getLogger('buildout')
level = self['buildout']['log-level']
if level in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
level = getattr(logging, level)
else:
try:
level = int(level)
except ValueError:
self._error("Invalid logging level %s", level)
verbosity = self['buildout'].get('verbosity', 0)
try:
verbosity = int(verbosity)
except ValueError:
self._error("Invalid verbosity %s", verbosity)
root_logger.setLevel(level-verbosity)
def _save_options(section, options, f): def _save_options(section, options, f):
print >>f, '[%s]' % section print >>f, '[%s]' % section
...@@ -435,23 +474,47 @@ def _error(*message): ...@@ -435,23 +474,47 @@ def _error(*message):
def main(args=None): def main(args=None):
if args is None: if args is None:
args = sys.argv[1:] args = sys.argv[1:]
if args and args[0] == '-c':
args.pop(0)
if not args:
_error("No configuration file specified,")
config_file = args.pop(0)
else:
config_file = 'buildout.cfg'
config_file = 'buildout.cfg'
verbosity = 0
options = [] options = []
while args and '=' in args[0]: while args:
option, value = args.pop(0).split('=', 1) if args[0][0] == '-':
if len(option.split(':')) != 2: op = orig_op = args.pop(0)
_error('Invalid option:', option) op = op[1:]
section, option = option.split(':') while op and op[0] in 'vq':
options.append((section.strip(), option.strip(), value.strip())) if op[0] == 'v':
verbosity += 10
else:
verbosity -= 10
op = op[1:]
if op[:1] == 'c':
op = op[1:]
if op:
config_file = op
else:
if args:
config_file = args.pop(0)
else:
_error("No file name specified for option", orig_op)
elif op:
_error("Invalid option", '-'+op[0])
elif '=' in args[0]:
option, value = args.pop(0).split('=', 1)
if len(option.split(':')) != 2:
_error('Invalid option:', option)
section, option = option.split(':')
options.append((section.strip(), option.strip(), value.strip()))
else:
# We've run out of command-line options and option assignnemnts
# The rest should be commands, so we'll stop here
break
if verbosity:
options.append(('buildout', 'verbosity', str(verbosity)))
buildout = Buildout(config_file, options) buildout = Buildout(config_file, options)
buildout._setup_logging()
if args: if args:
command = args.pop(0) command = args.pop(0)
...@@ -460,7 +523,10 @@ def main(args=None): ...@@ -460,7 +523,10 @@ def main(args=None):
else: else:
command = 'install' command = 'install'
getattr(buildout, command)(args) try:
getattr(buildout, command)(args)
finally:
logging.shutdown()
if sys.version_info[:2] < (2, 4): if sys.version_info[:2] < (2, 4):
def reversed(iterable): def reversed(iterable):
......
This diff is collapsed.
...@@ -24,8 +24,6 @@ ...@@ -24,8 +24,6 @@
- Local download cache - Local download cache
- Logging
- Some way to freeze versions so we can have reproducable buildouts. - Some way to freeze versions so we can have reproducable buildouts.
- Part dependencies - Part dependencies
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment