2024-07-03 13:09:47 +10:00
|
|
|
#!/usr/bin/python
|
2024-07-03 12:08:56 +10:00
|
|
|
import os
|
2024-07-03 19:24:12 +10:00
|
|
|
import socket
|
2024-07-03 12:08:56 +10:00
|
|
|
import subprocess
|
2024-07-04 13:08:32 +10:00
|
|
|
import shutil
|
2024-07-05 09:26:10 +10:00
|
|
|
from datetime import datetime
|
2024-07-03 19:24:12 +10:00
|
|
|
from colorama import Fore
|
2024-07-04 09:49:07 +10:00
|
|
|
from os.path import expanduser
|
2024-07-03 12:08:56 +10:00
|
|
|
|
2024-07-04 09:49:07 +10:00
|
|
|
version = "0.1.0"
|
2024-07-03 13:31:27 +10:00
|
|
|
user = os.getlogin()
|
2024-07-04 09:49:07 +10:00
|
|
|
initDirectory = expanduser("~")
|
|
|
|
currentDirectory = initDirectory
|
2024-07-03 12:08:56 +10:00
|
|
|
|
2024-07-04 09:00:53 +10:00
|
|
|
argList = ["", ""]
|
|
|
|
|
2024-07-04 13:08:32 +10:00
|
|
|
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == True:
|
|
|
|
configFile = open(expanduser("~") + "/.config/mash/mash.conf")
|
|
|
|
else:
|
|
|
|
print("Config file does not exist. Copying default file to ~/.config/mash/mash.conf")
|
|
|
|
shutil.copyfile("/etc/mash/mash.conf", (expanduser("~") + "/.config/mash/mash.conf"))
|
|
|
|
|
2024-07-05 09:26:10 +10:00
|
|
|
historyFile = open((expanduser("~") + "/.history.mash"), "a")
|
|
|
|
historyFile.write("\n")
|
|
|
|
historyFile.write(("Session on " + str(datetime.now())))
|
|
|
|
historyFile.write("\n")
|
|
|
|
|
2024-07-04 09:49:07 +10:00
|
|
|
print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version)
|
|
|
|
|
2024-07-03 13:09:47 +10:00
|
|
|
try:
|
|
|
|
while True:
|
2024-07-04 09:49:07 +10:00
|
|
|
os.chdir(currentDirectory)
|
2024-07-03 19:24:12 +10:00
|
|
|
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
|
2024-07-04 09:00:53 +10:00
|
|
|
argNumber = 0
|
|
|
|
argList.clear()
|
|
|
|
for letter in command:
|
|
|
|
if letter == " ":
|
|
|
|
argNumber = argNumber + 1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
argList[argNumber] = argList[argNumber] + letter
|
|
|
|
except Exception:
|
|
|
|
argList.append(letter)
|
2024-07-05 09:26:10 +10:00
|
|
|
for word in argList:
|
|
|
|
if word == "&&":
|
|
|
|
print("This hasnt been implemented yet hahahahah")
|
2024-07-04 09:00:53 +10:00
|
|
|
if argList[0] == "help":
|
2024-07-03 14:46:40 +10:00
|
|
|
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
|
|
|
|
print("Some commands you can run:")
|
|
|
|
print("")
|
|
|
|
print("pwd Print directory you are currently in")
|
|
|
|
print("cd Change your directory.")
|
2024-07-04 13:08:32 +10:00
|
|
|
print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.")
|
|
|
|
print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.")
|
2024-07-03 14:46:40 +10:00
|
|
|
print("version Print current version of Mash.")
|
|
|
|
print("exit Exit Mash.")
|
|
|
|
print("")
|
|
|
|
print("You can also run commands installed by your system. See your OS's documentation for more information.")
|
2024-07-04 09:00:53 +10:00
|
|
|
elif argList[0] == "exit":
|
2024-07-03 13:09:47 +10:00
|
|
|
exit()
|
2024-07-04 09:00:53 +10:00
|
|
|
elif argList[0] == "version":
|
2024-07-03 13:09:47 +10:00
|
|
|
print(version)
|
2024-07-04 13:08:32 +10:00
|
|
|
elif argList[0] == "mat":
|
|
|
|
matFile = open(argList[1])
|
|
|
|
print(matFile.read())
|
|
|
|
elif argList[0] == "mls":
|
2024-07-04 17:20:49 +10:00
|
|
|
try:
|
|
|
|
if argList[1] == "":
|
|
|
|
print(os.listdir(currentDirectory))
|
|
|
|
else:
|
|
|
|
argList[1] = mlsDirectory
|
|
|
|
if mlsDirectory[0] == "/":
|
|
|
|
print(os.listdir(mlsDirectory))
|
|
|
|
else:
|
|
|
|
print(os.listdir(currentDirectory + "/" + mlsDirectory))
|
|
|
|
except Exception:
|
|
|
|
print(os.listdir(currentDirectory))
|
2024-07-04 09:49:07 +10:00
|
|
|
elif argList[0] == "cd":
|
|
|
|
try:
|
|
|
|
directoryForCD = argList[1]
|
|
|
|
if directoryForCD[0] == "~":
|
|
|
|
currentDirectory = expanduser("~") + directoryForCD.replace("~", "")
|
|
|
|
if directoryForCD[0] == "/":
|
|
|
|
currentDirectory = directoryForCD
|
|
|
|
else:
|
|
|
|
currentDirectory = (currentDirectory + "/" + directoryForCD)
|
|
|
|
except Exception:
|
|
|
|
print("Either a directory was not provided or an error occured.")
|
2024-07-05 09:26:10 +10:00
|
|
|
elif argList[0] == "history":
|
|
|
|
historyReadFile = open((expanduser("~") + "/.history.mash"))
|
|
|
|
print(historyReadFile.read())
|
|
|
|
elif argList[0] == "mtime":
|
|
|
|
print(datetime.now())
|
2024-07-03 13:09:47 +10:00
|
|
|
else:
|
|
|
|
try:
|
2024-07-04 09:00:53 +10:00
|
|
|
subprocess.run(argList)
|
2024-07-03 13:09:47 +10:00
|
|
|
except Exception:
|
|
|
|
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
|
2024-07-05 09:26:10 +10:00
|
|
|
|
|
|
|
historyFile = open((expanduser("~") + "/.history.mash"), "a")
|
|
|
|
historyFile.write(command)
|
|
|
|
historyFile.write("\n")
|
|
|
|
historyFile.close()
|
|
|
|
#except Exception:
|
|
|
|
# print("Looks like there was an error. Exiting...")
|
2024-07-03 13:09:47 +10:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print(" detected! Exiting...")
|