Advertisement
MRtecno98

TechOS BETA V.1.2.4657

Oct 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. import subprocess , os , enum , time , sys
  2.  
  3. class Types(enum.Enum) :
  4.     TYPE = "<class 'type'>"
  5.     INTEGER = "<class 'int'>"
  6.  
  7. wd = "C:\\"
  8. prompt = wd + ">"
  9. path = "."
  10.  
  11. def handleBuiltIn(txt) :
  12.     global wd
  13.  
  14.     txt = calculateFormat(txt)
  15.    
  16.     if txt[0] == "cd" :
  17.         if len(txt) < 2 :
  18.             print(wd)
  19.             return 0
  20.         else :
  21.             if checkPath(txt[1] , 1) :  
  22.                 wd = os.path.abspath(txt[1])
  23.                 return 0
  24.             else :
  25.                 return 1
  26.         return 0
  27.  
  28.     if txt[0] == "ls" :
  29.         if len(txt) < 2 :
  30.             ls(wd)
  31.             return 0
  32.         else :
  33.             if checkPath(txt[1] , 1) :
  34.                 ls(txt[1])
  35.                 return 0
  36.             else :
  37.                 return 1
  38.     return 2
  39.        
  40. def checkPath(path , mode) :
  41.     if os.path.exists(path) :
  42.         if os.path.isdir(path) :
  43.             if mode == 0 :
  44.                 print("The path is a directory")
  45.                 return False
  46.             elif mode == 1 :
  47.                 return True
  48.         else :
  49.             if mode == 0 :
  50.                 return True
  51.             elif mode == 1 :
  52.                 print("The path is a file")
  53.                 return False
  54.     else :
  55.         print("Invalid Path")
  56.         return False
  57.  
  58. def ls(path) :
  59.     files = os.listdir(os.path.abspath(path))
  60.     totalsize = 0
  61.     print(".\n..")
  62.     for file in files :
  63.         if os.path.isfile(os.path.abspath(path) + os.sep + file) :
  64.             print(file)
  65.         else :
  66.             print(file + os.sep)
  67.         totalsize += os.path.getsize(os.path.abspath(path) + os.sep + file)
  68.     print("\nTotal Size: " + str(totalsize) + "B")
  69.  
  70. def updatePrompt() :
  71.     global prompt
  72.     global wd
  73.     prompt = wd + ">"
  74.     os.chdir(wd)
  75.  
  76. def calculateFormat(cmd) :
  77.     i = 0
  78.     cmd = list(cmd)
  79.     for c in cmd :
  80.         if c == " " :
  81.             cmd[i] = "§"
  82.         i += 1
  83.     cmd.append("§")
  84.  
  85.     calculated = list()
  86.     temp = ""
  87.     for c in cmd :
  88.         if c == '§' :
  89.             calculated.append(temp)
  90.             temp = ""
  91.             continue
  92.         temp+=c
  93.  
  94.     return calculated
  95.  
  96. def getPrograms() :
  97.     files = os.listdir(path)
  98.     programs = list()
  99.  
  100.     for file in files :
  101.         if file.endswith(".exe") :
  102.             programs.append(file)
  103.  
  104.     return programs
  105.  
  106. def handle(txt) :
  107.     if txt == "" :
  108.         return 7
  109.     code = handleBuiltIn(txt)
  110.     if code < 1 :
  111.         return 0
  112.    
  113.     programs = getPrograms()
  114.     for program in programs :
  115.         if txt.lower() == program.split(".")[0] :
  116.             try :
  117.                 subprocess.run(path + os.sep + program, shell=True, check=True)
  118.                 return 0
  119.             except :
  120.                 print("Process exited with non-0 exit code")
  121.                 return 1
  122.     return code
  123.    
  124. def handleError(error) :
  125.     if str(type(error)) == Types.INTEGER.value :
  126.         if error == 7 :
  127.             print(end='')
  128.            
  129.         if error == 2 :
  130.             print("Command not found")
  131.            
  132.         if error == -1 :
  133.             print("Exiting..." , end='')
  134.             time.sleep(1)
  135.             sys.exit(0)
  136.  
  137. def main() :
  138.     print("Tecnosoft OS [Versione 1.2.4657]")
  139.     print("Copyright <c> 2017 MRtecno98 Inc. Tutti i diritti riservati")
  140.  
  141.     try :
  142.         while True :
  143.             updatePrompt()
  144.             txt = input("\n" + prompt)
  145.             handleError(handle(txt))
  146.     except KeyboardInterrupt :
  147.         handleError(-1)
  148.  
  149. if __name__ == "__main__" :
  150.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement