# -*- coding: utf-8 -*-
from setuphelpers import *
import platform
import bs4 as BeautifulSoup
"""
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
bin_contains = '7z'
ext_file_association = ('.001','.7z','.arj','.bz2','.bzip2','.cab','.cpio','.deb','.dmg','.fat','.gz','.gzip','.hfs','.iso','.lha','.lzh','.lzma','.ntfs','.rar','.rpm','.squashfs','.swm','.tar','.taz','.tbz','.tbz2','.tgz','.tpz','.txz','.vhd','.wim','.xar','.xz','.z','.zip','.zipe')
def install():
# Initializing variables
package_version = control.version.split('-',1)[0]
app_name = control.name
bin_name = glob.glob('*%s*.msi' % bin_contains)[0]
# Uninstalling others versions
for uninstall in installed_softwares(name=app_name):
if Version(uninstall['version']) != Version(get_version_from_binary(bin_name)):
print('Incorrect version of %s found: %s (%s)' % (app_name, uninstall['name'], uninstall['version']))
print('Removing: %s (%s)' % (uninstall['name'], uninstall['version']))
run(uninstall_cmd(uninstall['key']))
# Installing the package
print('Installing: %s' % bin_name)
install_msi_if_needed(bin_name,
min_version=package_version)
# File association for 7-Zip
for ext in (ext_file_association):
register_ext('7-zip',ext,'"%s" "%%1"' % (makepath(programfiles, '7-zip', '7zFM.exe')), icon="%s,1" % (makepath(programfiles, '7-zip', '7z.dll')))
def update_package():
# Declaring local variables
result = False
proxies = get_proxies()
if not proxies:
proxies = get_proxies_from_wapt_console()
app_name = control.name
url = 'https://www.7-zip.org/download.html'
if control.architecture == 'x64':
sub_bin_name = '7z%s-x64.msi'
else:
sub_bin_name = '7z%s.msi'
# Getting latest version from official website
print('URL used is: ' + url)
version = bs_find_all(url, 'b', proxies=proxies)[1].text.split('Zip ')[-1].split(' (')[0]
splitted_version = version.replace('.', '')
latest_bin = sub_bin_name % splitted_version
url_dl = 'https://www.7-zip.org/a/%s' % 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_msi_properties(latest_bin)['ProductName'].split(' ')[1]
if Version(version) != Version(version_from_file) and version_from_file != '':
print("Changing version to the version number of the binary (from: %s to: %s)" % (version, version_from_file))
os.rename(latest_bin, sub_bin_name % version_from_file)
version = version_from_file
else:
print("Binary file version corresponds to online version")
# Changing version of the package
if Version(version) > Version(control.get_software_version()):
print("Software version updated (from: %s to: %s)" % (control.get_software_version(), Version(version)))
result = True
else:
print("Software version up-to-date (%s)" % Version(version))
control.version = '%s-%s' % (Version(version), control.version.split('-', 1)[-1])
#control.set_software_version(version)
control.save_control_to_wapt()
# Deleting outdated binaries
remove_outdated_binaries(splitted_version)
# Validating update-package-sources
return result
def get_proxies():
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 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_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