ProviderIndex uses json instead of YAML

- indexes should use json, not YAML, to optimize for speed
- only use YAML in human-editable files
- this makes ProviderIndex consistent with other indexes
This commit is contained in:
Todd Gamblin 2018-12-24 12:48:27 -08:00
parent c1d7adaaac
commit a9b69fa902
4 changed files with 18 additions and 21 deletions

View File

@ -6,13 +6,13 @@
"""
The ``virtual`` module contains utility classes for virtual dependencies.
"""
from itertools import product as iproduct
from six import iteritems
from pprint import pformat
import spack.error
import spack.util.spack_yaml as syaml
from ruamel.yaml.error import MarkedYAMLError
import spack.util.spack_json as sjson
class ProviderIndex(object):
@ -169,31 +169,26 @@ def satisfies(self, other):
return all(c in result for c in common)
def to_yaml(self, stream=None):
def to_json(self, stream=None):
provider_list = self._transform(
lambda vpkg, pset: [
vpkg.to_node_dict(), [p.to_node_dict() for p in pset]], list)
syaml.dump({'provider_index': {'providers': provider_list}},
stream=stream)
sjson.dump({'provider_index': {'providers': provider_list}}, stream)
@staticmethod
def from_yaml(stream):
try:
yfile = syaml.load(stream)
except MarkedYAMLError as e:
raise spack.spec.SpackYAMLError(
"error parsing YAML ProviderIndex cache:", str(e))
def from_json(stream):
data = sjson.load(stream)
if not isinstance(yfile, dict):
raise ProviderIndexError("YAML ProviderIndex was not a dict.")
if not isinstance(data, dict):
raise ProviderIndexError("JSON ProviderIndex data was not a dict.")
if 'provider_index' not in yfile:
if 'provider_index' not in data:
raise ProviderIndexError(
"YAML ProviderIndex does not start with 'provider_index'")
index = ProviderIndex()
providers = yfile['provider_index']['providers']
providers = data['provider_index']['providers']
index.providers = _transform(
providers,
lambda vpkg, plist: (

View File

@ -276,14 +276,14 @@ def _create(self):
return ProviderIndex()
def read(self, stream):
self.index = ProviderIndex.from_yaml(stream)
self.index = ProviderIndex.from_json(stream)
def update(self, pkg_fullname):
self.index.remove_provider(pkg_fullname)
self.index.update(pkg_fullname)
def write(self, stream):
self.index.to_yaml(stream)
self.index.to_json(stream)
class RepoIndex(object):

View File

@ -22,11 +22,13 @@ def test_it_just_runs(pkg):
@pytest.mark.parametrize('vpkg,provider_list', [
(('mpi',), ['intel-mpi',
(('mpi',), ['charmpp@6.7.1:',
'intel-mpi',
'intel-parallel-studio',
'mpich',
'mpich@1:',
'mpich@3:',
'mpilander',
'mvapich2',
'openmpi',
'openmpi@1.6.5',

View File

@ -25,14 +25,14 @@
from spack.spec import Spec
def test_yaml_round_trip(mock_packages):
def test_provider_index_round_trip(mock_packages):
p = ProviderIndex(spack.repo.all_package_names())
ostream = StringIO()
p.to_yaml(ostream)
p.to_json(ostream)
istream = StringIO(ostream.getvalue())
q = ProviderIndex.from_yaml(istream)
q = ProviderIndex.from_json(istream)
assert p == q