#!/usr/bin/python """ Updater for web gui """ import os import sys import getopt import re import shutil import glob import tarfile from datetime import datetime source_dir = "./webgui" target_dir = "/var/www/html/yms" backup_dir = "/var/backups" backup = False accept = False dryrun = False __author__ = "Thomas Hooge" __copyright__ = "Copyleft 2024-2026, all rights reversed" __version__ = "0.1" __email__ = "thomas@hoogi.de" __status__ = "Test" # Update files on top level update_files = ["*.php", "*.css", "globals.inc", "manifest.json", "sailboat.svg"] # Totally replace target dirs with new ones update_dirs_full = ["bootstrap", "css", "js", "lib", "shapes", "img"] # Locale: translation files locales = ["de_DE"] def ask_yn(prompt): answer = input(f"{prompt} (y/n): ").strip().lower() return answer in ("y", "yes") def read_version(globalfile): if os.path.isfile(globalfile): with open(globalfile, "r", encoding="utf-8") as fh: content = fh.read(4096) pattern = r"\$g_version\s*=\s*'(v\d+\.\d+\.\d+)';" match = re.search(pattern, content) if match: return tuple(map(int, match.group(1).lstrip("v").split("."))) return None def create_backup(source_dir, backup_dir, dryrun=False): if not os.path.isdir(backup_dir): print(f"Backup directory '{backup_dir}' does not exist.") return None if not os.access(backup_dir, os.W_OK): print(f"Backup directory '{backup_dir}' is not writeable.") return None dir_name = os.path.basename(os.path.normpath(source_dir)) date_stamp = datetime.now().strftime("%Y%m%d") backup_file = os.path.join(backup_dir, f"{dir_name}_{date_stamp}.tar.gz") def exclude_config(member): if member.name == f"{dir_name}/config.inc": return None return member if not dryrun: with tarfile.open(backup_file, "w:gz") as tar: tar.add(source_dir, arcname=dir_name, filter=exclude_config) else: print(f"creating backup of '{source_dir}' in '{backup_file}'") return backup_file def copy_dir_replace(dirname, source_dir, target_dir, dryrun=False): # Complete replaces old directory with new one source_path = os.path.join(source_dir, dirname) target_path = os.path.join(target_dir, dirname) print(f"full replace dir '{source_path}' -> '{target_path}'") if os.path.exists(target_path): if not dryrun: shutil.rmtree(target_path) else: print(f"remove directory tree '{target_path}'") else: if dryrun: print(f"directory '{target_path}' does not exist yet") if not dryrun: shutil.copytree(source_path, target_dir) else: print(f"copy directory tree '{source_path}' -> '{target_dir}'") def copy_wildcard(source_dir, target_dir, pattern, dryrun=False): print(f"copy pattern '{pattern}' from '{source_dir}' -> '{target_dir}'") if not dryrun: os.makedirs(target_dir, exist_ok=True) if dryrun: print(f"processing pattern '{pattern}'") for source_file in glob.glob(os.path.join(source_dir, pattern)): if os.path.isfile(source_file): target_file = os.path.join(target_dir, os.path.basename(source_file)) if not dryrun: shutil.copy2(source_file, target_file) else: print(f"copy file '{source_file}' -> '{target_file}'") def usage(): print("{} v{}".format(os.path.basename(__file__), __version__)) print(__copyright__) print() print("Command line options") print(f" -d --dir Target directory (default={target_dir})") print(" -b --backup Backup directory (no backup if missing)") print(" -h --help Show this help") print(" -y --accept Skip security questions") print(" --dryrun Test mode") print() if __name__ == "__main__": try: opts, args = getopt.gnu_getopt(sys.argv[1:], "d:b:yh", ["dir=", "backup=", "accept", "help", "dryrun"]) except getopt.GetoptError as err: print(str(err)) sys.exit(2) for opt, arg in opts: if opt in ("-d", "--dir"): target_dir = arg elif opt in ("-b", "--backup"): backup = True backup_dir = arg elif opt in ("-y", "--accept"): accept = True elif opt in ("-h", "--help"): usage() sys.exit(0) elif opt in ("--dryrun"): dryrun = True # Check if target dir exists if not os.path.isdir(target_dir): print(f"Target directory '{target_dir}' does not exist. Aborting.") sys.exit(1) # Check if target dir contains a valid yms installation # globals.inc must exist and contain version info in $g_version globalfile = os.path.join(target_dir, 'globals.inc') version = read_version(globalfile) if not version: print("Target contains no valid yms installation. Aborting.") sys.exit(1) # Check if version in source dir is greater than installed version version_new = read_version(os.path.join(source_dir, 'globals.inc')) if False: #version >= version_new: versionstring = "v" + ".".join(map(str,version)) versionstring_new = "v" + ".".join(map(str,version_new)) print(f"Version {versionstring_new} is not newer than installed version {versionstring}. Aborting.") sys.exit(1) # Check if target is writeable if False: #not os.access(target_dir, os.W_OK): print(f"Target directory '{target_dir}' is not writeable, Aborting.") sys.exit(1) # Query for updating found installation versionstring = "v" + ".".join(map(str,version)) if not accept and not ask_yn(f"Update yms {versionstring} installation in {target_dir}"): print("Update cancelled by user") sys.exit(0) # Create backup if backup and not create_backup(target_dir, backup_dir, dryrun): print("Aborting.") sys.exit(1) # First complete directories for dir in update_dirs_full: source = os.path.join(source_dir, dir) copy_dir_replace(dir, source_dir, target_dir, dryrun) # Second update files in main directory for pattern in update_files: copy_wildcard(source_dir, target_dir, pattern, dryrun) # At last the locales for language in locales: source_path = os.path.join(source_dir, "locale", language, "LC_MESSAGES") target_path = os.path.join(target_dir, "locale", language, "LC_MESSAGES") copy_wildcard(source_path, target_path, "*.mo", dryrun) print("Done.")