Skip to content

Commit

Permalink
fetch package_version and sort with packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykim1 committed Feb 2, 2024
1 parent aa5c17a commit 40ce9f0
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions .github/workflows/pypi_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,43 @@

from urllib.request import urlopen
import json
from packaging import version

# importlib.metadata
#####################################################################
import importlib.metadata
# import importlib.metadata


def use_importlib_metadata():
# List all installed distributions???
distributions = importlib.metadata.distributions()
# def use_importlib_metadata():
# # List all installed distributions???
# distributions = importlib.metadata.distributions()

# Create a dictionary to store package names and their versions .
installed_packages = {}
# # Create a dictionary to store package names and their versions .
# installed_packages = {}

# Iterate over the distributions and get their name and version .
for distribution in distributions:
name = distribution.metadata["Name"]
version = distribution.version
installed_packages[name] = version
# # Iterate over the distributions and get their name and version .
# for distribution in distributions:
# name = distribution.metadata["Name"]
# version = distribution.version
# installed_packages[name] = version

# Print pair of package and the version .
for package, version in installed_packages.items():
print(f"{package}: {version}")
#######################################################################
# # Print pair of package and the version .
# for package, version in installed_packages.items():
# print(f"{package}: {version}")
# #######################################################################


###### pip list way vs. importlib_metadata way ######
def get_installed_packages():
# Run "pip list" and output in JSON format
process = subprocess.run(
["pip", "list", "--format=json"], capture_output=True, text=True, check=True
)
# ###### pip list way vs. importlib_metadata way ######
# def get_installed_packages():
# # Run "pip list" and output in JSON format
# process = subprocess.run(
# ["pip", "list", "--format=json"], capture_output=True, text=True, check=True
# )

# Parse the JSON output
packages = json.loads(process.stdout)
# print(packages)
return packages
# # Parse the JSON output
# packages = json.loads(process.stdout)
# # print(packages)
# return packages


###### pip list way vs. importlib_metadata way ######
Expand All @@ -68,6 +69,19 @@ def get_latest_package_version(package_name):
### is it sorted? there could have been bug fix version


def fetch_package_versions(package_name):
url = f"https://pypi.org/pypi/{package_name}/json"
response = requests.get(url)

if response.status_code == 200:
data = response.json()
versions = data["releases"].keys()
return sorted(versions, key=lambda v: version.parse(v))
else:
print(f"Failed to fetch data for {package_name}")
return None


# Iterate through all package we have to fetch latest
def get_entire_latest_package_version(our_packages):
latest_versions = {}
Expand Down Expand Up @@ -95,8 +109,6 @@ def main():
encoding="utf-8"
)

# stricter ->

# Dictionary of package name and its version
packages = {}
for line in requirement_content.splitlines():
Expand Down Expand Up @@ -142,7 +154,7 @@ def main():
# targeted dependabot - instead of all dependencies
# if github action doesnt exist


# use version from packaging so we can sort
# tools repo target tool itself

# for python repo target pytest since want to test

0 comments on commit 40ce9f0

Please sign in to comment.