pep376 / test_pkgutil.py

from nose.tools import *
import pkgutil
from pkgutil import *
import sys
import os

SITE_PKG = os.path.join(os.path.dirname(__file__), 'site-packages')

def setup():
    sys.old = sys.path
    sys.path = [SITE_PKG]
    pkgutil.purge_cache()
    pkgutil._dist_dirs = DistributionDirectories()
    pkgutil._dist_dirs.load(SITE_PKG)

def teardown():
    if not hasattr(sys, 'old'):
        return
    sys.path = sys.old
    del sys.old

def test_distributions():
    projects = list(get_distributions())
    assert_equals(len(projects), 2)

def test_distribution():
    assert_equals(get_distribution('xxx'), None)

    project = get_distribution('mercurial')
    assert_equals(project.name, 'mercurial')

    project = get_distribution('processing')
    assert_equals(project.name, 'processing')

    dist = get_distribution('mercurial')
    assert_equals(str(dist), "Distribution('mercurial')")

    files = list(dist.get_installed_files())
    assert_equals(len(files), 4)
    assert_equals(files[0], ('mercurial/filelog.py', '98676876876876', '12'))

    files = list(dist.get_installed_files(local=True))

    location = os.path.join(dist.container, 'mercurial', 'filelog.py')
    assert_equals(files[0], (location, '98676876876876', '12'))

    f = dist.get_egginfo_file('RECORD')
    record = os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info', 'RECORD')
    assert_equals(open(record).read(), f.read())

    assert dist.uses('mercurial/filelog.py')
    assert not dist.uses('mercurial/filelog.sasasasa')

    assert_equals(list(dist.get_egginfo_files()),
                  ['mercurial-1.0.1.egg-info/PKG_INFO',
                   'mercurial-1.0.1.egg-info/RECORD'])

    assert_equals(list(dist.get_egginfo_files(local=True)),
                  [os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info/PKG_INFO'),
                   os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info/RECORD')])


def test_directory():
    dir = DistributionDirectory(SITE_PKG)
    dist = list(dir.get_file_users('mercurial/filelog.pyc'))[0]
    assert dist.name == 'mercurial'
    assert len(list(dir.get_file_users('mercurial/filelog.py'))) == 2

def test_get_file_users():
    users = list(get_file_users('mercurial/filelog.py'))
    assert_equals(len(users), 2)

def test_egginfo_dirname():
    assert egginfo_dirname('zlib', '2.5.2') == 'zlib-2.5.2.egg-info'
    assert egginfo_dirname('python-ldap', '2.5'), 'python_ldap-2.5.egg-info'
    assert egginfo_dirname('python-ldap', '2.5 a---5'), 'python_ldap-2.5.a_5.egg-info'


def test_zipped_directory():
    dir = ZippedDistributionDirectory(SITE_PKG+'.zip')
    dist = list(dir.get_file_users('mercurial/filelog.pyc'))[0]
    assert dist.name == 'mercurial'
    assert len(list(dir.get_file_users('mercurial/filelog.py'))) == 2

def setup_zip():
    sys.old = sys.path
    sys.path = [SITE_PKG+'.zip']
    pkgutil.purge_cache()
    pkgutil._dist_dirs = DistributionDirectories()
    pkgutil._dist_dirs.load(SITE_PKG+'.zip')

@with_setup(setup_zip, teardown)
def test_zipped_distribution():
    assert_equals(get_distribution('xxx'), None)

    project = get_distribution('mercurial')
    assert_equals(project.name, 'mercurial')

    project = get_distribution('processing')
    assert_equals(project.name, 'processing')

    dist = get_distribution('mercurial')
    assert_equals(str(dist), "Distribution('mercurial')")

    files = list(dist.get_installed_files())
    assert_equals(len(files), 4)
    assert_equals(files[0], ('mercurial/filelog.py', '98676876876876', '12'))

    files = list(dist.get_installed_files(local=True))
    location = os.path.join(dist.container, 'mercurial', 'filelog.py')
    assert_equals(files[0], (location, '98676876876876', '12'))

    f = dist.get_egginfo_file('RECORD')
    record = os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info', 'RECORD')
    assert_equals(open(record).read(), f.read())

    assert dist.uses('mercurial/filelog.py')
    assert not dist.uses('mercurial/filelog.sasasasa')

    assert_equals(list(dist.get_egginfo_files()),
                  ['mercurial-1.0.1.egg-info/PKG_INFO',
                   'mercurial-1.0.1.egg-info/RECORD'])

    assert_equals(list(dist.get_egginfo_files(local=True)),
                  [os.path.join(SITE_PKG+'.zip',
                                'mercurial-1.0.1.egg-info/PKG_INFO'),
                   os.path.join(SITE_PKG+'.zip',
                                'mercurial-1.0.1.egg-info/RECORD')])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.