resource : fetch strategy constructed from kwargs instead or hardcoded URLFetchStrategy

This commit is contained in:
Massimiliano Culpo 2015-12-01 12:56:46 +01:00
parent a173ab1e31
commit a075d581ef
2 changed files with 18 additions and 3 deletions

View file

@ -62,7 +62,7 @@ class OpenMpi(Package):
from spack.variant import Variant
from spack.spec import Spec, parse_anonymous_spec
from spack.resource import Resource
from spack.fetch_strategy import URLFetchStrategy
from spack.fetch_strategy import from_kwargs
#
# This is a list of all directives, built up as they are defined in
@ -279,8 +279,7 @@ def resource(pkg, **kwargs):
destination = kwargs.get('destination', "")
when_spec = parse_anonymous_spec(when, pkg.name)
resources = pkg.resources.setdefault(when_spec, [])
# FIXME : change URLFetchStrategy with a factory that selects based on kwargs
fetcher = URLFetchStrategy(**kwargs)
fetcher = from_kwargs(**kwargs)
# FIXME : should we infer the name somehow if not passed ?
name = kwargs.get('name')
resources.append(Resource(name, fetcher, destination))

View file

@ -634,6 +634,22 @@ def from_url(url):
return URLFetchStrategy(url)
def from_kwargs(**kwargs):
"""
Construct the appropriate FetchStrategy from the given keyword arguments.
:param kwargs: dictionary of keyword arguments
:return: fetcher or raise a FetchError exception
"""
for fetcher in all_strategies:
if fetcher.matches(kwargs):
return fetcher(**kwargs)
# Raise an error in case we can't instantiate any known strategy
message = "Cannot instantiate any FetchStrategy"
long_message = message + " from the given arguments : {arguments}".format(srguments=kwargs)
raise FetchError(message, long_message)
def args_are_for(args, fetcher):
fetcher.matches(args)