Last active
January 15, 2025 14:05
-
-
Save valgur/f94bc2cf455daf081095f6088551c24e to your computer and use it in GitHub Desktop.
Add missing sha256 hashes to conanadata.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import hashlib | |
import re | |
import sys | |
from pathlib import Path | |
def get_hash(url): | |
r = requests.get(url) | |
r.raise_for_status() | |
return hashlib.sha256(r.content).hexdigest() | |
def process(match, swapped): | |
url = match[3].strip().strip("'").strip('"') | |
hash = match[4].strip().strip("'").strip('"') | |
if swapped: | |
url, hash = hash, url | |
if not re.fullmatch("[0-9a-fA-f]{64}", hash): | |
print(f" Processing {url}", flush=True) | |
hash = get_hash(url) | |
return f'{match[1]}{match[2]}url: "{url}"\n{match[1]} sha256: "{hash}"' | |
def process_multi(match, swapped): | |
url = match[3].strip().strip("'").strip('"') | |
hash = match[4].strip().strip("'").strip('"') | |
if swapped: | |
url, hash = hash, url | |
if not re.fullmatch("[0-9a-fA-f]{64}", hash): | |
print(f" Processing {url}", flush=True) | |
hash = get_hash(url) | |
return f'{match[1]}{match[2]}"{url}"\n{match[1]} sha256: "{hash}"' | |
def main(conandata_file): | |
conandata_file = Path(conandata_file).resolve() | |
if conandata_file.name != "conandata.yml": | |
print("ERROR: Not a conandata.yml file", file=sys.stderr) | |
sys.exit(1) | |
content = conandata_file.read_text() | |
content = re.sub(r"^( +)([- ] )url: (.+)\n\1 sha256: (.+)$", lambda m: process(m, swapped=False), content, flags=re.M) | |
content = re.sub(r"^( +)([- ] )sha256: (.+)\n\1 url: (.+)$", lambda m: process(m, swapped=True), content, flags=re.M) | |
content = re.sub(r'^( +)([- ] )"url": (.+)\n\1 "sha256": (.+)$', lambda m: process(m, swapped=False), content, flags=re.M) | |
content = re.sub(r'^( +)([- ] )"sha256": (.+)\n\1 "url": (.+)$', lambda m: process(m, swapped=True), content, flags=re.M) | |
content = re.sub(r"^( +)( - )(.+)\n\1 sha256: (.+)$", lambda m: process_multi(m, swapped=False), content, flags=re.M) | |
conandata_file.write_text(content) | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment