mash/main.py

180 lines
7.1 KiB
Python
Executable File

#!/usr/bin/python
# Imports, of course.
# You'll need to install colorama from Pip if you want to use colours in the terminal. I'm probably going to replace this when I get around to making a config file properly.
import os
import socket
import subprocess
import shutil
import curses
from datetime import datetime
from colorama import Fore
from os.path import expanduser
# Some initial variables
version = "0.2.0"
user = os.getlogin()
initDirectory = expanduser("~")
argList = ["", ""]
# Start work on checking whether a config file exists
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == False:
print("Config file does not exist. Make a file at .config/mash/mash.conf to configure things")
# Parse config file, adding variables
configFileFile = open((expanduser("~") + "/.config/mash/mash.conf"))
configFile = configFileFile.read()
confArgNumber = 0
confArgList = ["", ""]
for line in configFile:
for letter in line:
if letter == ':':
confArgList[confArgNumber] = confArgList[confArgNumber] + ":"
confArgNumber = confArgNumber + 1
elif letter == ',':
confArgList.append(",")
confArgNumber = confArgNumber + 1
else:
try:
confArgList[confArgNumber] = confArgList[confArgNumber] + letter
except Exception:
confArgList.append(letter)
for word in confArgList:
if "\n" in word:
confArgList[confArgList.index(word)] = word.replace("\n", "")
for word in confArgList:
if word == "motd:":
motd = confArgList[confArgList.index(word) + 1]
if word == ",mashPath:":
mashPathStr = (confArgList[confArgList.index(word) + 1])
pathStrIndex = 0
mashPath = [""]
for letter in mashPathStr:
if letter == ";":
pathStrIndex = pathStrIndex + 1
mashPath.append("")
else:
try:
mashPath[pathStrIndex] = mashPath[pathStrIndex] + letter
except Exception:
mashPath.append(letter)
# Write the time to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write("\n")
historyFile.write(("Session on " + str(datetime.now())))
historyFile.write("\n")
# Welcome message. Configurable with "motd: (your motd here),"
print(motd)
os.chdir(initDirectory)
currentDirectory = initDirectory
# Main loop.
def mainLoop(startDir):
try:
currentDirectory = startDir
while True:
# Set directory and prompt for input.
os.chdir(currentDirectory)
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
# Clean some things up for the argument parser.
argNumber = 0
argList.clear()
# Argument parser
for letter in command:
if letter == " ":
argNumber = argNumber + 1
else:
try:
argList[argNumber] = argList[argNumber] + letter
except Exception:
argList.append(letter)
# Start work on pipes.
for word in argList:
if word == "&&":
print("This hasnt been implemented yet hahahahah")
elif word == "&":
print("This hasnt been implemented yet heheheheh")
elif word == "|":
print("What are you? A pipe master? We dont like those people here")
# Built in commands section
if argList[0] == "help":
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("cd Change your directory.")
print("history See previously ran commands.")
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.")
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.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
elif argList[0] == "mat":
matFile = open(argList[1])
print(matFile.read())
elif argList[0] == "mls":
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))
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.")
elif argList[0] == "history":
historyReadFile = open((expanduser("~") + "/.history.mash"))
print(historyReadFile.read())
elif argList[0] == "mtime":
print(datetime.now())
elif argList[0] == "printconf":
print(configFile)
# If a built in command isn't what you wanted, run a new subprocess.
else:
for word in mashPath:
if os.path.isfile(word + "/" + argList[0]):
argList[0] = (word + "/" + argList[0])
try:
subprocess.run(argList)
except Exception:
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
# Write to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write(command)
historyFile.write("\n")
historyFile.close()
# Handle errors in the main loop
#except Exception:
#print("Looks like there was an error. Exiting...")
# Handle ^C
except KeyboardInterrupt:
print("\n")
mainLoop(currentDirectory)
mainLoop(initDirectory)