nkpkg.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2024 DFC Team (thelittlepony) & Nikus Team (openov)
  2. # This File is licensed under GPLv3 License
  3. import requests
  4. import sys
  5. import json
  6. from types import SimpleNamespace
  7. import platform
  8. server_url = "https://notabug.org/Nikus-Team/nkpkg-repo/raw/master/*"
  9. args = sys.argv
  10. current = args[0]
  11. #print(args)
  12. current_arch = platform.processor()
  13. try:
  14. command = args[1]
  15. except:
  16. print(f"error: No command. Try {current} h")
  17. exit()
  18. try:
  19. target = args[2]
  20. except:
  21. target = None
  22. # class BasePackage:
  23. # def __init__(self, name: str, description: str, author: str, version: str, arch: str):
  24. # self.name = name
  25. # self.description = description
  26. # self.author = author
  27. # self.version = version
  28. # self.arch = arch
  29. class NKPKGInternal:
  30. def Search(name: str):
  31. payload = {"package": str}
  32. package_server = requests.get(f"{server_url}/api/search", json = payload)
  33. package = json.loads(package_server.content, object_hook=lambda d: SimpleNamespace(**d))
  34. package.name
  35. package.description
  36. package.author
  37. package.version
  38. package.arch
  39. packages = []
  40. if package.arch == current_arch:
  41. packages.append(Package)
  42. else:
  43. print(f"warn: {package.name} has {package.arch}, but your pc has {current_arch}. Skipping...")
  44. return packages
  45. def GetBinary(name: str, author: str, version: str, arch: str):
  46. payload = {"name": name, "author": author, "version": version, "arch": arch}
  47. try:
  48. binary_request = requests.get(f"{server_url}/api/get_binary")
  49. except:
  50. print("nkpkg: 52: can't get binary file")
  51. return {"error": True}
  52. try:
  53. binary_data = binary_request.content
  54. binary_file = open(f"{name}", "w")
  55. binary_file.write(binary_data)
  56. binary_file.close()
  57. except:
  58. print("nkpkg: 58-61: can't write file, or open it")
  59. return {"error": True}
  60. return {"error": False}
  61. if command == "h":
  62. print(
  63. f"""
  64. Usage: {current} [action] [package]
  65. action:
  66. s - <package> - build package from source
  67. su <package> - build package from source on user repository (user repo can be changed at /etc/nkpkg/make.conf)
  68. b <package> - install binary package
  69. ss <package> - search package in the repos (urepo & repo)
  70. r <package> - remove package
  71. ra <package> - removes package and package data
  72. u - update all packages
  73. # us - sync repos
  74. fu - full upgrade of OS
  75. """
  76. )
  77. exit()
  78. if command == "b":
  79. package = NKPKGInternal.Search(target)[0]
  80. binary_check_err = NKPKGInternal.GetBinary(package.name, package.author, package.version, package.arch)
  81. if binary_check_err["error"] == True:
  82. print(f"error: Can't install {target}. Check logs.")
  83. exit()
  84. if command == "ss":
  85. package = NKPKGInternal.Search(target)[0]
  86. if len(package) > 0:
  87. print(f"""
  88. name: {package.name}
  89. description: {package.description}
  90. author: {package.author}
  91. version: {package.version}
  92. arch: {package.arch}
  93. """)
  94. else:
  95. print(f"error: No package found for {target}")
  96. exit()
  97. else:
  98. print(f"error: Invalid command ({command}). Try {current} h")
  99. exit()