百度网盘爬虫百度图片爬虫后这是本人第三篇有关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)

python 差分包制作-如何来制作差分包?的更多相关文章

  1. python 跑服务器,访问自己制作的简单页面

    1  python 跑服务器,访问自己制作的简单页面 2 # win+b出现一个网址http:/0.0.1:5000/复制到浏览器查看# http://127.0.0.1:5000/home 做这个首 ...

  2. [TPYBoard-Micropython之会python就能做硬件 3] 制作电子时钟

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.TPYboard V102板  一块 2.DS3231 ...

  3. python模块安装查看、包制作

    一. 模块安装 ubuntu : apt-get install python-pip redhat: yum install python-pip pip install 模块 pip instal ...

  4. 将python包发布到PyPI和制作whl文件

    参考链接:wheel和egg的不同怎样将自己写的包传达到PyPi发布你自己的轮子 - PyPI打包上传实践PyPI官网上传包教程 wheel文件Wheel和Egg都是python的打包格式,目的是支持 ...

  5. Python 支付宝红包二维码制作步骤分享

    本文所有教程及源码.软件仅为技术研究.不涉及计算机信息系统功能的删除.修改.增加.干扰,更不会影响计算机信息系统的正常运行.不得将代码用于非法用途,如侵立删! 支付宝红包二维码制作步骤分享 2022. ...

  6. python与mongodb的交互 增删改差

    首先引入包: pip install pymongo需要用到如下对象: MongoClient对象:用于与MongoDB服务器建立连接 client=MongoClient('主机ip',端口) Da ...

  7. 万能的Python,还能用来制作高大上的进度条?

    对于开发或者运维来说,使用Python去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效的监控任务的进度,除了在任务中加上log外,还能不能有另一种方式来了解任务进展到哪一步了呢? ...

  8. python第十五天-原来还差一份作业

    作业 1: 员工信息表程序,实现增删改查操作 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 select ...

  9. Python 同ip网站查询(制作网站接口)

    老师专门打电话回来说不让玩电脑~~   呵呵.  LOL把被人坑死了. 五排果然不适合我. 去翻了下曾经的Python项目. 在爬虫文件夹下面找到了这个. 我也就厚脸皮的什么都不改就放上来了: #co ...

随机推荐

  1. dedecms会员注册注入漏洞

    一.首先访问“/data/admin/ver.txt”页面获取系统最后升级时间,(判断是否是dede的cms 以及看看是否和这个漏洞相对应)二.然后访问“/member/ajax_membergrou ...

  2. js正则表达式之中文验证(转)

    原文地址:http://houfeng0923.iteye.com/blog/1035321 今天做表单提交的输入框条件验证,验证是否包含中文:网上搜了一圈基于js正则表达式的验证基本不好用,而且大多 ...

  3. Python 面试中 8 个必考问题(转载)

    Python 面试中 8 个必考问题 1.下面这段代码的输出结果是什么?请解释. def extendList(val, list=[]): list.append(val) return list ...

  4. 转: Android中的签名机制

    转载请注明出处:http://www.blogjava.net/zh-weir/archive/2011/07/19/354663.html Android APK 签名比对 发布过Android应用 ...

  5. LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

    翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...

  6. NET:Checkboxlist,Dropdownlist 加入ToolTip说明

    ToolTip属性: ToolTip 类 (System.Windows.Controls)‎ 表示创建弹出项的控件.该弹出项可显示界面中元素的相关信息.命名空间: System.Windows.Co ...

  7. codeforces C. Diverse Permutation

    C. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Public Private Protect Inheritance and access specifiers

    In the previous lessons on inheritance, we've been making all of our data members public in order to ...

  9. 10-hibernate单表操作-组件属性

    组件属性: 实体类中某个属性属于用户自定义的类的对象,比如在实体类中某个属性是自定义类的对象: 这个Address是一个用户自定义类. 该自定义类Address定义如下: //地址类 public c ...

  10. ThreadLocal源码

    /* * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...