Advertisement
Techpad

Update.py

Mar 15th, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. import tkinter as tk
  2. import requests
  3. from threading import Thread
  4. from tkinter import messagebox as mb
  5. import time
  6. import sys
  7. import os
  8. import errno
  9.  
  10. root = tk.Tk()
  11. root.title("TechOS Update")
  12. root.attributes("-fullscreen", True)
  13. root.config(bg="#00afff", cursor="none")
  14.  
  15. step = tk.StringVar()
  16. step.set("Starting Update...")
  17.  
  18. percentage = tk.StringVar()
  19. percentage.set("0%")
  20.  
  21. updating = tk.Label(root, bg="#00afff", text="\n\n\n\n\nUpdating...", fg="white", font="TkDefaultFont 50")
  22. updating.pack(side="top", anchor="center", pady=20)
  23.  
  24. steplabel = tk.Label(root, bg="#00afff", textvariable=step, fg="white", font="TkDefaultFont 30")
  25. steplabel.pack(side="top", anchor="center", pady=20)
  26.  
  27. percentlabel = tk.Label(root, bg="#00afff", textvariable=percentage, fg="white", font="TkDefaultFont 40")
  28. percentlabel.pack(side="top", anchor="center", pady=20)
  29.  
  30. def mkdir_p(path):
  31. global os
  32. global errno
  33. try:
  34. os.makedirs(path)
  35. except OSError as exc:
  36. if exc.errno == errno.EEXIST and os.path.isdir(path):
  37. pass
  38. else: raise
  39.  
  40. def sow(path):
  41. global mkdir_p
  42. mkdir_p(os.path.dirname(path))
  43. return open(path, 'w')
  44.  
  45. def sowb(path):
  46. global mkdir_p
  47. mkdir_p(os.path.dirname(path))
  48. return open(path, 'wb')
  49.  
  50. def processcmd(cmd):
  51. params = cmd.split("|")
  52. if params[0] == "update" or params[0] == "updatefile":
  53. try:
  54. rawcode = requests.get(params[2]).text
  55. programfile = sowb(params[1])
  56. programfile.write(bytes(rawcode, "UTF-8"))
  57. programfile.close()
  58. except:
  59. updating.config(text="\n\n\n\n\nUpdate failed")
  60. step.set("The update seems to have invalid format.\nPlease contact Techpad for help.")
  61. percentage.set("Closing in 5...")
  62. time.sleep(1)
  63. percentage.set("Closing in 4...")
  64. time.sleep(1)
  65. percentage.set("Closing in 3...")
  66. time.sleep(1)
  67. percentage.set("Closing in 2...")
  68. time.sleep(1)
  69. percentage.set("Closing in 1...")
  70. time.sleep(1)
  71. root.destroy()
  72. sys.exit()
  73. else:
  74. updating.config(text="\n\n\n\n\nUpdate failed")
  75. step.set("The TechOS updater might be out of date.\nPlease contact Techpad for help.")
  76. percentage.set("Closing in 5...")
  77. time.sleep(1)
  78. percentage.set("Closing in 4...")
  79. time.sleep(1)
  80. percentage.set("Closing in 3...")
  81. time.sleep(1)
  82. percentage.set("Closing in 2...")
  83. time.sleep(1)
  84. percentage.set("Closing in 1...")
  85. time.sleep(1)
  86. root.destroy()
  87. sys.exit()
  88.  
  89. def startupdate():
  90. try:
  91. step.set("Checking latest update...")
  92.  
  93. cupfile = open("T:/TechOS/Virtual/Info/Version.info", "r")
  94. currentupdate = int(cupfile.read())
  95. cupfile.close()
  96. latestupdate = int(requests.get("https://pastebin.com/raw/BLuxc2Lm").text)
  97.  
  98. if currentupdate < latestupdate:
  99. percentage.set("1%")
  100. step.set("Getting update information...")
  101.  
  102. nextupdate = currentupdate + 1
  103. updateids = requests.get("https://pastebin.com/raw/M0r6cGSZ").text.replace("\r\n", "\n").split("\n")
  104. thisupdate = [i for i in updateids if i.startswith(str(nextupdate) + "|")][0]
  105. thisupdateid = thisupdate.split("|")[1]
  106. updateinfo = requests.get("https://pastebin.com/raw/" + thisupdateid).text
  107. updatecmds = updateinfo.replace("\r\n", "\n").split("\n")
  108.  
  109. percentage.set("2%")
  110. step.set("Installing update...")
  111.  
  112. percentint = 2
  113. percentageincrease = 98 / len(updatecmds)
  114.  
  115. for cmd in updatecmds:
  116. processcmd(cmd)
  117. percentint += percentageincrease
  118. percentage.set(str(round(percentint)) + "%")
  119.  
  120. percentage.set("100%")
  121. step.set("Finishing update...")
  122.  
  123. cupfile = open("T:/TechOS/Virtual/Info/Version.info", "w")
  124. cupfile.write(str(nextupdate))
  125. cupfile.close()
  126.  
  127. time.sleep(10)
  128.  
  129. updating.config(text="\n\n\n\n\nUpdate complete")
  130. step.set("You may need to run the updater again if you are behind on updates.")
  131. percentage.set("Closing in 5...")
  132. time.sleep(1)
  133. percentage.set("Closing in 4...")
  134. time.sleep(1)
  135. percentage.set("Closing in 3...")
  136. time.sleep(1)
  137. percentage.set("Closing in 2...")
  138. time.sleep(1)
  139. percentage.set("Closing in 1...")
  140. time.sleep(1)
  141. root.destroy()
  142. sys.exit()
  143. elif currentupdate == latestupdate:
  144. mb.showinfo("Software up to date", "Your version of TechOS is already up to date.")
  145. root.destroy()
  146. sys.exit()
  147. else:
  148. print(currentupdate)
  149. print(latestupdate)
  150. except:
  151. updating.config(text="\n\n\n\n\nUpdate failed")
  152. step.set("The TechOS update failed for an unknown reason.\nPlease contact Techpad for help.")
  153. percentage.set("Closing in 5...")
  154. time.sleep(1)
  155. percentage.set("Closing in 4...")
  156. time.sleep(1)
  157. percentage.set("Closing in 3...")
  158. time.sleep(1)
  159. percentage.set("Closing in 2...")
  160. time.sleep(1)
  161. percentage.set("Closing in 1...")
  162. time.sleep(1)
  163. root.destroy()
  164. sys.exit()
  165.  
  166. Thread(target=startupdate, daemon=True).start()
  167.  
  168. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement