# -*- coding: utf-8 -*-
from setuphelpers import *
import waptguihelper
import time
bin_name = "SpotifyFullSetup.exe"
app_name = "Spotify"
app_dir = makepath(programfiles, app_name)
bin_path = makepath(app_dir, bin_name)
user_bin_path = r"%AppData%\Spotify\Spotify.exe"
def install():
# Uninstalling before install to avoid code duplication of safe commands
uninstall()
# Installing the software
mkdirs(app_dir)
print("Copying: %s to: %s" % (bin_name, app_dir))
filecopyto(bin_name, app_dir)
# Adding Firewall Rules
add_netfirewallrule(app_name, user_bin_path, app_name)
def uninstall():
# Uninstalling the software
if isdir(app_dir):
remove_tree(app_dir)
# Removing Firewall Rules
remove_netfirewallrule(app_name)
remove_netfirewallrule("Spotify Music")
def session_setup():
# Declaring local variables
package_version = control.version.split("-")[0]
user_reg_version = ".".join(
registry_readstring(HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Spotify", "DisplayVersion").split(".")[:4]
)
user_app_dir = makepath(user_appdata, app_name)
user_app_path = makepath(user_app_dir, "Spotify.exe")
# Removing Spotify Appx
remove_user_appx("SpotifyAB.SpotifyMusic")
# Determining to install the software or not based on user agreement
if not registry_readstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "Install") or force:
if params.get("install_with_luti", False):
ask_to_install = 6
else:
ask_to_install = waptguihelper.message_dialog(
"Configuring %s" % app_name, "Do you want to install %s on this user account?" % app_name, 3 + 32
)
if ask_to_install == 6:
registry_setstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "Install", "Yes")
elif ask_to_install == 7:
registry_setstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "Install", "No")
# Fix disable autorun not working
if Version(user_reg_version) <= Version("1.2.25.1011") or user_reg_version == "":
registry_delete(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "DisableAutorunOnce")
# Installing the software in user environment
if registry_readstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "Install") == "Yes":
if Version(package_version) > Version(user_reg_version) or user_reg_version == "":
print("Installing: %s (%s) in user environment" % (bin_name, package_version))
if not running_as_admin() or params.get("running_as_luti"):
killalltasks("Spotify")
run('"%s" %s' % (bin_path, "/silent"))
# Disabling app autorun once
if not reg_value_exists(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "DisableAutorunOnce"):
# Reg value exists only after autostart
time.sleep(120)
registry_delete(HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", app_name)
registry_setstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "DisableAutorunOnce", "Done")
else:
# SpotifyFullSetup.exe /silent - returned non-zero exit status 23.
# Spotify Installer - Invalid user account for install.
error("You are running the session-setup as local administrator. The installation would crash.")
# Uninstalling the software from the user environment
elif registry_readstring(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "Install") == "No":
if isfile(user_app_path):
print("Uninstalling: %s from user environment" % (app_name))
killalltasks("Spotify")
run('"%s" %s' % (user_app_path, "/uninstall /silent"))
registry_delete(HKEY_CURRENT_USER, r"SOFTWARE\WAPT\%s" % app_name, "DisableAutorunOnce")
def add_netfirewallrule(
rule_name,
app_path,
group=None,
direction="Inbound",
profile=None,
enabled=True,
action="Allow",
protocol=None,
):
"""
Add a Windows Firewall rule using New-NetFirewallRule cmdlet in PowerShell for Windows 10 and newer,
or netsh advfirewall for older Windows versions.
https://learn.microsoft.com/troubleshoot/windows-server/networking/netsh-advfirewall-firewall-control-firewall-behavior
https://learn.microsoft.com/powershell/module/netsecurity/new-netfirewallrule
Args:
rule_name (str): Display name for the firewall rule.
app_path (str): Path to the program for which the rule is being created.
group (str, optional): Group name for the firewall rule (only works with PowerShell).
direction (str): Direction of the rule (Inbound or Outbound). Default: Inbound.
profile (str or list, optional): Profile(s) to which the rule should apply (e.g., "Domain,Private"). Default: Any.
enabled (bool): Specify if the created rule's state is Enabled (True) or not (False). Default: True.
action (str): Action for the rule (Allow or Block). Default: Allow.
protocol (str, optional): Protocol by name or number (e.g., "TCP", "UDP", "ICMPv4", or "ICMPv6"). Default: Any.
Returns:
waptutils.RunOutput: The result of the command execution.
.. versionadded:: 2.5
"""
if not isinstance(profile, list):
profile = ensure_list(profile)
profile = ",".join(profile)
message = f"Adding Firewall Rule: {rule_name}"
if direction:
message += f" ({direction})"
if protocol:
message += f" ({protocol})"
if action:
message += f" ({action})"
print(message)
if windows_version() < WindowsVersions.Windows10:
direction = "out" if direction.lower() == "Outbound".lower() else "in"
enabled = "no" if not enabled else "yes"
cmd_command = f'netsh advfirewall firewall add rule name="{rule_name}" dir={direction} action={action} program="{app_path}" enable={enabled}'
if profile: # any and all are working
cmd_command += f' profile="{profile}"'
if protocol:
cmd_command += f' protocol="{protocol}"'
result = run_notfatal(cmd_command)
else:
pwsh_command = (
f'New-NetFirewallRule -DisplayName "{rule_name}" -Direction {direction} -Action {action} -Program "{app_path}" -Enabled {str(enabled)}'
)
if group:
pwsh_command += f' -Group "{group}"'
if profile:
pwsh_command += f' -Profile "{profile}"'
if protocol:
pwsh_command += f' -Protocol "{protocol}"'
result = run_powershell(pwsh_command, output_format="text")
return result
def remove_netfirewallrule(rule_name):
"""
Remove Windows Firewall rule using New-NetFirewallRule cmdlet in PowerShell for Windows 10 and newer,
or netsh advfirewall for older Windows versions.
Args:
rule_name (str): Display name of the firewall rule to remove.
Returns:
waptutils.RunOutput: The result of the command execution.
.. versionadded:: 2.5
"""
print(f"Removing Firewall Rule: {rule_name}")
if windows_version() < WindowsVersions.Windows10:
result = run_notfatal(f'netsh advfirewall firewall delete rule name="{rule_name}"')
else:
result = run_powershell(
f'Remove-NetFirewallRule -DisplayName "{rule_name}" -ErrorAction SilentlyContinue', output_format="text", accept_returncodes=[0, 1, 3010]
)
return result