Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pico_ota.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env python3
  2. # Uses the Gitea API to fetch the latest revision of the project from a repo.
  3. #
  4. # Inspired by:
  5. # https://github.com/olivergregorius/micropython_ota
  6. #
  7. # ----------------------------------------------------------------------------
  8. # "THE BEER-WARE LICENSE" (Revision 42):
  9. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  10. # you can do whatever you want with this stuff. If we meet some day, and you
  11. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  12. # ----------------------------------------------------------------------------
  13. import util
  14. import sys
  15. import os
  16. # to check if we're actually running on MicroPython
  17. on_pico = False
  18. try:
  19. import machine
  20. on_pico = True
  21. except Exception as e:
  22. print()
  23. if hasattr(sys, "print_exception"):
  24. sys.print_exception(e)
  25. else:
  26. print(e)
  27. print()
  28. class PicoOTA:
  29. def __init__(self, host, repo, branch = None):
  30. self.host = host
  31. self.repo = repo
  32. self.branch = branch
  33. self.get = None
  34. self.update_path = "."
  35. self.exe_path = ""
  36. self.version_file = "ota_version"
  37. self.blacklist = []
  38. def path(self, p):
  39. self.update_path = p
  40. def exe(self, e):
  41. self.exe_path = e
  42. def ignore(self, path):
  43. if not path in self.blacklist:
  44. self.blacklist.append(path)
  45. def fetch(self, url):
  46. # lazily initialize WiFi
  47. if self.get == None:
  48. self.get = util.getRequests()
  49. if self.get == None:
  50. return None
  51. try:
  52. #print("GET " + url)
  53. r = self.get(url)
  54. r.close()
  55. return r
  56. except Exception as e:
  57. print()
  58. if hasattr(sys, "print_exception"):
  59. sys.print_exception(e)
  60. else:
  61. print(e)
  62. print()
  63. return None
  64. def get_stored_commit(self):
  65. current = "unknown"
  66. try:
  67. f = open(os.path.join(self.update_path, self.version_file), "r")
  68. current = f.readline().strip()
  69. f.close()
  70. except Exception as e:
  71. print()
  72. if hasattr(sys, "print_exception"):
  73. sys.print_exception(e)
  74. else:
  75. print(e)
  76. print()
  77. return current
  78. def get_previous_commit(self, commit):
  79. r = self.fetch(self.host + "/" + self.repo + "/commit/" + commit).text
  80. for line in r.splitlines():
  81. if not (self.repo + "/commit/") in line:
  82. continue
  83. line = line[line.find("/commit/") : ][8 : ][ : 40]
  84. if line != commit:
  85. return line
  86. return "unknown"
  87. def check(self, verbose = False):
  88. if self.branch == None:
  89. # get default branch
  90. r = self.fetch(self.host + "/api/v1/repos/" + self.repo).json()
  91. self.branch = r["default_branch"]
  92. if verbose:
  93. print("Selected default branch " + self.branch)
  94. # check for latest commit in branch
  95. r = self.fetch(self.host + "/api/v1/repos/" + self.repo + "/branches/" + self.branch).json()
  96. commit = r["commit"]["id"]
  97. if verbose:
  98. print("Latest commit is " + commit)
  99. current = self.get_stored_commit()
  100. if verbose:
  101. if current != commit:
  102. print("Current commit " + current + " is different!")
  103. else:
  104. print("No update required")
  105. return (current != commit, commit)
  106. def update_to_commit(self, commit, verbose = False):
  107. # list all files for a commit
  108. r = self.fetch(self.host + "/api/v1/repos/" + self.repo + "/git/trees/" + commit).json()
  109. # TODO does not support sub-folders
  110. if verbose:
  111. if len(r["tree"]) > 0:
  112. print(str(len(r["tree"])) + " files in repo:")
  113. for f in r["tree"]:
  114. if f["path"] in self.blacklist:
  115. print(" - (IGNORED) " + f["path"])
  116. else:
  117. print(" - " + f["path"])
  118. else:
  119. print("No files in repo?!")
  120. for f in r["tree"]:
  121. if f["path"] in self.blacklist:
  122. continue
  123. # get a file from a commit
  124. r = self.fetch(self.host + "/" + self.repo + "/raw/commit/" + commit + "/" + f["path"]).text
  125. if verbose:
  126. print("Writing " + f["path"] + " to " + self.update_path)
  127. # overwrite existing file
  128. fo = open(os.path.join(self.update_path, f["path"]), "w")
  129. fo.write(r)
  130. fo.close()
  131. if f["path"] == self.exe_path:
  132. if verbose:
  133. print("Writing " + f["path"] + " to main.py")
  134. fo = open(os.path.join(self.update_path, "main.py"), "w")
  135. fo.write(r)
  136. fo.close()
  137. # Write new commit id to local file
  138. f = open(os.path.join(self.update_path, self.version_file), "w")
  139. f.write(commit + os.linesep)
  140. f.close()
  141. def non_pico_ota_test(ota):
  142. if not os.path.exists("tmp"):
  143. os.makedirs("tmp")
  144. ota.path("tmp")
  145. print("Checking for updates")
  146. newer, commit = ota.check(True)
  147. print()
  148. # Just for testing
  149. previous = ota.get_previous_commit(commit)
  150. print("Previous commit (-1):", previous)
  151. previous = ota.get_previous_commit(previous)
  152. print("Previous commit (-2):", previous)
  153. print()
  154. if newer:
  155. print("Updating")
  156. ota.update_to_commit(commit, True)
  157. else:
  158. print("No update required")
  159. def pico_ota_run(ota):
  160. newer, commit = ota.check(True)
  161. if newer:
  162. ota.update_to_commit(commit, True)
  163. machine.soft_reset()
  164. fallback = False
  165. try:
  166. import camp_pico
  167. except Exception as e:
  168. fallback = True
  169. print()
  170. if hasattr(sys, "print_exception"):
  171. sys.print_exception(e)
  172. else:
  173. print(e)
  174. print()
  175. # TODO this would immediately cause another update on reboot
  176. # TODO set a flag to prevent updates after fallbacks?
  177. # TODO or better, blacklist failed commit_id!
  178. #if fallback:
  179. # previous = ota.get_previous_commit(commit, True)
  180. # ota.update_to_commit(previous, True)
  181. # machine.soft_reset()
  182. if __name__ == "__main__":
  183. ota = PicoOTA("https://git.xythobuz.de", "thomas/rgb-matrix-visualizer")
  184. # stuff not needed on Pico
  185. ota.ignore(".gitignore")
  186. ota.ignore("README.md")
  187. ota.ignore("copy.sh")
  188. ota.ignore("config.py")
  189. ota.ignore("fonts")
  190. ota.ignore("images")
  191. ota.ignore("bdf.py")
  192. ota.ignore("camp_small.py")
  193. ota.ignore("gamepad.py")
  194. ota.ignore("pi.py")
  195. ota.ignore("test.py")
  196. ota.exe("camp_pico.py")
  197. if not on_pico:
  198. non_pico_ota_test(ota)
  199. else:
  200. pico_ota_run(ota)