Skip to content

Commit

Permalink
add get_hashes script
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Feb 18, 2024
1 parent 32238ce commit f9ef0f3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ repos:
"types-mock==5.0.*",
"types-PyYAML==6.0",
"types-toml~=0.10",
"types-requests~=2.13",
]
48 changes: 48 additions & 0 deletions src/codemodder/scripts/get_hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
import sys


def get_package_hashes(package_name: str, version: str) -> list[str]:
"""
Fetch the SHA256 hashes for a given package version from PyPI.
"""
url = f"https://pypi.org/pypi/{package_name}/{version}/json"
response = requests.get(url)
hashes = []

if response.status_code == 200:
data = response.json()
for release in data.get("urls", []):
sha256 = release.get("digests", {}).get("sha256")
if sha256:
hashes.append(sha256)
else:
print(f"Failed to fetch data for {package_name}=={version}", file=sys.stderr)

return hashes


def main():
if len(sys.argv) < 2:
print("Usage: python script.py package1==version package2==version")
sys.exit(1)
for arg in sys.argv[1:]:
if "==" not in arg:
print(
f"Invalid format '{arg}'. Expected format: PackageName==Version",
file=sys.stderr,
)
continue

package_name, version = arg.split("==", 1)
hashes = get_package_hashes(package_name, version)
if hashes:
print(f"SHA256 hashes for {package_name}=={version}:")
for hash_value in hashes:
print(hash_value)
else:
print(f"No hashes found for {package_name}=={version}")


if __name__ == "__main__":
main()

0 comments on commit f9ef0f3

Please sign in to comment.