104 lines
2.6 KiB
Python
104 lines
2.6 KiB
Python
#!/usr/bin/python3
|
|
|
|
import os
|
|
import sys
|
|
import shutil
|
|
import threading
|
|
import re
|
|
import time
|
|
|
|
|
|
|
|
# 定义守护进程
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 日志文件路径
|
|
log_filepath = '/home/root/log/daemon_log.txt'
|
|
|
|
def write_info(text:str):
|
|
fm = '%Y-%m-%d %X'
|
|
nowtime = time.strftime(fm, time.localtime())
|
|
with open(log_filepath, 'a') as fp:
|
|
fp.write(nowtime+ " | "+text+"\n")
|
|
print(text)
|
|
def _do_cmd(cmd:str):
|
|
write_info(cmd)
|
|
ret = os.popen(cmd).readlines()
|
|
for i in range(len(ret)):
|
|
ret[i]=ret[i].strip()
|
|
write_info(ret[i])
|
|
return ret
|
|
|
|
class auto_updata(object):
|
|
def __init__(self):
|
|
self.sd_path="/run/media/sda"
|
|
self.file_path=self.sd_path+'/updata'
|
|
self.sd_inserd_state = False
|
|
self.file_list=[]
|
|
def sd_check(self):
|
|
ack=os.path.exists(self.sd_path)
|
|
if(ack!=self.sd_inserd_state):
|
|
if(ack==True):
|
|
write_info("sd inserd.")
|
|
self.copy_file()
|
|
else:
|
|
write_info("sd extracted.")
|
|
self.sd_inserd_state=ack
|
|
def copy_file(self):
|
|
self.file_list.clear()
|
|
try:
|
|
self.file_list=os.listdir(self.file_path)
|
|
except Exception as e:
|
|
write_info(str(e))
|
|
return False
|
|
for i in self.file_list:
|
|
write_info("|---| "+i)
|
|
_do_cmd("systemctl stop atk-qtapp-start.service")
|
|
_do_cmd("mkdir /home/root/config")
|
|
_do_cmd("cp "+self.find_file_by_type(".elf")+" /usr/local/QDesktop-fb")
|
|
_do_cmd("chmod 777 /usr/local/QDesktop-fb")
|
|
_do_cmd("cp "+self.find_file_by_type(".bin")+" /home/root/config/checker_slave.bin")
|
|
_do_cmd("cp "+self.find_file_by_type("scheme.json")+" /home/root/config/checker_ye_cfg.json")
|
|
_do_cmd("cp "+self.file_path+"/cfg.json /home/root/config/cfg.json")
|
|
_do_cmd("cp "+self.find_file_by_type(".axf")+" /lib/firmware/checker_m4.axf")
|
|
# _do_cmd("cp "+self.find_file_by_type(".dtb")+" /boot/stm32mp157d-atk.dtb")
|
|
_do_cmd("sync")
|
|
_do_cmd("systemctl restart atk-qtapp-start.service")
|
|
write_info("autoupdata end.")
|
|
return True
|
|
def find_file_by_type(self,type:str):
|
|
for i in self.file_list:
|
|
if(i[-len(type):]==type):
|
|
return self.file_path+'/'+i
|
|
return "unknown"
|
|
|
|
if __name__ == '__main__':
|
|
dir_name=os.path.dirname(log_filepath)
|
|
if not os.path.exists(dir_name):
|
|
os.makedirs(dir_name)
|
|
updata=auto_updata()
|
|
while True:
|
|
updata.sd_check()
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
# if __name__ == "__main__":
|
|
# scan_files("/run/media")
|
|
|
|
|
|
|
|
|
|
|