浏览代码

qvm-run: add color output and filtering escape sequences

This makes VM output clearly distinguishable
Marek Marczykowski-Górecki 10 年之前
父节点
当前提交
ac7746feed
共有 2 个文件被更改,包括 38 次插入4 次删除
  1. 9 1
      core-modules/000QubesVm.py
  2. 29 3
      qvm-tools/qvm-run

+ 9 - 1
core-modules/000QubesVm.py

@@ -1435,7 +1435,11 @@ class QubesVm(object):
                 print >>sys.stderr, "Failed to detach PCI device on the fly " \
                     "(%s), changes will be seen after VM restart" % str(e)
 
-    def run(self, command, user = None, verbose = True, autostart = False, notify_function = None, passio = False, passio_popen = False, passio_stderr=False, ignore_stderr=False, localcmd = None, wait = False, gui = True):
+    def run(self, command, user = None, verbose = True, autostart = False,
+            notify_function = None,
+            passio = False, passio_popen = False, passio_stderr=False,
+            ignore_stderr=False, localcmd = None, wait = False, gui = True,
+            filter_esc = False):
         """command should be in form 'cmdline'
             When passio_popen=True, popen object with stdout connected to pipe.
             When additionally passio_stderr=True, stderr also is connected to pipe.
@@ -1473,6 +1477,10 @@ class QubesVm(object):
         args = [system_path["qrexec_client_path"], "-d", str(xid), "%s:%s" % (user, command)]
         if localcmd is not None:
             args += [ "-l", localcmd]
+        if filter_esc:
+            args += ["-t"]
+        if os.isatty(sys.stderr.fileno()):
+            args += ["-T"]
         if passio:
             os.execv(system_path["qrexec_client_path"], args)
             exit(1)

+ 29 - 3
qvm-tools/qvm-run

@@ -76,6 +76,8 @@ def vm_run_cmd(vm, cmd, options):
 
     if options.verbose:
         print >> sys.stderr, "Running command on VM: '{0}'...".format(vm.name)
+    if options.passio and options.color_output:
+        print "\033[0;31m",
 
     try:
         def tray_notify_generic(level, str):
@@ -88,13 +90,19 @@ def vm_run_cmd(vm, cmd, options):
             verbose = options.verbose,
             user = options.user,
             notify_function = tray_notify_generic if options.tray else None,
-            passio = options.passio, localcmd = options.localcmd, gui = options.gui)
+            wait = options.passio, localcmd = options.localcmd,
+            gui = options.gui, filter_esc = options.filter_esc)
     except QubesException as err:
+        if options.passio and options.color_output:
+            print "\033[0m",
         if options.tray:
             tray_notify_error(str(err))
             notify_error_qubes_manager(vm.name, str(err))
         print >> sys.stderr, "ERROR(%s): %s" % (str(vm.name), str(err))
         return 1
+    finally:
+        if options.passio and options.color_output:
+            print "\033[0m",
 
 def main():
     usage = "usage: %prog [options] [<vm-name>] [<cmd>]"
@@ -106,7 +114,7 @@ def main():
                        help="Run command in a VM as a specified user")
     parser.add_option ("--tray", action="store_true", dest="tray", default=False,
                        help="Use tray notifications instead of stdout" )
- 
+
     parser.add_option ("--all", action="store_true", dest="run_on_all_running", default=False,
                       help="Run command on all currently running VMs (or all paused, in case of --unpause)")
 
@@ -138,6 +146,21 @@ def main():
     parser.add_option ("--nogui", action="store_false", dest="gui", default=True,
                       help="Run command without gui")
 
+    parser.add_option ("--filter-escape-chars", action="store_true",
+                       dest="filter_esc",
+                       default=os.isatty(sys.stdout.fileno()),
+                       help="Filter terminal escape sequences (default if "
+                            "output is terminal)")
+    parser.add_option("--no-filter-escape-chars", action="store_false",
+                      dest="filter_esc",
+                      help="Do not filter terminal escape sequences - "
+                           "overrides --filter-escape-chars, DANGEROUS when "
+                           "output is terminal")
+
+    parser.add_option("--no-color-output", action="store_false",
+                      dest="color_output", default=None,
+                      help="Disable marking VM output with red color")
+
     (options, args) = parser.parse_args ()
 
     if options.passio and options.run_on_all_running:
@@ -146,6 +169,9 @@ def main():
     if options.passio:
         options.verbose = False
 
+    if options.color_output is None:
+        options.color_output = os.isatty(sys.stdout.fileno())
+
     if options.shutdown:
         print >>sys.stderr, "WARNING: --shutdown is deprecated. Use qvm-shutdown instead."
 
@@ -213,7 +239,7 @@ def main():
 
         while len (vms_list):
             if options.verbose:
-                print >> sys.stderr, "Waiting for VMs: ", [vm.name for vm in vms_list] 
+                print >> sys.stderr, "Waiting for VMs: ", [vm.name for vm in vms_list]
             for vm in vms_list:
                 if not vm.is_running():
                     vms_list.remove (vm)