Cleaned up the installer output behavior.

This change also makes sure the logger handlers are not alterated after an
installation. That also fixes the remaining environment alteration issue in
test_packaging.
This commit is contained in:
Tarek Ziade 2011-05-30 12:07:49 +02:00
parent 43f289ad88
commit b1b6e1384c
6 changed files with 86 additions and 67 deletions

View file

@ -118,15 +118,15 @@ def install_local_project(path):
""" """
path = os.path.abspath(path) path = os.path.abspath(path)
if os.path.isdir(path): if os.path.isdir(path):
logger.info('installing from source directory: %s', path) logger.info('Installing from source directory: %s', path)
_run_install_from_dir(path) _run_install_from_dir(path)
elif _is_archive_file(path): elif _is_archive_file(path):
logger.info('installing from archive: %s', path) logger.info('Installing from archive: %s', path)
_unpacked_dir = tempfile.mkdtemp() _unpacked_dir = tempfile.mkdtemp()
shutil.unpack_archive(path, _unpacked_dir) shutil.unpack_archive(path, _unpacked_dir)
_run_install_from_archive(_unpacked_dir) _run_install_from_archive(_unpacked_dir)
else: else:
logger.warning('no projects to install') logger.warning('No projects to install.')
def _run_install_from_archive(source_dir): def _run_install_from_archive(source_dir):
@ -174,16 +174,16 @@ def install_dists(dists, path, paths=sys.path):
installed_dists = [] installed_dists = []
for dist in dists: for dist in dists:
logger.info('installing %s %s', dist.name, dist.version) logger.info('Installing %r %s...', dist.name, dist.version)
try: try:
_install_dist(dist, path) _install_dist(dist, path)
installed_dists.append(dist) installed_dists.append(dist)
except Exception as e: except Exception as e:
logger.info('failed: %s', e) logger.info('Failed: %s', e)
# reverting # reverting
for installed_dist in installed_dists: for installed_dist in installed_dists:
logger.info('reverting %s', installed_dist) logger.info('Reverting %s', installed_dist)
_remove_dist(installed_dist, paths) _remove_dist(installed_dist, paths)
raise e raise e
return installed_dists return installed_dists
@ -292,7 +292,7 @@ def get_infos(requirements, index=None, installed=None, prefer_final=True):
# or remove # or remove
if not installed: if not installed:
logger.info('reading installed distributions') logger.debug('Reading installed distributions')
installed = list(get_distributions(use_egg_info=True)) installed = list(get_distributions(use_egg_info=True))
infos = {'install': [], 'remove': [], 'conflict': []} infos = {'install': [], 'remove': [], 'conflict': []}
@ -306,7 +306,7 @@ def get_infos(requirements, index=None, installed=None, prefer_final=True):
if predicate.name.lower() != installed_project.name.lower(): if predicate.name.lower() != installed_project.name.lower():
continue continue
found = True found = True
logger.info('found %s %s', installed_project.name, logger.info('Found %s %s', installed_project.name,
installed_project.metadata['version']) installed_project.metadata['version'])
# if we already have something installed, check it matches the # if we already have something installed, check it matches the
@ -316,7 +316,7 @@ def get_infos(requirements, index=None, installed=None, prefer_final=True):
break break
if not found: if not found:
logger.info('project not installed') logger.debug('Project not installed')
if not index: if not index:
index = wrapper.ClientWrapper() index = wrapper.ClientWrapper()
@ -331,7 +331,7 @@ def get_infos(requirements, index=None, installed=None, prefer_final=True):
raise InstallationException('Release not found: "%s"' % requirements) raise InstallationException('Release not found: "%s"' % requirements)
if release is None: if release is None:
logger.info('could not find a matching project') logger.info('Could not find a matching project')
return infos return infos
metadata = release.fetch_metadata() metadata = release.fetch_metadata()
@ -348,7 +348,7 @@ def get_infos(requirements, index=None, installed=None, prefer_final=True):
# Get what the missing deps are # Get what the missing deps are
dists = depgraph.missing[release] dists = depgraph.missing[release]
if dists: if dists:
logger.info("missing dependencies found, retrieving metadata") logger.info("Missing dependencies found, retrieving metadata")
# we have missing deps # we have missing deps
for dist in dists: for dist in dists:
_update_infos(infos, get_infos(dist, index, installed)) _update_infos(infos, get_infos(dist, index, installed))
@ -401,7 +401,7 @@ def remove(project_name, paths=sys.path, auto_confirm=True):
finally: finally:
shutil.rmtree(tmp) shutil.rmtree(tmp)
logger.info('removing %r: ', project_name) logger.info('Removing %r: ', project_name)
for file_ in rmfiles: for file_ in rmfiles:
logger.info(' %s', file_) logger.info(' %s', file_)
@ -444,20 +444,20 @@ def remove(project_name, paths=sys.path, auto_confirm=True):
if os.path.exists(dist.path): if os.path.exists(dist.path):
shutil.rmtree(dist.path) shutil.rmtree(dist.path)
logger.info('success: removed %d files and %d dirs', logger.info('Success: removed %d files and %d dirs',
file_count, dir_count) file_count, dir_count)
def install(project): def install(project):
logger.info('getting information about %r', project) logger.info('Getting information about %r...', project)
try: try:
info = get_infos(project) info = get_infos(project)
except InstallationException: except InstallationException:
logger.info('cound not find %r', project) logger.info('Cound not find %r', project)
return return
if info['install'] == []: if info['install'] == []:
logger.info('nothing to install') logger.info('Nothing to install')
return return
install_path = get_config_var('base') install_path = get_config_var('base')

View file

@ -118,9 +118,10 @@ class Crawler(BaseClient):
def __init__(self, index_url=DEFAULT_SIMPLE_INDEX_URL, prefer_final=False, def __init__(self, index_url=DEFAULT_SIMPLE_INDEX_URL, prefer_final=False,
prefer_source=True, hosts=DEFAULT_HOSTS, prefer_source=True, hosts=DEFAULT_HOSTS,
follow_externals=False, mirrors_url=None, mirrors=None, follow_externals=False, mirrors_url=None, mirrors=None,
timeout=SOCKET_TIMEOUT, mirrors_max_tries=0): timeout=SOCKET_TIMEOUT, mirrors_max_tries=0, verbose=False):
super(Crawler, self).__init__(prefer_final, prefer_source) super(Crawler, self).__init__(prefer_final, prefer_source)
self.follow_externals = follow_externals self.follow_externals = follow_externals
self.verbose = verbose
# mirroring attributes. # mirroring attributes.
parsed = urllib.parse.urlparse(index_url) parsed = urllib.parse.urlparse(index_url)
@ -184,7 +185,7 @@ class Crawler(BaseClient):
if predicate.name.lower() in self._projects and not force_update: if predicate.name.lower() in self._projects and not force_update:
return self._projects.get(predicate.name.lower()) return self._projects.get(predicate.name.lower())
prefer_final = self._get_prefer_final(prefer_final) prefer_final = self._get_prefer_final(prefer_final)
logger.info('reading info on PyPI about %s', predicate.name) logger.debug('Reading info on PyPI about %s', predicate.name)
self._process_index_page(predicate.name) self._process_index_page(predicate.name)
if predicate.name.lower() not in self._projects: if predicate.name.lower() not in self._projects:
@ -321,8 +322,9 @@ class Crawler(BaseClient):
infos = get_infos_from_url(link, project_name, infos = get_infos_from_url(link, project_name,
is_external=not self.index_url in url) is_external=not self.index_url in url)
except CantParseArchiveName as e: except CantParseArchiveName as e:
logger.warning( if self.verbose:
"version has not been parsed: %s", e) logger.warning(
"version has not been parsed: %s", e)
else: else:
self._register_release(release_info=infos) self._register_release(release_info=infos)
else: else:

View file

