Advertisement
MRtecno98

TechOS BETA V.1.3.8439

Oct 25th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. import subprocess , os , time , sys , enum , platform , pathlib
  2.  
  3. os.chdir(str(pathlib.Path.home()))
  4. wd = str(pathlib.Path.home())
  5. prompt = wd + ">"
  6. path = os.getcwd() + ";" + os.path.abspath(os.sep)
  7.  
  8. def getSystem() :
  9.     system = platform.system()
  10.     if system == "Windows" :
  11.         return 0
  12.     elif system == "Linux" :
  13.         return 1
  14.     else :
  15.         return 2
  16.  
  17. def initializePath() :
  18.     global path
  19.     if getSystem() == 0 :
  20.         path+=";" + os.environ["PATH"]
  21.  
  22. class Types(enum.Enum) :
  23.     TYPE = "<class 'type'>"
  24.     INTEGER = "<class 'int'>"
  25.  
  26. def handleBuiltIn(txt) :
  27.     global wd
  28.     txt = calculateFormat(txt)
  29.    
  30.     if txt[0] == "cd" :
  31.         if len(txt) < 2 :
  32.             print(wd)
  33.             return 0
  34.         else :
  35.             if checkPath(txt[1] , 1) :
  36.                     wd = os.path.abspath(txt[1])
  37.                     return 0
  38.             else :
  39.                 return 1
  40.  
  41.     if txt[0] == "ls" :
  42.         if len(txt) < 2 :
  43.             ls(wd)
  44.             return 0
  45.         else :
  46.             if checkPath(txt[1] , 1) :
  47.                 ls(txt[1])
  48.                 return 0
  49.             else :
  50.                 return 2
  51.  
  52.     if txt[0] == "exit" :
  53.         sys.exit(0)
  54.  
  55.     if txt[0] == "cat" :
  56.         if len(txt) < 2 :
  57.             print("Illegal Args")
  58.         else :
  59.             cat(txt[1])
  60.            
  61.     return 1
  62.        
  63. def ls(path) :
  64.     files = os.listdir(os.path.abspath(path))
  65.     totalsize = 0
  66.     print(".\n..")
  67.     for file in files :
  68.         if os.path.isfile(os.path.abspath(path) + os.sep + file) :
  69.             print(file)
  70.         else :
  71.             print(file + os.sep)
  72.         totalsize += os.path.getsize(os.path.abspath(path) + os.sep + file)
  73.     print("\nTotal Size: " + str(totalsize) + "B")
  74.  
  75. def cat(path) :
  76.     if checkPath(path , 0) :
  77.         with open(path) as file :
  78.             print(file.read())
  79.         return 0
  80.     else :
  81.         return 1
  82.  
  83. def checkPath(path , mode) :
  84.     if os.path.exists(path) :
  85.         if os.path.isdir(path) :
  86.             if mode == 0 :
  87.                 print("The path is a directory")
  88.                 return False
  89.             elif mode == 1 :
  90.                 return True
  91.         else :
  92.             if mode == 0 :
  93.                 return True
  94.             elif mode == 1 :
  95.                 print("The path is a file")
  96.                 return False
  97.     else :
  98.         print("Invalid Path")
  99.         return False
  100.  
  101. def updatePrompt() :
  102.     global prompt
  103.     global wd
  104.     prompt = wd + ">"
  105.     os.chdir(wd)
  106.  
  107. def calculateFormat(cmd) :
  108.     a = False
  109.     i = 0
  110.     cmd = list(cmd)
  111.     for c in cmd :
  112.         if c == " " :
  113.             cmd[i] = "§"
  114.         i += 1
  115.     cmd.append("§")
  116.  
  117.     calculated = list()
  118.     temp = ""
  119.     for c in cmd :
  120.         if c == "§" :
  121.             calculated.append(temp)
  122.             temp = ""
  123.             continue
  124.         temp+=c
  125.     return calculated
  126.  
  127. def getPrograms() :
  128.     global path
  129.     files = list()
  130.     chpath = os.getcwd() + ";" + path
  131.     paths = chpath.split(";")
  132.     for path in paths :
  133.         try :
  134.             pfiles = os.listdir(path)
  135.             for pfile in pfiles :
  136.                 files.append([path,pfile])
  137.         except :
  138.             continue
  139.     programs = list()
  140.  
  141.     for file in files :
  142.         if file[1].endswith(".exe") :
  143.             programs.append(file)
  144.    
  145.     return programs
  146.  
  147. def handle(txt) :
  148.     if txt == "" :
  149.         return 7
  150.     code = handleBuiltIn(txt)
  151.     if code < 1 :
  152.         return 0
  153.     elif code == 2 :
  154.         print("Command not found")
  155.    
  156.     programs = getPrograms()
  157.     for program in programs :
  158.         if txt.lower() == program[1].split(".")[0] :
  159.             try :
  160.                 subprocess.run(program[0] + os.sep + program[1], shell=True, check=True)
  161.                 return 0
  162.             except :
  163.                 print("Process exited with non-0 exit code")
  164.                 return 2
  165.     return 1
  166.  
  167. def handleError(error) :
  168.     if str(type(error)) == Types.INTEGER.value :
  169.         if error == 7 :
  170.             print(end='')
  171.            
  172.         if error == 1 :
  173.             print("Command not found")
  174.            
  175.         if error == -1 :
  176.             print("Exiting..." , end='')
  177.             time.sleep(1)
  178.             sys.exit(0)
  179.  
  180. debug = True
  181. def debug() :
  182.     if debug :
  183.         global path
  184.  
  185. def init() :
  186.     initializePath()
  187.     debug()
  188.  
  189. print("Tecnosoft OS [Versione 1.3.8439]")
  190. print("Copyright <c> 2017 MRtecno98 Inc. Tutti i diritti riservati")
  191.  
  192. def main() :
  193.     init()
  194.     try :
  195.         while True :
  196.             updatePrompt()
  197.             cmd = input("\n" + prompt)
  198.             handleError(handle(cmd))
  199.     except KeyboardInterrupt :
  200.         handleError(-1)
  201.  
  202. if __name__ == "__main__" :
  203.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement