From 7661b0d28eb917fdbdff16eacb7bd7a5e3f569c6 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:41:56 +0000 Subject: [PATCH 1/4] Support multiple files for Predbat internal update feature --- apps/predbat/predbat.py | 77 ++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 711781dc5..9b415a442 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -22,6 +22,7 @@ import yaml THIS_VERSION = "v7.15.12" +PREDBAT_FILES = ["predbat.py"] TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z" TIME_FORMAT_SECONDS = "%Y-%m-%dT%H:%M:%S.%f%z" TIME_FORMAT_OCTOPUS = "%Y-%m-%d %H:%M:%S%z" @@ -10953,35 +10954,69 @@ def update_event(self, event, data, kwargs): """ self.log("Update event {} {} {}".format(event, data, kwargs)) + def download_predbat_file_from_github(self, filename, new_filename=None): + """ + Downloads a predbat source file from github and returns the contents + + Args: + filename (str): The filename to download (e.g. predbat.py) + new_filename (str): The new filename to save the file as + Returns: + str: The contents of the file + """ + url = "https://raw.githubusercontent.com/springfall2008/batpred/master/apps/predbat/{}".format(filename) + self.log("Downloading Predbat file from {}".format(url)) + r = requests.get(url, headers={}) + if r.ok: + data = r.text + if new_filename: + self.log("Write new version {} bytes to {}".format(len(data), new_filename)) + with open(new_filename, "w") as han: + han.write(data) + return data + else: + self.log("WARN: Downloading Predbat file failed, URL {}".format(url)) + return None + def download_predbat_version(self, version): """ - Download a version of Predbat + Download a version of Predbat from GitHub + + Args: + version (str): The version of Predbat to download. + + Returns: + bool: True if the download and update were successful, False otherwise. """ tag_split = version.split(" ") self.log("Split returns {}".format(tag_split)) if tag_split: tag = tag_split[0] - url = "https://raw.githubusercontent.com/springfall2008/batpred/" + tag + "/apps/predbat/predbat.py" - self.log("Downloading Predbat version {} from {}".format(tag, url)) - r = requests.get(url, headers={}) - if r.ok: - data = r.text - new_filename = __file__ + "." + tag - size = len(data) - if size >= 10000: - self.log("Write new version {} bytes to {}".format(len(data), new_filename)) - with open(new_filename, "w") as han: - han.write(data) - self.call_notify("Predbat: update to: {}".format(version)) - self.log("Perform the update.....") - os.system("mv -f {} {}".format(new_filename, __file__)) - return True - else: - self.log("File is too small, update failed") - return False + + predbat_code = self.download_predbat_file_from_github("predbat.py") + if predbat_code: + for line in predbat_code: + if line.startswith("PREDBAT_FILES"): + files = line.split("=")[1].strip() + files = files.replace("[", "") + files = files.replace("]", "") + files = files.replace("\"", "") + files = files.split(",") + for file in files: + self.download_predbat_file_from_github(file, file + "." + tag) + self.call_notify("Predbat: update to: {}".format(version)) + self.log("Perform the update.....") + cmd = "" + for file in files: + cmd += "mv -f {} {} && ".format(file + "." + tag, file) + cmd += "echo 'Update complete'" + self.log("Performing update with command: {}".format(cmd)) + os.system(cmd) + return True + self.log("Failed to find PREDBAT_FILES in predbat.py") else: - self.log("WARN: Downloading Predbat failed, URL {}".format(url)) - return False + self.log("Failed to download Predbat version {}".format(version)) + return False def select_event(self, event, data, kwargs): """ From bbe69227fbb65ebd6213a2c1f280afaafa7182f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:43:26 +0000 Subject: [PATCH 2/4] [pre-commit.ci lite] apply automatic fixes --- apps/predbat/predbat.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 9b415a442..a2c13d9bf 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -10977,16 +10977,16 @@ def download_predbat_file_from_github(self, filename, new_filename=None): else: self.log("WARN: Downloading Predbat file failed, URL {}".format(url)) return None - + def download_predbat_version(self, version): """ Download a version of Predbat from GitHub - + Args: version (str): The version of Predbat to download. Returns: - bool: True if the download and update were successful, False otherwise. + bool: True if the download and update were successful, False otherwise. """ tag_split = version.split(" ") self.log("Split returns {}".format(tag_split)) @@ -11000,7 +11000,7 @@ def download_predbat_version(self, version): files = line.split("=")[1].strip() files = files.replace("[", "") files = files.replace("]", "") - files = files.replace("\"", "") + files = files.replace('"', "") files = files.split(",") for file in files: self.download_predbat_file_from_github(file, file + "." + tag) From 2c8d930ded8f9a6907765063bf0f6c18d2fbfae7 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Sat, 3 Feb 2024 17:24:27 +0000 Subject: [PATCH 3/4] Fixes --- apps/predbat/predbat.py | 55 ++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index a2c13d9bf..133974df0 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -10954,7 +10954,7 @@ def update_event(self, event, data, kwargs): """ self.log("Update event {} {} {}".format(event, data, kwargs)) - def download_predbat_file_from_github(self, filename, new_filename=None): + def download_predbat_file_from_github(self, filename, new_filename): """ Downloads a predbat source file from github and returns the contents @@ -10965,7 +10965,7 @@ def download_predbat_file_from_github(self, filename, new_filename=None): str: The contents of the file """ url = "https://raw.githubusercontent.com/springfall2008/batpred/master/apps/predbat/{}".format(filename) - self.log("Downloading Predbat file from {}".format(url)) + self.log("Downloading Predbat file from {} to {}".format(url, new_filename)) r = requests.get(url, headers={}) if r.ok: data = r.text @@ -10977,45 +10977,60 @@ def download_predbat_file_from_github(self, filename, new_filename=None): else: self.log("WARN: Downloading Predbat file failed, URL {}".format(url)) return None - + def download_predbat_version(self, version): """ Download a version of Predbat from GitHub - + Args: version (str): The version of Predbat to download. Returns: - bool: True if the download and update were successful, False otherwise. + bool: True if the download and update were successful, False otherwise. """ tag_split = version.split(" ") + this_path = os.path.dirname(__file__) self.log("Split returns {}".format(tag_split)) if tag_split: tag = tag_split[0] - predbat_code = self.download_predbat_file_from_github("predbat.py") + # Download predbat.py + file = "predbat.py" + predbat_code = self.download_predbat_file_from_github(file, os.path.join(this_path, file + "." + tag)) if predbat_code: + # Get the list of other files to download by searching for PREDBAT_FILES in predbat.py + files = [] for line in predbat_code: if line.startswith("PREDBAT_FILES"): files = line.split("=")[1].strip() files = files.replace("[", "") files = files.replace("]", "") - files = files.replace('"', "") + files = files.replace("\"", "") files = files.split(",") - for file in files: - self.download_predbat_file_from_github(file, file + "." + tag) - self.call_notify("Predbat: update to: {}".format(version)) - self.log("Perform the update.....") - cmd = "" - for file in files: - cmd += "mv -f {} {} && ".format(file + "." + tag, file) - cmd += "echo 'Update complete'" - self.log("Performing update with command: {}".format(cmd)) - os.system(cmd) - return True - self.log("Failed to find PREDBAT_FILES in predbat.py") + self.log("Predbat update files are {}".format(files)) + break + + # Download the remaining files + if files: + for file in files: + # Download the remaining files + if file != "predbat.py": + self.download_predbat_file_from_github(file, os.path.join(this_path, file + "." + tag)) + + # Notify that we are about to update + self.call_notify("Predbat: update to: {}".format(version)) + + # Perform the update + self.log("Perform the update.....") + cmd = "" + for file in files: + cmd += "mv -f {} {} && ".format(os.path.join(this_path, file + "." + tag), os.path.join(this_path, file)) + cmd += "echo 'Update complete'" + self.log("Performing update with command: {}".format(cmd)) + os.system(cmd) + return True else: - self.log("Failed to download Predbat version {}".format(version)) + self.log("WARN: Predbat update failed to download Predbat version {}".format(version)) return False def select_event(self, event, data, kwargs): From d3e7dd7cf15a5bb0accf0f01cfe1a0b188cc2d0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 3 Feb 2024 17:25:22 +0000 Subject: [PATCH 4/4] [pre-commit.ci lite] apply automatic fixes --- apps/predbat/predbat.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 133974df0..af65034ec 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -10977,16 +10977,16 @@ def download_predbat_file_from_github(self, filename, new_filename): else: self.log("WARN: Downloading Predbat file failed, URL {}".format(url)) return None - + def download_predbat_version(self, version): """ Download a version of Predbat from GitHub - + Args: version (str): The version of Predbat to download. Returns: - bool: True if the download and update were successful, False otherwise. + bool: True if the download and update were successful, False otherwise. """ tag_split = version.split(" ") this_path = os.path.dirname(__file__) @@ -11005,7 +11005,7 @@ def download_predbat_version(self, version): files = line.split("=")[1].strip() files = files.replace("[", "") files = files.replace("]", "") - files = files.replace("\"", "") + files = files.replace('"', "") files = files.split(",") self.log("Predbat update files are {}".format(files)) break