Last active
September 6, 2024 17:30
-
-
Save codeperfectplus/2e5196fc4789cecb67ebb85176376119 to your computer and use it in GitHub Desktop.
Get latest version on pypi packages script
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
import subprocess | |
import pkg_resources | |
import requests | |
from tabulate import tabulate | |
def get_installed_version(package): | |
try: | |
# Fetch the installed version of the package | |
return pkg_resources.get_distribution(package).version | |
except pkg_resources.DistributionNotFound: | |
return None | |
def get_latest_version(package): | |
try: | |
# Query PyPI for the latest version of the package | |
response = requests.get(f"https://pypi.org/pypi/{package}/json") | |
if response.status_code == 200: | |
return response.json()["info"]["version"] | |
else: | |
return None | |
except Exception as e: | |
print(f"Error fetching the latest version for {package}: {e}") | |
return None | |
def read_requirements(file_path="requirements.txt"): | |
try: | |
with open(file_path, "r") as f: | |
return [line.strip() for line in f.readlines() if line.strip()] | |
except FileNotFoundError: | |
print(f"File {file_path} not found!") | |
return [] | |
def write_requirements(requirements, file_path="requirements.txt"): | |
try: | |
with open(file_path, "w") as f: | |
f.write("\n".join(requirements)) | |
print(f"Updated {file_path} successfully.") | |
except Exception as e: | |
print(f"Error writing to {file_path}: {e}") | |
def compare_versions(requirements): | |
old_vs_new = [] | |
for req in requirements: | |
# Handle packages with versions (e.g., package==1.0.0) | |
if "==" in req: | |
package, current_version = req.split("==") | |
else: | |
package = req | |
current_version = get_installed_version(package) | |
current_version = current_version if current_version else "Unknown" | |
latest_version = get_latest_version(package) | |
# Append to the comparison table | |
old_vs_new.append([package, current_version, latest_version]) | |
return old_vs_new | |
def prompt_update(requirements, comparison_table): | |
print(tabulate(comparison_table, headers=["Package", "Current Version", "Latest Version"])) | |
update = input("\nDo you want to update the requirements.txt file with the latest versions? (y/n): ").strip().lower() | |
if update == "y": | |
updated_requirements = [] | |
for row in comparison_table: | |
package, current_version, latest_version = row | |
if latest_version and current_version != latest_version: | |
updated_requirements.append(f"{package}=={latest_version}") | |
else: | |
updated_requirements.append(f"{package}=={current_version}") | |
# Write the updated requirements to the file | |
write_requirements(updated_requirements) | |
else: | |
print("No changes made to requirements.txt.") | |
if __name__ == "__main__": | |
# Step 1: Read requirements.txt file | |
requirements = read_requirements() | |
if not requirements: | |
print("No requirements to process.") | |
exit(1) | |
# Step 2: Compare versions | |
comparison_table = compare_versions(requirements) | |
# Step 3: Show comparison and prompt to update | |
prompt_update(requirements, comparison_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment