Skip to content

Commit 0ec00a9

Browse files
authored
rewiring.py: eliminate code duplication from bindist (spack#48723)
1 parent 5e3020a commit 0ec00a9

File tree

2 files changed

+22
-72
lines changed

2 files changed

+22
-72
lines changed

lib/spack/spack/binary_distribution.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import codecs
66
import collections
77
import concurrent.futures
8+
import contextlib
89
import copy
910
import hashlib
1011
import io
@@ -2270,6 +2271,24 @@ def relocate_package(spec: spack.spec.Spec) -> None:
22702271
with fsys.edit_in_place_through_temporary_file(binary) as tmp_binary:
22712272
codesign("-fs-", tmp_binary)
22722273

2274+
install_manifest = os.path.join(
2275+
spec.prefix,
2276+
spack.store.STORE.layout.metadata_dir,
2277+
spack.store.STORE.layout.manifest_file_name,
2278+
)
2279+
if not os.path.exists(install_manifest):
2280+
spec_id = spec.format("{name}/{hash:7}")
2281+
tty.warn("No manifest file in tarball for spec %s" % spec_id)
2282+
2283+
# overwrite old metadata with new
2284+
if spec.spliced:
2285+
# rewrite spec on disk
2286+
spack.store.STORE.layout.write_spec(spec, spack.store.STORE.layout.spec_file_path(spec))
2287+
2288+
# de-cache the install manifest
2289+
with contextlib.suppress(FileNotFoundError):
2290+
os.unlink(install_manifest)
2291+
22732292

22742293
def _extract_inner_tarball(spec, filename, extract_to, signature_required: bool, remote_checksum):
22752294
stagepath = os.path.dirname(filename)
@@ -2436,15 +2455,6 @@ def extract_tarball(spec, download_result, force=False, timer=timer.NULL_TIMER):
24362455
except Exception as e:
24372456
shutil.rmtree(spec.prefix, ignore_errors=True)
24382457
raise e
2439-
else:
2440-
manifest_file = os.path.join(
2441-
spec.prefix,
2442-
spack.store.STORE.layout.metadata_dir,
2443-
spack.store.STORE.layout.manifest_file_name,
2444-
)
2445-
if not os.path.exists(manifest_file):
2446-
spec_id = spec.format("{name}/{hash:7}")
2447-
tty.warn("No manifest file in tarball for spec %s" % spec_id)
24482458
finally:
24492459
if tmpdir:
24502460
shutil.rmtree(tmpdir, ignore_errors=True)
@@ -2549,10 +2559,6 @@ def install_root_node(
25492559
tty.msg('Installing "{0}" from a buildcache'.format(spec.format()))
25502560
extract_tarball(spec, download_result, force)
25512561
spec.package.windows_establish_runtime_linkage()
2552-
if spec.spliced: # overwrite old metadata with new
2553-
spack.store.STORE.layout.write_spec(
2554-
spec, spack.store.STORE.layout.spec_file_path(spec)
2555-
)
25562562
spack.hooks.post_install(spec, False)
25572563
spack.store.STORE.db.add(spec, allow_missing=allow_missing)
25582564

lib/spack/spack/rewiring.py

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
44

55
import os
6-
import shutil
76
import tempfile
87

98
import spack.binary_distribution as bindist
10-
import spack.deptypes as dt
119
import spack.error
1210
import spack.hooks
13-
import spack.platforms
14-
import spack.relocate as relocate
1511
import spack.store
1612

1713

@@ -42,63 +38,11 @@ def rewire_node(spec, explicit):
4238

4339
spack.hooks.pre_install(spec)
4440
bindist.extract_buildcache_tarball(tarball, destination=spec.prefix)
45-
buildinfo = bindist.read_buildinfo_file(spec.prefix)
41+
bindist.relocate_package(spec)
4642

47-
# compute prefix-to-prefix for every node from the build spec to the spliced
48-
# spec
49-
prefix_to_prefix = {spec.build_spec.prefix: spec.prefix}
50-
build_spec_ids = set(id(s) for s in spec.build_spec.traverse(deptype=dt.ALL & ~dt.BUILD))
51-
for s in bindist.specs_to_relocate(spec):
52-
analog = s
53-
if id(s) not in build_spec_ids:
54-
analogs = [
55-
d
56-
for d in spec.build_spec.traverse(deptype=dt.ALL & ~dt.BUILD)
57-
if s._splice_match(d, self_root=spec, other_root=spec.build_spec)
58-
]
59-
if analogs:
60-
# Prefer same-name analogs and prefer higher versions
61-
# This matches the preferences in Spec.splice, so we will find same node
62-
analog = max(analogs, key=lambda a: (a.name == s.name, a.version))
63-
64-
prefix_to_prefix[analog.prefix] = s.prefix
65-
66-
platform = spack.platforms.by_name(spec.platform)
67-
68-
text_to_relocate = [
69-
os.path.join(spec.prefix, rel_path) for rel_path in buildinfo["relocate_textfiles"]
70-
]
71-
if text_to_relocate:
72-
relocate.relocate_text(files=text_to_relocate, prefix_to_prefix=prefix_to_prefix)
73-
links = [os.path.join(spec.prefix, f) for f in buildinfo["relocate_links"]]
74-
relocate.relocate_links(links, prefix_to_prefix)
75-
bins_to_relocate = [
76-
os.path.join(spec.prefix, rel_path) for rel_path in buildinfo["relocate_binaries"]
77-
]
78-
if bins_to_relocate:
79-
if "macho" in platform.binary_formats:
80-
relocate.relocate_macho_binaries(bins_to_relocate, prefix_to_prefix)
81-
if "elf" in platform.binary_formats:
82-
relocate.relocate_elf_binaries(bins_to_relocate, prefix_to_prefix)
83-
relocate.relocate_text_bin(binaries=bins_to_relocate, prefix_to_prefix=prefix_to_prefix)
84-
shutil.rmtree(tempdir)
85-
install_manifest = os.path.join(
86-
spec.prefix,
87-
spack.store.STORE.layout.metadata_dir,
88-
spack.store.STORE.layout.manifest_file_name,
89-
)
90-
try:
91-
os.unlink(install_manifest)
92-
except FileNotFoundError:
93-
pass
94-
# Write the spliced spec into spec.json. Without this, Database.add would fail because it
95-
# checks the spec.json in the prefix against the spec being added to look for mismatches
96-
spack.store.STORE.layout.write_spec(spec, spack.store.STORE.layout.spec_file_path(spec))
97-
# add to database, not sure about explicit
98-
spack.store.STORE.db.add(spec, explicit=explicit)
99-
100-
# run post install hooks
43+
# run post install hooks and add to db
10144
spack.hooks.post_install(spec, explicit)
45+
spack.store.STORE.db.add(spec, explicit=explicit)
10246

10347

10448
class RewireError(spack.error.SpackError):

0 commit comments

Comments
 (0)