AutoGenSystem
#coding=utf-8 #
# AutoGenSystem
# 声明:
# 该软件主要是为了解决Android系统更新时,由于版本很多,管理起来复杂,容易出错,于是采用软件
# 自动对系统软件进行组合,目前该软件还没有对生成的文件夹加入命名规范。
#
# 2016-1-2 深圳 南山平山村 曾剑锋
# 参考文档:
# 1. PySide : How to get the clicked QPushButton object in the QPushButton clicked slot?
# http://stackoverflow.com/questions/20320298/pyside-how-to-get-the-clicked-qpushbutton-object-in-the-qpushbutton-clicked-sl
# 2. python 字符串split的用法分享
# http://www.jb51.net/article/34998.htm
# 3. Python中使用定时器Sleep定时秒及毫秒。
# http://jingyan.baidu.com/article/4b07be3c65263a48b380f3b4.html
# 4. 去空格及特殊符号
# http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html
# 5. Python中函数的参数定义和可变参数
# http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944416.html
# 6. python 多线程就这么简单
# http://www.cnblogs.com/fnng/p/3670789.html
# 7. python threads can only be started once
# http://zhidao.baidu.com/link?url=8x7GpDdNmcL3TQZ0iDibdkx5Y2MdgcxnlATPkvMX4EwXsR2ZxJ37lDapbeehaHNvGmQxjupjnx4ynsxfAuGipC1wbOUoTaHqmzyLcmcffza
#
# import sys
import os
import shutil
import time
import threading
from PyQt4.QtCore import *
from PyQt4.QtGui import * from ui_AutoGenSystem import Ui_GenSystemUI class AutoGenSystem(QDialog): def __init__(self): QDialog.__init__(self) self.ui = Ui_GenSystemUI()
self.ui.setupUi(self) self.setWindowTitle("AutoGenSystem")
self.setFixedHeight(self.height())
self.setFixedWidth(self.width())
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint); # 连接信号与槽
self.ui.chooseUbootPath.clicked.connect(lambda :self.dealWithPushButton(self.ui.chooseUbootPath))
self.ui.chooseRecoveryPath.clicked.connect(lambda :self.dealWithPushButton(self.ui.chooseRecoveryPath))
self.ui.chooseBootingPath.clicked.connect(lambda :self.dealWithPushButton(self.ui.chooseBootingPath))
self.ui.chooseFileSystemPath.clicked.connect(lambda :self.dealWithPushButton(self.ui.chooseFileSystemPath))
self.ui.clean.clicked.connect(self.cleanOnClicked)
self.ui.current.clicked.connect(self.currentOnClicked)
self.ui.autoAll.clicked.connect(self.autoAllOnClicked) #初始化一些全局变量,compound是指合成,也就合成的目录
self.ubootImagePath = "image/uboot/"
self.fileSystemImagePath = "image/file_system/"
self.recoveryImagePath = "image/recovery/"
self.bootingImagePath = "image/booting/"
self.readmeFilePath = "image/readme.txt"
self.compoundPath = "compound/"
self.ubootName = "uboot.img"
self.bootingNmae = "boot.img"
self.fileSystemName = "system.img"
self.recoveryName = "recovery.img" # 检查这些文件路径是否存在
self.checkImagePathExist(self.ubootImagePath, self.fileSystemImagePath, self.recoveryImagePath, self.bootingImagePath, self.readmeFilePath) # 读取readme文件
if os.path.exists(self.readmeFilePath) :
readme = open(self.readmeFilePath)
self.ui.readme.setPlainText(QString.fromUtf8(readme.read()))
readme.close() def currentOnClicked(self): # 检查文本框是否为空
if ( (len(self.ui.ubootPath.text()) == 0)
or (len(self.ui.recoveryPath.text()) == 0)
or (len(self.ui.bootingPath.text()) == 0)
or (len(self.ui.fileSystemPath.text()) == 0) ):
QMessageBox.about(self, "mesg", "Please choose all The image files on the left side.")
return # 删除之前合成的目录,并重新创建合成目录
self.remakeCompoundDir() # 文件拷贝,并显示进度条、提示信息,5个文件,每次拷贝完一个文件,进度条往前跑20个点,跑完最后一个点就是100了
# 这里的self.compound字符串最后添加了"/",所以生成distPath的时候可以直接字符串连接。
i = 0;
i += 1
self.copyImageFile(self.ui.ubootPath.text(), self.compoundPath + self.ubootName, 20 * i, "Copying uboot.img")
i += 1
self.copyImageFile(self.ui.recoveryPath.text(), self.compoundPath + self.recoveryName, 20 * i, "Copying recovery.img")
i += 1
self.copyImageFile(self.ui.bootingPath.text(), self.compoundPath + self.bootingNmae, 20 * i, "Copying boot.img")
i += 1
self.copyImageFile(self.ui.fileSystemPath.text(), self.compoundPath + self.fileSystemName, 20 * i, "Copying system.img")
i += 1
self.saveReadMeFile(self.compoundPath + "readme.txt", 20 * i, "Copying readme.txt.") self.ui.info.setText("Current copying is over.") def autoAllOnClicked(self): # 获取文件列表
ubootFiles = os.listdir(self.ubootImagePath)
recoveryFiles = os.listdir(self.recoveryImagePath)
bootingFiles = os.listdir(self.bootingImagePath)
fileSystemFiles = ""
if self.ui.systemType.currentIndex() == 0 :
fileSystemFiles = os.listdir(self.fileSystemImagePath + "RES")
else:
fileSystemFiles = os.listdir(self.fileSystemImagePath + "PTC") # 删除之前合成的目录,并重新创建合成目录
self.remakeCompoundDir() # 拷贝文件,并计算进度条每次跳跃的阶数值,总循环次数allFilesCount,每套系统里面需要生成5个文件
# 设置进度条进度,Information tips信息
allFilesCount = len(ubootFiles) * len(recoveryFiles) * len(bootingFiles) * len(fileSystemFiles)
processBarStep = 100.0 / allFilesCount / 5 # 每个文件夹5个文件 i = 0
for ubootFile in ubootFiles:
for recoveryFile in recoveryFiles:
for bootingFile in bootingFiles:
for fileSystemFile in fileSystemFiles: newPath = self.compoundPath + ubootFile+"_"+recoveryFile+"_"+bootingFile+"_"+fileSystemFile + "/"
os.mkdir(newPath) i += 1
self.copyImageFile(self.ubootImagePath + ubootFile, newPath + self.ubootName, processBarStep * i, "Copying uboot.img")
i += 1
self.copyImageFile(self.recoveryImagePath + recoveryFile, newPath + self.recoveryName, processBarStep * i, "Copying recovery.img")
i += 1
self.copyImageFile(self.bootingImagePath + bootingFile, newPath + self.bootingNmae, processBarStep * i, "Copying boot.img")
i += 1
if self.ui.systemType.currentIndex() == 0 :
self.copyImageFile(self.fileSystemImagePath + "RES/" + fileSystemFile, newPath + self.fileSystemName, processBarStep * i, "Copying system.img")
else:
self.copyImageFile(self.fileSystemImagePath + "PTC/" + fileSystemFile, newPath + self.fileSystemName, processBarStep * i, "Copying system.img")
i += 1
self.saveReadMeFile("%s/readme.txt" % newPath, processBarStep * i, "Saving readme.txt.") self.ui.info.setText("AutoAll copying is over.")
self.ui.progressBar.setValue(100) def cleanOnClicked(self): self.ui.systemType.setCurrentIndex(0)
self.ui.ubootPath.setText("")
self.ui.recoveryPath.setText("")
self.ui.bootingPath.setText("")
self.ui.fileSystemPath.setText("")
self.ui.readme.setPlainText("")
self.ui.progressBar.setValue(0)
self.ui.info.setText("Information Tips.") def dealWithPushButton(self, button): filePath = ""
if button.objectName() == "chooseUbootPath" :
filePath = self.ubootImagePath
elif button.objectName() == "chooseRecoveryPath" :
filePath = self.recoveryImagePath
elif button.objectName() == "chooseBootingPath" :
filePath = self.bootingImagePath
elif button.objectName() == "chooseFileSystemPath" :
filePath = self.fileSystemImagePath
if self.ui.systemType.currentIndex() == 0 :
filePath += "/RES"
else :
filePath += "/PTC" # 检查文件路径是否存在
self.checkImagePathExist(filePath) # 获取文件路径,并将文件路径保存在UI界面的控件里去
choosePathString = QFileDialog.getOpenFileName(self, "Select file", filePath, "*")
if len(choosePathString) == 0 :
return
self.setUIControlsPath(button, choosePathString) def checkImagePathExist(self, *paths): # 由于这里传入的参数可以是多个,可能有些路径有效,有些无效,在这里进行检查,将无效的路径提取出来,并给出提示
unexistPath = ""
for path in paths :
if not os.path.exists(path) :
unexistPath += "\n\t"
unexistPath += path unexistPath = unexistPath.strip() # 剔除字符串行头行尾的不显示字符,用于下面的比较和分割
if len(unexistPath) != 0 :
if len(unexistPath.split("\n\t")) == 1 :
QMessageBox.about(self, "mesg", "Please create this path in current software path:\n\t" + unexistPath + ".")
else :
QMessageBox.about(self, "mesg", "Please create those path in current software path:\n\t" + unexistPath + ".")
exit() def copyImageFile(self, srcPath, distPath, progressBarValue, informationTips): self.ui.info.setText(informationTips)
QFile.copy(srcPath, distPath) # 拷贝文件
self.ui.progressBar.setValue(progressBarValue) def copyASystemFile(self, ubootSrcPath, ubootDistPath, bootingSrcPath, bootingDistPath,
recoverySrcPath, recoveryDistPath, fileSystemSrcPath, fileSystemDistPath, readmePath, processBarStep, index):
# 文件拷贝,并显示进度条、提示信息,5个文件,每次拷贝完一个文件
# 这里的self.compound字符串最后添加了"/",所以生成distPath的时候可以直接字符串连接。
index
self.copyImageFile(ubootSrcPath, ubootDistPath, processBarStep * index, "Copying uboot.img")
index += 1
self.copyImageFile(bootingSrcPath, bootingDistPath, processBarStep * index, "Copying recovery.img")
index += 1
self.copyImageFile(recoverySrcPath, recoveryDistPath, processBarStep * index, "Copying boot.img")
index += 1
self.copyImageFile(fileSystemSrcPath, fileSystemDistPath, processBarStep * index, "Copying system.img")
index += 1
self.saveReadMeFile(readmePath, processBarStep * index, "Copying readme.txt.") def saveReadMeFile(self, distPath, progressBarValue, informationTips): self.ui.info.setText(informationTips)
readme = open( distPath, "w" )
readme.write(self.ui.readme.toPlainText().toUtf8()) # 保存的时候用Utf8,读的时候也是Utf8
readme.flush()
readme.close()
self.ui.progressBar.setValue(progressBarValue) def remakeCompoundDir(self): # 删除文件夹,创建文件夹,这个过程时间太短貌似有问题,所以加了延时,但目前没有摸清楚这个延时情况该如何解决
if os.path.exists("compound") :
shutil.rmtree("compound")
time.sleep(1)
os.mkdir("compound") def disableAllControls(self): self.ui.systemType.setDisabled(True)
self.ui.ubootPath.setDisabled(True)
self.ui.recoveryPath.setDisabled(True)
self.ui.bootingPath.setDisabled(True)
self.ui.fileSystemPath.setDisabled(True)
self.ui.readme.setDisabled(True)
self.ui.clean.setDisabled(True)
self.ui.current.setDisabled(True)
self.ui.autoAll.setDisabled(True) def enableAllControls(self): self.ui.systemType.setDisabled(False)
self.ui.ubootPath.setDisabled(False)
self.ui.recoveryPath.setDisabled(False)
self.ui.bootingPath.setDisabled(False)
self.ui.fileSystemPath.setDisabled(False)
self.ui.readme.setDisabled(False)
self.ui.clean.setDisabled(False)
self.ui.current.setDisabled(False)
self.ui.autoAll.setDisabled(False) def setUIControlsPath(self, button, choosePathString): # 设置UI界面
if button.objectName() == "chooseUbootPath" :
self.ui.ubootPath.setText(choosePathString)
elif button.objectName() == "chooseRecoveryPath" :
self.ui.recoveryPath.setText(choosePathString)
elif button.objectName() == "chooseBootingPath" :
self.ui.bootingPath.setText(choosePathString)
elif button.objectName() == "chooseFileSystemPath" :
self.ui.fileSystemPath.setText(choosePathString)
AutoGenSystem的更多相关文章
- pycharm Working directory error
/***************************************************************************** * pycharm Working dir ...
- Windows pyqt4 bat自动转换UI文件-->.pyw文件
/***************************************************************************** * Windows pyqt4 bat自动 ...
随机推荐
- C#&java重学笔记(泛型)
C#部分: 1.泛型的出现主要用于解决类.接口.委托.方法的通用性,通过定义泛型类.接口.委托.方法,可以让不同类型的数据使用相同运算规则处理数据,方便了开发. 2.利用System.Nullable ...
- ZOJ3762 The Bonus Salary!(最小费用最大流)
题意:给你N个的任务一定要在每天的[Li,Ri]时段完成,然后你只有K天的时间,每个任务有个val,然后求K天里能够获得的最大bonus. 思路:拿到手第一直觉是最小费用最大流,然后不会建图,就跑去想 ...
- [C++]内存字节对齐
当我们写一个class类,然后sizeof(),然后发现这个值往往比你想象的大,这是为什么呢?这里就要讲到内存对齐的问题. 先来看一下内存对齐的几条原则: 1.对于class(struct/union ...
- 关于Try/Catch 代码块
应当放在Try/Catch 代码块中的常见任务包括连接到一个数据库或与其交互.处理文件.调用Web 服务. 老实说,我这人很少有打破沙锅问到底的精神.不过昨晚听一技术人员跟他的项目经理说要在程序中使用 ...
- UVALive 6187 Never Wait for Weights 带权并查集
题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...
- 【poj2478-Farey Sequence】递推求欧拉函数-欧拉函数的几个性质和推论
http://poj.org/problem?id=2478 题意:给定一个数x,求<=x的数的欧拉函数值的和.(x<=10^6) 题解:数据范围比较大,像poj1248一样的做法是不可行 ...
- SQL server 为多个表添加新的列
作为一名.NET未入门的程序员,第一次发随笔. 前不久参与写的公司业务程序,目前这个程序的后期维护修复漏洞工作由我来负责.由于业务关系重大,每一步对程序代码的操作都非常谨慎,一旦操作失误,造成的损失和 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- BestCoder 1st Anniversary($) 1003 Sequence
题目传送门 /* 官方题解: 这个题看上去是一个贪心, 但是这个贪心显然是错的. 事实上这道题目很简单, 先判断1个是否可以, 然后判断2个是否可以. 之后找到最小的k(k>2), 使得(m-k ...
- Android核心分析 之九Zygote Service
Zygote Service 在本章我们会接触到这两个单词: Zygote [生物] 受精卵, 接合子, 接合体 Spawn:产卵 通过这两个单词,我们就可以大体知道Zygote是干什么的了,就是叫老 ...