* Some refactor in main.py, filesio.py
* Fix settings.json
* Update README.md
* Fix indents in .gitmodules

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2024-10-25 00:49:16 +07:00
parent e866ff0b91
commit 6128a67b20
5 changed files with 48 additions and 44 deletions

4
.gitmodules vendored
View File

@@ -1,3 +1,3 @@
[submodule "m_match_name"] [submodule "m_match_name"]
path = m_match_name path = m_match_name
url = https://github.com/JDM170/m_match_name url = https://github.com/JDM170/m_match_name

View File

@@ -20,7 +20,12 @@ python main.py
--- ---
Сборка: Сборка (без UPX):
```
pyinstaller --clean --console --onefile --name comp_auto_restart main.py
```
Сборка (с UPX):
``` ```
pyinstaller --clean --console --onefile --upx-dir=upx\ --name comp_auto_restart main.py pyinstaller --clean --console --onefile --upx-dir=upx\ --name comp_auto_restart main.py
``` ```

View File

@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from json import load, dump, JSONDecodeError from json import load, dump, JSONDecodeError
from os.path import exists from os.path import isfile
from os import remove from os import remove
@@ -12,28 +12,28 @@ class FilesIO:
self.default_values = default_values self.default_values = default_values
def create_default_file(self): def create_default_file(self):
if self.default_values is not None: if self.default_values is None:
if exists(self.file_name): return
remove(self.file_name) if isfile(self.file_name):
with open(self.file_name, encoding="utf-8", mode="w") as f: remove(self.file_name)
dump(self.default_values, f, indent=4, separators=(",", " : ")) with open(self.file_name, encoding="utf-8", mode="w") as f:
dump(self.default_values, f, indent=4, separators=(",", " : "))
def check_file_structure(self, file_data): def check_file_structure(self, file_data):
if self.default_values is not None: if self.default_values is None:
return file_data.keys() == self.default_values.keys() return False
return file_data.keys() == self.default_values.keys()
def get_data(self): def get_data(self):
ret_data, err_msg = False, "" ret_data, err_msg = False, ""
if isfile(self.file_name) is False:
self.create_default_file()
try: try:
with open(self.file_name, encoding="utf-8", mode="r") as f: with open(self.file_name, encoding="utf-8", mode="r") as f:
ret_data = load(f) ret_data = load(f)
if not self.check_file_structure(ret_data):
err_msg = "В файле настроек не хватает данных. Пересоздайте файл с настройками."
ret_data = False
except FileNotFoundError:
err_msg = "Файл не найден. Создан новый шаблонный файл."
self.create_default_file()
except JSONDecodeError: except JSONDecodeError:
err_msg = "Ошибка загрузки файла, проверьте на наличие ошибок." return False, "Ошибка загрузки файла, проверьте файл на наличие ошибок."
finally: if not self.check_file_structure(ret_data):
return ret_data, err_msg ret_data = False
err_msg = "В файле настроек не хватает данных. Удалите или пересоздайте файл с настройками."
return ret_data, err_msg

43
main.py
View File

@@ -2,7 +2,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from sys import exit, argv from sys import exit, argv
from os import system, getcwd
from subprocess import Popen from subprocess import Popen
from filesio import FilesIO from filesio import FilesIO
from m_match_name.main import MatchIO from m_match_name.main import MatchIO
@@ -24,41 +23,41 @@ default_values = {
} }
} }
loaded_file = None loaded_file = None
is_debug = False
def main(comp_key, is_debug): def main(comp_key):
global loaded_file global loaded_file, is_debug
list_pc_names = loaded_file.get("pc").get(comp_key) list_pc_names = loaded_file.get("pc").get(comp_key)
if list_pc_names is not None: if list_pc_names is None:
restart_time = loaded_file.get("restart_time")
restart_message = loaded_file.get("restart_message")
for pc_name in list_pc_names:
formatted_pc_name = matchio.check_pc_name(pc_name)
if formatted_pc_name is not False:
if is_debug is False:
# system("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name))
Popen("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message)).wait()
sleep(2)
else:
print("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message))
else:
print("Попробуйте еще раз выбрать область запуска скрипта!") print("Попробуйте еще раз выбрать область запуска скрипта!")
exit()
restart_time = loaded_file.get("restart_time")
restart_message = loaded_file.get("restart_message")
for pc_name in list_pc_names:
formatted_pc_name = matchio.check_pc_name(pc_name)
if formatted_pc_name is False:
continue
if is_debug is False:
Popen("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message)).wait()
sleep(2)
else:
print("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message))
if __name__ == "__main__": if __name__ == "__main__":
print(argv)
if len(argv) < 2: if len(argv) < 2:
print("Укажите номер запускаемой области!\n1 - Кабинеты; 2 - Цех") print("Укажите номер запускаемой области!\n1 - Кабинеты; 2 - Цех")
exit() exit()
if len(argv) == 3:
is_debug = argv[2].strip() == "debug"
try: try:
filesio = FilesIO(default_values=default_values) filesio = FilesIO(default_values=default_values)
loaded_file, err_msg = filesio.get_data() loaded_file, err_msg = filesio.get_data()
if loaded_file is False: if loaded_file is False:
raise Exception(err_msg) print(err_msg)
exit()
matchio = MatchIO() matchio = MatchIO()
main(argv[1], argv[2].strip() == "debug") main(argv[1])
except Exception as exc:
print(exc)
exit()
except KeyboardInterrupt: except KeyboardInterrupt:
exit() exit()

View File

@@ -2,13 +2,13 @@
"restart_time": 60, "restart_time": 60,
"restart_message": "Плановая перезагрузка компьютера через 1 минуту!", "restart_message": "Плановая перезагрузка компьютера через 1 минуту!",
"pc" : { "pc" : {
"2" : [ "1" : [
"IT01", "IT01",
"IT02", "IT02",
"630870MMP01", "630870MMP01",
"R54-630870MMP02" "R54-630870MMP02"
], ],
"7" : [ "2" : [
"PIS09", "PIS09",
"PIS10", "PIS10",
"630870THE01", "630870THE01",