878f880
From 76793cc0ac34523aa237cad407cb8dcb816dac7b Mon Sep 17 00:00:00 2001
878f880
From: lethliel <mstrigl@suse.de>
878f880
Date: Wed, 11 Dec 2019 14:20:54 +0100
878f880
Subject: [PATCH] fix regression in osc chroot
878f880
878f880
This fixes some regressions with osc chroot:
878f880
878f880
- osc chroot --wipe --root=/dir/ can now be called outside
878f880
  a working copy
878f880
- osc chroot --noinit --root=/dir/ can now be called outside
878f880
  a working copy and behaves like the old code (Just entering
878f880
  the chroot without any modifications)
878f880
- The confirmation of the deletion is implemented again and thus
878f880
  the --force option was implemented too.
878f880
---
878f880
 osc/build.py       | 32 ++++++++++++++++++++++++++------
878f880
 osc/commandline.py | 32 +++++++++++++++++++++++++++++---
878f880
 2 files changed, 55 insertions(+), 9 deletions(-)
878f880
878f880
diff --git a/osc/build.py b/osc/build.py
878f880
index 8f599b85..46bd61f0 100644
878f880
--- a/osc/build.py
878f880
+++ b/osc/build.py
878f880
@@ -524,6 +524,32 @@ def get_kiwipath_from_buildinfo(apiurl, bi_filename, prj, repo):
878f880
     kiwipath.insert(0, myprp)
878f880
     return kiwipath
878f880
 
878f880
+def calculate_prj_pac(opts, descr):
878f880
+    project = opts.alternative_project or store_read_project('.')
878f880
+    if opts.local_package:
878f880
+        package = os.path.splitext(os.path.basename(descr))[0]
878f880
+    else:
878f880
+        package = store_read_package('.')
878f880
+    return project, package
878f880
+
878f880
+def calculate_build_root(apihost, prj, pac, repo, arch):
878f880
+    buildroot = os.environ.get('OSC_BUILD_ROOT', config['build-root']) \
878f880
+                            % {'repo': repo, 'arch': arch, 'project': prj, 'package': pac, 'apihost': apihost}
878f880
+    return buildroot
878f880
+
878f880
+def run_build(*args):
878f880
+    cmd = [config['build-cmd']]
878f880
+    cmd += args
878f880
+
878f880
+    sucmd = os.environ.get('OSC_SU_WRAPPER', config['su-wrapper']).split()
878f880
+    if sucmd[0] == 'su':
878f880
+        if sucmd[-1] == '-c':
878f880
+            sucmd.pop()
878f880
+        cmd = sucmd + ['-s', cmd[0], 'root', '--'] + cmd[1:]
878f880
+    else:
878f880
+        cmd = sucmd + cmd
878f880
+    return run_external(cmd[0], *cmd[1:])
878f880
+
878f880
 def main(apiurl, opts, argv):
878f880
 
878f880
     repo = argv[0]
878f880
@@ -696,12 +722,6 @@ def main(apiurl, opts, argv):
878f880
 
878f880
     if opts.shell:
878f880
         buildargs.append("--shell")
878f880
-        if os.path.exists(build_root) and os.path.exists(bi_filename) and not opts.clean and not opts.extra_pkgs:
878f880
-            opts.noinit = True
878f880
-            opts.offline = True
878f880
-            # we should check if the service did run before and only skip it then,
878f880
-            # but we have no save point for this atm
878f880
-            opts.noservice = True
878f880
 
878f880
     if opts.noinit:
878f880
         buildargs.append('--noinit')
878f880
diff --git a/osc/commandline.py b/osc/commandline.py
878f880
index 78e66e02..cbb8dfe0 100644
878f880
--- a/osc/commandline.py
878f880
+++ b/osc/commandline.py
878f880
@@ -6341,6 +6341,8 @@ Please submit there instead, or use --nodevelproject to force direct submission.
878f880
                   help=SUPPRESS_HELP)
878f880
     @cmdln.option('--shell-cmd', metavar='COMMAND',
878f880
                   help='run specified command instead of bash')
878f880
+    @cmdln.option('-f', '--force', action='store_true',
878f880
+                  help='Do not ask for confirmation to wipe')
878f880
     @cmdln.option('--host', metavar='HOST',
878f880
             help='perform the build on a remote server - user@server:~/remote/directory')
878f880
     @cmdln.option('--trust-all-projects', action='store_true',
878f880
@@ -6425,9 +6427,6 @@ Please submit there instead, or use --nodevelproject to force direct submission.
878f880
         if opts.debuginfo and opts.disable_debuginfo:
878f880
             raise oscerr.WrongOptions('osc: --debuginfo and --disable-debuginfo are mutual exclusive')
878f880
 
878f880
-        if subcmd == 'chroot' or subcmd == 'shell':
878f880
-            opts.shell = True
878f880
-
878f880
         if subcmd == 'wipe':
878f880
             opts.wipe = True
878f880
 
878f880
@@ -6450,6 +6449,33 @@ Please submit there instead, or use --nodevelproject to force direct submission.
878f880
                if not opts.vm_type:
878f880
                   opts.vm_type = lastbuildroot[2]
878f880
 
878f880
+        vm_chroot = opts.vm_type or conf.config['build-type']
878f880
+        if (subcmd in ('shell', 'chroot') or opts.shell or opts.wipe) and not vm_chroot:
878f880
+            if opts.root:
878f880
+                build_root = opts.root
878f880
+            else:
878f880
+                args = self.parse_repoarchdescr(args, opts.noinit or opts.offline, opts.alternative_project, False, opts.vm_type, opts.multibuild_package)
878f880
+                repo, arch, build_descr = args
878f880
+                prj, pac = osc.build.calculate_prj_pac(opts, build_descr)
878f880
+                apihost = urlsplit(self.get_api_url())[1]
878f880
+                build_root = osc.build.calculate_build_root(apihost, prj, pac, repo,
878f880
+                                                    arch)
878f880
+            if opts.wipe and not opts.force:
878f880
+                    # Confirm delete
878f880
+                    print("Really wipe '%s'? [y/N]: " % build_root)
878f880
+                    choice = raw_input().lower()
878f880
+                    if choice != 'y':
878f880
+                        print('Aborting')
878f880
+                        sys.exit(0)
878f880
+            build_args = ['--root=' + build_root, '--noinit', '--shell']
878f880
+            if opts.wipe:
878f880
+                build_args.append('--wipe')
878f880
+            sys.exit(osc.build.run_build(*build_args))
878f880
+        elif subcmd in ('shell', 'chroot') or opts.shell:
878f880
+            print('--shell in combination with build-type %s is experimental.' % vm_chroot)
878f880
+            print('The semantics may change at any time!')
878f880
+            opts.shell = True
878f880
+
878f880
         args = self.parse_repoarchdescr(args, opts.noinit or opts.offline, opts.alternative_project, False, opts.vm_type, opts.multibuild_package)
878f880
 
878f880
         if not opts.local_package:
878f880
-- 
878f880
2.23.0
878f880