需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制。

简单GUI

python 调用exe执行 并传入参数_测试流程

python 调用exe执行 并传入参数_ci_02

python 调用exe执行 并传入参数_测试流程_03

python 调用exe执行 并传入参数_ci_04

思路:

1、通过python自身Tkinter实现GUI

2、ggsci.exe执行会出现交互窗口,如下

python 调用exe执行 并传入参数_测试流程_05

通过windows命令echo, 执行echo info all | D:\ogg\ggsci.exe,可以将命令传入ggsci中进行执行

问题:需要将多条命令要同时在ggsci交互窗口执行,通过echo无法实现

例如执行delete trandata dbo.fundasset 命令前,需要先执行DBLOGIN SOURCEDB run, USERID ogg, PASSWORD 123456

python 调用exe执行 并传入参数_测试流程_06

通过ggsci help命令,可以看到ggsci执行将命令放入文件中,通过文件将多条命令逐步执行。

 

代码:



#/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 取绝对路径
sys.path.append(BASE_DIR)  # 将目录加入系统环境变量,之后导入此目录下各模块不会提示找不到
from Tkinter import *
import tkMessageBox
# from tkinter import ttk
import subprocess
import logging
import logging.config


# 日志配置
LOGCONF_FILENAME = "../etc/logging.conf"
logging.config.fileConfig(LOGCONF_FILENAME)
logger = logging.getLogger('wj')

ogg_path = r"D:\ogg\ggsci.exe"
table_path = r"D:\ogg\dirdef\run1_defgen.def"


root = Tk()
root.geometry('800x600')
root.title('同步开启和关闭小工具')
root.resizable(width=True, height=True)  # 宽可变, 高可变,默认为True
Label(root, text='同步开启和关闭小工具', font=('Arial', 20)).grid(row=0)
Label(root, text="测试流程步骤一").grid(row=1)
Label(root, text="测试流程步骤二").grid(row=2)
Label(root, text="测试流程步骤三").grid(row=3)
Label(root, text="").grid(row=4)
Label(root, text="").grid(row=5)
Label(root, text="").grid(row=6)
Label(root, text="恢复节点流程步骤一").grid(row=7)
Label(root, text="恢复节点流程步骤二").grid(row=8)
Label(root, text="恢复节点流程步骤三").grid(row=9)
Label(root, text="恢复节点流程步骤四").grid(row=10)
Label(root, text="").grid(row=11)

def stop_log_func():
    stoplog = "../var/"
    filename = 'stop_log.txt'

    with open(table_path, "rb") as obj_read:
        table_list = []
        for line in obj_read.readlines():
            line = line.strip()
            if "table" in line:
                logger.debug(line)
                index = line.strip().find('table', 0)  # 获取table起始位
                table = line[index:]  # 获取table之后的数据
                table = table.replace('table', 'trandata', 2)  # 将table改成trandata
                table = 'delete ' + table
                table_list.append(table)
        logger.debug(table_list)

    with open(stoplog + filename, 'wb') as obj_write:
        delete_table = '\n'.join(table_list) + '\n'
        dblogin = 'DBLOGIN SOURCEDB run, USERID ogg, PASSWORD 123456\n'
        data = dblogin + delete_table
        logger.debug(data)
        obj_write.write(data)

    f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='关掉补充日志', message=f.stdout.read())


def stop_ogg_func():
    stopogg = "../var/"
    filename = 'stop_ogg.txt'

    with open(stopogg + filename, 'wb') as obj_write:
        stop1 = 'stop 829*\n'
        stop2 = 'kill mgr\n'
        data = stop1 + stop2
        logger.debug(data)
        obj_write.write(data)

    f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='停掉ogg程序', message=f.stdout.read())


