Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pico_ota.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 uos
  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.blacklist = []
  36. def path(self, p):
  37. self.update_path = p
  38. def ignore(self, path):
  39. if not path in self.blacklist:
  40. self.blacklist.append(path)
  41. def fetch(self, url):
  42. # lazily initialize WiFi
  43. if self.get == None:
  44. self.get = util.getRequests()
  45. if self.get == None:
  46. return None
  47. try:
  48. #print("GET " + url)
  49. r = self.get(url)
  50. r.close()
  51. return r
  52. except Exception as e:
  53. print()
  54. if hasattr(sys, "print_exception"):
  55. sys.print_exception(e)
  56. else:
  57. print(e)
  58. print()
  59. return None
  60. def check(self, verbose = False):
  61. if self.branch == None:
  62. # get default branch
  63. r = self.fetch(self.host + "/api/v1/repos/" + self.repo).json()
  64. self.branch = r["default_branch"]
  65. if verbose:
  66. print("Selected default branch " + self.branch)
  67. # check for latest commit in branch
  68. r = self.fetch(self.host + "/api/v1/repos/" + self.repo + "/branches/" + self.branch).json()
  69. commit = r["commit"]["id"]
  70. if verbose:
  71. print("Latest commit is " + commit)
  72. current = ""
  73. try:
  74. f = open(os.path.join(self.update_path, "ota_version"), "r")
  75. current = f.readline().strip()
  76. f.close()
  77. except Exception as e:
  78. print()
  79. if hasattr(sys, "print_exception"):
  80. sys.print_exception(e)
  81. else:
  82. print(e)
  83. print()
  84. if verbose:
  85. if current != commit:
  86. print("Current commit " + current + " is different!")
  87. else:
  88. print("No update required")
  89. return (current != commit, commit)
  90. def update_to_commit(self, commit, verbose = False):
  91. # list all files for a commit
  92. r = self.fetch(self.host + "/api/v1/repos/" + self.repo + "/git/trees/" + commit).json()
  93. # TODO does not support sub-folders
  94. if verbose:
  95. if len(r["tree"]) > 0:
  96. print(str(len(r["tree"])) + " files in repo:")
  97. for f in r["tree"]:
  98. if f["path"] in self.blacklist:
  99. print(" - (IGNORED) " + f["path"])
  100. else:
  101. print(" - " + f["path"])
  102. else:
  103. print("No files in repo?!")
  104. for f in r["tree"]:
  105. if f["path"] in self.blacklist:
  106. continue
  107. # get a file from a commit
  108. r = self.fetch(self.host + "/" + self.repo + "/raw/commit/" + commit + "/" + f["path"]).text
  109. if verbose:
  110. print("Writing " + f["path"] + " to " + self.update_path)
  111. # overwrite existing file
  112. f = open(os.path.join(self.update_path, f["path"]), "w")
  113. f.write(r)
  114. f.close()
  115. # Write new commit id to local file
  116. f = open(os.path.join(self.update_path, "ota_version"), "w")
  117. f.write(commit + os.linesep)
  118. f.close()
  119. if __name__ == "__main__":
  120. ota = PicoOTA("https://git.xythobuz.de", "thomas/rgb-matrix-visualizer")
  121. # stuff not needed on Pico
  122. ota.ignore(".gitignore")
  123. ota.ignore("README.md")
  124. ota.ignore("copy.sh")
  125. ota.ignore("config.py")
  126. ota.ignore("fonts")
  127. ota.ignore("images")
  128. ota.ignore("bdf.py")
  129. ota.ignore("camp_small.py")
  130. ota.ignore("gamepad.py")
  131. ota.ignore("pi.py")
  132. ota.ignore("test.py")
  133. if not on_pico:
  134. if not os.path.exists("tmp"):
  135. os.makedirs("tmp")
  136. ota.path("tmp")
  137. newer, commit = ota.check(True)
  138. if newer:
  139. ota.update_to_commit(commit, True)