日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/13 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

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

總結

以上是生活随笔為你收集整理的python实现程序安装_使用python实现对windows软件包的安装和卸载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。