-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.py
55 lines (45 loc) · 1.84 KB
/
terminal.py
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
import subprocess
import os
from pathlib import Path
class Terminal:
""" Class to represent an instance of a terminal which is initiated
by a discord user via the discord bot.
"""
def __init__(self, name, terminalPath, directoryPath=str(Path.home())):
"""Object constructor"""
# The name of the terminal instance (supplied when the
# constructor is called)
self.name = name
# The path of the actual terminal program to be run via
# discord bot
self.terminalPath = terminalPath
# The current working directory that the user is in (starts
# in the home directory)
self.currentDirectory = directoryPath
def executeCommand(self, command):
""" Execute a bash terminal command (passed as an argument to
the function) on the machine running the bot. Returns a
string message which can be displayed for the user.
"""
# Create a Popen object which will take in the command and
# the working directory and run the command when communicate
# is called
self.terminalInstance = subprocess.Popen(
[command],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = True,
cwd = self.currentDirectory
)
# Use communicate to run the command and get the bytes-like
# objects as output/error
stdout, stderr = self.terminalInstance.communicate()
# Object containing data returned by the terminal after the
# command was executed
return ({
"terminalName": self.name,
"currentDirectory": self.currentDirectory,
"executedCommand": command,
"outputString": stdout.decode("utf-8"),
"errorString": stderr.decode("utf-8")
})