spack.repo: remove "import from" statements (#15505)

spack.repo: remove "import from" statements
This commit is contained in:
Massimiliano Culpo 2020-03-24 22:26:35 +01:00 committed by GitHub
parent 862e13183d
commit 1a5e4232ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 39 deletions

View File

@ -8,10 +8,9 @@
import os
import llnl.util.tty as tty
import spack.spec
import spack.config
from spack.repo import Repo, create_repo, canonicalize_path, RepoError
import spack.repo
import spack.util.path
description = "manage package source repositories"
section = "config"
@ -61,7 +60,9 @@ def setup_parser(subparser):
def repo_create(args):
"""Create a new package repository."""
full_path, namespace = create_repo(args.directory, args.namespace)
full_path, namespace = spack.repo.create_repo(
args.directory, args.namespace
)
tty.msg("Created repo with namespace '%s'." % namespace)
tty.msg("To register it with spack, run this command:",
'spack repo add %s' % full_path)
@ -72,7 +73,7 @@ def repo_add(args):
path = args.path
# real_path is absolute and handles substitution.
canon_path = canonicalize_path(path)
canon_path = spack.util.path.canonicalize_path(path)
# check if the path exists
if not os.path.exists(canon_path):
@ -83,7 +84,7 @@ def repo_add(args):
tty.die("Not a Spack repository: %s" % path)
# Make sure it's actually a spack repository by constructing it.
repo = Repo(canon_path)
repo = spack.repo.Repo(canon_path)
# If that succeeds, finally add it to the configuration.
repos = spack.config.get('repos', scope=args.scope)
@ -104,9 +105,9 @@ def repo_remove(args):
namespace_or_path = args.namespace_or_path
# If the argument is a path, remove that repository from config.
canon_path = canonicalize_path(namespace_or_path)
canon_path = spack.util.path.canonicalize_path(namespace_or_path)
for repo_path in repos:
repo_canon_path = canonicalize_path(repo_path)
repo_canon_path = spack.util.path.canonicalize_path(repo_path)
if canon_path == repo_canon_path:
repos.remove(repo_path)
spack.config.set('repos', repos, args.scope)
@ -116,14 +117,14 @@ def repo_remove(args):
# If it is a namespace, remove corresponding repo
for path in repos:
try:
repo = Repo(path)
repo = spack.repo.Repo(path)
if repo.namespace == namespace_or_path:
repos.remove(path)
spack.config.set('repos', repos, args.scope)
tty.msg("Removed repository %s with namespace '%s'."
% (repo.root, repo.namespace))
return
except RepoError:
except spack.repo.RepoError:
continue
tty.die("No repository with path or namespace: %s"
@ -136,8 +137,8 @@ def repo_list(args):
repos = []
for r in roots:
try:
repos.append(Repo(r))
except RepoError:
repos.append(spack.repo.Repo(r))
except spack.repo.RepoError:
continue
msg = "%d package repositor" % len(repos)

View File

@ -16,22 +16,20 @@
import stat
import sys
import traceback
from six import string_types, add_metaclass
import types
try:
from collections.abc import Mapping # novm
except ImportError:
from collections import Mapping
from types import ModuleType
import six
import ruamel.yaml as yaml
import llnl.util.lang
import llnl.util.tty as tty
from llnl.util.filesystem import mkdirp, install
import llnl.util.filesystem as fs
import spack.config
import spack.caches
import spack.error
@ -39,11 +37,9 @@
import spack.spec
import spack.util.spack_json as sjson
import spack.util.imp as simp
from spack.provider_index import ProviderIndex
from spack.util.path import canonicalize_path
from spack.util.naming import NamespaceTrie, valid_module_name
from spack.util.naming import mod_to_class, possible_spack_module_names
import spack.provider_index
import spack.util.path
import spack.util.naming as nm
#: Super-namespace for all packages.
#: Package modules are imported as spack.pkg.<namespace>.<pkg-name>.
@ -95,7 +91,7 @@ def converter(self, spec_like, *args, **kwargs):
return converter
class SpackNamespace(ModuleType):
class SpackNamespace(types.ModuleType):
""" Allow lazy loading of modules."""
def __init__(self, namespace):
@ -151,7 +147,7 @@ def _create_new_cache(self):
pkg_dir = os.path.join(self.packages_path, pkg_name)
# Warn about invalid names that look like packages.
if not valid_module_name(pkg_name):
if not nm.valid_module_name(pkg_name):
if not pkg_name.startswith('.'):
tty.warn('Skipping package at {0}. "{1}" is not '
'a valid Spack module name.'.format(
@ -247,7 +243,7 @@ def update_package(self, pkg_name):
self._tag_dict[tag].append(package.name)
@add_metaclass(abc.ABCMeta)
@six.add_metaclass(abc.ABCMeta)
class Indexer(object):
"""Adaptor for indexes that need to be generated when repos are updated."""
@ -305,10 +301,10 @@ def write(self, stream):
class ProviderIndexer(Indexer):
"""Lifecycle methods for virtual package providers."""
def _create(self):
return ProviderIndex()
return spack.provider_index.ProviderIndex()
def read(self, stream):
self.index = ProviderIndex.from_json(stream)
self.index = spack.provider_index.ProviderIndex.from_json(stream)
def update(self, pkg_fullname):
self.index.remove_provider(pkg_fullname)
@ -447,7 +443,7 @@ class RepoPath(object):
def __init__(self, *repos):
self.repos = []
self.by_namespace = NamespaceTrie()
self.by_namespace = nm.NamespaceTrie()
self._all_package_names = None
self._provider_index = None
@ -456,7 +452,7 @@ def __init__(self, *repos):
# Add each repo to this path.
for repo in repos:
try:
if isinstance(repo, string_types):
if isinstance(repo, six.string_types):
repo = Repo(repo)
self.put_last(repo)
except RepoError as e:
@ -544,7 +540,7 @@ def all_packages(self):
def provider_index(self):
"""Merged ProviderIndex from all Repos in the RepoPath."""
if self._provider_index is None:
self._provider_index = ProviderIndex()
self._provider_index = spack.provider_index.ProviderIndex()
for repo in reversed(self.repos):
self._provider_index.merge(repo.provider_index)
@ -707,7 +703,7 @@ def __init__(self, root):
"""
# Root directory, containing _repo.yaml and package dirs
# Allow roots to by spack-relative by starting with '$spack'
self.root = canonicalize_path(root)
self.root = spack.util.path.canonicalize_path(root)
# check and raise BadRepoError on fail.
def check(condition, msg):
@ -803,7 +799,7 @@ def real_name(self, import_name):
if import_name in self:
return import_name
options = possible_spack_module_names(import_name)
options = nm.possible_spack_module_names(import_name)
options.remove(import_name)
for name in options:
if name in self:
@ -921,18 +917,18 @@ def dump_provenance(self, spec, path):
% (self.namespace, spec.fullname))
# Install patch files needed by the package.
mkdirp(path)
fs.mkdirp(path)
for patch in itertools.chain.from_iterable(
spec.package.patches.values()):
if patch.path:
if os.path.exists(patch.path):
install(patch.path, path)
fs.install(patch.path, path)
else:
tty.warn("Patch file did not exist: %s" % patch.path)
# Install the package.py file itself.
install(self.filename_for_package_name(spec.name), path)
fs.install(self.filename_for_package_name(spec.name), path)
def purge(self):
"""Clear entire package instance cache."""
@ -1082,7 +1078,7 @@ def get_pkg_class(self, pkg_name):
raise InvalidNamespaceError('Invalid namespace for %s repo: %s'
% (self.namespace, namespace))
class_name = mod_to_class(pkg_name)
class_name = nm.mod_to_class(pkg_name)
module = self._get_pkg_module(pkg_name)
cls = getattr(module, class_name)
@ -1107,7 +1103,7 @@ def create_repo(root, namespace=None):
If the namespace is not provided, use basename of root.
Return the canonicalized path and namespace of the created repository.
"""
root = canonicalize_path(root)
root = spack.util.path.canonicalize_path(root)
if not namespace:
namespace = os.path.basename(root)
@ -1141,7 +1137,7 @@ def create_repo(root, namespace=None):
config_path = os.path.join(root, repo_config_name)
packages_path = os.path.join(root, packages_dir_name)
mkdirp(packages_path)
fs.mkdirp(packages_path)
with open(config_path, 'w') as config:
config.write("repo:\n")
config.write(" namespace: '%s'\n" % namespace)
@ -1163,7 +1159,7 @@ def create_repo(root, namespace=None):
def create_or_construct(path, namespace=None):
"""Create a repository, or just return a Repo if it already exists."""
if not os.path.exists(path):
mkdirp(path)
fs.mkdirp(path)
create_repo(path, namespace)
return Repo(path)

View File

@ -0,0 +1,35 @@
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import os.path
import pytest
import spack.main
repo = spack.main.SpackCommand('repo')
def test_help_option():
# Test 'spack repo --help' to check basic import works
# and the command exits successfully
with pytest.raises(SystemExit):
repo('--help')
assert repo.returncode in (None, 0)
def test_create_add_list_remove(mutable_config, tmpdir):
# Create a new repository and check that the expected
# files are there
repo('create', str(tmpdir), 'mockrepo')
assert os.path.exists(os.path.join(str(tmpdir), 'repo.yaml'))
# Add the new repository and check it appears in the list output
repo('add', '--scope=site', str(tmpdir))
output = repo('list', '--scope=site', output=str)
assert 'mockrepo' in output
# Then remove it and check it's not there
repo('remove', '--scope=site', str(tmpdir))
output = repo('list', '--scope=site', output=str)
assert 'mockrepo' not in output