Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess , os , time , sys , enum , platform , pathlib
- os.chdir(str(pathlib.Path.home()))
- wd = str(pathlib.Path.home())
- prompt = wd + ">"
- path = os.getcwd() + ";" + os.path.abspath(os.sep)
- def getSystem() :
- system = platform.system()
- if system == "Windows" :
- return 0
- elif system == "Linux" :
- return 1
- else :
- return 2
- def initializePath() :
- global path
- if getSystem() == 0 :
- path+=";" + os.environ["PATH"]
- class Types(enum.Enum) :
- TYPE = "<class 'type'>"
- INTEGER = "<class 'int'>"
- def handleBuiltIn(txt) :
- global wd
- txt = calculateFormat(txt)
- if txt[0] == "cd" :
- if len(txt) < 2 :
- print(wd)
- return 0
- else :
- if checkPath(txt[1] , 1) :
- wd = os.path.abspath(txt[1])
- return 0
- else :
- return 1
- if txt[0] == "ls" :
- if len(txt) < 2 :
- ls(wd)
- return 0
- else :
- if checkPath(txt[1] , 1) :
- ls(txt[1])
- return 0
- else :
- return 2
- if txt[0] == "exit" :
- sys.exit(0)
- if txt[0] == "cat" :
- if len(txt) < 2 :
- print("Illegal Args")
- else :
- cat(txt[1])
- return 1
- def ls(path) :
- files = os.listdir(os.path.abspath(path))
- totalsize = 0
- print(".\n..")
- for file in files :
- if os.path.isfile(os.path.abspath(path) + os.sep + file) :
- print(file)
- else :
- print(file + os.sep)
- totalsize += os.path.getsize(os.path.abspath(path) + os.sep + file)
- print("\nTotal Size: " + str(totalsize) + "B")
- def cat(path) :
- if checkPath(path , 0) :
- with open(path) as file :
- print(file.read())
- return 0
- else :
- return 1
- def checkPath(path , mode) :
- if os.path.exists(path) :
- if os.path.isdir(path) :
- if mode == 0 :
- print("The path is a directory")
- return False
- elif mode == 1 :
- return True
- else :
- if mode == 0 :
- return True
- elif mode == 1 :
- print("The path is a file")
- return False
- else :
- print("Invalid Path")
- return False
- def updatePrompt() :
- global prompt
- global wd
- prompt = wd + ">"
- os.chdir(wd)
- def calculateFormat(cmd) :
- a = False
- i = 0
- cmd = list(cmd)
- for c in cmd :
- if c == " " :
- cmd[i] = "§"
- i += 1
- cmd.append("§")
- calculated = list()
- temp = ""
- for c in cmd :
- if c == "§" :
- calculated.append(temp)
- temp = ""
- continue
- temp+=c
- return calculated
- def getPrograms() :
- global path
- files = list()
- chpath = os.getcwd() + ";" + path
- paths = chpath.split(";")
- for path in paths :
- try :
- pfiles = os.listdir(path)
- for pfile in pfiles :
- files.append([path,pfile])
- except :
- continue
- programs = list()
- for file in files :
- if file[1].endswith(".exe") :
- programs.append(file)
- return programs
- def handle(txt) :
- if txt == "" :
- return 7
- code = handleBuiltIn(txt)
- if code < 1 :
- return 0
- elif code == 2 :
- print("Command not found")
- programs = getPrograms()
- for program in programs :
- if txt.lower() == program[1].split(".")[0] :
- try :
- subprocess.run(program[0] + os.sep + program[1], shell=True, check=True)
- return 0
- except :
- print("Process exited with non-0 exit code")
- return 2
- return 1
- def handleError(error) :
- if str(type(error)) == Types.INTEGER.value :
- if error == 7 :
- print(end='')
- if error == 1 :
- print("Command not found")
- if error == -1 :
- print("Exiting..." , end='')
- time.sleep(1)
- sys.exit(0)
- debug = True
- def debug() :
- if debug :
- global path
- def init() :
- initializePath()
- debug()
- print("Tecnosoft OS [Versione 1.3.8439]")
- print("Copyright <c> 2017 MRtecno98 Inc. Tutti i diritti riservati")
- def main() :
- init()
- try :
- while True :
- updatePrompt()
- cmd = input("\n" + prompt)
- handleError(handle(cmd))
- except KeyboardInterrupt :
- handleError(-1)
- if __name__ == "__main__" :
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement