# -*- coding: utf-8 -*-
from setuphelpers import *
import time
import psutil
import win32api
import win32con
import waptguihelper
import tempfile
try:
from waptenterprise.waptservice.enterprise import get_active_sessions, start_interactive_process
except:
from waptservice.enterprise import get_active_sessions, start_interactive_process
from setupdevhelpers import get_proxies_from_wapt_console
# Declaring global variables - Warnings: 1) WAPT context is only available in package functions; 2) Global variables are not persistent between calls
terminator_path = makepath(programfiles, "Terminator")
terminator_ico = "terminator.ico"
launch_script = "start_terminator.vbs"
wscript = makepath(system32(), "wscript.exe")
arg = makepath(terminator_path, launch_script)
icon = makepath(terminator_path, terminator_ico)
app_name = "Terminator"
def install():
mkdirs(terminator_path)
filecopyto(launch_script, terminator_path)
filecopyto(terminator_ico, terminator_path)
def uninstall():
remove_desktop_shortcut(app_name)
remove_tree(terminator_path)
def session_setup():
print("Configuring Terminator for user Debian WSL")
# Declaring local variables
proxies = get_proxies()
if not proxies:
proxies = get_proxies_from_wapt_console()
app_reg_path = r"SOFTWARE\WAPT\Terminator"
start_menu_path = makepath(winshell.programs(0), "StartUp")
my_username = get_current_user()
# Skip Luti
if params.get("install_with_luti", False):
return "OK"
def is_term_to_install():
return registry_readstring(HKEY_CURRENT_USER, app_reg_path, "Install")
def is_term_configured():
return registry_readstring(HKEY_CURRENT_USER, app_reg_path, "Configured")
def is_debian_configured():
return registry_readstring(HKEY_CURRENT_USER, app_reg_path, "Debian Configured")
first_install = False
user_debian_path = makepath(user_local_appdata, "Microsoft", "WindowsApps", "debian.exe")
vcxsrv_path = installed_softwares("VcXsrv")[0]["uninstall_string"].replace("uninstall.exe", "vcxsrv.exe").split('"')[1]
vcxsrv_start_cmd = r":0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto"
debian_proxy_conf = "/etc/apt/apt.conf.d/proxy.conf"
# Asking if Terminator have to be installed on this user profile
if is_term_to_install() == "" or force:
if messagebox("Configuring Terminator", "Do you want to install Terminator on this user account?", style=win32con.MB_YESNOCANCEL) == 6:
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Install", "Yes", type=REG_SZ)
first_install = True
else:
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Install", "No", type=REG_SZ)
if is_term_to_install() == "No":
return "OK"
# Asking if debian have already been launch once and that the defines username is the same than Windows
if is_debian_configured() == "No" or is_debian_configured() == "" or force:
if (
messagebox(
"Configuring Terminator",
"Have you already run WSL Debian once and configured the username? Please verify!",
style=win32con.MB_YESNOCANCEL,
)
== 6
):
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Debian Configured", "Yes", type=REG_SZ)
else:
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Debian Configured", "No", type=REG_SZ)
print(
"If your Debian username is NOT %s, we do recommend you to uninstall Debian then reintalling it with: wapt-get session-setup tis-wsl-debian -f"
% my_username
)
# print("If reinstalling your Debian is not desirable for you, you may configure the Debian requirements manually")
return "OK"
# Checking if Terminator is configured on this user profile
try:
if "terminator" in run('bash -c "dpkg -l | grep terminator"'):
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Configured", "Yes", type=REG_SZ)
except:
registry_setstring(HKEY_CURRENT_USER, app_reg_path, "Configured", "No", type=REG_SZ)
# Configuring Terminator on this user profile
if is_term_configured() == "No" or is_term_configured() == "" or force:
if proxies:
is_using_proxy = True
user_http_proxy = proxies["http"]
user_https_proxy = proxies["http"]
else:
is_using_proxy = False
prompt_proxy = ""
if not proxies:
prompt_proxy = waptguihelper.input_dialog(
"Configuring Terminator", "Please provide your proxy if needed (example: http://srvproxy.mydomain.lan:3128)", ""
)
if prompt_proxy:
is_using_proxy = True
user_http_proxy = prompt_proxy
user_https_proxy = prompt_proxy
# wsl_username = run_powershell("wsl --exec whoami")
wsl_username = waptguihelper.input_dialog("Configuring Terminator", "Please provide your WSL Debian username", my_username)
try:
print("## Set default bash user as root to perform install")
run('"%s" config --default-user root' % user_debian_path)
if is_using_proxy:
print("## Set APT Proxy")
proxy_temp_file = makepath(tempfile.gettempdir(), "proxy.conf")
run('''echo Acquire::http::Proxy "%s"; > "%s"''' % (user_http_proxy, proxy_temp_file))
run('''echo Acquire::http::Proxy "%s"; >> "%s"''' % (user_https_proxy, proxy_temp_file))
run('''start bash.exe -c -l "cp "/mnt/c/%s" "%s""''' % (proxy_temp_file.replace("\\", "/").split(":", 1)[-1], debian_proxy_conf))
remove_file(proxy_temp_file)
print("## Install Terminator and Dbus-x11")
run('start bash.exe -c -l "apt-get update"')
time.sleep(5)
run('start bash.exe -c -l "apt-get install dbus-x11 terminator -y"')
print("## Restore current user as default bash user")
run_notfatal('"%s" config --default-user %s' % (user_debian_path, wsl_username))
except:
print("ERROR: Terminator configuration failed")
# Adding Terminator Sortcuts
# C:\Windows\System32\wscript.exe "C:\Program Files\Terminator\start_terminator.vbs"
create_user_desktop_shortcut("Terminator", wscript, '"%s"' % arg, r"%USERPROFILE%", icon)
create_user_programs_menu_shortcut("Terminator", wscript, '"%s"' % arg, r"%USERPROFILE%", icon)
# Starting VcXsrv at user session startup
vcxsrv_startup_shortcut = makepath(start_menu_path, "VcXsrv.lnk")
vcxsrv_menu_shortcut = makepath(application_data, r"Microsoft\Windows\Start Menu\Programs", "VcXsrv.lnk")
create_shortcut(vcxsrv_startup_shortcut, vcxsrv_path, vcxsrv_start_cmd)
create_shortcut(vcxsrv_menu_shortcut, vcxsrv_path, vcxsrv_start_cmd)
if not isrunning("vcxsrv.exe"):
try:
vcx_process_list = []
vcx_process_list.append(vcxsrv_path)
vcx_process_list.extend(vcxsrv_start_cmd.split(" "))
psutil.Popen(vcx_process_list)
# run(app_exe, wait=False)
except:
print("Restart session for using Terminator, or launch VcXsrv manually")
if first_install:
messagebox("Terminator", "Installation of Terminator done, the shortcut is available on user desktop")
def update_package():
control.set_software_version(control.get_software_version())
control.save_control_to_wapt()
def messagebox(title, msg, style=win32con.MB_ICONINFORMATION):
r"""Open a message box to interact with the end user
Args:
title (str): Title of the message box
msg (str): Text contained in the message box
style (win32con): Format of the message box, usable values:
MB_OK
MB_ICONINFORMATION
MB_OKCANCEL
MB_YESNOCANCEL
MB_YESNO
Returns:
ID_OK = 0
ID_CANCEL = 2
ID_ABORT = 3
ID_YES = 6
ID_NO = 7
"""
return win32api.MessageBox(0, ensure_unicode(msg), ensure_unicode(title), style)