浏览代码

上传文件至 'preset/app'

junhuanchen 1 年之前
父节点
当前提交
0536674cb3
共有 5 个文件被更改,包括 232 次插入0 次删除
  1. 39 0
      preset/app/checkout.py
  2. 118 0
      preset/app/git.py
  3. 5 0
      preset/app/ota.py
  4. 47 0
      preset/app/pull.py
  5. 23 0
      preset/app/reset.py

+ 39 - 0
preset/app/checkout.py

@@ -0,0 +1,39 @@
+localrep = "/home/backup"
+otaPath = 'ota.py'
+
+from maix import camera, display, image
+
+image.load_freetype("/root/preset/fonts/simhei.ttf")
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+hello_img.draw_string(10, 115, 'Checkout', scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+import git
+from io import RawIOBase
+
+
+class NewStream(RawIOBase):
+    def write(self, b):
+        hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+        hello_img.draw_string(10, 115, str(b), scale=1.0, color=(255, 255, 255), thickness=1)
+        display.show(hello_img)
+        return None
+
+
+git.rw()
+steam = NewStream()
+
+if git.activeBranch(localrep) != b'zh':
+    git.checkout(localrep, 'zh', steam)
+else:
+    git.checkout(localrep, 'en', steam)
+
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+hello_img.draw_string(10, 115, 'Reconstruct directory', scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+with open(otaPath) as f:
+    code = f.read()
+exec(code)
+
+git.ro()

+ 118 - 0
preset/app/git.py

@@ -0,0 +1,118 @@
+import os
+import sys
+from io import RawIOBase
+from dulwich import porcelain
+from dulwich.porcelain import get_remote_repo
+from dulwich.repo import Repo
+from dulwich.walk import Walker
+import requests
+
+
+def ro():
+    os.system("/etc/init.d/S01mount_ro start")
+
+
+def rw():
+    os.system("/etc/init.d/S01mount_ro stop")
+
+
+class NoneStream(RawIOBase):
+    """Fallback if stdout or stderr are unavailable, does nothing."""
+
+    def read(self, size=-1):
+        return None
+
+    def readall(self):
+        return None
+
+    def readinto(self, b):
+        return None
+
+    def write(self, b):
+        return None
+
+
+def log(localRep):
+    repo = Repo(localRep)
+    walker = Walker(repo, sorted(repo.get_refs().values(), reverse=True))
+    for entry in walker:
+        commit = entry.commit
+        print(f"commit {commit.id.decode()}")
+        print(f"Author: {commit.author.decode()} <{commit.author.decode()}>")
+        print(f"Date: {commit.author_time}")
+        print()
+        print(f"    {commit.message.decode()}")
+        print()
+
+
+def __log__(localRep):
+    repo = Repo(localRep)
+    walker = Walker(repo, sorted(repo.get_refs().values(), reverse=True))
+    for entry in walker:
+        commit = entry.commit
+        return f"commit {commit.id.decode()}", f"Author: {commit.author.decode()} <{commit.author.decode()}>", \
+               f"Date: {commit.author_time}", f"    {commit.message.decode()}"
+
+
+def showChanges(localRep, outStream=sys.stdout):
+    porcelain.show(localRep, outstream=outStream)
+
+
+def activeBranch(localRep):
+    return porcelain.active_branch(localRep)
+
+
+def checkout(localRep, branch, progStream=None):
+    r = Repo(localRep)
+    if progStream is None:
+        progStream = NoneStream()
+    try:
+        porcelain.checkout_branch(r, 'origin/' + branch, outstream=progStream)
+    except porcelain.CheckoutError as e:
+        print("CheckoutError:", e)
+        porcelain.reset(r, 'hard')
+        porcelain.checkout_branch(r, 'origin/' + branch, outstream=progStream)
+
+
+def pull(repo_path, progStream=None, depth=3):
+    if progStream is None:
+        progStream = NoneStream()
+    porcelain.pull(repo_path, refspecs=porcelain.active_branch(repo_path), outstream=progStream, depth=depth)
+
+
+def reset(localRep):
+    repo = Repo(localRep)
+    head_ref = repo.head()
+    head_commit = repo[head_ref]
+    parent_commit = repo[head_commit.parents[0]]
+    porcelain.reset(repo, 'hard', parent_commit.tree)
+
+
+def recovery(localRep):
+    r = Repo(localRep)
+    porcelain.reset(r, 'hard')
+
+
+def clone(source, target=None, depth: int = None):
+    porcelain.clone(source, target, depth=depth)
+
+
+def isOnline(localRep):
+    r = Repo(localRep)
+    (remote_name, remote_location) = get_remote_repo(r, None)
+    try:
+        response = requests.get(remote_location)
+        if response.status_code == 200:
+            # print("Connection successful.")
+            return True
+        else:
+            # print(f"Connection failed with status code: {response.status}")
+            return False
+    except Exception as e:
+        # print(f"Connection failed: {e}")
+        return False
+
+
+if __name__ == '__main__':
+    localrep = "/home/backup"
+    log(localrep)

+ 5 - 0
preset/app/ota.py

@@ -0,0 +1,5 @@
+import os
+
+os.system('chmod -R 777 /home')
+os.system('rsync -r --checksum /home/backup/ /root')
+os.system("ps | grep main.py | grep -v grep | awk '{print $1}' | xargs kill")

+ 47 - 0
preset/app/pull.py

@@ -0,0 +1,47 @@
+localrep = "/home/backup"
+otaPath = 'ota.py'
+
+from maix import camera, display, image  # 引入python模块包
+
+image.load_freetype("/root/preset/fonts/simhei.ttf")
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+hello_img.draw_string(10, 115, 'Pull', scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+import git, time
+
+if not git.isOnline(localrep):
+    hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+    hello_img.draw_string(10, 115, 'Network connection error', scale=1.0, color=(255, 255, 255), thickness=1)
+    display.show(hello_img)
+    time.sleep(2)
+    exit()
+
+from io import RawIOBase
+
+
+class NewStream(RawIOBase):
+    def write(self, b):
+        hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+        hello_img.draw_string(10, 115, str(b), scale=1.0, color=(255, 255, 255), thickness=1)
+        display.show(hello_img)
+        return None
+
+
+git.rw()
+steam = NewStream()
+git.pull(localrep, steam)
+
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+commit, Author, Date, message = git.__log__(localrep)
+hello_img.draw_string(10, 50, commit, scale=1.0, color=(255, 255, 255), thickness=1)
+hello_img.draw_string(10, 100, Author, scale=1.0, color=(255, 255, 255), thickness=1)
+hello_img.draw_string(10, 150, Date, scale=1.0, color=(255, 255, 255), thickness=1)
+hello_img.draw_string(10, 200, message, scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+with open(otaPath) as f:
+    code = f.read()
+exec(code)
+
+git.ro()

+ 23 - 0
preset/app/reset.py

@@ -0,0 +1,23 @@
+localrep = "/home/backup"
+otaPath = 'ota.py'
+
+from maix import camera, display, image
+
+image.load_freetype("/root/preset/fonts/simhei.ttf")
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+hello_img.draw_string(10, 115, 'Reset', scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+import git
+
+git.rw()
+git.reset(localrep)
+hello_img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")
+hello_img.draw_string(10, 115, 'Reconstruct directory', scale=1.0, color=(255, 255, 255), thickness=1)
+display.show(hello_img)
+
+with open(otaPath) as f:
+    code = f.read()
+exec(code)
+
+git.ro()