一键式机密Django项目GUI
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '加密11.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. import re from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QPushButton, QFileDialog import sys import os from tempfile import TemporaryDirectory from PyQt5.QtCore import Qt class Ui_MainWindow(QMainWindow): def __init__(self): super().__init__() self.setupUi(self) self.retranslateUi(self) self.noencrytion_file_name = ["manage", "__init__", "models", "model", "main"] # 不需要加密的文件名称 self.noencrytion_file_folder = ["migrations"] # 不需要加密的文件名称 self.openfile_name = '' self.progressbar_percent = 0 def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(781, 511) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("./GIS_web_logo.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off) MainWindow.setWindowIcon(icon) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.widget_2 = QtWidgets.QWidget(self.centralwidget) self.widget_2.setGeometry(QtCore.QRect(0, 0, 781, 491)) self.widget_2.setStyleSheet("background-color: rgb(102, 102, 102) ;border-radius: 6px;") self.widget_2.setObjectName("widget_2") self.pushButton_4 = QtWidgets.QPushButton(self.widget_2) self.pushButton_4.setGeometry(QtCore.QRect(340, 60, 151, 41)) self.pushButton_4.setStyleSheet("background-color: rgb(255, 155, 0); border-radius: 6px; color: rgb(102, 102, 102);") self.pushButton_4.setObjectName("pushButton_4") self.progressBar = QtWidgets.QProgressBar(self.widget_2) self.progressBar.setGeometry(QtCore.QRect(200, 160, 431, 21)) self.progressBar.setStyleSheet( "QProgressBar { border: 2px solid grey; border-radius: 5px; background-color: #FFFFFF;}" "QProgressBar::chunk { background-color: rgb(255, 155, 0); width: 10px; margin: 0.8px;}" "QProgressBar { border: 1px solid grey; border-radius: 1px; text-align: center; color: rgb(102, 102, 102);} ") self.progressBar.setProperty("value", 0) self.textBrowser = QtWidgets.QTextBrowser(self.widget_2) self.textBrowser.setGeometry(QtCore.QRect(100, 250, 561, 192)) self.textBrowser.setStyleSheet( "border-width: 1px;border-style: solid;border-color: rgb(255, 155, 0); border-radius: 6px; color: rgb(255, 155, 0);") self.textBrowser.setObjectName("textBrowser") self.line = QtWidgets.QFrame(self.widget_2) self.line.setGeometry(QtCore.QRect(50, 200, 661, 20)) self.line.setStyleSheet("border-width: 2px;border-color: rgb(255, 155, 102);") self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.commandLinkButton = QtWidgets.QCommandLinkButton(self.widget_2) self.commandLinkButton.setGeometry(QtCore.QRect(100, 60, 201, 41)) self.commandLinkButton.setStyleSheet( "border-width: 2px;border-style: solid;border-color: rgb(255, 155, 0); color:rgb(255, 155, 0); ") self.commandLinkButton.setObjectName("commandLinkButton") self.commandLinkButton.setCursor(QtCore.Qt.PointingHandCursor) self.textBrowser_2 = QtWidgets.QTextBrowser(self.widget_2) self.textBrowser_2.setGeometry(QtCore.QRect(80, 50, 591, 61)) self.textBrowser_2.setStyleSheet("border-width: 1px;border-style: solid; border-radius: 6px; border-color: rgb(255, 155, 0);") self.textBrowser_2.setObjectName("textBrowser_2") self.textBrowser_3 = QtWidgets.QTextBrowser(self.widget_2) self.textBrowser_3.setGeometry(QtCore.QRect(60, 160, 121, 41)) self.textBrowser_3.setStyleSheet("background:transparent;border-width:0;border-style:outset") self.textBrowser_3.setObjectName("textBrowser_3") self.line_2 = QtWidgets.QFrame(self.widget_2) self.line_2.setGeometry(QtCore.QRect(50, 470, 661, 20)) self.line_2.setStyleSheet("") self.line_2.setFrameShape(QtWidgets.QFrame.HLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.pushButton_5 = QtWidgets.QPushButton(self.widget_2) self.pushButton_5.setGeometry(QtCore.QRect(500, 60, 151, 41)) self.pushButton_5.setStyleSheet("background-color: rgb(255, 155, 0); border-radius: 6px; color: rgb(102, 102, 102); ") self.pushButton_5.setObjectName("pushButton_5") self.textBrowser_2.raise_() self.pushButton_4.raise_() self.progressBar.raise_() self.textBrowser.raise_() self.line.raise_() self.commandLinkButton.raise_() self.textBrowser_3.raise_() self.line_2.raise_() self.pushButton_5.raise_() self.widget_3 = QtWidgets.QWidget(self.centralwidget) self.widget_3.setGeometry(QtCore.QRect(30, 530, 781, 16)) self.widget_3.setObjectName("widget_3") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 781, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "GIS 项目加密")) self.pushButton_4.setText(_translate("MainWindow", "确认加密")) self.commandLinkButton.setText(_translate("MainWindow", "选择文件")) self.textBrowser_3.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'PMingLiU\'; font-size:9pt; font-weight:400; font-style:normal;\">\n" "<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt; font-weight:600; color:#ffaa00;\">加密进度:</span></p></body></html>")) self.pushButton_5.setText(_translate("MainWindow", "重置")) # 按钮触发上传 self.commandLinkButton.clicked.connect(self.read_file_path) # 按钮触发main事件 self.pushButton_4.clicked.connect(self.main) # 按钮触发重置界面 self.pushButton_5.clicked.connect(self.clear_content) def clear_content(self): self.textBrowser.setText("") self.openfile_name = '' self.progressBar.setProperty("value", 0) def read_file_path(self): # 打开Excel 文件 # openfile_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'Excel files(*.xlsx , *.xls)') openfile_name = QFileDialog.getExistingDirectory(self, '选择文件', '') self.openfile_name = openfile_name return openfile_name def read_file(self, file_type): import os # 打开目录 encrytion_list = [] for dir_path, dir_names, files in os.walk(self.openfile_name): if dir_path.split('\\')[-1] not in self.noencrytion_file_folder: if len(files) > 0: for i in files: if len(i) > 0 and "." in i: if i.split(".")[-1] == file_type: if i.split(".")[0] not in self.noencrytion_file_name: encrytion_list.append(dir_path + '\\' + i) return encrytion_list def delete_files(self, files_path): """ @summary: 删除文件 --------- @param files_path: 文件路径 py 及 c 文件 --------- @result: """ try: # 删除python文件及c文件 for file in files_path: os.remove(file) # py文件 os.remove(file.replace(".py", ".c")) # c文件 except Exception as e: self.textEdit.append("加密成功") def rename_excrypted_file(self, output_filepath): for file in output_filepath: if file.endswith(".pyd") or file.endswith(".so"): new_filename = re.sub("(.*)\..*\.(.*)", r"\1.\2", file) os.rename(file, new_filename) def Encrytion_method(self): from Cython.Build import cythonize from Cython import Plex from Cython.Compiler import Lexicon from Cython import Utility from distutils.core import setup encrytion_list = self.read_file("py") # 读取项目中python 文件 encrypted_py = [] total_count = len(encrytion_list) if total_count == 0: return encrypted_py QtWidgets.QApplication.processEvents() # 实时显示信息 self.textBrowser.append("需要加密的python 文件个数:{} ".format(total_count)) task_process = int(95 / total_count) # 每加密一个文件进度加多少 with TemporaryDirectory() as td: for i, py_file in enumerate(encrytion_list): try: dir_name = os.path.dirname(py_file) # 文件路径 file_name = os.path.basename(py_file) # 文件名称 os.chdir(dir_name) # 选择到该目录 setup(ext_modules=cythonize([file_name], quiet=True, language_level=3), script_args=["build_ext", "-t", td, "--inplace"], ) # 加密算法 QtWidgets.QApplication.processEvents() # 实时显示信息 self.progressbar_percent += task_process self.progressBar.setProperty("value", self.progressbar_percent) self.textBrowser.append(py_file + "加密成功") # 机密成功页面显示信息 encrypted_py.append(py_file) except Exception as e: print(e) error_info = ("加密失败 {} , error {}".format(os.path.basename(py_file), e)) self.textBrowser.append(error_info) temp_c = py_file.replace(".py", ".c") if os.path.exists(temp_c): os.remove(temp_c) return encrypted_py def main(self): self.textBrowser.append("开始加密") encrypted_py = self.Encrytion_method() if len(encrypted_py) > 0: self.delete_files(encrypted_py) output_filepath = self.read_file("pyd") self.rename_excrypted_file(output_filepath) self.textBrowser.append("加密成功") self.progressBar.setProperty("value", 100) else: self.textBrowser.append("文件夹中无可加密文件") if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app MainWindow = QtWidgets.QMainWindow() # 创建一个QMainWindow,用来装载你需要的各种组件、控件 ui = Ui_MainWindow() # ui是你创建的ui类的实例化对象 ui.setupUi(MainWindow) # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow MainWindow.show() # 执行QMainWindow的show()方法,显示这个QMainWindow sys.exit(app.exec_()) # 使用exit()或者点击关闭按钮退出QApplication python3: input("please input any key to exit!")