Skip to content

Instantly share code, notes, and snippets.

@tgamblin
Last active March 3, 2022 00:28
Show Gist options
  • Save tgamblin/bc6566228a6a31a09a09b7aa25b31eb6 to your computer and use it in GitHub Desktop.
Save tgamblin/bc6566228a6a31a09a09b7aa25b31eb6 to your computer and use it in GitHub Desktop.
Replace GitHub patch URLs and sha256's with ?full_index=1 URLs and new sha256's
#!/usr/bin/env spack-python
import os
import tempfile
import spack.patch
import spack.repo
import spack.util.crypto
from spack.util.executable import which
full_index_arg = "?full_index=1"
curl = which("curl", required=True)
def sedify(pkg, patch):
if not isinstance(patch, spack.patch.UrlPatch):
return
if "github.com" not in patch.url:
return
if patch.url.endswith(full_index_arg):
return
if not patch.url.endswith(".patch"):
return
sha256_algo = spack.util.crypto.hash_fun_for_algo("sha256")
with tempfile.NamedTemporaryFile() as tmp:
full_index_url = patch.url + full_index_arg
curl("-Lo", tmp.name, full_index_url)
tmp.flush()
sha256 = spack.util.crypto.checksum(sha256_algo, tmp.name)
package_py_path = pkg.module.__file__
with open(package_py_path) as package_file:
contents = package_file.read()
contents = contents.replace(patch.sha256, sha256)
contents = contents.replace(patch.url, full_index_url)
tmp_path = "%s.tmp" % package_py_path
with open(tmp_path, "w") as new_package_file:
new_package_file.write(contents)
try:
os.rename(tmp_path, package_py_path)
except:
os.unlink(package_py_path)
for pkg in spack.repo.path.all_packages():
for condition, patches in pkg.patches.items():
for patch in patches:
sedify(pkg, patch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment