方案一: 直接套用脚本
首先定义了 setup_file 变量来表示静默安装程序的路径,install_path 变量表示安装程序的安装目录。这两个变量需要根据实际情况进行设置。
然后,我们将静默安装程序的路径和参数放入 cmd变量中。这里使用了 /s 参数来执行静默安装,并使用 /D 参数来指定安装目录。
最后,我们使用 subprocess.run() 方法来运行静默安装程序,将 cmd变量作为参数传递给该方法。该方法会启动静默安装程序,并等待安装过程完成。此时安装过程将在后台运行,不会弹出任何对话框或提示框,直接安装。
import subprocess
# 静默安装程序路径,即exe程序存放文件夹
setup_file = 'path/node.exe'
# 安装程序的安装目录
install_path = 'D:/Program Files/InstallFolder/'
# cmd 中的/s 参数用于执行静默安装,/D 参数用于指定安装目录
cmd = [setup_file, '/s', '/D=' + install_path]
# 使用 subprocess 模块运行静默安装程序
subprocess.run(cmd)
首先定义了 uninstall_file 变量来表示静默卸载程序的路径(注意:uninstall.exe是安装程序默认有的卸载exe,有的程序没有此卸载exe,根据实际情况而定或替换),install_path 变量表示卸载程序的安装目录。这两个变量需要根据实际情况进行设置。
然后,我们将静默卸载程序的路径和参数放入 args 变量中。这里使用了 /silent 或 /S 参数来执行静默卸载。
最后,我们使用 subprocess.run() 方法来运行静默卸载程序,将 cmd变量作为参数传递给该方法。该方法会启动静默卸载程序,并等待卸载过程完成。此时卸载过程将在后台运行,不会弹出任何对话框或提示框,直接卸载
import subprocess
# 静默卸载程序路径
uninstall_file = 'path/uninstall.exe'
# 卸载程序的安装目录
install_path = 'D:/Program Files/InstallFolder/'
# cmd 中的 /silent 或 /S 参数用于执行静默卸载
cmd = [uninstall_file, '/silent']
# 使用 subprocess 模块运行静默卸载程序
subprocess.run(cmd)
方案二:直接调用封装脚本
脚本实现封装后,只需要在Exe.yaml文件中写用例即可,此后执行ExE.py脚本即可实现程序的安装、卸载
目录介绍:
Exe.yaml配置信息
exe_process_name:程序的进程名
exe_path: exe软件包存放的路径
exe_install_path:静默安装的路径
exe_uninstall_path:卸载程序所在的路径
EXE:
exe_process_name: node.exe
exe_path: D:
pp.exe
exe_install_path: D:Program FilesNode
exe_uninstall_path: D:Program FilesNodeuninstall.exe
PublicConfig.py脚本: 配置读取信息,方便调用
import os
from Public_Utils.util_yaml import YamlReader
class YamlPath:
def __init__(self):
current = os.path.abspath(__file__)
self.base_dir = os.path.dirname(os.path.dirname(current))
def get_exe_yaml_file(self):
_config_path = self.base_dir + os.sep + "Public_ConfigPublic_yaml"
self._config_file = _config_path + os.sep + "Exe.yaml"
return self._config_file
class ConfigYaml:
def __init__(self): #初始yaml读取配置文件
self.exe_config = YamlReader(YamlPath().get_exe_yaml_file()).yaml_data()
def get_exe_process_name(self):
return self.exe_config['EXE']['exe_process_name']
def get_exe_path(self):
return self.exe_config['EXE']['exe_path']
def get_exe_install_path(self):
return self.exe_config['EXE']['exe_install_path']
def get_exe_uninstall_path(self):
return self.exe_config['EXE']['exe_uninstall_path']
if __name__ == '__main__':
pass
ExE.py执行脚本 其中函数包括
def win_process() 杀进程
def install_exe() 静默安装exe程序
def uninstall_exe() 静默卸载exe程序
import ctypes
import os
import psutil
import subprocess
from Public_Config.PublicConfig import ConfigYaml
class ExE:
def win_process(self):
for process in psutil.process_iter():
'''获取所有进程列表'''
try:
processinfo = process.as_dict(attrs=['pid','name'])
'''获取所有进程详情,并根据进程名匹配杀掉进程'''
if processinfo['name'] == ConfigYaml().get_exe_process_name():
process.kill()
except psutil.NoSuchProcess:
pass
def install_exe(self):
'''安装exe程序到指定的目录下'''
cmd = f'{ConfigYaml().get_exe_path()} /S /D={ConfigYaml().get_exe_install_path()}'
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
'''
通过subprocess模块以静默方式安装exe程序到指定的目录下
exe_path = 实际的安装程序存放路径
/S 静默模式安装
install_path = 安装到指定的目录
'''
if result.returncode == 0:
print("exe has installed")
else:
print(result.stderr.decode("cp936"))
def uninstall_exe(self): #根据exe程序自带的uninstall.exe卸载
if os.name == 'nt':
if ctypes.windll.shell32.IsUserAnAdmin() != 1:
raise Exception('权限不足,无法卸载程序!')
result = subprocess.run([ConfigYaml().get_exe_install_path(), '/S'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
if result.returncode == 0:
print("exe has uninstalled")
else:
print(result.stderr.decode("cp936"))
if __name__ == '__main__':
pass
util_yaml.py
import os
import yaml
class YamlReader:
#初始化,判断文件是否存在
def __init__(self,yaml_file):
if os.path.exists(yaml_file):
self.yaml_file = yaml_file
else:
raise FileNotFoundError("yaml文件不存在")
self._data = None
self._data_all = None
def yaml_data(self): #yaml文件读取 --单个文档读取
#第一次调用data,读取yaml文档,如果不是,直接返回之前保存的数据
if not self._data:
with open(self.yaml_file,'rb') as f:
self._data = yaml.safe_load(f)
return self._data
def yaml_data_all(self): #多个文档的读取
if not self._data_all:
with open(self.yaml_file,'rb') as f:
self._data_all = yaml.safe_load_all(f)
return self._data_all
页面更新:2024-04-26
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号