From 521ee62aa1b5f514d90165a9010820574e97573b Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Fri, 27 Jun 2025 09:18:32 +0200 Subject: [PATCH] Implement web progress --- gitlib/filesystem.py | 2 +- gitlib/gitlib.py | 150 +++++++++++++++++++++++------ interfaceAdm/CrearImagenGit.py | 4 +- interfaceAdm/RestaurarImagenGit.py | 3 +- 4 files changed, 124 insertions(+), 35 deletions(-) diff --git a/gitlib/filesystem.py b/gitlib/filesystem.py index bbaa69e..97c1e53 100644 --- a/gitlib/filesystem.py +++ b/gitlib/filesystem.py @@ -172,7 +172,7 @@ class FilesystemLibrary: done = False start_time = time.time() - timeout = 60 + timeout = 60*15 while not done and (time.time() - start_time) < timeout: diff --git a/gitlib/gitlib.py b/gitlib/gitlib.py index 4db0d09..b444c5c 100755 --- a/gitlib/gitlib.py +++ b/gitlib/gitlib.py @@ -46,13 +46,38 @@ import uuid from tqdm import tqdm from kernel import parse_kernel_cmdline -class OgProgressPrinter(git.RemoteProgress): +#import requests + + + +def _git_op_to_string(op): + op = op & git.RemoteProgress.OP_MASK + + if op == git.RemoteProgress.COMPRESSING: + return "Compressing", "Obj" + elif op == git.RemoteProgress.CHECKING_OUT: + return "Checking out", "Obj" + elif op == git.RemoteProgress.COUNTING: + return "Counting", "Obj" + elif op == git.RemoteProgress.RECEIVING: + return "Receiving", "B" + elif op == git.RemoteProgress.WRITING: + return "Writing", "B" + elif op == git.RemoteProgress.RESOLVING: + return "Resolving deltas", "Obj" + + return "Unknown", "?" + +class OgProgressPrinterWeb(git.RemoteProgress): + """ A class to print progress updates for Git operations. This class extends `git.RemoteProgress` to provide custom logging and printing of progress updates to the standard error stream. + This version reports progress to the web UI. + Attributes: logger (Logger): The logger instance used to log debug messages. prev_len (int): The length of the previous status string printed. @@ -67,9 +92,89 @@ class OgProgressPrinter(git.RemoteProgress): __del__(): Ensures a newline is printed when the instance is deleted. """ - def __init__(self, parentLogger, use_tqdm = False): + def __init__(self): super().__init__() - self.logger = parentLogger + + #self.ogcore_ip = get_IPcore() + #self.endpoint_url = f"https://{ogcore_ip}:8443/og-repository/webhook" + self.logger = logging.getLogger(f"{__package__}.{self.__class__.__name__}") + #self.jobId = jobId + + """Total stages of the git process. This comes from Git itself.""" + self.total_stages = 6 + self.simple_progress = 0 + self.prev_progress = -1 + + + def _get_stage_number(self, op): + op = op & git.RemoteProgress.OP_MASK + + if op == git.RemoteProgress.COMPRESSING: + return 0 + elif op == git.RemoteProgress.CHECKING_OUT: + return 1 + elif op == git.RemoteProgress.COUNTING: + return 2 + elif op == git.RemoteProgress.RECEIVING: + return 3 + elif op == git.RemoteProgress.WRITING: + return 4 + elif op == git.RemoteProgress.RESOLVING: + return 5 + + def update(self, op_code, cur_count, max_count=None, message=""): + # This shouldn't normally fail, but if it ever does, we don't want to fail the entire + # Git operation just because we're unable to report the status. + + stage_portion = (100 / self.total_stages) + + if max_count: + self.simple_progress = int((stage_portion * self._get_stage_number(op_code)) + ((stage_portion / max_count) * cur_count)) + + if self.prev_progress != self.simple_progress: + print(f"[{self.simple_progress}]") + + self.prev_progress = self.simple_progress + + + op_text, _ = _git_op_to_string(op_code) + self.logger.debug(f"Progress: {op_text} ({op_code}) {cur_count}/{max_count}: {message}") + + + #try: + # data = json.dumps({"job_id" : self.jobId, "progress" : progress}) + # headers = {'Content-Type': 'application/json'} + # + # response = requests.post(self.endpoint_url, data=data, headers=headers, verify=False) + #except Exception as e: + # self.logger.error("Exception %s while trying to report progress", e) + +class OgProgressPrinterConsole(git.RemoteProgress): + """ + A class to print progress updates for Git operations. + + This class extends `git.RemoteProgress` to provide custom logging and + printing of progress updates to the standard error stream. + + This version is made for command-line usage. + + Attributes: + logger (Logger): The logger instance used to log debug messages. + prev_len (int): The length of the previous status string printed. + + Methods: + __init__(parentLogger): + Initializes the OgProgressPrinter with a logger instance. + + update(op_code, cur_count, max_count=None, message=""): + Updates the progress status and prints it to the standard error stream. + + __del__(): + Ensures a newline is printed when the instance is deleted. + """ + def __init__(self, use_tqdm = True): + super().__init__() + self.logger = logging.getLogger(f"{__package__}.{self.__class__.__name__}") if sys.stdin.isatty() and use_tqdm: self.progress = tqdm() @@ -82,30 +187,7 @@ class OgProgressPrinter(git.RemoteProgress): op = op_code & git.RemoteProgress.OP_MASK stage = op_code & git.RemoteProgress.STAGE_MASK - - op_text = "Unknown" - op_unit = "?" - - if op == git.RemoteProgress.COMPRESSING: - op_text = "Compressing" - op_unit = "Obj" - elif op == git.RemoteProgress.CHECKING_OUT: - op_text = "Checking out" - op_unit = "Obj" - elif op == git.RemoteProgress.COUNTING: - op_text = "Counting" - op_unit = "Obj" - elif op == git.RemoteProgress.RECEIVING: - op_text = "Receiving" - op_unit = "B" - elif op == git.RemoteProgress.WRITING: - op_text = "Writing" - op_unit = "B" - elif op == git.RemoteProgress.RESOLVING: - op_text = "Resolving deltas" - op_unit = "Obj" - - + op_text, op_unit = _git_op_to_string(op) self.logger.debug(f"Progress: {op_text} ({op_code}) {cur_count}/{max_count}: {message}") @@ -281,6 +363,10 @@ class OpengnsysGitLibrary: else: self.logger.debug(f"Git repository: {self.repo_server}") + + """Which progress callback to use, console or web-targeted one""" + self.progress_callback = OgProgressPrinterConsole() + def _is_efi(self): """Determina si hemos arrancado con EFI @@ -1302,7 +1388,7 @@ class OpengnsysGitLibrary: repo.git.push("--force") # Obliterate whatever might have been there self.logger.debug("Fetching origin") - origin.fetch(progress=OgProgressPrinter(self.logger)) + origin.fetch(progress=self.progress_callback) repo.heads.master.set_tracking_branch(origin.refs.master) @@ -1385,7 +1471,7 @@ class OpengnsysGitLibrary: self.logger.info("Uploading to ogrepository") - origin.push(progress=OgProgressPrinter(self.logger)) + origin.push(progress=self.progress_callback) #repo.git.push("--set-upstream", "origin", repo.head.ref, "--force") self.logger.info("initRepo done") @@ -1454,7 +1540,7 @@ class OpengnsysGitLibrary: self.logger.info("Cloning repository from %s", repo_url) - repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=OgProgressPrinter(self.logger)) + repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) if repo_is_efi: self._efi_install(root_directory=destination_dir) @@ -1547,7 +1633,7 @@ class OpengnsysGitLibrary: origin = repo.remotes["origin"] repo.heads.master.set_tracking_branch(origin.refs.master) - origin.push(progress=OgProgressPrinter(self.logger)) + origin.push(progress=self.progress_callback) #repo.git.push("--set-upstream", "origin", repo.head.ref, "--force") # force = True) @@ -1568,7 +1654,7 @@ class OpengnsysGitLibrary: if origin: self.logger.debug("Fetching from origin") - origin.fetch(progress=OgProgressPrinter(self.logger)) + origin.fetch(progress=self.progress_callback) else: self.logger.error("Origin not found, can't fetch") diff --git a/interfaceAdm/CrearImagenGit.py b/interfaceAdm/CrearImagenGit.py index 28a99bf..e23fdfa 100755 --- a/interfaceAdm/CrearImagenGit.py +++ b/interfaceAdm/CrearImagenGit.py @@ -43,13 +43,15 @@ import NetLib -from gitlib import OpengnsysGitLibrary, NTFSImplementation +from gitlib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb def create_image(disk_num, partition_num, repo, image_name, tagName): ntfs_impl = NTFSImplementation.NTFS3G og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) + og_git.progress_callback = OgProgressPrinterWeb() + device = DiskLib.ogDiskToDev(disk_num, partition_num) if og_git.initRepo(device, image_name): return 0 diff --git a/interfaceAdm/RestaurarImagenGit.py b/interfaceAdm/RestaurarImagenGit.py index 3738a72..55dc36c 100755 --- a/interfaceAdm/RestaurarImagenGit.py +++ b/interfaceAdm/RestaurarImagenGit.py @@ -39,7 +39,7 @@ import ogGlobals import SystemLib import DiskLib -from gitlib import OpengnsysGitLibrary, NTFSImplementation +from gitlib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb if __name__ == "__main__": if len(sys.argv) < 6: @@ -75,6 +75,7 @@ if __name__ == "__main__": ntfs_impl = NTFSImplementation.NTFS3G og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) + og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(disk, partition)