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

View File

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

View File

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

View File

@ -25,14 +25,14 @@
from spack.spec import Spec 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()) p = ProviderIndex(spack.repo.all_package_names())
ostream = StringIO() ostream = StringIO()
p.to_yaml(ostream) p.to_json(ostream)
istream = StringIO(ostream.getvalue()) istream = StringIO(ostream.getvalue())
q = ProviderIndex.from_yaml(istream) q = ProviderIndex.from_json(istream)
assert p == q assert p == q