Skip to content

Commit f19daa8

Browse files
committed
Extract different classes to load specs, according to the format
1 parent c4ccd49 commit f19daa8

11 files changed

Lines changed: 13671 additions & 219 deletions

File tree

lib/spack/spack/database.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
# DB version. This is stuck in the DB file to track changes in format.
6464
# Increment by one when the database format changes.
6565
# Versions before 5 were not integers.
66-
_db_version = Version("6")
66+
_db_version = Version("7")
6767

6868
# For any version combinations here, skip reindex when upgrading.
6969
# Reindexing can take considerable time and is not always necessary.
@@ -75,6 +75,7 @@
7575
# version is saved to disk the first time the DB is written.
7676
(Version("0.9.3"), Version("5")),
7777
(Version("5"), Version("6")),
78+
(Version("6"), Version("7")),
7879
]
7980

8081
# Default timeout for spack database locks in seconds or None (no timeout).
@@ -107,6 +108,15 @@
107108
]
108109

109110

111+
def reader(version):
112+
reader_cls = {
113+
Version("5"): spack.spec.SpecfileV1,
114+
Version("6"): spack.spec.SpecfileV3,
115+
Version("7"): spack.spec.SpecfileV4,
116+
}
117+
return reader_cls[version]
118+
119+
110120
def _now():
111121
"""Returns the time since the epoch"""
112122
return time.time()
@@ -674,7 +684,7 @@ def _write_to_file(self, stream):
674684
except (TypeError, ValueError) as e:
675685
raise sjson.SpackJSONError("error writing JSON database:", str(e))
676686

677-
def _read_spec_from_dict(self, hash_key, installs, hash=ht.dag_hash):
687+
def _read_spec_from_dict(self, spec_reader, hash_key, installs, hash=ht.dag_hash):
678688
"""Recursively construct a spec from a hash in a YAML database.
679689
680690
Does not do any locking.
@@ -692,7 +702,7 @@ def _read_spec_from_dict(self, hash_key, installs, hash=ht.dag_hash):
692702
spec_dict[hash.name] = hash_key
693703

694704
# Build spec from dict first.
695-
spec = spack.spec.Spec.from_node_dict(spec_dict)
705+
spec = spec_reader.from_node_dict(spec_dict)
696706
return spec
697707

698708
def db_for_spec_hash(self, hash_key):
@@ -732,7 +742,7 @@ def query_local_by_spec_hash(self, hash_key):
732742
with self.read_transaction():
733743
return self._data.get(hash_key, None)
734744

735-
def _assign_dependencies(self, hash_key, installs, data):
745+
def _assign_dependencies(self, spec_reader, hash_key, installs, data):
736746
# Add dependencies from other records in the install DB to
737747
# form a full spec.
738748
spec = data[hash_key].spec
@@ -742,7 +752,7 @@ def _assign_dependencies(self, hash_key, installs, data):
742752
spec_node_dict = spec_node_dict[spec.name]
743753
if "dependencies" in spec_node_dict:
744754
yaml_deps = spec_node_dict["dependencies"]
745-
for dname, dhash, dtypes, _, virtuals in spack.spec.Spec.read_yaml_dep_specs(
755+
for dname, dhash, dtypes, _, virtuals in spec_reader.read_specfile_dep_specs(
746756
yaml_deps
747757
):
748758
# It is important that we always check upstream installations
@@ -799,6 +809,7 @@ def check(cond, msg):
799809

800810
# TODO: better version checking semantics.
801811
version = Version(db["version"])
812+
spec_reader = reader(version)
802813
if version > _db_version:
803814
raise InvalidDatabaseVersionError(_db_version, version)
804815
elif version < _db_version:
@@ -834,7 +845,7 @@ def invalid_record(hash_key, error):
834845
for hash_key, rec in installs.items():
835846
try:
836847
# This constructs a spec DAG from the list of all installs
837-
spec = self._read_spec_from_dict(hash_key, installs)
848+
spec = self._read_spec_from_dict(spec_reader, hash_key, installs)
838849

839850
# Insert the brand new spec in the database. Each
840851
# spec has its own copies of its dependency specs.
@@ -850,7 +861,7 @@ def invalid_record(hash_key, error):
850861
# Pass 2: Assign dependencies once all specs are created.
851862
for hash_key in data:
852863
try:
853-
self._assign_dependencies(hash_key, installs, data)
864+
self._assign_dependencies(spec_reader, hash_key, installs, data)
854865
except MissingDependenciesError:
855866
raise
856867
except Exception as e:

lib/spack/spack/environment/environment.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,17 @@ def default_manifest_yaml():
102102
valid_environment_name_re = r"^\w[\w-]*$"
103103

104104
#: version of the lockfile format. Must increase monotonically.
105-
lockfile_format_version = 4
105+
lockfile_format_version = 5
106+
107+
108+
READER_CLS = {
109+
1: spack.spec.SpecfileV1,
110+
2: spack.spec.SpecfileV1,
111+
3: spack.spec.SpecfileV2,
112+
4: spack.spec.SpecfileV3,
113+
5: spack.spec.SpecfileV4,
114+
}
115+
106116

107117
# Magic names
108118
# The name of the standalone spec list in the manifest yaml
@@ -1974,10 +1984,11 @@ def _read_lockfile_dict(self, d):
19741984

19751985
# Track specs by their DAG hash, allows handling DAG hash collisions
19761986
first_seen = {}
1977-
1987+
current_lockfile_format = d["_meta"]["lockfile-version"]
1988+
reader = READER_CLS[current_lockfile_format]
19781989
# First pass: Put each spec in the map ignoring dependencies
19791990
for lockfile_key, node_dict in json_specs_by_hash.items():
1980-
spec = Spec.from_node_dict(node_dict)
1991+
spec = reader.from_node_dict(node_dict)
19811992
if not spec._hash:
19821993
# in v1 lockfiles, the hash only occurs as a key
19831994
spec._hash = lockfile_key
@@ -1986,7 +1997,8 @@ def _read_lockfile_dict(self, d):
19861997
# Second pass: For each spec, get its dependencies from the node dict
19871998
# and add them to the spec
19881999
for lockfile_key, node_dict in json_specs_by_hash.items():
1989-
for _, dep_hash, deptypes, _, virtuals in Spec.dependencies_from_node_dict(node_dict):
2000+
name, data = reader.name_and_data(node_dict)
2001+
for _, dep_hash, deptypes, _, virtuals in reader.dependencies_from_node_dict(data):
19902002
specs_by_hash[lockfile_key]._add_dependency(
19912003
specs_by_hash[dep_hash], deptypes=deptypes, virtuals=virtuals
19922004
)

lib/spack/spack/provider_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ def from_json(stream, repository):
292292
index.providers = _transform(
293293
providers,
294294
lambda vpkg, plist: (
295-
spack.spec.Spec.from_node_dict(vpkg),
296-
set(spack.spec.Spec.from_node_dict(p) for p in plist),
295+
spack.spec.SpecfileV4.from_node_dict(vpkg),
296+
set(spack.spec.SpecfileV4.from_node_dict(p) for p in plist),
297297
),
298298
)
299299
return index

0 commit comments

Comments
 (0)