Rename packages.py to repository.py, PackageFinder to RepoPath.

This commit is contained in:
Todd Gamblin 2015-11-25 09:58:10 -08:00
parent 8c06b92225
commit e6d232bfef
9 changed files with 25 additions and 128 deletions

View File

@ -54,11 +54,11 @@
#
# Set up the default packages database.
#
import spack.packages
import spack.repository
_repo_paths = spack.config.get_repos_config()
if not _repo_paths:
tty.die("Spack configuration contains no package repositories.")
db = spack.packages.PackageFinder(*_repo_paths)
db = spack.repository.RepoPath(*_repo_paths)
sys.meta_path.append(db)
#

View File

@ -32,7 +32,7 @@
import spack.spec
import spack.config
from spack.util.environment import get_path
from spack.packages import repo_config_filename
from spack.repository import repo_config_filename
import os
import exceptions

View File

@ -30,7 +30,7 @@
import spack
import spack.cmd
import spack.packages
import spack.repository
from spack.cmd.find import display_specs
from spack.package import PackageStillNeededError
@ -80,7 +80,7 @@ def uninstall(parser, args):
# should work if package is known to spack
pkgs.append(s.package)
except spack.packages.UnknownPackageError, e:
except spack.repository.UnknownPackageError, e:
# The package.py file has gone away -- but still want to uninstall.
spack.Package(s).do_uninstall(force=True)

View File

@ -1,110 +0,0 @@
import re
import sys
import types
import traceback
from llnl.util.lang import *
import spack
# Name of module under which packages are imported
imported_packages_module = 'spack.repos'
# Name of the package file inside a package directory
package_file_name = 'package.py'
class LazyLoader:
"""The LazyLoader handles cases when repo modules or classes
are imported. It watches for 'spack.repos.*' loads, then
redirects the load to the appropriate module."""
def find_module(self, fullname, pathname):
if not fullname.startswith(imported_packages_module):
return None
partial_name = fullname[len(imported_packages_module)+1:]
print "partial: ", partial_name
print
last_dot = partial_name.rfind('.')
repo = partial_name[:last_dot]
module = partial_name[last_dot+1:]
repo_loader = spack.db.repo_loaders.get(repo)
if repo_loader:
try:
self.mod = repo_loader.get_module(module)
return self
except (ImportError, spack.packages.UnknownPackageError):
return None
def load_module(self, fullname):
return self.mod
#sys.meta_path.append(LazyLoader())
_reponames = {}
class RepoNamespace(types.ModuleType):
"""The RepoNamespace holds the repository namespaces under
spack.repos. For example, when accessing spack.repos.original
this class will use __getattr__ to translate the 'original'
into one of spack's known repositories"""
def __init__(self):
sys.modules[imported_packages_module] = self
def __getattr__(self, name):
if name in _reponames:
return _reponames[name]
raise AttributeError
@property
def __file__(self):
return None
@property
def __path__(self):
return []
class RepoLoader(types.ModuleType):
"""Each RepoLoader is associated with a repository, and the RepoLoader is
responsible for loading packages out of that repository. For example,
a RepoLoader may be responsible for spack.repos.original, and when someone
references spack.repos.original.libelf that RepoLoader will load the
libelf package."""
def __init__(self, reponame, repopath):
self.path = repopath
self.reponame = reponame
self.module_name = imported_packages_module + '.' + reponame
if not reponame in _reponames:
_reponames[reponame] = self
sys.modules[self.module_name] = self
@property
def __path__(self):
return [ self.path ]
def __getattr__(self, name):
if name[0] == '_':
raise AttributeError
return self.get_module(name)
@memoized
def get_module(self, pkg_name):
import os
import imp
import llnl.util.tty as tty
try:
module_name = imported_packages_module + '.' + self.reponame + '.' + pkg_name
module = imp.load_source(module_name, file_path)
except ImportError, e:
tty.die("Error while importing %s from %s:\n%s" % (
pkg_name, file_path, e.message))
return module

View File

@ -67,8 +67,8 @@ def _make_namespace_module(ns):
return module
class PackageFinder(object):
"""A PackageFinder is a wrapper around a list of Repos.
class RepoPath(object):
"""A RepoPath is a list of repos that function as one.
It functions exactly like a Repo, but it operates on the
combined results of the Repos in its list instead of on a
@ -83,7 +83,10 @@ def __init__(self, *repo_dirs):
self._provider_index = None
for root in repo_dirs:
repo = Repo(root)
# Try to make it a repo if it's not one.
if not isinstance(root, Repo):
repo = Repo(root)
# Add the repo to the path.
self.put_last(repo)
@ -94,7 +97,11 @@ def swap(self, other):
TODO: Maybe there is a cleaner way.
"""
attrs = ['repos', 'by_namespace', 'by_path', '_all_package_names', '_provider_index']
attrs = ['repos',
'by_namespace',
'by_path',
'_all_package_names',
'_provider_index']
for attr in attrs:
tmp = getattr(self, attr)
setattr(self, attr, getattr(other, attr))
@ -185,7 +192,7 @@ def find_module(self, fullname, path=None):
return repo
# No repo provides the namespace, but it is a valid prefix of
# something in the PackageFinder.
# something in the RepoPath.
if self.by_namespace.is_prefix(fullname):
return self
@ -603,7 +610,7 @@ def __init__(self, name, repo=None):
class DuplicateRepoError(spack.error.SpackError):
"""Raised when duplicate repos are added to a PackageFinder."""
"""Raised when duplicate repos are added to a RepoPath."""
def __init__(self, msg, repo1, repo2):
super(UnknownPackageError, self).__init__(
"%s: %s, %s" % (msg, repo1, repo2))

View File

@ -34,7 +34,7 @@
import spack
from spack.spec import Spec
from spack.packages import PackageFinder
from spack.repository import RepoPath
from spack.directory_layout import YamlDirectoryLayout
# number of packages to test (to reduce test time)
@ -123,7 +123,7 @@ def test_handle_unknown_package(self):
information about installed packages' specs to uninstall
or query them again if the package goes away.
"""
mock_db = PackageFinder(spack.mock_packages_path)
mock_db = RepoPath(spack.mock_packages_path)
not_in_mock = set.difference(
set(spack.db.all_package_names()),

View File

@ -27,7 +27,7 @@
import spack
import spack.config
from spack.packages import PackageFinder
from spack.repository import RepoPath
from spack.spec import Spec
@ -36,7 +36,7 @@ def initmock(self):
# Use the mock packages database for these tests. This allows
# us to set up contrived packages that don't interfere with
# real ones.
self.db = PackageFinder(spack.mock_packages_path)
self.db = RepoPath(spack.mock_packages_path)
spack.db.swap(self.db)
spack.config.clear_config_caches()

View File

@ -28,7 +28,7 @@
import unittest
import spack
from spack.packages import PackageFinder
from spack.repository import RepoPath
class PackageSanityTest(unittest.TestCase):
@ -46,7 +46,7 @@ def test_get_all_packages(self):
def ztest_get_all_mock_packages(self):
"""Get the mock packages once each too."""
db = PackageFinder(spack.mock_packages_path)
db = RepoPath(spack.mock_packages_path)
spack.db.swap(db)
self.check_db()
spack.db.swap(db)

View File

@ -27,7 +27,7 @@
from llnl.util.filesystem import join_path
import spack
from spack.packages import Repo
from spack.repository import Repo
from spack.util.naming import mod_to_class
from spack.test.mock_packages_test import *