File: execute.py

package info (click to toggle)
mate-menu 22.04.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,028 kB
  • sloc: python: 3,777; xml: 364; makefile: 15; sh: 2
file content (77 lines) | stat: -rw-r--r-- 2,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-

# Copyright (C) 2007-2014 Clement Lefebvre <root@linuxmint.com>
# Copyright (C) 2015 Martin Wimpress <code@ubuntu-mate.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os

from gi.repository import Gdk, Gtk, Gio

def RemoveArgs(Execline):
	NewExecline = []
	Specials=["\"%c\"", "%f","%F","%u","%U","%d","%D","%n","%N","%i","%c","%k","%v","%m","%M", "-caption", "/bin/sh", "sh", "-c", "STARTED_FROM_MENU=yes"]
	for elem in Execline:
		elem = elem.replace("'","")
		elem = elem.replace("\"", "")
		if elem not in Specials:
			print(elem)
			NewExecline.append(elem)
	return NewExecline

# Actually launch the application
def Launch(cmd, cwd=None):
	if cwd:
		os.chdir(cwd)

	app_info = Gio.AppInfo.create_from_commandline(cmd,
						       None,
						       Gio.AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION)

	display = Gdk.Display.get_default()
	context = display.get_app_launch_context()
	context.set_desktop(-1) # use default screen & desktop
	context.set_timestamp(Gtk.get_current_event_time())

	app_info.launch(None, context)

def Execute(cmd , commandCwd=None):
	if commandCwd:
		tmpCwd = os.path.expanduser( commandCwd );
		if (os.path.exists(tmpCwd)):
			cwd = tmpCwd
	else:
		cwd = None

	if isinstance( cmd, str ):
		if (cmd.find("/home/") >= 0) or (cmd.find("xdg-su") >= 0) or (cmd.find("\"") >= 0):
			try:
				Launch(cmd, cwd)
				return True
			except Exception as detail:
				print(detail)
				return False
	cmd = cmd.split()
	cmd = RemoveArgs(cmd)

	try:
		string = ' '.join(cmd)
		Launch(string, cwd)
		return True
	except Exception as detail:
		print(detail)
		return False