# -*- coding: utf-8 -*-
from setuphelpers import *
import platform
uninstallkey = []
# Installation procedure: https://wiki.documentfoundation.org/Deployment_and_Migration
# Defining variables
bin_name_string = 'LibreOffice_%s_Win_x64.msi'
silent_args_dict = {'ALLUSERS' : 1, 'CREATEDESKTOPLINK' : 0, 'REBOOTYESNO' : 'No', 'ISCHECKFORPRODUCTUPDATES' : 0, 'VC_REDIST' : 0, 'QUICKSTART' : 0, 'ADDLOCAL' : 'ALL', 'REMOVE' :'gm_o_Onlineupdate'} # INSTALLLOCATION
def install():
# Initializing variables
package_version = control.version.split('-',1)[0]
bin_name = bin_name_string % package_version
processes_to_kill = control.impacted_process
major_version = package_version.split('.')[0]
# Uninstalling OpenOffice if detected
for uninstall in installed_softwares(name="OpenOffice"):
print('Removing: %s' % (uninstall['name']))
run(uninstall_cmd(uninstall['key']))
# Uninstalling LibreOffice if too old
for uninstall in installed_softwares(name="LibreOffice"):
if not uninstall['version'].startswith(major_version):
if 'help' in uninstall['name'].lower():
continue
print('Removing: %s' % (uninstall['name']))
run(uninstall_cmd(uninstall['key']))
# Defining Office default applications
"""
REGISTER_ALL_MSO_TYPES (default=0) - use LibreOffice as the default application for Microsoft Office file formats
REGISTER_NO_MSO_TYPES (default=0) – don't use LibreOffice as default application for any Microsoft Office file formats
REGISTER_DOC (default=0) - use LibreOffice as the default application for Microsoft Word file format .doc (You can use similar for .xls, .ppt etc.)
"""
#if not installed_softwares(name="Microsoft Office"):
if not file_assoc('.doc').startswith('word') or not file_assoc('.docx').startswith('word'):
silent_args_dict['REGISTER_ALL_MSO_TYPES']=1
else:
silent_args_dict['REGISTER_NO_MSO_TYPES']=1
# Do not use flags: SELECT_WORD, SELECT_EXCEL, SELECT_POWERPOINT (making install crash)
""" if not file_assoc('.doc').startswith('word') or not file_assoc('.docx').startswith('word'):
silent_args_dict['SELECT_WORD']=1
if not file_assoc('.xls').startswith('excel') or not file_assoc('.xlsx').startswith('excel'):
silent_args_dict['SELECT_EXCEL']=1
if not file_assoc('.ppt').startswith('powerPoint') or not file_assoc('.pptx').startswith('powerpoint'):
silent_args_dict['SELECT_POWERPOINT']=1 """
# Installing the package
print('Installing: %s' % bin_name)
install_msi_if_needed(bin_name,
properties=silent_args_dict,
killbefore=processes_to_kill,
timeout=1200)
def update_package():
print('Downloading/Updating package content from upstream binary sources')
# Initializing variables
proxies = get_proxies()
if not proxies:
proxies = get_proxies_from_wapt_console()
app_name = control.name
url = control.sources
app_arch = control.architecture
if app_arch == 'x64':
dl_arch = 'x86_64'
else:
dl_arch = 'x86'
# Getting latest version from official website
sub_version = bs_find_all(url, 'span', 'class', 'dl_version_number',proxies=proxies)[0].text
version = bs_find_all(url, 'a', 'class', 'dl_download_link',proxies=proxies)[0]['href'].split('/')[-1].split('_')[1]
latest_bin = bin_name_string % version
url_dl = 'https://download.documentfoundation.org/libreoffice/stable/%s/win/%s/%s' % (sub_version, dl_arch, latest_bin)
print("Latest %s version is: %s" % (app_name,version))
print("Download url is: %s" % url_dl)
# Downloading latest binaries
if not isfile(latest_bin):
print('Downloading: %s' % latest_bin)
wget(url_dl,latest_bin,proxies=proxies)
# Checking version from file
version_from_file = get_version_from_binary(latest_bin)
if version != version_from_file:
os.rename(latest_bin,bin_name_string % version_from_file)
version = version_from_file
# Changing version of the package
control.version = '%s-%s'%(version,int(control.version.split('-')[-1])+1)
control.save_control_to_wapt()
print('Changing version to: %s in WAPT\\control' % control.version)
# Deleting outdated binaries
remove_outdated_binaries(version)
def file_assoc(ext):
"""Renvoie le type d'application associe à une extension (exemple : .doc)"""
return registry_readstring(HKEY_CLASSES_ROOT,ext,None).lower()
def get_proxies():
import platform
if platform.python_version_tuple()[0] == '3':
from urllib.request import getproxies
else:
from urllib import getproxies
return getproxies()
def get_version_from_binary(filename):
if filename.endswith('.msi'):
return get_msi_properties(filename)['ProductVersion']
else:
return get_file_properties(filename)['ProductVersion']
def get_version_from_binary(filename):
if filename.endswith('.msi'):
return get_msi_properties(filename)['ProductVersion']
else:
return get_file_properties(filename)['ProductVersion']
def remove_outdated_binaries(version, list_extensions=['exe','msi','deb','rpm','dmg','pkg'], list_filename_contain=None):
if type(list_extensions) != list:
list_extensions = [list_extensions]
if list_filename_contain:
if type(list_filename_contain) != list:
list_filename_contain = [list_filename_contain]
list_extensions = ['.' + ext for ext in list_extensions if ext[0] != '.']
for file_ext in list_extensions:
for bin_in_dir in glob.glob('*%s' % file_ext):
if not version in bin_in_dir:
remove_file(bin_in_dir)
if list_filename_contain:
for filename_contain in list_filename_contain:
if not filename_contain in bin_in_dir:
remove_file(bin_in_dir)
def bs_find(url, element, attribute=None, value=None, headers=None, proxies=None, features='html.parser', **kwargs):
""""You may need to use a header for some websites. For example: headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'}
"""
import requests
page = requests.get(url, proxies=proxies, headers=headers, **kwargs).text
try:
import bs4 as BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(page, features=features)
except:
import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(page)
if value:
return soup.find(element,{attribute:value})
else:
return soup.find(element)
def bs_find_all(url, element, attribute=None, value=None, headers=None, proxies=None, features='html.parser', **kwargs):
""""You may need to use a header for some websites. For example: headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'}
"""
import requests
page = requests.get(url, proxies=proxies, headers=headers, **kwargs).text
try:
import bs4 as BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(page, features=features)
except:
import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(page)
if value:
return soup.findAll(element,{attribute:value})
else:
return soup.findAll(element)
def get_proxies_from_wapt_console():
proxies = {}
if platform.system() == 'Windows':
waptconsole_ini_path = makepath(user_local_appdata(), 'waptconsole', 'waptconsole.ini')
else:
waptconsole_ini_path = makepath(user_home_directory(), '.config', 'waptconsole', 'waptconsole.ini')
if isfile(waptconsole_ini_path):
proxy_wapt = inifile_readstring(waptconsole_ini_path, 'global', 'http_proxy')
if proxy_wapt:
proxies = {'http': proxy_wapt, 'https': proxy_wapt}
return proxies