License.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import sys
  2. import shutil
  3. from PyQt5.QtGui import QIcon
  4. from cryptography.fernet import Fernet
  5. from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QVBoxLayout, QLabel, QMessageBox
  6. class FileDialogExample(QWidget):
  7. def __init__(self):
  8. super().__init__()
  9. # 设置窗口
  10. self.setWindowTitle("许可更新管理器")
  11. self.setGeometry(100, 100, 300, 150)
  12. # 设置窗口图标
  13. self.setWindowIcon(QIcon("icon.png")) # 使用你自己的图标文件路径
  14. self.resize(300, 120)
  15. # 创建布局
  16. layout = QVBoxLayout()
  17. # 创建标签
  18. self.label = QLabel("请选择一个许可文件", self)
  19. self.label.setWordWrap(True)
  20. layout.addWidget(self.label)
  21. # 创建选择文件按钮
  22. self.select_file_button = QPushButton("选择文件", self)
  23. self.select_file_button.setFixedSize(60, 25)
  24. self.select_file_button.clicked.connect(self.select_file)
  25. layout.addWidget(self.select_file_button)
  26. # 创建确定按钮
  27. self.ok_button = QPushButton("确定", self)
  28. self.ok_button.setFixedSize(60, 25)
  29. self.ok_button.clicked.connect(self.ok_button_clicked)
  30. layout.addWidget(self.ok_button)
  31. # 设置窗口布局
  32. self.setLayout(layout)
  33. def copy_file(self, src_path, dst_path):
  34. shutil.copy(src_path, dst_path)
  35. def select_file(self):
  36. # 打开文件对话框选择文件
  37. options = QFileDialog.Options()
  38. file, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "Text Files (*.txt);;All Files (*)",
  39. options=options)
  40. if file:
  41. print(file)
  42. try:
  43. self.selected_file = file # 保存选择的文件路径
  44. licensefile = file
  45. # 打开文件并读取内容
  46. text = []
  47. with open(licensefile, 'r') as file:
  48. for line in file:
  49. text.append(line.replace('\n', ' '))
  50. # 打印文件内容
  51. print(text)
  52. # 尝试执行的代码
  53. fernet = Fernet(text[0].encode("utf-8"))
  54. decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
  55. license = decrypted_message.decode('utf-8')
  56. licenseDict = self.str_to_dict(license)
  57. host = licenseDict["host"]
  58. date = licenseDict["license"]
  59. # QMessageBox.information(self, "提示信息", f"许可文件解析成功,授权机器名:{host},授权到期时间:{date}")
  60. self.label.setStyleSheet("color: green;") # 这里 #999999 是一种浅灰色
  61. self.label.setText("许可文件解析成功,授权机器名:{},授权到期时间:{}。".format(host, date))
  62. except Exception as e:
  63. del self.selected_file # 保存选择的文件路径
  64. # 处理异常的代码
  65. # QMessageBox.critical(None, 'Error', "许可文件解析失败,请检查!")
  66. self.label.setStyleSheet("color: red;") # 这里 #999999 是一种浅灰色
  67. self.label.setText("许可文件解析失败,请检查!")
  68. def ok_button_clicked(self):
  69. # 执行业务逻辑
  70. if hasattr(self, 'selected_file'):
  71. licensefile = "license.txt"
  72. self.copy_file(self.selected_file, licensefile)
  73. # 打开文件并读取内容
  74. # text = []
  75. # with open(licensefile, 'r') as file:
  76. # for line in file:
  77. # text.append(line.replace('\n', ' '))
  78. # # 打印文件内容
  79. # print(text)
  80. # # 尝试执行的代码
  81. # fernet = Fernet(text[0].encode("utf-8"))
  82. # decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
  83. # license = decrypted_message.decode('utf-8')
  84. # licenseDict = self.str_to_dict(license)
  85. # host = licenseDict["host"]
  86. # date = licenseDict["license"]
  87. # QMessageBox.information(self, "提示信息", f"许可文件更新成功,授权机器名:{host},授权到期时间:{date}")
  88. QMessageBox.information(self, "提示信息", f"许可文件更新成功")
  89. # # 假设这里是业务逻辑:读取文件内容
  90. # try:
  91. # with open(self.selected_file, 'r') as f:
  92. # content = f.read()
  93. # print(f"文件内容: {content}")
  94. # except Exception as e:
  95. # print(f"读取文件失败: {e}")
  96. else:
  97. QMessageBox.information(self, "提示信息", "请选择许可文件")
  98. def str_to_dict(self, string):
  99. # 去除首尾的花括号
  100. string = string.strip("{}")
  101. # 分割每个键值对
  102. pairs = string.split(", ")
  103. # 创建空字典
  104. result = {}
  105. # 遍历键值对并添加到字典中
  106. for pair in pairs:
  107. key, value = pair.split(": ")
  108. # 去除键和值两边的引号
  109. key = key.strip("'")
  110. value = value.strip("'")
  111. # 添加到字典中
  112. result[key] = value
  113. return result
  114. if __name__ == "__main__":
  115. app = QApplication(sys.argv)
  116. window = FileDialogExample()
  117. window.show()
  118. sys.exit(app.exec_())