Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2022-03-12 17:48:46 +07:00
parent 870feb9901
commit 0d743cdab7
4 changed files with 57 additions and 47 deletions

View File

@@ -7,19 +7,20 @@ from os import remove
class FilesIO: class FilesIO:
def __init__(self, file_name, default_values): def __init__(self, file_name=".\\settings.json", default_values=None):
self.file_name = file_name self.file_name = file_name
self.default_values = default_values self.default_values = default_values
def create_default_file(self): def create_default_file(self):
if exists(self.file_name): if self.default_values is not None:
remove(self.file_name) if exists(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 get_data(self): def get_data(self):
try: try:
with open(self.file_name, encoding="utf-8") as f: with open(self.file_name, encoding="utf-8", mode="r") as f:
ret_data = load(f) ret_data = load(f)
except FileNotFoundError: except FileNotFoundError:
self.create_default_file() self.create_default_file()

View File

@@ -2,19 +2,20 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from sys import exit, argv from sys import exit, argv
# from os import system from os import system
from subprocess import Popen from subprocess import Popen
from time import sleep
from filesio import FilesIO from filesio import FilesIO
from match_name import MatchIO from match_name import MatchIO
file_name = ".\\computer_names.json"
default_values = { default_values = {
"expr_list": [ "expr_list": [
[r"^[a-zA-Z]+\d+$", "R54-630300"], # THE01 [r"^[a-zA-Z]+\d+$", "R54-630300"], # THE01
[r"^\d+[a-zA-Z]+\d+$", "R54-"], # 630300THE01 [r"^\d+[a-zA-Z]+\d+$", "R54-"], # 630300THE01
[r"^[rR]\d*[-]\d+[a-zA-Z]+\d+$", ""] # R54-630300THE01 [r"^[rR]\d*[-]\d+[a-zA-Z]+\d+$", ""] # R54-630300THE01
], ],
"comp": { "pc": {
"2": [ # Пятидневка "2": [ # Пятидневка
"IT01", "IT01",
"IT02" "IT02"
@@ -28,31 +29,37 @@ default_values = {
loaded_file = None loaded_file = None
def main(comp_key): def main(comp_key, is_prod):
global loaded_file global loaded_file
list_pc_names = loaded_file.get("comp").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 not None:
for pc_name in list_pc_names: for pc_name in list_pc_names:
formatted_pc_name = matchio.check_arm_name(pc_name) formatted_pc_name = matchio.check_arm_name(pc_name)
if formatted_pc_name is not False: if formatted_pc_name is not False:
# print("pc_name:", formatted_pc_name) # print("pc_name:", formatted_pc_name)
# system("shutdown /m \\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name)) if is_prod is True:
# Popen("shutdown /m \\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name)).wait() # system("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name))
print("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name)) Popen("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name)).wait()
else:
print("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name))
else: else:
print("Попробуйте еще раз выбрать область запуска скрипта!") print("Попробуйте еще раз выбрать область запуска скрипта!")
if __name__ == "__main__": if __name__ == "__main__":
if len(argv) < 2: if len(argv) < 3:
print("Укажите номер запускаемой области!\n2 - Кабинеты; 7 - Цех") print("Укажите номер запускаемой области!\n2 - Кабинеты; 7 - Цех")
exit() exit()
try: try:
filesio = FilesIO(file_name, default_values) filesio = FilesIO(default_values=default_values)
loaded_file = filesio.get_data() loaded_file = filesio.get_data()
if loaded_file is False: if loaded_file is False:
return raise OSError
matchio = MatchIO(loaded_file.get("expr_list")) matchio = MatchIO(loaded_file.get("expr_list"))
main(argv[1]) main(argv[1], argv[2].strip() == "prod")
except OSError:
print("Файл с настройками не найден. Создан файл со стандартными настройками.")
sleep(20)
exit()
except KeyboardInterrupt: except KeyboardInterrupt:
exit() exit()

View File

@@ -13,8 +13,8 @@ default_expr_list = [
class MatchIO: class MatchIO:
def __init__(self, expr_list): def __init__(self, expr_list=None):
self.expr_list = expr_list is not None and expr_list or default_expr_list self.expr_list = expr_list is None and default_expr_list or expr_list
def check_arm_name(self, pc_name): def check_arm_name(self, pc_name):
for r in self.expr_list: for r in self.expr_list:

View File

@@ -1,28 +1,30 @@
{ {
"expr_list" : [ "expr_list" : [
[ [
"^[a-zA-Z]+\\d+$", "^[a-zA-Z]+\\d+$",
"R54-630300" "R54-630300"
], ],
[ [
"^\\d+[a-zA-Z]+\\d+$", "^\\d+[a-zA-Z]+\\d+$",
"R54-" "R54-"
], ],
[ [
"^[rR]\\d*[-]\\d+[a-zA-Z]+\\d+$", "^[rR]\\d*[-]\\d+[a-zA-Z]+\\d+$",
"" ""
] ]
], ],
"comp" : { "pc" : {
"2" : [ "2" : [
"IT01", "IT01",
"IT02", "IT02",
"630870MMP01", "630870MMP01",
"R54-630870MMP02" "R54-630870MMP02"
], ],
"7" : [ "7" : [
"PIS09", "PIS09",
"PIS10" "PIS10",
] "630870THE01",
} "R54-630870THE02"
]
}
} }