diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 711781dc5..af65034ec 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,84 @@ 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): + """ + 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 {} to {}".format(url, new_filename)) + 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(" ") + this_path = os.path.dirname(__file__) 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 + + # 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.split(",") + 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("WARN: Downloading Predbat failed, URL {}".format(url)) - return False + self.log("WARN: Predbat update failed to download Predbat version {}".format(version)) + return False def select_event(self, event, data, kwargs): """