@ -5,6 +5,7 @@ import re
import sys import sys
import getopt import getopt
import logging import logging
from copy import copy
from packaging import logger from packaging import logger
from packaging.dist import Distribution from packaging.dist import Distribution
@ -227,12 +228,13 @@ def _install(dispatcher, args, **kw):
logger.warning('no project to install') logger.warning('no project to install')
return return
target = args[1]
# installing from a source dir or archive file? # installing from a source dir or archive file?
if os.path.isdir(args[1]) or _is_archive_file(args[1]): if os.path.isdir(target) or _is_archive_file(target):
install_local_project(args[1]) install_local_project(target)
else: else:
# download from PyPI # download from PyPI
install(args[1]) install(target)
@action_help(metadata_usage) @action_help(metadata_usage)
@ -399,6 +401,17 @@ class Dispatcher:
msg = 'Unrecognized action "%s"' % self.action msg = 'Unrecognized action "%s"' % self.action
raise PackagingArgError(msg) raise PackagingArgError(msg)
self._set_logger()
# for display options we return immediately
option_order = self.parser.get_option_order()
self.args = args
if self.help or self.action is None:
self._show_help(self.parser, display_options_=False)
def _set_logger(self):
# setting up the logging level from the command-line options # setting up the logging level from the command-line options
# -q gets warning, error and critical # -q gets warning, error and critical
if self.verbose == 0: if self.verbose == 0:
@ -416,13 +429,11 @@ class Dispatcher:
else: # -vv and more for debug else: # -vv and more for debug
level = logging.DEBUG level = logging.DEBUG
# for display options we return immediately # setting up the stream handler
option_order = self.parser.get_option_order() handler = logging.StreamHandler(sys.stderr)
handler.setLevel(level)
self.args = args logger.addHandler(handler)
logger.setLevel(level)
if self.help or self.action is None:
self._show_help(self.parser, display_options_=False)
def _parse_command_opts(self, parser, args): def _parse_command_opts(self, parser, args):
# Pull the current command from the head of the command line # Pull the current command from the head of the command line
@ -635,11 +646,17 @@ class Dispatcher:
def main(args=None): def main(args=None):
dispatcher = Dispatcher(args) old_level = logger.level
if dispatcher.action is None: old_handlers = copy(logger.handlers)
return try:
dispatcher = Dispatcher(args)
if dispatcher.action is None:
return
return dispatcher()
finally:
logger.setLevel(old_level)
logger.handlers[:] = old_handlers
return dispatcher()
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())

View file