def start_log_func():
    startlog = "../var/"
    filename = 'start_log.txt'

    with open(table_path, "rb") as obj_read:
        table_list = []
        for line in obj_read.readlines():
            line = line.strip()
            if "table" in line:
                logger.debug(line)
                index = line.strip().find('table', 0)  # 获取table起始位
                table = line[index:]  # 获取table之后的数据
                table = table.replace('table', 'trandata', 2)  # 将table改成trandata
                table = 'add ' + table
                table_list.append(table)
        logger.debug(table_list)

    with open(startlog + filename, 'wb') as obj_write:
        delete_table = '\n'.join(table_list) + '\n'
        dblogin = 'DBLOGIN SOURCEDB run, USERID ogg, PASSWORD Ogg123\n'
        data = dblogin + delete_table
        logger.debug(data)
        obj_write.write(data)

    f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='打开补充日志', message=f.stdout.read())

def dump_log_func():
    dumplog = "../var/"
    filename = 'dump_log.txt'

    with open(dumplog + filename, 'wb') as obj_write:
        dump1 = 'alter 829 begin now\n'
        dump2 = 'alter 829P1 begin now'
        data = dump1 + dump2
        logger.debug(data)
        obj_write.write(data)

    f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='停掉ogg程序', message=f.stdout.read())

def start_ogg_func():
    f = subprocess.Popen('echo start mgr|' + ogg_path, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='开启ogg程序', message=f.stdout.read())


def info_all_func():
    f = subprocess.Popen('echo info all|' + ogg_path, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='查看状态和相关日志', message=f.stdout.read())



stop_log = Button(root, text='关掉补充日志', command=stop_log_func, activeforeground='white', activebackground='red')
stop_log.grid(row=1, column=2)

stop_ogg = Button(root, text='停掉ogg程序', command=stop_ogg_func, activeforeground='white', activebackground='red')
stop_ogg.grid(row=2, column=2)

info_all_stop = Button(root, text='查看状态和相关日志', command=info_all_func, activeforeground='white', activebackground='red')
info_all_stop.grid(row=3, column=2)


start_log = Button(root, text='打开补充日志', command=start_log_func, activeforeground='white', activebackground='red')
start_log.grid(row=7, column=2)

dump_log = Button(root, text='跳过日志', command=dump_log_func, activeforeground='white', activebackground='red')
dump_log.grid(row=8, column=2)

start_ogg = Button(root, text='开启ogg程序', command=start_ogg_func, activeforeground='white', activebackground='red')
start_ogg.grid(row=9, column=2)

info_all_start = Button(root, text='查看状态和相关日志', command=info_all_func, activeforeground='white', activebackground='red')
info_all_start.grid(row=10, column=2)

quit = Button(root, text='退出', command=root.quit, activeforeground='white', activebackground='red')
quit.grid(row=12, column=2)

root.mainloop()



日志配置文件



###############################################
[loggers]
keys=root,wj

[logger_root]
level=DEBUG
handlers=hand01

[logger_wj]
handlers=hand02
qualname=wj
propagate=0

###############################################
[handlers]
keys=hand01,hand02

[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=formatter02
args=(sys.stdout,)

[handler_hand02]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=formatter02
args=('../log/ogg_gui.log','midnight',1,30)

###############################################
[formatters]
keys=formatter01,formatter02

[formatter_formatter01]
format=%(asctime)s - %(process)d - %(module)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_formatter02]
format=%(asctime)s - %(process)d - %(module)s - %(levelname)s - %(message)s
datefmt=



 

更新程序

python 调用exe执行 并传入参数_测试流程_07

python 调用exe执行 并传入参数_python_08

python 调用exe执行 并传入参数_ci_09

停止ogg的流程:关掉补充日志-->停掉ogg程序,并判断每步执行结果是否正常,返回结果。
开启ogg的流程:打开补充日志-->跳过日志-->开启ogg程序,并判断每步执行结果是否正常,返回结果。



#/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 取绝对路径
sys.path.append(BASE_DIR)  # 将目录加入系统环境变量,之后导入此目录下各模块不会提示找不到
from Tkinter import *
import tkMessageBox
# from tkinter import ttk
import subprocess
import logging
import logging.config
# from tkinter import messagebox

