百度网盘爬虫百度图片爬虫后这是本人第三篇有关python的文章了,由于本人之前做过嵌入式的一些东西,其中会涉及到差分包的制作,所以这篇文章想谈谈如何利用python来制作差分包,如果你对嵌入式的东西不是感兴趣,可以看看我做的网站:去转盘网或者ok搜搜,本人也是个互联网开发爱好者。

不喜欢说那么多,直接上代码:

#!/usr/bin/env python2.7
import os
import sys
import commands

CurPath = ""


class Script(object):
def __init__(self, filepath):
self.HomePath = "/mnt"
self.TargetPath = "/system"
commands.getstatusoutput("rm -rf " + filepath)
os.mkdir(filepath)
self.PatchPath = "patchfiles"
os.mkdir(filepath + "/" + self.PatchPath)
os.mkdir(filepath + "/script")
self.FD = open(filepath + "/script/update.script", "w")
self.zipPath = filepath
self.FD.writelines("mount -t ext4 /dev/ndsystem /system\n")

def getPatchPath(self):
return self.zipPath + "/" + self.PatchPath

def baseName(self, filename):
assert isinstance(filename, str)
s = filename.rfind("/")
return filename[s + 1:len(filename)]

def dirName(self, filename):
assert isinstance(filename, str)
s = filename.rfind("/")
return filename[0:s]

def getSrcFileName(self, filename):
return self.HomePath + "/" + self.baseName(filename)

def chmod(self, mode, s):
self.FD.writelines(("chmod %o %s\n" % (mode, self.TargetPath + s)))

def chown(self, uid, gid, s):
self.FD.writelines(("chown %d:%d %s\n" % (gid, uid, self.TargetPath + s)))

def copy(self, s):
self.FD.writelines("mkdir -p %s\n" % self.dirName(self.TargetPath + s))
self.FD.writelines("cp -f %s %s\n" % (self.getSrcFileName(s), self.TargetPath + s))

def mkDir(self, s):
self.FD.writelines("mkdir -p %s\n" % (self.TargetPath + s))

def delete(self, s):
self.FD.writelines("rm -rf %s\n" % (self.TargetPath + s))

def deleteSrc(self, s):
self.FD.writelines("rm -rf %s\n" % (self.getSrcFileName(s)))

def patch(self,s):
self.FD.writelines("/bin/bspatch %s %s %s\n" % (self.TargetPath + s, self.TargetPath + s, self.getSrcFileName(s)))

def ln(self,s,t):
self.FD.writelines("ln -s %s %s\n" % (s,self.TargetPath + t))

def outStr(self, s):
self.FD.writelines(s)

def extract(self, s):
self.FD.writelines("extract %s\n" % (self.PatchPath + s))

def __del__(self):
self.FD.writelines("reboot\n");
self.FD.close()


class Package(object):
def __init__(self, fs_config, dir_path, scrip):
self.PackagePath = dir_path
self.fsStats = []
self.fileList = []
self.ReadDir(dir_path, dir_path)
self.ReadConfig(fs_config)
self.Gen = scrip
print(dir_path)

def getPath(self):
return self.PackagePath

def runCommand(self, cmd):
(status, output) = commands.getstatusoutput(cmd)
return status

def ChFsStat(self, filename):
for f in self.fsStats:
if (filename == f.Path):
self.Gen.chmod(f.Mode, filename)
self.Gen.chown(f.Gid, f.Uid, filename)
return
self.Gen.chmod(int("0755", 8), filename)
self.Gen.chown(1000, 0, filename)

#http://www.quzhuanpan.com/

def Patch(self, filename, p):
fn = self.PackagePath + filename
if os.path.islink(fn):
if (os.readlink(fn) != os.readlink(p.getPath() + filename)):
self.Gen.delete(filename)
self.Gen.ln(os.readlink(fn),filename)
return
if not os.path.isdir(fn):
command = "diff " + fn + " " + p.getPath() + filename
# print(command)
if (self.runCommand(command) != 0):
command = "mkdir -p " + self.Gen.getPatchPath() + os.path.dirname(filename)
self.runCommand(command)
command = CurPath + "/bsdiff " + p.getPath() + filename + " " + fn + " " + self.Gen.getPatchPath() + filename
print(command)
self.runCommand(command)
self.Gen.extract(filename)
self.Gen.patch(filename)
self.ChFsStat(filename)
self.Gen.deleteSrc(filename)

