记录 Jenkins 自动部署 GKfmc 程序
#!/usr/local/env python3
# -*- coding:utf-8 -*-
import psutil, shutil, os
# 杀死进程
def kill_process_name(process_name):
pid_list = psutil.pids()
for pid in pid_list:
try:
each_pro = psutil.Process(pid)
if process_name.lower() in each_pro.name().lower():
# logger.info('find and kill {}'.format(process_name))
each_pro.terminate()
each_pro.wait(timeout=3)
except psutil.NoSuchProcess as pid:
pass
# copy文件
def copy_file(s_dir, d_dir):
if os.path.exists(d_dir):
shutil.rmtree(d_dir) # 删除zzinfo工作目录
os.makedirs(d_dir)
for root, dirs, files in os.walk(s_dir):
for d in dirs:
s_dir_name = os.path.join(root, d) # 列出workspace里面的子目录
d_dir_name = s_dir_name.replace(s_dir, d_dir)
if not os.path.exists(d_dir_name):
os.makedirs(d_dir_name) # 创建zzinfo里面的子目录
for f in files:
s_file_name = os.path.join(root, f) # 列出workspace里面的所有文件的绝对路径
d_file_name = s_file_name.replace(s_dir, d_dir) # 列出目标文件的绝对路径
shutil.copyfile(s_file_name, d_file_name)
# main函数入口
if __name__ == '__main__':
os.environ['BUILD_ID'] = "dontKillMe"
kill_process_name("gkfmc64.exe")
# copy lib部分
lib_s_dir = os.getenv("WORKSPACE") + r'\lib'
lib_d_dir = r"d:\zzinfo\gkfmc64\lib"
copy_file(lib_s_dir, lib_d_dir)
# copy bin部分
bin_s_dir = os.getenv("WORKSPACE") + r'\target\classes'
bin_d_dir = r"d:\zzinfo\gkfmc64\bin"
copy_file(bin_s_dir, bin_d_dir)
# 启动gkfmc64.exe
os.popen(r"start /d d:\zzinfo\PTJYgkfmc64 gkfmc64.exe")