Rework mirror configuration.

- All of these work:
  - `spack mirror add`
  - `spack mirror remove`
  - `spack mirror list`

- `spack mirror` subcommands (except create) now have their own
  --scope argument.

- Mirror config is now stored sanely as an ordered list.
This commit is contained in:
Todd Gamblin 2015-12-28 01:14:41 -08:00
parent 1f8ba53ca7
commit e8e6368cc8
2 changed files with 44 additions and 12 deletions

View File

@ -60,15 +60,22 @@ def setup_parser(subparser):
'-o', '--one-version-per-spec', action='store_const', const=1, default=0, '-o', '--one-version-per-spec', action='store_const', const=1, default=0,
help="Only fetch one 'preferred' version per spec, not all known versions.") help="Only fetch one 'preferred' version per spec, not all known versions.")
add_parser = sp.add_parser('add', help=mirror_add.__doc__) add_parser = sp.add_parser('add', help=mirror_add.__doc__)
add_parser.add_argument('name', help="Mnemonic name for mirror.") add_parser.add_argument('name', help="Mnemonic name for mirror.")
add_parser.add_argument( add_parser.add_argument(
'url', help="URL of mirror directory created by 'spack mirror create'.") 'url', help="URL of mirror directory created by 'spack mirror create'.")
add_parser.add_argument('--scope', choices=spack.config.config_scopes,
help="Configuration scope to modify.")
remove_parser = sp.add_parser('remove', help=mirror_remove.__doc__) remove_parser = sp.add_parser('remove', help=mirror_remove.__doc__)
remove_parser.add_argument('name') remove_parser.add_argument('name')
remove_parser.add_argument('--scope', choices=spack.config.config_scopes,
help="Configuration scope to modify.")
list_parser = sp.add_parser('list', help=mirror_list.__doc__) list_parser = sp.add_parser('list', help=mirror_list.__doc__)
list_parser.add_argument('--scope', choices=spack.config.config_scopes,
help="Configuration scope to read from.")
def mirror_add(args): def mirror_add(args):
@ -77,31 +84,54 @@ def mirror_add(args):
if url.startswith('/'): if url.startswith('/'):
url = 'file://' + url url = 'file://' + url
mirror_dict = { args.name : url } mirrors = spack.config.get_config('mirrors', scope=args.scope)
spack.config.update_config('mirrors', { args.name : url }, 'user') if not mirrors:
mirrors = []
for m in mirrors:
for name, u in m.items():
if name == args.name:
tty.die("Mirror with name %s already exists." % name)
if u == url:
tty.die("Mirror with url %s already exists." % url)
# should only be one item per mirror dict.
mirrors.insert(0, { args.name : url })
spack.config.update_config('mirrors', mirrors, scope=args.scope)
def mirror_remove(args): def mirror_remove(args):
"""Remove a mirror by name.""" """Remove a mirror by name."""
name = args.name name = args.name
rmd_something = spack.config.remove_from_config('mirrors', name) mirrors = spack.config.get_config('mirrors', scope=args.scope)
if not rmd_something: if not mirrors:
tty.die("No such mirror: %s" % name) mirrors = []
names = [n for m in mirrors for n,u in m.items()]
if not name in names:
tty.die("No mirror with name %s" % name)
old_mirror = mirrors.pop(names.index(name))
spack.config.update_config('mirrors', mirrors, scope=args.scope)
tty.msg("Removed mirror %s with url %s." % old_mirror.popitem())
def mirror_list(args): def mirror_list(args):
"""Print out available mirrors to the console.""" """Print out available mirrors to the console."""
sec_names = spack.config.get_config('mirrors') mirrors = spack.config.get_config('mirrors', scope=args.scope)
if not sec_names: if not mirrors:
tty.msg("No mirrors configured.") tty.msg("No mirrors configured.")
return return
max_len = max(len(s) for s in sec_names) names = [n for m in mirrors for n,u in m.items()]
max_len = max(len(n) for n in names)
fmt = "%%-%ds%%s" % (max_len + 4) fmt = "%%-%ds%%s" % (max_len + 4)
for name, val in sec_names.iteritems(): for m in mirrors:
print fmt % (name, val) for name, url in m.items():
print fmt % (name, url)
# should only be one item per mirror dict.
def _read_specs_from_file(filename): def _read_specs_from_file(filename):

View File

@ -26,6 +26,7 @@
import re import re
import shutil import shutil
import tempfile import tempfile
from urlparse import urljoin
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.filesystem import * from llnl.util.filesystem import *
@ -242,8 +243,9 @@ def fetch(self):
# TODO: move mirror logic out of here and clean it up! # TODO: move mirror logic out of here and clean it up!
if self.mirror_path: if self.mirror_path:
urls = ["%s/%s" % (u, self.mirror_path) mirrors = spack.config.get_config('mirrors')
for name, u in spack.config.get_config('mirrors')] mirrors = [(n,u) for m in mirrors for n,u in m.items()]
urls = [urljoin(u, self.mirror_path) for name, u in mirrors]
digest = None digest = None
if isinstance(self.fetcher, fs.URLFetchStrategy): if isinstance(self.fetcher, fs.URLFetchStrategy):