from setuphelpers import *
import winreg
import os
json_file = ".installed_softwares_user.json"
def install():
pass
def session_setup():
data_dict = installed_softwares_user()
json_path = makepath(os.environ["USERPROFILE"], json_file)
print(f"Writing: {json_path}")
json_write_file(json_path, data_dict)
set_file_hidden(json_path)
return "RERUN"
def audit():
audited_data = {}
for p in get_local_profiles():
json_path = makepath(p["profile_path"], json_file)
if r"C:\WINDOWS".lower() in p["profile_path"].lower():
continue
if not isfile(json_path):
print(f"session-setup have NOT been run for the profile_path: {p['profile_path']}")
continue
try:
print(f"session-setup have been run for the profile_path: {p['profile_path']}")
data_dict = json_load_file(json_path)
audited_data[p["profile_path"]] = data_dict
except:
pass
WAPT.write_audit_data_if_changed("audit-user-installed-softwares", "audit-user-installed-softwares", audited_data)
return "OK"
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