# 日志配置
LOGCONF_FILENAME = "../etc/logging.conf"
logging.config.fileConfig(LOGCONF_FILENAME)
logger = logging.getLogger('wj')

ogg_path = r"D:\ogg\ggsci.exe"
table_path = r"D:\ogg\dirdef\run1_defgen.def"


root = Tk()
root.geometry('800x600')
root.title('同步开启和关闭小工具')
root.resizable(width=True, height=True)  # 宽可变, 高可变,默认为True
Label(root, text='同步开启和关闭小工具', font=('Arial', 20)).grid(row=0)
Label(root, text="测试流程步骤").grid(row=1)
Label(root, text="").grid(row=2)
Label(root, text="").grid(row=3)
Label(root, text="").grid(row=4)
Label(root, text="恢复节点流程步骤").grid(row=5)
Label(root, text="").grid(row=6)
Label(root, text="").grid(row=7)
Label(root, text="").grid(row=8)
Label(root, text="状态检查步骤").grid(row=9)
Label(root, text="").grid(row=10)
Label(root, text="").grid(row=11)

def stop_log_func():
    """
    :return: 获取需要关闭的补充日志列表
    """
    stoplog = "../var/"
    filename = 'stop_log.txt'

    with open(table_path, "rb") as obj_read:
        table_list = []
        for line in obj_read.readlines():
            line = line.strip()
            if "table" in line:
                logger.debug(line)
                index = line.strip().find('table', 0)  # 获取table起始位
                table = line[index:]  # 获取table之后的数据
                table = table.replace('table', 'trandata', 2)  # 将table改成trandata
                table = 'delete ' + table
                table_list.append(table)
        logger.debug(table_list)

    with open(stoplog + filename, 'wb') as obj_write:
        delete_table = '\n'.join(table_list) + '\n'
        dblogin = 'DBLOGIN SOURCEDB run, USERID ogg, PASSWORD Ogg123\n'
        data = dblogin + delete_table
        logger.debug(data)
        obj_write.write(data)


def stop_ogg_func():
    """
    :return: 关闭ogg进程和补充日志
    """
    stopogg = "../var/"
    filename1 = 'stop_ogg.txt'
    filename2 = 'stop_log.txt'

    r = tkMessageBox.askokcancel('同步开启和关闭小工具', '确定清算完成,并挂好公告!\n关闭过程需要一段时间,请等待操作结果')
    # r = messagebox.askokcancel('Python Tkinter', '确定清算完成,并挂好公告!')
    if r:
        with open(stopogg + filename1, 'wb') as obj_write:
            stop1 = 'stop 829*\n'
            stop2 = 'kill mgr\n'
            status = 'info all\n'
            data = stop1 + stop2 + status
            logger.debug(data)
            obj_write.write(data)

        f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename1, shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stop_reslut = f.stdout.read()
        logger.debug(stop_reslut)
        for line in stop_reslut.split('\n'):
            if "RUNNING" in line or "ABENDED" in line:
                tkMessageBox.showinfo(title='停掉ogg程序', message="ogg进程停止失败,请检查日志")
                exit(0)

        stop_log_func()
        l = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename2, shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        log_reslut = l.stdout.read()
        logger.debug(log_reslut)
        for line in log_reslut.split('\n'):
            if "table" in line:
                logger.debug(line)
                if "Logging of supplemental log data is disabled" not in line:
                    tkMessageBox.showinfo(title='关掉补充日志', message="关掉补充日志失败,请检查日志")
                    exit(0)

        tkMessageBox.showinfo(title='停掉ogg程序', message="成功")
    else:
        tkMessageBox.showinfo(title='停掉ogg程序', message="操作已取消")


