]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
command: Add function to call shell commands
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 12:42:07 +0000 (12:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 12:42:07 +0000 (12:42 +0000)
These commands will be executed and an error might be shown if something
went wrong.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py

index 724aca3b1d4ea9082fbb8250827b36ac3d9117b9..4c09d38f63fcd6662fff92aa3f167c2601d619d5 100644 (file)
@@ -19,6 +19,7 @@
 ###############################################################################
 
 import logging
+import subprocess
 import sys
 import traceback
 
@@ -95,6 +96,29 @@ class Bricklayer(object):
                                except InstallAbortedError:
                                        return 1
 
+                               # Catch any failed commands
+                               except subprocess.CalledProcessError as e:
+                                       args = {
+                                               "command"    : " ".join(e.cmd),
+                                               "output"     : e.output.decode(),
+                                               "returncode" : e.returncode,
+                                       }
+
+                                       # Log the error
+                                       log.error("Command \"%(command)s\" failed with error code "
+                                               "%(returncode)s:\n%(output)s" % args)
+
+                                       # Format the error message
+                                       error = _("Command \"%(command)s\" failed with error code "
+                                               "%(returncode)s:\n\n%(output)s" % args)
+
+                                       # Show it
+                                       self.tui.error(_("An Unexpected Error Occured"), error,
+                                               buttons=[_("Exit")], width=78)
+
+                                       # Exit
+                                       return e.returncode
+
                                # Catch all other exceptions and show an error
                                except:
                                        type, value, tb = sys.exc_info()
@@ -133,3 +157,24 @@ class Bricklayer(object):
                """
                with open("/etc/os-release") as f:
                        return util.config_read(f)
+
+       def command(self, command):
+               """
+                       Runs a command in a shell environment
+               """
+               log.debug("Running command: %s" % " ".join(command))
+
+               # Execute the command
+               p = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+
+               # Check the return code (raises CalledProcessError on non-zero)
+               p.check_returncode()
+
+               # Decode output
+               output = p.stdout.decode()
+
+               # Log output
+               if output:
+                       log.debug(output)
+
+               return output