python实现程序安装_使用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:
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
總結
以上是生活随笔為你收集整理的python实现程序安装_使用python实现对windows软件包的安装和卸载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日方舟魏彦吾立绘及相关资料
- 下一篇: python课程设计编写电子通讯录_用P