From a075d581eff5e07cd4b703cbcd1d1e5f490b1fb4 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Tue, 1 Dec 2015 12:56:46 +0100 Subject: resource : fetch strategy constructed from kwargs instead or hardcoded URLFetchStrategy --- lib/spack/spack/directives.py | 5 ++--- lib/spack/spack/fetch_strategy.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/directives.py b/lib/spack/spack/directives.py index 4d8333fd7d..741df3a31b 100644 --- a/lib/spack/spack/directives.py +++ b/lib/spack/spack/directives.py @@ -62,7 +62,7 @@ from spack.patch import Patch 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)) diff --git a/lib/spack/spack/fetch_strategy.py b/lib/spack/spack/fetch_strategy.py index 5e6850d14b..1f5ef16caa 100644 --- a/lib/spack/spack/fetch_strategy.py +++ b/lib/spack/spack/fetch_strategy.py @@ -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) -- cgit v1.2.3-60-g2f50