@ -150,8 +150,7 @@ class TestTest(TempdirManager,
cmd.tests_require = [phony_project] cmd.tests_require = [phony_project]
cmd.ensure_finalized() cmd.ensure_finalized()
logs = self.get_logs(logging.WARNING) logs = self.get_logs(logging.WARNING)
self.assertEqual(1, len(logs)) self.assertIn(phony_project, logs[-1])
self.assertIn(phony_project, logs[0])
def prepare_a_module(self): def prepare_a_module(self):
tmp_dir = self.mkdtemp() tmp_dir = self.mkdtemp()

View file

@ -818,51 +818,51 @@ class PackagingLibChecks(support.TempdirManager,
def test_is_setuptools_logs_setup_py_text_found(self): def test_is_setuptools_logs_setup_py_text_found(self):
is_setuptools(self._setuptools_setup_py_pkg()) is_setuptools(self._setuptools_setup_py_pkg())
expected = ['setup.py file found', 'found setuptools text in setup.py'] expected = ['setup.py file found.',
self.assertEqual(expected, self.get_logs(logging.INFO)) 'No egg-info directory found.',
'Found setuptools text in setup.py.']
self.assertEqual(expected, self.get_logs(logging.DEBUG))
def test_is_setuptools_logs_setup_py_text_not_found(self): def test_is_setuptools_logs_setup_py_text_not_found(self):
directory = self._random_setup_py_pkg() directory = self._random_setup_py_pkg()
is_setuptools(directory) is_setuptools(directory)
info_expected = ['setup.py file found'] expected = ['setup.py file found.', 'No egg-info directory found.',
warn_expected = ['no egg-info directory found', 'No setuptools text found in setup.py.']
'no setuptools text found in setup.py'] self.assertEqual(expected, self.get_logs(logging.DEBUG))
self.assertEqual(info_expected, self.get_logs(logging.INFO))
self.assertEqual(warn_expected, self.get_logs(logging.WARN))
def test_is_setuptools_logs_egg_info_dir_found(self): def test_is_setuptools_logs_egg_info_dir_found(self):
is_setuptools(self._setuptools_egg_info_pkg()) is_setuptools(self._setuptools_egg_info_pkg())
expected = ['setup.py file found', 'found egg-info directory'] expected = ['setup.py file found.', 'Found egg-info directory.']
self.assertEqual(expected, self.get_logs(logging.INFO)) self.assertEqual(expected, self.get_logs(logging.DEBUG))
def test_is_distutils_logs_setup_py_text_found(self): def test_is_distutils_logs_setup_py_text_found(self):
is_distutils(self._distutils_setup_py_pkg()) is_distutils(self._distutils_setup_py_pkg())
expected = ['setup.py file found', 'found distutils text in setup.py'] expected = ['setup.py file found.',
self.assertEqual(expected, self.get_logs(logging.INFO)) 'No PKG-INFO file found.',
'Found distutils text in setup.py.']
self.assertEqual(expected, self.get_logs(logging.DEBUG))
def test_is_distutils_logs_setup_py_text_not_found(self): def test_is_distutils_logs_setup_py_text_not_found(self):
directory = self._random_setup_py_pkg() directory = self._random_setup_py_pkg()
is_distutils(directory) is_distutils(directory)
info_expected = ['setup.py file found'] expected = ['setup.py file found.', 'No PKG-INFO file found.',
warn_expected = ['no PKG-INFO file found', 'No distutils text found in setup.py.']
'no distutils text found in setup.py'] self.assertEqual(expected, self.get_logs(logging.DEBUG))
self.assertEqual(info_expected, self.get_logs(logging.INFO))
self.assertEqual(warn_expected, self.get_logs(logging.WARN))
def test_is_distutils_logs_pkg_info_file_found(self): def test_is_distutils_logs_pkg_info_file_found(self):
is_distutils(self._distutils_pkg_info()) is_distutils(self._distutils_pkg_info())
expected = ['setup.py file found', 'PKG-INFO file found'] expected = ['setup.py file found.', 'PKG-INFO file found.']
self.assertEqual(expected, self.get_logs(logging.INFO)) self.assertEqual(expected, self.get_logs(logging.DEBUG))
def test_is_packaging_logs_setup_cfg_found(self): def test_is_packaging_logs_setup_cfg_found(self):
is_packaging(self._valid_setup_cfg_pkg()) is_packaging(self._valid_setup_cfg_pkg())
expected = ['setup.cfg file found'] expected = ['setup.cfg file found.']
self.assertEqual(expected, self.get_logs(logging.INFO)) self.assertEqual(expected, self.get_logs(logging.DEBUG))
def test_is_packaging_logs_setup_cfg_not_found(self): def test_is_packaging_logs_setup_cfg_not_found(self):
is_packaging(self._empty_dir) is_packaging(self._empty_dir)
expected = ['no setup.cfg file found'] expected = ['No setup.cfg file found.']
self.assertEqual(expected, self.get_logs(logging.WARN)) self.assertEqual(expected, self.get_logs(logging.DEBUG))
def _write_setuptools_setup_py(self, directory): def _write_setuptools_setup_py(self, directory):
self.write_file((directory, 'setup.py'), self.write_file((directory, 'setup.py'),

View file

@ -1224,9 +1224,9 @@ def _has_egg_info(srcdir):
for item in os.listdir(srcdir): for item in os.listdir(srcdir):
full_path = os.path.join(srcdir, item) full_path = os.path.join(srcdir, item)
if item.endswith('.egg-info') and os.path.isdir(full_path): if item.endswith('.egg-info') and os.path.isdir(full_path):
logger.info("found egg-info directory") logger.debug("Found egg-info directory.")
return True return True
logger.warning("no egg-info directory found") logger.debug("No egg-info directory found.")
return False return False
@ -1243,9 +1243,9 @@ def _has_text(setup_py, installer):
with open(setup_py, 'r', encoding='utf-8') as setup: with open(setup_py, 'r', encoding='utf-8') as setup:
for line in setup: for line in setup:
if re.search(installer_pattern, line): if re.search(installer_pattern, line):
logger.info("found %s text in setup.py", installer) logger.debug("Found %s text in setup.py.", installer)
return True return True
logger.warning("no %s text found in setup.py", installer) logger.debug("No %s text found in setup.py.", installer)
return False return False
@ -1261,15 +1261,16 @@ def _has_pkg_info(srcdir):
pkg_info = os.path.join(srcdir, 'PKG-INFO') pkg_info = os.path.join(srcdir, 'PKG-INFO')
has_pkg_info = os.path.isfile(pkg_info) has_pkg_info = os.path.isfile(pkg_info)
if has_pkg_info: if has_pkg_info:
logger.info("PKG-INFO file found") logger.debug("PKG-INFO file found.")
logger.warning("no PKG-INFO file found") else:
logger.debug("No PKG-INFO file found.")
return has_pkg_info return has_pkg_info
def _has_setup_py(srcdir): def _has_setup_py(srcdir):
setup_py = os.path.join(srcdir, 'setup.py') setup_py = os.path.join(srcdir, 'setup.py')
if os.path.isfile(setup_py): if os.path.isfile(setup_py):
logger.info('setup.py file found') logger.debug('setup.py file found.')
return True return True
return False return False
@ -1277,9 +1278,9 @@ def _has_setup_py(srcdir):
def _has_setup_cfg(srcdir): def _has_setup_cfg(srcdir):
setup_cfg = os.path.join(srcdir, 'setup.cfg') setup_cfg = os.path.join(srcdir, 'setup.cfg')
if os.path.isfile(setup_cfg): if os.path.isfile(setup_cfg):
logger.info('setup.cfg file found') logger.debug('setup.cfg file found.')
return True return True
logger.warning("no setup.cfg file found") logger.debug("No setup.cfg file found.")
return False return False