def NewFile(self, filename, p):
fn = self.PackagePath + filename
if os.path.isdir(fn):
if os.listdir(fn):
return
self.Gen.mkDir(filename)
self.ChFsStat(filename)
return
if os.path.islink(fn):
self.Gen.ln(os.readlink(fn),filename)
return
if os.path.dirname(filename) != "/":
command = "mkdir -p " + self.Gen.getPatchPath() + os.path.dirname(filename)
self.runCommand(command)
print(command)
command = "cp -lf " + self.PackagePath + filename + " " + self.Gen.getPatchPath() + filename
print(command)
self.runCommand(command)
self.Gen.extract(filename)
self.Gen.copy(filename)
self.ChFsStat(filename)
self.Gen.deleteSrc(filename)

def DelFile(self, filename, p):
command = "rm -rf " + filename
print(command)
self.Gen.delete(filename)

class FsStat:
def __init__(self, _str):
s = str(_str[1])
s = s.lstrip("'")
if len(s) > 0:
s = s.rstrip("'")
self.Path = s.replace("system/", "/")
s = str(_str[2])
s = s.lstrip("'")
if len(s) > 0:
s = s.rstrip("'")
self.Prefix = s
self.Uid = int(_str[3])
self.Gid = int(_str[4])
s = _str[5]
s = s[len(s) - 4:len(s)]
self.Mode = int(s, 8)

#http://www.quzhuanpan.com/home/sourceList.jsp?type=1
def ReadDir(self, dir_path, src_path):
for dir in os.listdir(dir_path):
subdir = dir_path + "/" + dir
self.fileList.append(subdir.replace(src_path, ""))
if os.path.isdir(subdir):
self.ReadDir(subdir, src_path)


def ReadConfig(self, fs_config):
fileName = fs_config
fd = open(fileName, "r")
for line in fd.readlines():
# print(line)
if line.startswith("<"):
str = line.split(" ")
self.fsStats.append(self.FsStat(str))
fd.close()


def DumpFileList(self):
for file in self.fileList:
print(file)

def DumpFsStats(self):
for fs_stat in self.fsStats:
assert isinstance(fs_stat, self.FsStat)
print("%s, %s, %d, %d, %o" % (fs_stat.Path, fs_stat.Prefix, fs_stat.Uid, fs_stat.Gid, fs_stat.Mode))

def checkFile(self):
for file_st in self.fsStats:
# print("CheckFile file_st.Path dddd %s %d" % (file_st.Path,len(self.fileList)))
try:
self.fileList.index(file_st.Path)
except:
print("CheckFile file_st.Path %s" % (file_st.Path))
return False
return True


def DiffPackage(old, new):
assert isinstance(new, Package)
assert isinstance(old, Package)
for s in new.fileList:
try:
if old.fileList.index(s) >= 0:
# print("xxxxx %s, index:%d oldfile:%s " % (s,old.fileList.index(s), old.fileList[old.fileList.index(s)]))
new.Patch(s, old)
except:
new.NewFile(s, old)

for s in old.fileList:
try:
new.fileList.index(s)
except:
old.DelFile(s, old)


if __name__ == '__main__':
CurPath = sys.argv[1]
script = Script(CurPath + "/updatezip")

oldpackage = Package(CurPath + "/old_fs_config.txt", CurPath + "/rootfs_system_old", script)
# oldpackage.DumpFileList()
#oldpackage.DumpFsStats()
if (oldpackage.checkFile() == False):
print("Old Package Fail!")
newpackage = Package(CurPath + "/new_fs_config.txt", CurPath + "/rootfs_system_new", script)
#oldpackage.DumpFileList()
#newpackage.DumpFileList()
#newpackage.DumpFsStats()
if (newpackage.checkFile() == False):
print("New Package Fail!")
DiffPackage(oldpackage, newpackage)