2021-09-08 09:08:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
""" check requirements.txt for outdated packages """
|
|
|
|
|
2021-09-13 04:08:28 +00:00
|
|
|
import pathlib
|
2021-09-18 13:02:54 +00:00
|
|
|
import sys
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
class Requirements:
|
2021-09-21 09:25:22 +00:00
|
|
|
"""handle requirements.txt"""
|
2021-09-08 09:08:20 +00:00
|
|
|
|
2021-09-21 09:25:22 +00:00
|
|
|
FILE_PATH = "tubearchivist/requirements.txt"
|
|
|
|
LOCK = "/tmp/tubearchivist-requirements.lock"
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2021-09-13 04:08:28 +00:00
|
|
|
self.exists = self.checked_today()
|
|
|
|
self.all_requirements = False
|
|
|
|
self.all_updates = False
|
|
|
|
|
|
|
|
def checked_today(self):
|
2021-09-21 09:25:22 +00:00
|
|
|
"""skip requirements check when lock file exists"""
|
2021-09-13 04:08:28 +00:00
|
|
|
exists = pathlib.Path(self.LOCK).exists()
|
|
|
|
return exists
|
|
|
|
|
|
|
|
def look_for_updates(self):
|
2021-09-21 09:25:22 +00:00
|
|
|
"""look through requirements and check for updates"""
|
2021-09-08 09:08:20 +00:00
|
|
|
self.all_requirements = self.get_dependencies()
|
|
|
|
self.all_updates = self.check_packages()
|
|
|
|
|
|
|
|
def get_dependencies(self):
|
2021-09-21 09:25:22 +00:00
|
|
|
"""read out requirements.txt"""
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
all_requirements = []
|
2021-09-21 09:25:22 +00:00
|
|
|
with open(self.FILE_PATH, "r", encoding="utf-8") as f:
|
2021-09-08 09:08:20 +00:00
|
|
|
dependencies = f.readlines()
|
|
|
|
|
|
|
|
for dependency in dependencies:
|
2021-09-21 09:25:22 +00:00
|
|
|
package, version = dependency.split("==")
|
2021-09-08 09:08:20 +00:00
|
|
|
all_requirements.append((package, version.strip()))
|
|
|
|
|
2021-09-18 13:02:54 +00:00
|
|
|
all_requirements.sort(key=lambda x: x[0].lower())
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
return all_requirements
|
|
|
|
|
|
|
|
def check_packages(self):
|
2021-09-21 09:25:22 +00:00
|
|
|
"""compare installed with remote version"""
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
total = len(self.all_requirements)
|
2021-09-21 09:25:22 +00:00
|
|
|
print(f"checking versions for {total} packages...")
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
all_updates = {}
|
|
|
|
|
|
|
|
for dependency in self.all_requirements:
|
|
|
|
package, version_installed = dependency
|
2021-09-21 09:25:22 +00:00
|
|
|
url = f"https://pypi.org/pypi/{package}/json"
|
2021-09-08 09:08:20 +00:00
|
|
|
response = requests.get(url).json()
|
2021-09-21 09:25:22 +00:00
|
|
|
version_remote = response["info"]["version"]
|
|
|
|
homepage = response["info"]["home_page"]
|
2021-09-08 09:08:20 +00:00
|
|
|
if version_remote != version_installed:
|
|
|
|
to_update = {
|
2021-09-21 09:25:22 +00:00
|
|
|
package: {"from": version_installed, "to": version_remote}
|
2021-09-08 09:08:20 +00:00
|
|
|
}
|
|
|
|
all_updates.update(to_update)
|
2021-09-21 09:25:22 +00:00
|
|
|
message = (
|
|
|
|
f"update {package} {version_installed}"
|
|
|
|
+ f"==> {version_remote}\n {homepage}"
|
|
|
|
)
|
2021-09-08 09:08:20 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
if not all_updates:
|
2021-09-21 09:25:22 +00:00
|
|
|
print("no updates found")
|
2021-09-08 09:08:20 +00:00
|
|
|
|
2021-09-13 04:08:28 +00:00
|
|
|
# remember that
|
|
|
|
pathlib.Path(self.LOCK).touch()
|
|
|
|
|
2021-09-08 09:08:20 +00:00
|
|
|
return all_updates
|
|
|
|
|
|
|
|
def apply_updates(self):
|
2021-09-21 09:25:22 +00:00
|
|
|
"""update requirements.txt file with new versions"""
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
to_write = []
|
|
|
|
|
|
|
|
for requirement in self.all_requirements:
|
|
|
|
package, old_version = requirement
|
|
|
|
|
|
|
|
if package in self.all_updates.keys():
|
2021-09-21 09:25:22 +00:00
|
|
|
package_version = self.all_updates[package]["to"]
|
2021-09-08 09:08:20 +00:00
|
|
|
else:
|
|
|
|
package_version = old_version
|
|
|
|
|
2021-09-21 09:25:22 +00:00
|
|
|
to_write.append(f"{package}=={package_version}\n")
|
2021-09-08 09:08:20 +00:00
|
|
|
|
2021-09-21 09:25:22 +00:00
|
|
|
with open(self.FILE_PATH, "w", encoding="utf-8") as f:
|
2021-09-08 09:08:20 +00:00
|
|
|
f.writelines(to_write)
|
|
|
|
|
2021-09-21 09:25:22 +00:00
|
|
|
print("requirements.txt updates")
|
2021-09-08 09:08:20 +00:00
|
|
|
|
|
|
|
|
2021-09-13 04:08:28 +00:00
|
|
|
def main():
|
2021-09-21 09:25:22 +00:00
|
|
|
"""main to check for updates"""
|
2021-09-08 09:08:20 +00:00
|
|
|
handler = Requirements()
|
2021-09-13 04:08:28 +00:00
|
|
|
if handler.exists:
|
|
|
|
return
|
|
|
|
|
|
|
|
handler.look_for_updates()
|
2021-09-08 09:08:20 +00:00
|
|
|
if handler.all_updates:
|
2021-09-21 09:25:22 +00:00
|
|
|
input_response = input("\nupdate requirements.txt? [y/n] ")
|
|
|
|
if input_response == "y":
|
2021-09-08 09:08:20 +00:00
|
|
|
handler.apply_updates()
|
|
|
|
else:
|
2021-09-21 09:25:22 +00:00
|
|
|
print("cancel update...")
|
2021-09-08 09:08:20 +00:00
|
|
|
sys.exit(1)
|
2021-09-13 04:08:28 +00:00
|
|
|
|
2021-09-18 13:02:54 +00:00
|
|
|
|
2021-09-13 04:08:28 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|