Skip to content

Commit

Permalink
Merge branch 'feature/find-unused-eggs' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Feb 22, 2014
2 parents cda0a2a + 5690f73 commit 9eb02d2
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 10 deletions.
34 changes: 24 additions & 10 deletions README.rst
Expand Up @@ -92,16 +92,6 @@ With this part into your buildout, a new script named ``./bin/evolve`` will
be created. It will check for the available updates of the eggs listed in the
``versions`` section of the ``versions.cfg`` file, then write the updates found.

Extra
-----

Buildout-versions-checker provides an extra script named
``indent-buildout``, designed for just (re)indent your buildout files.
Because the buildout files are sometimes mixed with spaces and tabulations
which may affect viewing and editing. ::

$ ./indent-buildout -s buildout.cfg

Python compatibility
--------------------

Expand All @@ -114,6 +104,30 @@ Requirements
* six >= 1.4.1
* futures >= 2.1.4

Extras
------

Buildout-versions-checker also provides extra scripts for simplify the
maintenance of yours versions files.

``indent-buildout``
===================

``indent-buildout`` is designed for just (re)indenting your buildout files.
Because the buildout files are sometimes mixed with spaces and tabulations
which may affect viewing and editing. ::

$ ./indent-buildout -s buildout.cfg -s versions.cfg

``find-unused-versions``
========================

``find-unused-versions`` just check if your have not pinned eggs which are
not used in your installation. For better results, run the script after a
full and fresh install. ::

$ ./find-unused-versions

.. _`zc.buildout`: http://www.buildout.org/
.. |travis-develop| image:: https://travis-ci.org/Fantomas42/buildout-versions-checker.png?branch=develop
:alt: Build Status - develop branch
Expand Down
42 changes: 42 additions & 0 deletions bvc/checker.py
@@ -1,6 +1,7 @@
"""Version checker for Buildout Versions Checker"""
import futures

import os
import socket
from collections import OrderedDict
from distutils.version import LooseVersion
Expand Down Expand Up @@ -134,3 +135,44 @@ def find_updates(self, versions, last_versions):
updates.append((package, last_version))
logger.info('- %d package updates found.' % len(updates))
return updates


class UnusedVersionsChecker(VersionsChecker):
"""
Checks unused eggs in a config file.
"""

def __init__(self, source, egg_directory, excludes=[]):
"""
Parses a config file containing pinned versions
of eggs and check their installation in the egg_directory.
"""
self.source = source
self.excludes = excludes
self.egg_directory = egg_directory
self.source_versions = OrderedDict(
self.parse_versions(self.source))
self.versions = self.include_exclude_versions(
self.source_versions, excludes=self.excludes)
self.used_versions = self.get_used_versions(self.egg_directory)
self.unused = self.find_unused_versions(
self.versions.keys(), self.used_versions)

def get_used_versions(self, egg_directory):
"""
Walk into the egg_directory to know the packages installed.
"""
return [egg.split('-')[0] for egg in os.listdir(egg_directory)
if egg.endswith('.egg')]

def find_unused_versions(self, versions, used_versions):
"""
Make the difference between the listed versions and
the used versions.
"""
unused = versions[:]
used_version_lower = [x.lower() for x in used_versions]
for version in versions:
if version.lower().replace('-', '_') in used_version_lower:
unused.remove(version)
return unused
79 changes: 79 additions & 0 deletions bvc/scripts/find_unused_versions.py
@@ -0,0 +1,79 @@
"""Command line for finding unused pinned versions"""
from six import string_types

import sys
import logging
from argparse import ArgumentParser

from bvc.logger import logger
from bvc.checker import UnusedVersionsChecker
from bvc.configparser import VersionsConfigParser


def cmdline(argv=sys.argv[1:]):
parser = ArgumentParser(
description='Find unused pinned eggs')
parser.add_argument(
'-s', '--source', dest='source', default='versions.cfg',
help='The file where versions are pinned '
'(default: versions.cfg)')
parser.add_argument(
'--eggs', dest='eggs', default='./eggs/',
help='The directory where the eggs are located')
parser.add_argument(
'-e', '--exclude', action='append', dest='excludes', default=[],
help='Exclude package when checking updates '
'(can be used multiple times)')
parser.add_argument(
'-w', '--write', action='store_true', dest='write', default=False,
help='Write the updates in the source file')
parser.add_argument(
'--indent', dest='indentation', type=int, default=32,
help='Spaces used when indenting "key = value" (default: 32)')
parser.add_argument(
'--sorting', dest='sorting', default='', choices=['alpha', 'length'],
help='Sorting algorithm used on the keys when writing source file '
'(default: None)')
parser.add_argument(
'-v', action='count', dest='verbosity', default=1,
help='Increase verbosity (specify multiple times for more)')
parser.add_argument(
'-q', action='count', dest='quietly', default=0,
help='Decrease verbosity (specify multiple times for more)')

if isinstance(argv, string_types):
argv = argv.split()
options = parser.parse_args(argv)

verbose_logs = {0: 100,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG}
verbosity = min(3, max(0, options.verbosity - options.quietly))
console = logging.StreamHandler(sys.stdout)
console.setLevel(verbose_logs[verbosity])
logger.addHandler(console)

source = options.source
try:
checker = UnusedVersionsChecker(
source, options.eggs, options.excludes)
except Exception as e:
sys.exit(str(e))

if not checker.unused:
sys.exit(0)

for package in checker.unused:
logger.warning('- %s is unused.' % package)

if options.write:
config = VersionsConfigParser()
config.read(source)
for package in checker.unused:
config.remove_option('versions', package)

config.write(source, options.indentation, options.sorting)
logger.info('- %s updated.' % source)

sys.exit(0)

0 comments on commit 9eb02d2

Please sign in to comment.