#!usr/bin/python
#-*-coding:utf-8-*-
#根据git变化,将变化的文件推送到手机上
import os
import sys
print("文件编码格式:"+ sys.getdefaultencoding())
###############配置变量###############
# 手机项目存储目录
prefix="storage/emulated/0/sdcard/project/"
projectName="XXXX"
# 忽略文件,不向手机发送的文件
gnoreFile=[
"README.md",
"compile.py",
"change.txt",
"tmp.txt",
"complate.txt",
"verifycode.txt",
]
############系统变量#######
changeFile="change.txt" #记录变化的文件
tmpFile="tmp.txt"#临时文件
##########################
#上传文件到手机
def uploadPhoneFile(change):
print("sync {} to {}".format(change,prefix+projectName+"/"+change))
pushCode ="adb push {} {}".format(change, prefix+projectName+"/"+change)
os.system(pushCode)
#创建文件夹到手机
def mkdirPhoneFile(change):
print("sync {} to {}".format(change,prefix+projectName+"/"+change))
pushCode ="adb shell mkdir -p {}".format(prefix+projectName+"/"+change)
os.system(pushCode)
#删除手机上的文件
def delletePhoneFile(change):
print("delete {}".format(prefix+projectName+"/"+change))
delCode ="adb shell rm {}".format(prefix+projectName+"/"+change)
os.system(delCode)
#删除手机上的文件夹
def delletePhoneFileDir(change):
print("delete {}".format(prefix+projectName+"/"+change))
delCode ="adb shell rm -r {}".format(prefix+projectName+"/"+change)
os.system(delCode)
#清空文件
def clearFile(name):
open(name, 'w').close()
#获取git变化并写入到文件
def diffGit():
diffGit = 'git status --short'
diff = os.popen(diffGit,'r')
diffStrs = diff.buffer.readlines()
clearFile(tmpFile)
ft = open(tmpFile,"a+",encoding='utf8')
for c in diffStrs:
change = c.decode(encoding='utf8').replace("\n", "")
if "../" not in change:
if len(change) > 0:
ft.write(change+"\n")
changes = change.split()
if len(changes) ==2 :
changeStr = changes[1]
if changeStr not in gnoreFile:
print(change.split()[0]+"---"+change)
if changes[0] == "M" or changes[0] == "??":
print("新增或修改文件:"+changeStr)
if os.path.isdir(changeStr):
mkdirPhoneFile(changeStr)
for root, dirs, files in os.walk(changeStr):
for f in files:
print(changeStr+f)
ft.write(changes[0]+" "+changeStr+f+"\n")
uploadPhoneFile(changeStr+f)
else:
uploadPhoneFile(changeStr)
elif changes[0] == "D" :
print("删除文件:"+changeStr)
delletePhoneFile(changeStr)
ft.close()
#对比上次同步结果
def writeChangeContent():
fch = open(changeFile,"r",encoding='utf8')
ftm = open(tmpFile,"r",encoding='utf8')
fclines = ftm.readlines()
for change in fch.readlines():
if change not in fclines:
print("-------上次变化,这次没有变化--------{}".format(change))
changes = change.split()
changeStr = changes[1]
if changes[0] == "M":
uploadPhoneFile(changeStr)
elif changes[0] == "??":
delletePhoneFileDir(changeStr)
elif changes[0] == "D" :
uploadPhoneFile(changeStr)
fch.close()
ftm.close()
#将这次结果放到变化文件
def copyStrToFile():
file1 = open(tmpFile,"r",encoding='utf8')
file2 = open(changeFile,"w",encoding='utf8')
s = file1.read()
w = file2.write(s)
file1.close()
file2.close()
diffGit()
writeChangeContent()
copyStrToFile()
#os.system("adb pull /storage/emulated/0/sdcard/XXX_tmp/")