シャットダウン時の処理を別ファイルに分割
This commit is contained in:
parent
b96496018f
commit
8a69b25d52
1 changed files with 152 additions and 0 deletions
152
dislocker_client_shutdown.py
Normal file
152
dislocker_client_shutdown.py
Normal file
|
@ -0,0 +1,152 @@
|
|||
import os
|
||||
import json
|
||||
import tkinter.messagebox
|
||||
import customtkinter
|
||||
import subprocess
|
||||
import requests
|
||||
import tkinter
|
||||
import sys
|
||||
import time
|
||||
import shutil
|
||||
|
||||
app_name = "Dislocker"
|
||||
dislocker_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
||||
|
||||
os.chdir(dislocker_dir)
|
||||
|
||||
sp_startupinfo = subprocess.STARTUPINFO()
|
||||
sp_startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
|
||||
sp_startupinfo.wShowWindow = subprocess.SW_HIDE
|
||||
|
||||
resource_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "resource")
|
||||
config_dir_path = "./config/"
|
||||
client_config_path = config_dir_path + "client.json"
|
||||
if not os.path.isfile(client_config_path):
|
||||
msgbox = tkinter.messagebox.showerror(title=f"{app_name} | エラー", message=f"設定ファイルが正しく構成されていません。")
|
||||
|
||||
elif os.path.isfile(client_config_path):
|
||||
with open(client_config_path, "r") as r:
|
||||
client_config = json.load(r)
|
||||
|
||||
def init(**kwargs):
|
||||
task_exist = subprocess.run('tasklist /fi "IMAGENAME eq dislocker_shutdown.exe"', startupinfo=sp_startupinfo, stdout=subprocess.PIPE, text=True)
|
||||
if 'dislocker_shutdown.exe' in task_exist.stdout:
|
||||
task_count = task_exist.stdout.count("dislocker_shutdown.exe")
|
||||
if task_count == 1:
|
||||
pass
|
||||
else:
|
||||
return 1
|
||||
|
||||
if client_config["initial"] == True:
|
||||
msgbox = tkinter.messagebox.showerror(title=f"{app_name} | エラー", message=f"設定ファイルが正しく構成されていません。")
|
||||
return 2
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
class App(customtkinter.CTk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
if client_config["testing"] == True:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
self.geometry("100x100")
|
||||
|
||||
self.title(f"{app_name} | てすと")
|
||||
self.iconbitmap(default=resource_path + '\\icon\\dislocker.ico')
|
||||
|
||||
self.protocol("WM_DELETE_WINDOW", self.handler_close)
|
||||
|
||||
self.frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
|
||||
self.frame.grid(row=0, column=0, sticky='nsew')
|
||||
|
||||
self.withdraw()
|
||||
|
||||
def delete_appdata(self, **kwargs):
|
||||
process_name = kwargs["process_name"]
|
||||
dir_path = kwargs["dir_path"]
|
||||
|
||||
if not os.path.exists(dir_path):
|
||||
print(f"エラー: 指定されたディレクトリ {dir_path} が存在しません。")
|
||||
return 1
|
||||
|
||||
i = 0
|
||||
i_max = 10
|
||||
result = 1
|
||||
while i != i_max:
|
||||
i += 1
|
||||
try:
|
||||
# プロセスの終了
|
||||
subprocess.run(f'taskkill /f /t /im {process_name}', startupinfo=sp_startupinfo, stdout=subprocess.PIPE, text=True)
|
||||
print(f"{process_name} を終了しました。")
|
||||
time.sleep(0.1)
|
||||
# ディレクトリの削除
|
||||
shutil.rmtree(dir_path)
|
||||
if os.path.isdir(dir_path):
|
||||
pass
|
||||
else:
|
||||
print(f"{dir_path} を削除しました。")
|
||||
result = 0
|
||||
i = i_max
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"プロセス終了エラー: {e}")
|
||||
|
||||
except PermissionError as e:
|
||||
print(f"権限エラー: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print("エラーが発生しました。\nエラー内容:")
|
||||
print(f"エラータイプ: {e.__class__.__name__}")
|
||||
print(f"エラー引数: {e.args}")
|
||||
print(f"エラーメッセージ: {str(e)}")
|
||||
|
||||
def handler_close(self):
|
||||
print("停止処理を実行。")
|
||||
if client_config["eraser"] == True:
|
||||
appdata_local = os.path.expandvars("%LOCALAPPDATA%")
|
||||
appdata_roaming = os.path.expandvars("%APPDATA%")
|
||||
epic_del = self.delete_appdata(process_name="EpicGamesLauncher.exe", dir_path=f"{appdata_local}\\EpicGamesLauncher\\Saved")
|
||||
chrome_del = self.delete_appdata(process_name="chrome.exe", dir_path=f"{appdata_local}\\Google\\Chrome\\User Data")
|
||||
discord_del = self.delete_appdata(process_name="discord.exe", dir_path=f"{appdata_roaming}\\discord")
|
||||
steam_del = self.delete_appdata(process_name="steam.exe", dir_path=f"{appdata_local}\\Steam")
|
||||
ea_del = self.delete_appdata(process_name="EADesktop.exe", dir_path=f"{appdata_local}\\Electronic Arts")
|
||||
riot_del = self.delete_appdata(process_name="RiotClientServices.exe", dir_path=f"{appdata_local}\\Riot Games\\Riot Client")
|
||||
else:
|
||||
print("削除処理をスキップ。")
|
||||
stop_url = client_config["auth_host_url"] + "/stop"
|
||||
stop_json = {
|
||||
"pc_number": int(client_config["pc_number"]),
|
||||
"pc_uuid": str(client_config["pc_uuid"]),
|
||||
"pc_token": str(client_config["pc_token"])
|
||||
}
|
||||
try:
|
||||
responce = requests.post(stop_url, json=stop_json)
|
||||
if responce.status_code == 200:
|
||||
print("停止処理は成功しました。")
|
||||
elif responce.status_code == 401:
|
||||
print("認証に失敗しました。")
|
||||
else:
|
||||
print("内部エラーにより停止処理に失敗しました。")
|
||||
except:
|
||||
print("ネットワークエラーにより停止処理に失敗しました。")
|
||||
finally:
|
||||
self.destroy()
|
||||
|
||||
def icon(self):
|
||||
self.iconify()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_result = init()
|
||||
if init_result == 1:
|
||||
warning_msgbox = tkinter.messagebox.showwarning(title=f"{app_name} | 多重起動エラー", message=f"すでに {app_name} は実行されています。\n正常に起動しない場合は、既に起動しているプロセスを終了してから、もう一度起動してみてください。")
|
||||
elif init_result == 2:
|
||||
pass
|
||||
else:
|
||||
app = App()
|
||||
app.protocol("WM_DELETE_WINDOW", app.handler_close)
|
||||
app.mainloop()
|
Loading…
Reference in a new issue