前几天仿照perl写了个练手的脚本。
构造函数定义了后续gateway指令用到的变量
get_pid方法直接用tasklist执行cmd命令来获取任意一个pid作为导入接口
import方法:先导入,导入完用gateway的ERR命令获取返回值进行导入结果检查
不打包的执行方法:
打包成exe后直接将tgz的打开方式改成该exe即可。
#!/bin/env python
# -*- encoding : utf-8 -*-
'''
Class: import_tgz.py
Programmer: tangzhiying
Date: 2021-12-07
Purpose: balabalabala
'''
import subprocess
import os
import sys
import tkinter.messagebox
import re
class importJob(object):
def __init__(self, job_path):
self.gateway = os.getenv('GENESIS_EDIR') + '/misc/gateway.exe'
self.hostname = os.getenv('computername')
self.import_name = self.get_job_name(job_path)
self.job_path = os.path.realpath(job_path)
def get_job_name(self, path):
match_str = re.match( r'(.+).tgz', path)
if match_str:
name = match_str.group(1)
filename = os.path.split(name)[1]
return filename
def get_pid(self):
cmd = "tasklist | grep get.exe"
get_info = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in get_info.stdout.readlines():
pid_info = line.decode("utf-8").strip()
pid_list = pid_info.split()
return pid_list[1]
tkinter.messagebox.showerror(title='Error', message='请开启genesis!')
def import_job(self):
pid = self.get_pid()
pid_str = '%' + pid + '@' + self.hostname + '.' + self.hostname
com_str = '"COM import_job,db=genesis,path={_path},name={_job_name},analyze_surfaces=no,verify_tgz=no"'.format(_path = self.job_path, _job_name = self.import_name)
cmd = self.gateway + ' ' + pid_str + ' ' + com_str
res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out, err = res.communicate()
res_flag = str(out.splitlines()[0], encoding = "utf8")
if int(res_flag) != 0:
com_str = '"ERR {_code}"'.format(_code = res_flag)
cmd = self.gateway + ' ' + pid_str + ' ' + com_str + '.'
code_txt = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out, err = code_txt.communicate()
txt = str(out.splitlines()[0], encoding = "utf8")
tkinter.messagebox.showerror(title='Error', message=str(txt))
else:
tkinter.messagebox.showerror(title='Error', message='完成!')
def main():
from import_tgz import importJob
job_path = sys.argv[1]
my_job = importJob(job_path)
my_job.import_job()
if __name__ == '__main__':
main()



