import os
import subprocess
import ctypes
import re

class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)

with disable_file_system_redirection():
	def app_name(command):
		obj = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
		result,error=obj.communicate()
		if error:
			return error
		else:
			keyword = "Name"
			before_keyword, keyword, after_keyword = result.partition(keyword)
			return after_keyword.strip()

	def uninstall(command,app):
		obj = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
		result,error=obj.communicate()
		if error:
			return error
		else:
			print "Uninstalling "+app+"........"
			print app+" uninstalled Succesfully"

	## Checking whether the app is present in the device ##
	hp_client = app_name('wmic product where name="HP Client Security Manager" get name')
	hp_security = app_name('wmic product where name="HP Security Update Service" get name')

	if hp_client == "HP Client Security Manager":
		## uninstalling the app if it is present ###
		uninstall('wmic product where name="HP Client Security Manager" call uninstall',"HP Client Security Manager")
	else:
		print "HP Client Security Manager is not present in your device"

	if hp_security == "HP Security Update Service":
		uninstall('wmic product where name="HP Security Update Service" call uninstall',"HP Security Update Service")
	else:
		print "HP Security Update Service is not present in your device"
	
