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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/246139.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图形学教程Lecture 13: RayTracing1(Whitted-Style Ray Tracing)知识点总结

课程地址:https://www.bilibili.com/video/BV1X7411F744?p13 课件地址:https://sites.cs.ucsb.edu/~lingqi/teaching/games101.html 感谢大神的课程 1.光线追踪的好处:真实,但是效率低,所以目前是离线渲染 2.whitt…

python课程设计编写电子通讯录_用Python实现简单通讯录

一个简单的通讯录例子 #!/usr/bin/python __author__ fierce #coding:utf-8 import os #引用os模块 import pickle #应用pickle模块 Path/home/Person.data #全局变量,这里是通讯录物理路径 #判断通讯录是否存在,不存在则创建空通讯录 if os.path.exist…

图形学教程Lecture 14: RayTracing1(Acceleration Radiometry)知识点总结

课程地址:https://www.bilibili.com/video/BV1X7411F744?p14 课件地址:https://sites.cs.ucsb.edu/~lingqi/teaching/games101.html 感谢大神的课程 1.用统一的空间划分(Grid)加速计算光线相交。划分颗粒度需要注意&#xff0…

Unity项目代码书写规范

以Google的代码规范为主,稍加改动 https://google.github.io/styleguide/csharp-style.html 书写规范 基础写法 Pascal和驼峰混用,参数用驼峰写法,除参数外,都以Pascal写法为主。括号建议用换行方式书写Code 类, 方法, 枚举, …

python常用的包_Python3之常用包汇总

原博文 2020-06-04 10:53 − Python包网站: https://pypi.org/ 1. 繁体与简体转换(https://github.com/berniey/hanziconv.git) pip install hanziconv 示例: from hanziconv impo... 相关推荐 2019-12-09 20:32 − [TOC] # 1. 模块 ## 1.1 模块是什么? - 模块就是个…

Unity游戏中的一些规范和优化建议

一.代码规范和建议 避免Update LateUpdate等函数内频繁的GC Alloc,避免在Update和LateUpdate内有以下操作: 调用GetComponet() 调用FindObjectsOfType() 使用GameObject.Tag和GameObject.Name 等等其他有堆内存分配的操作 避免频繁调用T…

vue如何获取年月日_BootstrapVue——Vue和Bootstrap的相结合,构建响应式应用更简单...

介绍BootStrap是世界上最受欢迎的构建响应式移动优先网站的框架,Vue是当前最流行的前端框架之一,BootstrapVue则是将两者相结合,使用BootstrapVue,可以使用Vue.js和前端CSS库--Bootstrap v4在Web上构建响应式,移动优先…

计算机图形学:光场

https://zhuanlan.zhihu.com/p/47492390 https://zhuanlan.zhihu.com/c_1035195596059222016

cpu队列长度太长解决方法_CPU中的调试算法对比

批处理系统中采用的调度算法重要指标(吞吐量,周转时间,CPU利用率,公平平衡)非抢占式的先来先服务算法(FCFS):按照进程就绪的先后顺序使用CPU特点:公平,实现简单,但是长进程后面的短进程需要等待…

Mono,CLR,.net,Net Framework之间的关系

先粗略看下各自的意义: .Net:以下这些技术的统称。是一个平台,而.NET平台有一个实现标准,叫做.Net Standard;.Net Framework/.Net Core/Mono:实现了这个标准,其选择的组件不一定相同CLR&#x…

c++矩阵类_数据结构-JavaScript矩阵类的设计与实现

矩阵是线性代数课学习的重点内容之一,也是线性代数常见工具之一,在应用数学、统计分析、计算机科学、计算机图像处理级物理等多学科中均有应用。矩阵主要是指数据的行列排列的形式,由行row与列col所组成,在数学中M*N矩阵是指具有M…

GPU Skin

转自:http://geekfaner.com/unity/blog4_GPUSkin.html GPU Skin这门技术在端游时代属于标配,特别是MMO游戏,但是手游时代就要case by case了,因为手机的GPU资源还是很珍贵的(后处理之类的)。作为技术人员&…

button按钮onclick触发不了_单按钮启停:测试模拟脉冲发生器的动作

灯闪烁所模拟是PLC单方面向控件输出信号的过程,控件也可以向模拟PLC输出信号。做一个带反馈灯的按钮,被按下后点亮,再按一下熄灭,且使按钮自保需要一定的接触时间。对应的PLC程序需要一个脉冲发生器(假设地址为L602) 把按钮触点信…

图形学教程Lecture 2: Review of Linear Algebra知识点总结

视频链接:https://www.bilibili.com/video/BV1X7411F744?p2 课程主页链接:http://games-cn.org/intro-graphics/ 课件PPT链接:http://games-cn.org/graphics-intro-ppt-video/ 1. 点乘 2. 点乘应用 获得两个向量的夹角:衡量两…

code换取微信openid_「干货」微信支付前后端流程整理(Vue+Node)

作者:河畔一角转发链接:https://mp.weixin.qq.com/s/ANLjtieWELr39zhgRAeF1w前言最近有不少同学希望我能够把微信支付的前后端流程整理一下,"虽然买了课程,依然看的比较晕"。实际上,我在2019年下半年出了一篇…

Games101现代图形学入门Lecture 3: Transformation知识点总结

视频链接:https://www.bilibili.com/video/BV1X7411F744?p3 课程主页链接:http://games-cn.org/intro-graphics/ 课件PPT链接:http://games-cn.org/graphics-intro-ppt-video/ 1. 缩放矩阵 2. 反射矩阵 3. 切变矩阵 4. 旋转矩阵 5. 线性…

pytorch forward_pytorch使用hook打印中间特征图、计算网络算力等

0、参考https://oldpan.me/archives/pytorch-autograd-hookhttps://pytorch.org/docs/stable/search.html?qhook&check_keywordsyes&areadefaulthttps://github.com/pytorch/pytorch/issues/598https://github.com/sksq96/pytorch-summaryhttps://github.com/allensll…

Games101现代图形学入门Lecture 4: Transformation Cont知识点总结

视频链接:https://www.bilibili.com/video/BV1X7411F744?p4 课程主页链接:http://games-cn.org/intro-graphics/ 课件PPT链接:http://games-cn.org/graphics-intro-ppt-video/ 1. 3D变换 缩放和平移矩阵 旋转矩阵 欧拉角:rol…

python3 for_Python3: for 表达式

#1.在控制台输入一个成绩score #2.判断成绩, #*如果成绩小于60输出不及格 #60到70 及格 #70到80 中等 #80到90 良好 #90 100 优秀 def level(score_list): # score input("请输入成绩:") # while score!"stop": for sc in score_li…

Hash和红黑树以及其在C#中的应用

参考资料&#xff1a; .Net 中HashTable&#xff0c;HashMap 和 Dictionary<key,value> 和List<T>和DataTable的比较 - 王若伊_恩赐解脱 - 博客园 c#HashSet源码解析_fdyshlk的博客-CSDN博客_c# hashset 红黑树和哈希表的区别 - 安全技术 - 亿速云 一、基本概念…