def start_log_func():
    """
    :return: 打开补充日志
    """
    startlog = "../var/"
    filename = 'start_log.txt'

    with open(table_path, "rb") as obj_read:
        table_list = []
        for line in obj_read.readlines():
            line = line.strip()
            if "table" in line:
                logger.debug(line)
                index = line.strip().find('table', 0)  # 获取table起始位
                table = line[index:]  # 获取table之后的数据
                table = table.replace('table', 'trandata', 2)  # 将table改成trandata
                table = 'add ' + table
                table_list.append(table)
        logger.debug(table_list)

    with open(startlog + filename, 'wb') as obj_write:
        delete_table = '\n'.join(table_list) + '\n'
        dblogin = 'DBLOGIN SOURCEDB run, USERID ogg, PASSWORD Ogg123\n'
        data = dblogin + delete_table
        logger.debug(data)
        obj_write.write(data)


def dump_log_func():
    """
    :return: 跳过日志
    """
    dumplog = "../var/"
    filename = 'dump_log.txt'

    with open(dumplog + filename, 'wb') as obj_write:
        dump1 = 'alter 829 begin now\n'
        dump2 = 'alter 829P1 begin now'
        data = dump1 + dump2
        logger.debug(data)
        obj_write.write(data)


def start_ogg_func():
    """
    :return: 开启ogg程序
    """
    filename1 = 'dump_log.txt'
    filename2 = 'start_log.txt'

    r = tkMessageBox.askokcancel('同步开启和关闭小工具', '确定复制建立完成,备库开启完整模式!\n关闭过程需要一段时间,请等待操作结果')
    if r:
        start_log_func()
        f = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename2, shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        log_reslut = f.stdout.read()
        logger.debug(log_reslut)
        for line in log_reslut.split('\n'):
            if "table" in line:
                logger.debug(line)
                if "Logging of supplemental log data is enabled" not in line:
                    tkMessageBox.showinfo(title='打开补充日志', message="打开补充日志失败,请检查日志")
                    exit(0)

        dump_log_func()
        d = subprocess.Popen(ogg_path + ' paramfile ' + BASE_DIR + '\\var\\' + filename1, shell=True,
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        dump_reslut = d.stdout.read()
        logger.debug(dump_reslut)
        for line in dump_reslut.split('\n'):
            if "EXTRACT" in line:
                logger.debug(line)
                if "EXTRACT altered" not in line:
                    tkMessageBox.showinfo(title='跳过日志', message="跳过日志失败,请检查日志")
                    exit(0)

        s = subprocess.Popen('echo start mgr|' + ogg_path, shell=True, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        logger.debug(s.stdout.read())
        time.sleep(5)
        i = subprocess.Popen('echo info all|' + ogg_path, shell=True, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        start_reslut = i.stdout.read().split('\n')
        for line in start_reslut:
            if "STOPPED" in line or "ABENDED" in line:
                tkMessageBox.showinfo(title='开启ogg程序', message="开启ogg进程失败,请检查日志")
                exit(0)

        tkMessageBox.showinfo(title='开启ogg程序', message="成功")
    else:
        tkMessageBox.showinfo(title='开启ogg程序', message="操作已取消")

def info_all_func():
    f = subprocess.Popen('echo info all|' + ogg_path, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    tkMessageBox.showinfo(title='查看状态和相关日志', message=f.stdout.read())


# buttontext = StringVar()
# buttontext.set('停掉ogg程序')
stop_ogg = Button(root, text='停掉ogg程序', command=stop_ogg_func, activeforeground='white', activebackground='red')
stop_ogg.grid(row=1, column=2)

start_ogg = Button(root, text='开启ogg程序', command=start_ogg_func, activeforeground='white', activebackground='red')
start_ogg.grid(row=5, column=2)

info_all_start = Button(root, text='查看状态和相关日志', command=info_all_func, activeforeground='white', activebackground='red')
info_all_start.grid(row=9, column=2)

quit = Button(root, text='退出', command=root.quit, activeforeground='white', activebackground='red')
quit.grid(row=12, column=2)

root.mainloop()