Following the Baidu network disk crawler, Baidu image crawler After this is my third article about Python, because I have done some embedded things, which will involve the production of differential packets, so this article would like to talk about how to use Python to make the difference package, if you are not interested in embedded things, Can look at my website: go to the turntable network or OK search, I am also an Internet development enthusiasts.
Do not like to say so much, directly on the code:
#!/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 (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):
SE Lf. 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.getpat H () + 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)
Python differential packet production-How to make a differential packet?