python卸载windows程序_使用python实现对windows软件包的安装和卸载

在对windows安装包产品进行测试时,安装和卸载是难免的,并且人工的手动安装和卸载会花费大量的精力和时间,为此需要编写一个脚本来实现对windows安装包产品的自动卸载和安装。

首先参考了 http://www.cnblogs.com/TankXiao/archive/2012/10/18/2727072.html#msiexec ,该博文详细讲解了使用msiexe命令进行卸载的内容,同时提供了使用C#实现的卸载程序。对此,我想到了使用python编写脚本实现类似的功能。主要的算法大致是使用软件名称去注册表中搜索到该软件的包括productCode在内的uninstallString,而后根据这个字符串进行默认卸载,再根据软件的msi包路径进行默认安装。小弟菜鸟一枚,初次编写脚本,望大家多指点。

#this function is used to get the uninstall string of a software in windows

#input:the dir which is a register key,the name of software product

#output:the uninstall string,if None,means no find

def getProductCode(dir,prodcutName):

uninstallString = ''

#get the key of the uninstall path

#get the subkey,get the one which have the same name with productName

#by the subkey,get the value of uninstall string of it

try:

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE ,dir)

j=0

while 1:

name = _winreg.EnumKey(key,j)

#name = repr(name)

path = dir + '\\' + name

subkey = _winreg.OpenKey(key ,name)

value,type ='',''

try:

value,type = _winreg.QueryValueEx(subkey,'DisplayName')

except Exception,e:

pass

if value == prodcutName:

try:

value2,type2 = _winreg.QueryValueEx(subkey,'UninstallString')

except Exception,e:

pass

uninstallString = value2

return uninstallString

_winreg.CloseKey(subkey)

#print value,' ',type

j+=1

except WindowsError,e:

print

finally:

_winreg.CloseKey(key)

pass

#define the function uninstall_productbyCode(),to uninstall the product by code

def uninstall_productbyCode(code):

#uninstall_cmd = "msiexec /x /quiet /norestart " + path

uninstall_cmd = code + ' /quiet'

print uninstall_cmd

if os.system(uninstall_cmd) == 0:

return 0;

else:

return -1;

#define the function install_product(),to install the product

def install_product(path):

install_cmd = "msiexec /qn /i " + path

print install_cmd

if os.system(install_cmd) == 0:

return 0;

else:

return -1;

#define the function Is64Windows(),to judge the system is whether 64Windows

def Is64Windows():

return 'PROGRAMFILES(X86)' in os.environ

#define the function agent_install(),to auto install product

def product_install():

if Is64Windows():

product_path = product_loc + "softwarename_x64.msi"

else:

product_path = product_loc + "softwarename.msi"

reg_dir = cst_path4_x86

uninstallString = getProductCode(reg_dir,u'软件中文名')

print uninstallString

#for maybe in english system,we need to get english version product code,if we don't get chinese of that

if uninstallString == None:

uninstallString = getProductCode(reg_dir,u'软件英文名')

print uninstallString

# uninstall product

if uninstallString != None and 0 == uninstall_productbyCode(uninstallString):

print "uninstall softwarename scuessful"

else:

print "uninstall softwarename fail"

# install product

if 0 == install_product(product_path):

print "install softwarename scuessful"

else:

print "install softwarename fail"

pass

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值