import configparser import threading from datetime import datetime import os import random import time import tkinter from tkinter import * from tkinter import filedialog from tkinter.ttk import * from JarManager import scanfile from JarManager.copyfile import CopyFile from JarManager.scanfile import FileInfo ini_path = '' app_name = '' excel_path = '' dsl_path = '' log_path_list = [] config_path = '' src_dll_path = [] target_package_path = [] bat_path = [] ip_address = [] b_local_test = 'False' b_remote_test = 'False' jar_name = '' package_folder = '' package_folder_sel_bat = '' class WinGUI(Tk): def __init__(self): super().__init__() self.__win() self.check_local_test_var = IntVar() self.check_remote_test_var = IntVar() self.tips_label = StringVar() self.tk_button_running = self.__tk_button_running(self) self.tk_input_srcdir = self.__tk_input_srcdir(self) self.tk_button_srcBtn = self.__tk_button_srcBtn(self) self.tk_input_targetdir = self.__tk_input_targetdir(self) self.tk_radio_button_local = self.__tk_radio_button_local(self) self.tk_input_batpath = self.__tk_input_batpath(self) self.tk_input_remoteip = self.__tk_input_remoteip(self) self.tk_radio_button_remote = self.__tk_radio_button_remote(self) self.tk_label_lwi29sds = self.__tk_label_lwi29sds(self) self.tk_button_targetdirBtn = self.__tk_button_targetdirBtn(self) self.tk_button_Update = self.__tk_button_Update(self) self.tk_button_selectbatBtn = self.__tk_button_selectbatBtn(self) self.tk_label_status = self.__tk_label_status(self) self.tk_button_exit = self.__tk_button_exit(self) self.tk_input_jar_name = self.__tk_input_jar_name(self) self.tk_label_lwkwa4hj = self.__tk_label_lwkwa4hj(self) self.tk_input_Mid_file_path = self.__tk_input_Mid_file_path(self) self.tk_button_slect_mid_forlder = self.__tk_button_slect_mid_forlder(self) self.tk_input_mid_select_bat = self.__tk_input_mid_select_bat(self) self.tk_button_selectMidTmpBat = self.__tk_button_selectMidTmpBat(self) self.src_dll_path = '' self.target_package_path = '' self.bat_path = '' self.ip_address = '' self.b_local_test = '' self.b_remote_test = '' self.select_status = 0 self.keep_run = False self.jar_name = '' self.package_folder = '' # 组装装文件夹 self.package_folder_sel_bat = '' self.fileopt = FileInfo(self.tk_input_srcdir, self.package_folder) self.filecopy = CopyFile(self.bat_path,self.fileopt.local_bat) def __win(self): self.title(app_name) # 设置窗口大小、居中 width = 649 height = 453 screenwidth = self.winfo_screenwidth() screenheight = self.winfo_screenheight() geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) self.geometry(geometry) self.resizable(width=False, height=False) def scrollbar_autohide(self, vbar, hbar, widget): """自动隐藏滚动条""" def show(): if vbar: vbar.lift(widget) if hbar: hbar.lift(widget) def hide(): if vbar: vbar.lower(widget) if hbar: hbar.lower(widget) hide() widget.bind("<Enter>", lambda e: show()) if vbar: vbar.bind("<Enter>", lambda e: show()) if vbar: vbar.bind("<Leave>", lambda e: hide()) if hbar: hbar.bind("<Enter>", lambda e: show()) if hbar: hbar.bind("<Leave>", lambda e: hide()) widget.bind("<Leave>", lambda e: hide()) def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph): widget.configure(yscrollcommand=vbar.set) vbar.config(command=widget.yview) vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne') def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph): widget.configure(xscrollcommand=hbar.set) hbar.config(command=widget.xview) hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw') def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph): vbar, hbar = None, None if is_vbar: vbar = Scrollbar(master) self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph) if is_hbar: hbar = Scrollbar(master, orient="horizontal") self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph) self.scrollbar_autohide(vbar, hbar, widget) def loop_task(self): icount = 0 while True: icount = icount + 1 if not self.keep_run: return print("Running loop task...") bat_file_path = os.path.dirname(self.bat_path) self.fileopt.update_path_Infor(self.src_dll_path, self.package_folder) #self.fileopt.scan_files() time.sleep(2) print('======================= Change =================================') txt_path = "D:\\PycharmProjects\\pythonProject\\JarManager\\Package\\123\\win32\\1.txt" self.fileopt.save_cust_data_to_txt(txt_path) txt_path = "D:\\PycharmProjects\\pythonProject\\JarManager\\Package\\123\\win32\\2.txt" self.fileopt.save_cust_data_to_txt(txt_path) self.fileopt.scan_files() self.fileopt.get_all_update_file() self.fileopt.create_copy_file_infor(bat_file_path) self.fileopt.update_file_infor() # 1、检查文件更新 # 2、比较批处理是否一致,如果一致则不进行如下3、4操作 # 3、执行拷贝批处理 # 4、打包 # 5、更新时间 self.get_setting() gen_bat_path = self.fileopt.local_bat if not self.filecopy.is_file_content_equal(self.bat_path, gen_bat_path): print('run copy bat') self.filecopy.updata_path(self.bat_path, gen_bat_path) self.filecopy.copy_and_backup() print('run package') self.filecopy.execute_bat_commands(self.package_folder_sel_bat) self.fileopt.package_dll_to_target_by_dir(self.target_package_path, self.jar_name, self.package_folder) now = datetime.now() newname = now.strftime("[%Y-%m-%d %H:%M:%S]") self.tips_label.set(newname) if icount == 3: self.keep_run =False return def loop_task_remote(self): icount = 0 while True: icount = icount + 1 if not self.keep_run: return print("Running loop task...") bat_file_path = os.path.dirname(self.bat_path) self.fileopt.update_path_Infor(self.src_dll_path, self.package_folder) #self.fileopt.scan_files() time.sleep(2) print('======================= Change =================================') txt_path = "D:/Testlog/JarManagerTool/Package/123/win32/1.txt" self.fileopt.save_cust_data_to_txt(txt_path) txt_path = "D:/Testlog/JarManagerTool/Package/123/win32/2.txt" self.fileopt.save_cust_data_to_txt(txt_path) self.fileopt.scan_files() self.fileopt.get_all_update_file() self.fileopt.create_remote_copy_file_infor(bat_file_path, self.ip_address) batfilename = os.path.basename(self.bat_path) # 追加拷bat # tmp = 'xcopy {} \\\\{}\\win32\\{} /Y'.format(self.fileopt.local_bat, self.ip_address, batfilename) # with open(self.fileopt.local_bat, 'a', encoding='utf-8') as f: # f.write(tmp) # self.fileopt.update_file_infor() # 1、检查文件更新 # 2、比较批处理是否一致,如果一致则不进行如下3、4操作 # 3、执行拷贝批处理 # 4、更新时间 self.get_setting() gen_bat_path = self.fileopt.local_bat if not self.filecopy.is_file_content_equal(self.bat_path, gen_bat_path): print('run copy bat') self.filecopy.updata_path(self.bat_path, gen_bat_path) self.filecopy.copy_and_backup() now = datetime.now() newname = now.strftime("[%Y-%m-%d %H:%M:%S]") self.tips_label.set(newname) if icount == 3: self.keep_run =False return def run(self): print('run') self.get_setting() if self.keep_run: # 已经在运行线程 self.fileopt.update_path_Infor(self.tk_input_srcdir, self.tk_input_srcdir) else: self.keep_run = True self.tk_button_exit.config(state='normal') self.tk_button_running.config(state='hidden') self.fileopt.update_path_Infor(self.tk_input_srcdir, self.tk_input_targetdir) if self.select_status == 0: thread = threading.Thread(target=self.loop_task) thread.daemon = True # 设置为守护线程,确保主程序退出时子线程也退出 thread.start() else: thread = threading.Thread(target=self.loop_task_remote) thread.daemon = True # 设置为守护线程,确保主程序退出时子线程也退出 thread.start() def __tk_button_running(self, parent): btn = Button(parent, text="运行", command=self.run) btn.place(x=492, y=329, width=138, height=34) return btn def __tk_input_srcdir(self, parent): ipt = Entry(parent, ) ipt.place(x=9, y=51, width=462, height=32) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, src_dll_path) return ipt def set_src_path(self): path = filedialog.askdirectory() if os.path.exists(path): self.tk_input_srcdir.delete(0, tkinter.END) # 删除现有内容 self.tk_input_srcdir.insert(0, path) print(path) else: print("no changed") def __tk_button_srcBtn(self, parent): btn = Button(parent, text="A-选择文件夹[SRC]", command=self.set_src_path) btn.place(x=492, y=53, width=138, height=32) return btn def __tk_input_targetdir(self, parent): ipt = Entry(parent, ) ipt.place(x=9, y=180, width=462, height=32) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, target_package_path) return ipt def __tk_input_batpath(self, parent): ipt = Entry(parent, ) ipt.place(x=9, y=232, width=462, height=30) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, bat_path) return ipt def __tk_input_remoteip(self, parent): ipt = Entry(parent, ) ipt.place(x=9, y=283, width=462, height=30) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, ip_address) return ipt def __tk_radio_button_local(self, parent): if b_local_test == 'False': self.check_local_test_var.set(0) else: self.check_local_test_var.set(1) rb = Radiobutton(parent, text="本地执行", variable=self.check_local_test_var, value=0) rb.place(x=10, y=322, width=140, height=34) return rb def __tk_radio_button_remote(self, parent): if b_remote_test == 'False': self.check_local_test_var.set(0) else: self.check_local_test_var.set(1) rb = Radiobutton(parent, text="远程调试", variable=self.check_local_test_var, value=1) rb.place(x=296, y=323, width=145, height=34) return rb def __tk_label_lwi29sds(self, parent): label = Label(parent, text="远程IP", anchor="center", ) label.place(x=492, y=285, width=138, height=30) return label def set_target_path(self): path = filedialog.askdirectory() if os.path.exists(path): self.tk_input_targetdir.delete(0, tkinter.END) # 删除现有内容 self.tk_input_targetdir.insert(0, path) print(path) else: print("no changed") def __tk_button_targetdirBtn(self, parent): btn = Button(parent, text="C-选择文件夹[TARGET]", command=self.set_target_path) btn.place(x=492, y=182, width=138, height=31) return btn def get_setting(self): self.src_dll_path = self.tk_input_srcdir.get() self.target_package_path = self.tk_input_targetdir.get() self.bat_path = self.tk_input_batpath.get() self.ip_address = self.tk_input_remoteip.get() self.select_status = self.check_local_test_var.get() self.jar_name = self.tk_input_jar_name.get() self.package_folder = self.tk_input_Mid_file_path.get() self.package_folder_sel_bat = self.tk_input_mid_select_bat.get() def update_setting(self): self.get_setting() if self.src_dll_path != src_dll_path: write_ini_value(config_path, 'src_path', self.src_dll_path) if self.target_package_path != target_package_path: write_ini_value(config_path, 'target_path', self.target_package_path) if self.bat_path != bat_path: write_ini_value(config_path, 'bat_path', self.bat_path) if self.ip_address != ip_address: write_ini_value(config_path, 'ip_address', self.ip_address) # 0:表示选择 1表示 if self.select_status == 0: self.b_local_test = 'True' self.b_remote_test = 'False' else: self.b_local_test = 'False' self.b_remote_test = 'True' if self.jar_name != jar_name: write_ini_value(config_path, 'jar_name', self.jar_name) if self.package_folder != package_folder: write_ini_value(config_path, 'Mid_path', self.package_folder) if self.package_folder_sel_bat != package_folder_sel_bat: write_ini_value(config_path, 'Mid_bat_path', self.package_folder_sel_bat) write_ini_value(config_path, 'local_test', self.b_local_test) write_ini_value(config_path, 'remote_test', self.b_remote_test) def __tk_button_Update(self, parent): btn = Button(parent, text="更新配置", takefocus=False, command= self.update_setting) btn.place(x=293, y=365, width=174, height=60) return btn def get_bat_path(self): print('') path = filedialog.askopenfilename() if os.path.exists(path): self.tk_input_batpath.delete(0, tkinter.END) # 删除现有内容 self.tk_input_batpath.insert(0, path) print(path) else: print('no changed') def __tk_button_selectbatBtn(self, parent): btn = Button(parent, text="选择批处理", command=self.get_bat_path) btn.place(x=492, y=232, width=138, height=30) return btn def __tk_label_status(self, parent): label = Label(parent, text="标签", anchor="center",textvariable=self.tips_label) label.place(x=9, y=367, width=265, height=57) return label def toggle_button(self): if self.tk_button_running.winfo_viewable(): self.tk_button_running.grid() self.tk_button_exit.grid_remove() if self.tk_button_exit.winfo_viewable(): self.tk_button_running.grid() self.tk_button_exit.grid_remove() def stop_run(self): print('stop_run') self.keep_run = False now = datetime.now() newname = now.strftime("[%Y%m%d%H%M%S]") time.sleep(1) self.tips_label.set(newname) #self.toggle_button() self.tk_button_exit.config(state='hidden') self.tk_button_running.config(state='normal') def __tk_button_exit(self, parent): btn = Button(parent, text="停止", command=self.stop_run) btn.place(x=492, y=389, width=138, height=32) return btn def __tk_input_jar_name(self,parent): ipt = Entry(parent, ) ipt.place(x=9, y=9, width=462, height=30) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, jar_name) return ipt def __tk_label_lwkwa4hj(self,parent): label = Label(parent,text="Jar包名称",anchor="center", ) label.place(x=492, y=7, width=138, height=34) return label def __tk_input_Mid_file_path(self,parent): ipt = Entry(parent, ) ipt.place(x=9, y=99, width=462, height=30) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, package_folder) return ipt def set_Mid_path(self): path = filedialog.askdirectory() if os.path.exists(path): self.tk_input_Mid_file_path.delete(0, tkinter.END) # 删除现有内容 self.tk_input_Mid_file_path.insert(0, path) print(path) else: print("no changed") def __tk_button_slect_mid_forlder(self,parent): btn = Button(parent, text="B-组装文件夹", command=self.set_Mid_path) btn.place(x=492, y=100, width=138, height=30) return btn def __tk_input_mid_select_bat(self,parent): ipt = Entry(parent, ) ipt.place(x=9, y=141, width=462, height=30) ipt.delete(0, tkinter.END) # 删除现有内容 ipt.insert(0, package_folder_sel_bat) return ipt def set_Sel_Mid_bat_path(self): path = filedialog.askopenfilename() if os.path.exists(path): self.tk_input_mid_select_bat.delete(0, tkinter.END) # 删除现有内容 self.tk_input_mid_select_bat.insert(0, path) print(path) else: print("no changed") def __tk_button_selectMidTmpBat(self,parent): btn = Button(parent, text="选择筛选秕处理", command=self.set_Sel_Mid_bat_path) btn.place(x=492, y=141, width=138, height=30) return btn class Win(WinGUI): def __init__(self, controller): self.ctl = controller super().__init__() self.__event_bind() self.__style_config() self.ctl.init(self) def __event_bind(self): pass def __style_config(self): pass def read_ini_value(path, key): # 创建一个ConfigParser对象 config = configparser.ConfigParser() # 读取INI文件 config.read(path + 'config.ini') # 获取指定section和key的值 value = config.get(key, 'value') print(value) return value def write_ini_value(path, key, value): # 创建一个ConfigParser对象 config = configparser.ConfigParser() # 读取INI文件 config.read(path) # 修改指定section和key的值 config.set(key, 'value', value) # 将更改写回INI文件 with open(path, 'w') as configfile: config.write(configfile) return True if __name__ == "__main__": path_infor = os.getcwd() path_infor = path_infor + '\\' run_dir = path_infor src_dll_path = read_ini_value(path_infor,'src_path') target_package_path = read_ini_value(path_infor,'target_path') bat_path = read_ini_value(path_infor,'bat_path') ip_address = read_ini_value(path_infor,'ip_address') b_local_test = read_ini_value(path_infor,'local_test') b_remote_test = read_ini_value(path_infor,'remote_test') ini_path = read_ini_value(path_infor,'current_path') app_name = read_ini_value(path_infor, 'app_name') jar_name = read_ini_value(path_infor, 'jar_name') package_folder = read_ini_value(path_infor, 'Mid_path') package_folder_sel_bat = read_ini_value(path_infor, 'Mid_bat_path') print('++:' + path_infor) print('++:' + app_name) config_path = path_infor + 'config.ini' if ini_path != path_infor + 'config.ini': write_ini_value(config_path, 'current_path', config_path) # config_path win = WinGUI() win.mainloop()