#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的更多相关文章

  1. pycharm Working directory error

    /***************************************************************************** * pycharm Working dir ...

  2. Windows pyqt4 bat自动转换UI文件-->.pyw文件

    /***************************************************************************** * Windows pyqt4 bat自动 ...

随机推荐

  1. 由浅入深了解Thrift之服务模型和序列化机制

    一.Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎.其允许你定义一个简单的定义文件中的数据类型和服务接口.以作为输入文件,编 ...

  2. poj 3684

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 784   Accepted: 266 ...

  3. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  4. SPL 全面剖析

    SPL 全面剖析 来自百度百科 http://baike.baidu.com/view/1130234.htm?fr=aladdin SPL(Standard PHP Library) IN PHP5 ...

  5. ExtJs布局之accordion,fit,auto

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  6. PHP5.4最新特性

     PHP5.4最新特性   官网:ChangeLog-5.php#5.4.0 原文Oracle:LAMP 体系有了新的竞争,但此版本中的特性使 PHP 再次挑战极限. 稍微做了修改.: 概述总结:1. ...

  7. Java常用类库

    System System:类中的方法和属性都是静态的. out:标准输出,默认是控制台. in:标准输入,默认是键盘. System描述系统一些信息.获取系统属性信息:Properties getP ...

  8. Tomcat SSL 设置

    1. 先用如下命令生成tomcat 证书 cls rem please set the env JAVA_HOME before run this bat file SET JAVA_HOME=C:\ ...

  9. java、el表达式中保留小数的方法

    Java中: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; p ...

  10. 阿里云centos6搭建vpn

    下载脚本 wget http://latrell.me/wp-content/uploads/vpn_centos6.sh 运行脚本 chmod a+x vpn_centos6.sh ./vpn_ce ...