2020-03-30 16:11:23 +01:00
|
|
|
#!/usr/bin/python3
|
2014-03-28 15:44:13 +00:00
|
|
|
|
2009-07-27 11:45:34 +01:00
|
|
|
import os
|
2020-05-09 17:08:32 +01:00
|
|
|
from gi.repository import Gio, GLib, Gdk
|
2009-07-27 11:45:34 +01:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
|
2009-07-27 11:45:34 +01:00
|
|
|
def RemoveArgs(Execline):
|
2019-01-21 15:50:58 +00:00
|
|
|
if isinstance(Execline, list):
|
|
|
|
Execline = ' '.join(Execline)
|
|
|
|
|
|
|
|
Specials = ["%f", "%F", "%u", "%U", "%d", "%D", "%n", "%N", "%i", "%c", "%k", "%v", "%m", "%M",
|
|
|
|
"STARTED_FROM_MENU=yes"]
|
|
|
|
for spec in Specials:
|
|
|
|
if spec in Execline:
|
|
|
|
Execline = Execline.replace(spec, "")
|
|
|
|
|
|
|
|
return Execline
|
2009-07-27 11:45:34 +01:00
|
|
|
|
2020-03-31 12:29:07 +01:00
|
|
|
def dummy_child_watch (pid, status, data):
|
|
|
|
# Do nothing, this is just to ensure we don't double fork
|
|
|
|
# and break pkexec: https://bugzilla.gnome.org/show_bug.cgi?id=675789
|
|
|
|
pass
|
|
|
|
|
|
|
|
def gather_pid_callback(appinfo, pid, data):
|
|
|
|
GLib.child_watch_add(pid, dummy_child_watch, None)
|
|
|
|
|
2009-07-27 11:45:34 +01:00
|
|
|
# Actually execute the command
|
2020-05-09 17:08:32 +01:00
|
|
|
def Execute(cmd , commandCwd=None, desktopFile=None, offload=False):
|
2019-01-21 15:50:58 +00:00
|
|
|
if desktopFile:
|
|
|
|
launcher = Gio.DesktopAppInfo.new_from_filename(desktopFile)
|
2020-05-09 17:08:32 +01:00
|
|
|
context = Gdk.Display.get_default().get_app_launch_context()
|
|
|
|
if offload:
|
|
|
|
print("Offloading '%s' to discrete gpu." % launcher.get_name());
|
|
|
|
|
|
|
|
context.setenv("__NV_PRIME_RENDER_OFFLOAD", "1")
|
|
|
|
context.setenv("__GLX_VENDOR_LIBRARY_NAME", "nvidia");
|
|
|
|
|
|
|
|
try:
|
|
|
|
retval = launcher.launch_uris_as_manager(uris=[],
|
|
|
|
launch_context=context,
|
|
|
|
spawn_flags=GLib.SpawnFlags.SEARCH_PATH|GLib.SpawnFlags.DO_NOT_REAP_CHILD,
|
|
|
|
user_setup=None, user_setup_data=None,
|
|
|
|
pid_callback=gather_pid_callback, pid_callback_data=None)
|
|
|
|
return retval
|
|
|
|
except GLib.Error as e:
|
|
|
|
print("Error launching %s: %s" % (launcher.get_name(), e.message))
|
|
|
|
return False
|
2019-01-21 15:50:58 +00:00
|
|
|
|
|
|
|
cwd = os.path.expanduser("~")
|
|
|
|
|
|
|
|
if commandCwd:
|
|
|
|
tmpCwd = os.path.expanduser(commandCwd)
|
2016-05-21 17:11:36 +01:00
|
|
|
if (os.path.exists(tmpCwd)):
|
|
|
|
cwd = tmpCwd
|
|
|
|
|
|
|
|
cmd = RemoveArgs(cmd)
|
|
|
|
|
|
|
|
try:
|
2019-01-21 15:50:58 +00:00
|
|
|
os.chdir(cwd)
|
|
|
|
os.system(cmd + " &")
|
2016-05-21 17:11:36 +01:00
|
|
|
return True
|
2019-02-11 13:54:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2016-05-21 17:11:36 +01:00
|
|
|
return False
|