Files
info-shutdown/setup.py
2024-03-11 10:16:21 +01:00

182 lines
6.6 KiB
Python

# -*- coding: utf-8 -*-
from setuphelpers import *
from setupdevhelpers import *
import psutil
import time
import datetime
import ctypes
try:
from waptenterprise.waptservice.enterprise import get_active_sessions, start_interactive_process
except:
from waptservice.enterprise import get_active_sessions, start_interactive_process
import win32api
import sys
import platform
# Usable WAPT package functions: install(), uninstall(), session_setup(), audit(), update_package()
# Declaring global variables - Warnings: 1) WAPT context is only available in package functions; 2) Global variables are not persistent between calls
allow_force_reboot = True
boot_limit = 4
boot_limit_max = 7
force_reboot_timeout = 7200 # In seconds
message_box_title = "Information message about your PC uptime - The IT Support"
message_box_title_fr = "Message d'information à propos de la durée d'allumage du PC - Le Support Informatique"
# boot_limit_message = "This PC must be reboot because he's up since too long, Would you like to reboot now?"
boot_limit_message_string = "This PC must be rebooted because he's up since: %s, Would you like to reboot now?"
boot_limit_message_fr_string = "Ce PC doit être redémarré, car il est allumé depuis : %s, Voulez-vous redémarrer maintenant ?"
# boot_limit_max_message = "This PC will be FORCE reboot in 2 hours because he's up since too long"
boot_limit_max_message_string = "This PC will be FORCE reboot in %s hours because he's up since: %s"
boot_limit_max_message_fr_string = "Ce PC va être redémarré de manière FORCÉ dans %s heures, car il est allumé depuis : %s"
def install():
pass
r""" # Initializing variables
wapt_enterprise_dir = makepath(WAPT.wapt_base_dir,'waptenterprise')
# Checking that the WAPT Agent is well in Enterprise version
if isdir(wapt_enterprise_dir):
print('Continuing: WAPT Agent is well installed in Enterprise version')
else:
error('WAPT Agent is NOT installed in Enterprise version, please upgrade')
"""
def session_setup():
print("This PC is up since:")
print(convert_time_to_date(uptime_seconds()))
# Translating message
if get_language() == "fr":
message_box_title = message_box_title_fr
boot_limit_message_string = boot_limit_message_fr_string
boot_limit_max_message_string = boot_limit_max_message_fr_string
# Avoiding user pop-up spamming
check_timeout = 1800
date_format = "%Y-%m-%d %H:%M:%S" # datetime.datetime.strptime(date_pwsh, "%Y/%m/%d %H:%M:%S")
date = run_powershell('Get-Date -Format "yyyy-MM-dd HH:mm:ss"') # datetime.datetime.now()
date_reg = registry_readstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\Shutdown Information", "Last Check")
if date_reg != "":
date_diff = datetime.datetime.strptime(date, date_format) - datetime.datetime.strptime(date_reg, date_format)
if date_diff.total_seconds() < check_timeout:
return "Already checked"
registry_set(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\Shutdown Information", "Last Check", date)
# Notify the user
if allow_force_reboot:
if uptime_days() > boot_limit_max:
# message_box(
# message_box_title,
# boot_limit_max_message_string % (convert_time_to_hours(force_reboot_timeout), convert_time_to_date(uptime_seconds(), translate=True)),
# )
ask_message(message_box_title, boot_limit_max_message_string % (convert_time_to_hours(force_reboot_timeout), convert_time_to_date(uptime_seconds(), translate=True)), flags=0, raise_error=False,)
#reboot_machine(force=1, timeout=force_reboot_timeout)
print("reboot")
return "ERROR"
else :
print("toto")
if uptime_days() > boot_limit:
#message_box_ask_reboot(message_box_title, boot_limit_message_string % convert_time_to_date(uptime_seconds(), translate=True))
if ask_message(message_box_title, boot_limit_message_string % convert_time_to_date(uptime_seconds(), translate=True), flags=4, raise_error=False,) == "6" :
reboot_machine()
return "WARNING"
if uptime_days() < boot_limit:
return "OK"
def audit():
print("This PC is up since:")
print(convert_time_to_date(uptime_seconds()))
if allow_force_reboot:
if uptime_days() > boot_limit_max:
run_session_setup(control.package)
print("Force rebooting the PC after the defined timeout.")
reboot_machine(force=1, timeout=force_reboot_timeout)
return "ERROR"
if uptime_days() > boot_limit:
run_session_setup(control.package)
return "WARNING"
if uptime_days() < boot_limit:
return "OK"
def run_session_setup(package_name):
for session_id in get_active_sessions():
start_interactive_process("wapt-get", "--hide session-setup %s -f" % package_name, session_id=session_id) # , minimize=True
# def message_box(title, text, style=0):
# if platform.python_version_tuple()[0] == "3":
# import importlib
# importlib.reload(sys)
# else:
# reload(sys)
# sys.setdefaultencoding("utf-8")
# MB_OK = 0
# MB_OKCANCEL = 1
# MB_YESNOCANCEL = 3
# MB_YESNO = 4
# ctypes.windll.user32.MessageBoxW(0, "%s" % text, "%s" % title, style)
# return
# def message_box_ask_reboot(title, text):
# if platform.python_version_tuple()[0] == "3":
# import importlib
# importlib.reload(sys)
# else:
# reload(sys)
# sys.setdefaultencoding("utf-8")
# ID_OK = 0
# ID_CANCEL = 2
# ID_ABORT = 3
# ID_YES = 6
# ID_NO = 7
# response = ctypes.windll.user32.MessageBoxW(0, "%s" % text, "%s" % title, 4)
# if response == ID_YES:
# reboot_machine()
# else:
# pass
def uptime_seconds():
return time.time() - psutil.boot_time()
def uptime_minutes():
return (time.time() - psutil.boot_time()) / 60
def uptime_hours():
return (time.time() - psutil.boot_time()) / 3600
def uptime_days():
return ((time.time() - psutil.boot_time()) / 3600) / 24
def convert_time_to_hours(seconds):
return seconds / 3600
def convert_time_to_date(seconds, translate=False):
from datetime import datetime, timedelta
sec = timedelta(seconds=seconds)
d = datetime(1, 1, 1) + sec
# print("DAYS:HOURS:MIN:SEC")
# up 23 weeks, 3 days, 23 hours, 16 minutes
# up 23 weeks 3 days 23 hours 16 minutes
if translate:
if get_language() == "fr":
return "%d jours %d heures %d minutes %d secondes" % (d.day - 1, d.hour, d.minute, d.second)
return "%d days %d hours %d minutes %d seconds" % (d.day - 1, d.hour, d.minute, d.second)