# -*- coding: utf-8 -*- from setuphelpers import * import requests import json from configparser import ConfigParser def install(): plugin_inifile = makepath(WAPT.private_dir, "wapt_api.ini") if not isfile(plugin_inifile): filecopyto("wapt_api.ini", WAPT.private_dir) def audit(): plugin_inifile = makepath(WAPT.private_dir, "wapt_api.ini") conf_wapt = ConfigParser() conf_wapt.read(plugin_inifile) wapt_url = conf_wapt.get("wapt", "wapt_url") wapt_user = conf_wapt.get("wapt", "wapt_username") wapt_password = conf_wapt.get("wapt", "wapt_password") app_to_update_json_path = makepath(WAPT.private_dir, "app_to_update.json") if isfile(app_to_update_json_path): print("suppression de l'ancienne version du fichier json") remove_file(app_to_update_json_path) # Download JSON data from the URL json_data = download_json_from_url("https://wapt.tranquil.it/store/packages_list") if json_data is None: return "ERROR" online_dict = {} transformed_package_dict = {} app_to_update_list = [] # Transform data from JSON into a dictionary for element in json_data: online_dict[element[0].replace("tis-", "comi-")] = element[1] # Assuming WAPT.list() returns a list of dictionaries containing package data local_package_dict = json.loads(wgets("https://%s:%s@%s/api/v3/packages" % (wapt_user, wapt_password, wapt_url))) # count the number of packages in the WAPT repository print("Nombre de paquets dans le dépôt WAPT : " + str(len(local_package_dict))) # Transform package data into a dictionary for element in local_package_dict["result"]: transformed_package_dict[element["package"]] = element["version"] # Compare versions and find packages that need to be updated for key in online_dict: if key in transformed_package_dict: if Version(online_dict[key]) > Version(transformed_package_dict[key]): print("ajout de " + key + " à la liste des paquets à mettre à jour") app_to_update_list.append({"package": key, "version": online_dict[key]}) with open(app_to_update_json_path, "w") as json_file: json.dump(app_to_update_list, json_file) return "OK" def download_json_from_url(url): try: response = requests.get(url, verify=False) response.raise_for_status() # Raise an exception for unsuccessful responses json_data = response.json() return json_data except requests.exceptions.RequestException as e: print(f"Error downloading JSON from URL: {e}") return None