getput-void-updates.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import subprocess
  4. import os
  5. DISTRO_NAME = os.popen("lsb_release -a 2>/dev/null | grep -i 'distributor id' | awk '{print $3}'").read().strip()
  6. HOST_NAME = os.popen("hostname").read().strip()
  7. def print_available_updates():
  8. updates_list = []
  9. try:
  10. updates = subprocess.check_output(["xbps-install", "-n", "-u", "-M", "-S"], encoding="utf-8").strip()
  11. # updates = subprocess.check_output(["apt-show-versions", "-u", "-b"]).decode("utf-8").strip()
  12. updates_list = updates.splitlines()
  13. except subprocess.CalledProcessError:
  14. updates_output = ""
  15. else:
  16. updates_output = "\nAvailable: {}{}".format(len(updates_list), updates)
  17. just_update_names_list = []
  18. for line in updates_list:
  19. name_and_version_str = line.split(" ")[0]
  20. splitted_list = name_and_version_str.split("-")
  21. just_update_names_list.append("-".join(splitted_list[:-1]))
  22. for line in just_update_names_list:
  23. print(line)
  24. def get_installed_from_the_file(
  25. path_to_dir=f"{os.getenv('HOME')}/Dropbox/InstalledPackages/",
  26. file_name=f"{DISTRO_NAME}_InstalledPackages_{HOST_NAME}.txt"):
  27. try:
  28. for line in open(f"{path_to_dir}{file_name}", mode="r", encoding="utf-8"):
  29. print(line, end="")
  30. except FileNotFoundError:
  31. print(f"File '{path_to_dir}{file_name}' not found.")
  32. def put_installed_to_the_file(
  33. path_to_dir=f"{os.getenv('HOME')}/Dropbox/InstalledPackages/",
  34. file_name=f"{DISTRO_NAME}_InstalledPackages_{HOST_NAME}.txt"):
  35. try:
  36. installed = subprocess.check_output(["xbps-query", "-m"], encoding="utf-8").strip()
  37. installed_list = installed.splitlines()
  38. except subprocess.CalledProcessError:
  39. print("No updates available.")
  40. else:
  41. with open(f"{path_to_dir}{file_name}", mode="w", encoding="utf-8") as f:
  42. for line in installed_list:
  43. name_and_version_str = line.split(" ")[0]
  44. splitted_list = name_and_version_str.split("-")
  45. ready_str = "-".join(splitted_list[:-1])
  46. f.write(f"{ready_str}\n")
  47. print(f"Wrote {len(installed_list)} packages into \n{path_to_dir}{file_name}")
  48. if __name__ == "__main__":
  49. print(
  50. """
  51. Choose what you want:
  52. 1 Get installed manually packages from the file
  53. 2 Put installed manually packages into the given file
  54. 3 Print available updates
  55. """
  56. )
  57. choice = input()
  58. if choice == "1":
  59. get_installed_from_the_file()
  60. elif choice == "2":
  61. put_installed_to_the_file()
  62. elif choice == "3":
  63. print_available_updates()
  64. else:
  65. print("Exit...")