File 0009-Add-buildlog-option-to-fetch-buildlog-not-relative-t.patch of Package osc

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= <bjorn.bidar@jolla.com>
Date: Tue, 2 Jan 2024 12:28:20 +0200
Subject: [PATCH] Add buildlog option to fetch buildlog not-relative to pkg-dir
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Björn Bidar <bjorn.bidar@jolla.com>
---
 osc/commandline.py | 79 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 70 insertions(+), 9 deletions(-)

diff --git a/osc/commandline.py b/osc/commandline.py
index 5942af4adfc28332852a8b3fbe8cbfbaea456109..6680e7b6a3eb9fd5137bea1c21461964e30714f3 100644
--- a/osc/commandline.py
+++ b/osc/commandline.py
@@ -6681,6 +6681,10 @@ Please submit there instead, or use --nodevelproject to force direct submission.
                   help='get log start or end from the offset')
     @cmdln.option('-s', '--strip-time', action='store_true',
                         help='strip leading build time from the log')
+    @cmdln.option('-O', '--all-packages', action='store_true',
+                  help='Show buildlog for all packages of project')
+    @cmdln.option('-S', '--status', action='store', metavar='STATUS',
+                  help='Only show build of packages with STATUS')
     def do_buildlog(self, subcmd, opts, *args):
         """
         Shows the build log of a package
@@ -6696,6 +6700,9 @@ Please submit there instead, or use --nodevelproject to force direct submission.
         results' output. If the buildlog url is used buildlog command has the
         same behavior as remotebuildlog.
 
+        buildlog PROJECT PACKAGE [REPOSITORY] [ARCH]
+        buildlog PROJECT [REPOSITORY] [ARCH]
+        buildlog PACKAGE [REPOSITORY] [ARCH]
         buildlog [REPOSITORY ARCH | BUILDLOGURL]
         """
 
@@ -6708,30 +6715,52 @@ Please submit there instead, or use --nodevelproject to force direct submission.
         from .core import print_buildlog
         from .core import store_read_package
         from .core import store_read_project
+        from .core import is_package_dir
+        from .core import is_project_dir
 
         project = package = repository = arch = None
 
         apiurl = self.get_api_url()
+        repo_arg_offset = 0
 
         if len(args) == 1 and args[0].startswith('http'):
             apiurl, project, package, repository, arch = parse_buildlogurl(args[0])
         else:
-            project = store_read_project(Path.cwd())
-            package = store_read_package(Path.cwd())
-            if len(args) == 1:
+            cwd = Path.cwd()
+            if is_project_dir(cwd) or is_package_dir(cwd):
+                project = store_read_project(cwd)
+            if len(args) > 2:
+                project = args[0]
+                package = args[1]
+                repo_arg_offset = 2
+            if len(args) >= 1:
+                if opts.all_packages:
+                    project = args[0]
+                    repo_arg_offset = 1
+                else:
+                    if not package and is_project_dir(cwd):
+                        package = args[0]
+                        prj = Project(cwd)
+                        if package not in prj.pacs_avialable:
+                            raise oscerr.WrongArgs(f'Package {package} doesn\'t exist in {project}')
+                        repo_arg_offset = 1
+            if not package and is_package_dir(cwd):
+                    package = store_read_package(cwd)
+            if len(args) - repo_arg_offset == 1:
                 repository, arch = self._find_last_repo_arch(args[0], fatal=False)
                 if repository is None:
                     # no local build with this repo was done
                     print('failed to guess arch, using hostarch')
-                    repository = args[0]
+                    repository = args[repo_arg_offset]
                     arch = osc_build.hostarch
-            elif len(args) < 2:
+            elif len(args) - repo_arg_offset < 2:
                 self.print_repos()
-            elif len(args) > 2:
+            elif len(args) - repo_arg_offset > 2:
+                print(len(args), repo_arg_offset, project, package)
                 raise oscerr.WrongArgs('Too many arguments.')
             else:
-                repository = args[0]
-                arch = args[1]
+                repository = args[0 + repo_arg_offset]
+                arch = args[1 + repo_arg_offset]
 
         if opts.multibuild_package:
             package = package + ":" + opts.multibuild_package
@@ -6756,7 +6785,39 @@ Please submit there instead, or use --nodevelproject to force direct submission.
         elif opts.offset:
             offset = int(opts.offset)
         strip_time = opts.strip_time or conf.config['buildlog_strip_time']
-        print_buildlog(apiurl, project, package, repository, arch, offset, strip_time, opts.last, opts.lastsucceeded)
+
+        if opts.all_packages:
+            self.print_buildl_prj_pkgs(apiurl, project,
+                                 repository, arch, offset,
+                                 strip_time, opts.last, opts.lastsucceeded, opts.status)
+        else:
+            print_buildlog(apiurl, project, package, repository, arch, offset, strip_time, opts.last, opts.lastsucceeded)
+
+    def print_buildl_prj_pkgs(self,
+            apiurl: str,
+            prj: str,
+            repository: str,
+            arch: str,
+            offset=0,
+            strip_time=False,
+            last=False,
+            lastsucceeded=False,
+            status='succeeded'
+    ):
+        r = []
+        pkgs = []
+        for results in get_package_results(apiurl, prj,
+                                           None,
+                                           repository,
+                                           arch):
+            for res, is_multi in result_xml_to_dicts(results):
+                if res['code'] == status:
+                    pkgs.append(res['package'])
+        for pkg in pkgs:
+            with open(f'{prj}:{pkg}.log', 'w+b') as pkg_log_file:
+                print_buildlog(apiurl, prj, pkg,
+                               repository, arch,
+                               offset, strip_time, None, None, pkg_log_file)
 
     def print_repos(
         self, repos_only=False, exc_class=oscerr.WrongArgs, exc_msg="Missing arguments", project=None
openSUSE Build Service is sponsored by