from setuphelpers import *
import winreg
import json
import os
def installed_softwares_user(keywords=None, uninstallkey=None, name=None, ignore_empty_names=True):
"""Return list of installed software from registry (both 32bit and 64bit)
Args:
keywords (str or list): string to lookup in key, display_name or publisher fields
uninstallkey : filter on a specific uninstall key instead of fuzzy search
.. versionchanged:: 1.3.11
name (str regexp) : filter on a regular expression on software name
Returns:
list of dicts: [{'key', 'name', 'version', 'install_date', 'install_location'
'uninstall_string', 'publisher','system_component'}]
>>> softs = installed_softwares('libre office')
>>> if softs:
... for soft in softs:
... print uninstall_cmd(soft['key'])
???
"""
name_re = re.compile(name) if name is not None else None
def check_words(target, words):
mywords = target.lower()
result = not words or mywords
for w in words:
result = result and w in mywords
return result
def list_fromkey(uninstall, noredir=True):
result = []
with reg_openkey_noredir(winreg.HKEY_CURRENT_USER, uninstall, noredir=noredir) as key:
if isinstance(keywords, str):
mykeywords = keywords.lower().split()
elif isinstance(keywords, bytes):
mykeywords = str(keywords).lower().split()
elif keywords is not None:
mykeywords = [ensure_unicode(k).lower() for k in keywords]
else:
mykeywords = None
i = 0
while True:
try:
subkey = winreg.EnumKey(key, i)
appkey = reg_openkey_noredir(winreg.HKEY_CURRENT_USER, "%s\\%s" % (uninstall, subkey), noredir=noredir)
display_name = reg_getvalue(appkey, 'DisplayName', '')
display_version = reg_getvalue(appkey, 'DisplayVersion', '')
try:
date = str(reg_getvalue(appkey, 'InstallDate', '')).replace('\x00', '')
try:
install_date = datetime.datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d %H:%M:%S')
except:
try:
install_date = datetime.datetime.strptime(date, '%d/%m/%Y').strftime('%Y-%m-%d %H:%M:%S')
except:
install_date = date
except:
date = reg_getvalue(appkey, 'InstallDate', '')
install_location = reg_getvalue(appkey, 'InstallLocation', '')
uninstallstring = reg_getvalue(appkey, 'UninstallString', '')
publisher = reg_getvalue(appkey, 'Publisher', '')
if reg_getvalue(appkey, 'ParentKeyName', '') == 'OperatingSystem' or reg_getvalue(appkey, 'SystemComponent', 0) == 1:
system_component = 1
else:
system_component = 0
if (not ignore_empty_names or display_name != '') and (
(uninstallkey is None or (subkey == uninstallkey)) and
(mykeywords is None or check_words(subkey+' '+display_name+' '+publisher, mykeywords)) and
(name_re is None or name_re.match(display_name))):
result.append({'key': subkey,
'name': display_name.replace('\x00', ''),
'version': ("%s" % display_version).replace('\x00', ''),
'install_date': ("%s" % install_date),
'install_location': install_location.replace('\x00', ''),
'uninstall_string': uninstallstring.strip('\x00'),
'publisher': publisher.replace('\x00', ''),
'system_component': system_component})
i += 1
except WindowsError as e:
if e.winerror == 259:
break
else:
raise
return result
result = list_fromkey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
return result
def install():
pass
def session_setup():
data = installed_softwares_user()
datajson = json.dumps(data)
path_backup_soft=makepath(os.environ['USERPROFILE'],'.installed_softwares_user.json')
if isfile(path_backup_soft):
remove_file(path_backup_soft)
with open(path_backup_soft,'w') as f:
f.write(datajson)
set_file_hidden(path_backup_soft)
def audit():
dict_profile_path = {}
for p in get_local_profiles():
jsonfile = makepath(p['profile_path'],".installed_softwares_user.json")
if not isfile(jsonfile):
continue
try:
with open(jsonfile,'r') as f:
data = f.read()
datajson = json.loads(data)
dict_profile_path[p['profile_path']]=datajson
except:
pass
WAPT.write_audit_data_if_changed("audit-user-installed-softwares", "audit-user-installed-softwares", dict_profile_path)
return "OK"