forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
74 lines (68 loc) · 2.88 KB
/
__init__.py
File metadata and controls
74 lines (68 loc) · 2.88 KB
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
"""Unix 'kill' wrapper extension."""
import os
from signal import SIGKILL, SIGTERM
from albert import *
md_iid = '2.0'
md_version = "1.3"
md_name = "Kill Process"
md_description = "Kill processes"
md_license = "BSD-3"
md_url = "https://github.com/albertlauncher/python/tree/master/kill"
md_maintainers = "@Pete-Hamlin"
md_credits = "Original idea by Benedict Dudel & Manuel Schneider"
class Plugin(PluginInstance, TriggerQueryHandler):
def __init__(self):
TriggerQueryHandler.__init__(self,
id=md_id,
name=md_name,
description=md_description,
defaultTrigger='kill ')
PluginInstance.__init__(self, extensions=[self])
def handleTriggerQuery(self, query):
if not query.isValid:
return
results = []
uid = os.getuid()
for dir_entry in os.scandir("/proc"):
try:
if dir_entry.name.isdigit() and dir_entry.stat().st_uid == uid:
proc_command = (
open(os.path.join(dir_entry.path, "comm"), "r").read().strip()
)
if query.string in proc_command:
debug(proc_command)
proc_cmdline = (
open(os.path.join(dir_entry.path, "cmdline"), "r")
.read()
.strip()
.replace("\0", " ")
)
results.append(
StandardItem(
id="kill",
iconUrls=["xdg:process-stop"],
text=proc_command,
subtext=proc_cmdline,
actions=[
Action(
"terminate",
"Terminate process",
lambda pid=int(dir_entry.name): os.kill(
pid, SIGTERM
),
),
Action(
"kill",
"Kill process",
lambda pid=int(dir_entry.name): os.kill(
pid, SIGKILL
),
),
],
)
)
except FileNotFoundError: # TOCTOU dirs may disappear
continue
except IOError: # TOCTOU dirs may disappear
continue
query.add(results)