# -*- coding: utf-8 -*-
##################################################
# This file is part of WAPT Enterprise
# All right reserved, (c) Tranquil IT Systems 2023
# For more information please refer to
# https://wapt.tranquil.it/store/licences.html
##################################################
from setuphelpers import *
uninstallkey_list = [
"OneDrive.app",
"Microsoft Word.app",
"Microsoft Excel.app",
"Microsoft Outlook.app",
"Microsoft OneNote.app",
"Microsoft Defender.app",
"Microsoft PowerPoint.app",
"Microsoft Teams classic.app",
"pkgid:com.microsoft.package.Microsoft_Excel.app",
"pkgid:com.microsoft.package.Microsoft_Word.app",
"pkgid:com.microsoft.OneDrive",
"pkgid:com.microsoft.package.Frameworks",
"pkgid:com.microsoft.teams",
"pkgid:com.microsoft.package.Microsoft_OneNote.app",
"pkgid:com.microsoft.wdav",
"pkgid:com.microsoft.package.Microsoft_Outlook.app",
"pkgid:com.microsoft.package.Proofing_Tools",
"pkgid:com.microsoft.package.DFonts",
"pkgid:com.microsoft.package.Microsoft_AutoUpdate.app",
"pkgid:com.microsoft.pkg.licensing",
"pkgid:com.microsoft.package.Microsoft_PowerPoint.app",
]
def install():
bin_name = glob.glob("Microsoft_365_and_Office_*.pkg")[0]
print("Installing %s" % control.package)
install_pkg_with_timeout(
bin_name,
key="pkgid:com.microsoft.package.Microsoft_Excel.app", # key to avoid install loops and for registry version checking
uninstallkeylist=uninstallkey_list,
timeout=1800,
)
def uninstall():
for to_uninstall in uninstallkey_list:
if "pkgid:" in to_uninstall:
print(f"Uninstalling: {to_uninstall}")
to_uninstall = to_uninstall.split("pkgid:")[-1]
uninstall_pkg(to_uninstall)
else:
print(f"Uninstalling: {to_uninstall}")
uninstall_app(to_uninstall)
def install_pkg_with_timeout(pkg_path, key="", min_version="", get_version=None, killbefore=None, force=False, uninstallkeylist=None, timeout=600):
"""Installs a pkg file, given its name or a path to it."""
if key:
if not need_install(key=key, min_version=min_version, get_version=get_version, force=force):
print("The dmg file {0} is already installed on this machine.".format(pkg_path))
if key and isinstance(uninstallkeylist, list) and not key in uninstallkeylist:
uninstallkeylist.append(key)
return False
pkg_name = os.path.basename(pkg_path)
if killbefore:
killalltasks(killbefore)
run('sudo installer -package "{0}" -target /'.format(pkg_path), timeout=timeout)
if key:
if need_install(key=key):
error("%s has been installed but the %s can not be found" % (pkg_path, key))
if need_install(key=key, min_version=min_version, get_version=get_version):
error(
"%s has been executed and %s has been found, but version does not match requirements of min_version=%s" % (pkg_path, key, min_version)
)
# add the key to the caller uninstallkeylist
if key and isinstance(uninstallkeylist, list) and not key in uninstallkeylist:
uninstallkeylist.append(key)
print("Package {0} has been installed.".format(pkg_name))
def need_install(key="", min_version="", get_version=None, force=False, higher_version_warning=True):
if force:
return True
if key:
if uninstall_key_exists(key):
if not min_version:
return False
if get_version:
installed_version = get_version([p for p in installed_softwares() if p["key"] == key][0])
else:
if key.startswith("pkgid:"):
pkg = get_pkg_info(key[6:])
installed_version = pkg.get("pkg-version", "")
else:
plist_obj = get_plist_obj(get_info_plist_path(key))
installed_version = (
plist_obj.get("CFBundleShortVersionString", "")
if plist_obj.get("CFBundleShortVersionString", "")
else plist_obj.get("CFBundleVersion", "")
)
if Version(installed_version) >= Version(min_version):
if higher_version_warning:
if Version(min_version) < Version(installed_version):
print("WARNING the installed version (%s) is higher than the requested version (%s)" % (installed_version, min_version))
return False
else:
return True
return True