博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sw_tools_check.py
阅读量:6258 次
发布时间:2019-06-22

本文共 7985 字,大约阅读时间需要 26 分钟。

hot3.png

#!/usr/bin/env python#coding=utf-8import csvimport csvtodicfrom csvtodic import csvtodictlist,dictlisttocsvfrom optparse import OptionParserfrom optparse import OptionGroupimport loggingfrom pprint import pprintfrom pprint import pformatimport syslogimport subprocessimport signalimport timeimport selectimport commandsimport osimport sysdef subproc(cmd, timeout=3600,mute=True) :        """        subprogress to run command        """        rc = None        output = ''        #        #print('subproc : ' + cmd)        if not mute : print('timout : ' + str(timeout))        try :            #            p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)            pi, po, pe = p.stdin, p.stdout, p.stderr            while_begin = time.time()            while True :                to = 600                fs = select.select([p.stdout, p.stderr], [], [], to)                #if p.poll() : break                #print '.....',                if p.stdout in fs[0]:                    #print '---xxxifs0:',fs[0]                    tmp = p.stdout.readline()                    if tmp :                        output += tmp                        #print(tmp)                        #if not mute : print(tmp)                    else :                        #print '---end'                        #print time.time()                        while None == p.poll() : pass                        break                elif p.stderr in fs[0]:                    #print "!!!!!!!!!!!!!!!"                    tmp = p.stderr.readline()                    if tmp :                        output += tmp                        #plogger.error(tmp)                    else :                        #print 'end'                        #print time.time()                        while None == p.poll() : pass                        break                else:                    #print 'Timeout'                    #os.system('ps -f')                    s = os.popen('ps -f| grep -v grep |grep sleep').read()                    if len(s.strip()) :                        print('No output in sleep : ' + s)                        continue                    print('Timeout ' + str(to) + ' seconds without any output!')                    print(os.popen('ps -p ' + str(p.pid)).read())                    p.kill()                    #os.kill(p.pid, signal.SIGKILL)                    break                #timeout = timeout - (time.time() - while_begin)                # Check the total timeout                dur = time.time() - while_begin                if dur > timeout :                    print('The subprocess is timeout more than ' + str(timeout))                    break            # return            rc = p.poll()            # close all fds            p.stdin.close()            p.stdout.close()            p.stderr.close()            #print('return value : ' + str(rc))        except Exception, e :            print('Exception : ' + str(e))            #rc = False        return rc, output def sw_online(dic, online_flag ) :     if online_flag == 'online':       if dic["cmd_install_online"] == '':          print( '%s have no online install command' % (str(dic["description"])))       else:             cmd_install = dic["cmd_install_online"].splitlines()          #print ('%s installing latest version online !!' % (str(dic["description"])))          i = 0           for h in cmd_install:              install_code,install_output =  subproc(h)              if install_code != 0:                      print ('%s install failed:%s' % (str(dic["description"]),str (install_output)))                     i+=1          if i == 0 :                            print( '%s online install success!!' % (str(dic["description"])))def sw_offline(dic, online_flag, pk_path, cmdlist ) :     if online_flag == 'offline':       if dic["cmd_install_offline"] == '':          print( '%s have no offline install command' % (str(dic["description"])))          cmdlist.append(dic["description"])        elif pk_path == '':          print( '%s have no offline packet dir! ' % (str(dic["description"])))       else:               #pk_path = 'cd ' + pk_path           # print'pk_path is: ', pk_path            if 'all' not in pk_path :                pk_path = pk_path+'/'+'all'            os.chdir(pk_path)            dir = os.getcwd()            #print 'dir1 is :',dir            if dir != pk_path :               print( '%s cd  packet dir error : %s! ' % ((str(dic["description"]), str(pk_path))))            cmd_install = dic["cmd_install_offline"].splitlines()           # print ('%s installing offline !!' % (str(dic["description"])))            j = 0             #s = 'already installed'            str_cd = 'cd'            for h in cmd_install:                  new_str = str(h)                  if str_cd in new_str:                   # print 'dir is :',dir                      new_path = dir + '/'+new_str[3:]                     #print "new_path is:", new_path                         os.chdir(new_path)                      new_dir = os.getcwd()                    #print 'new_dir is :',new_dir                    else:                    install_code,install_output =  subproc(h)                    #print 'install code is :' ,install_code                    #stri = str (install_output)                    if install_code != 0:                        #if s not in stri :                          print ('%s install failed:%s' % (str(dic["description"]),str (install_output)))                          j +=1            if j == 0 :               print( '%s offline install success!!' % (str(dic["description"])))               os.chdir(pk_path)               dir = os.getcwd()               #print 'dir2 is :',dirdef main() :    """    """    lie = []    sys_argv = len(sys.argv)    #print 'sys_argv:',sys_argv    if sys_argv==1:       path = 'software_checking.csv'       online = None       pk_path = None       check_flag = 'check'            print 'checking software ,wait a moment!'           elif sys.argv[1] =='--with-version':       path = 'software_checking.csv'       online = None       pk_path = None       check_flag ='version'    elif sys.argv[1] =='--force-install-online':       path = 'software_checking.csv'       online = 'online'       pk_path = None       check_flag ='version'    elif sys.argv[1] =='--force-install-offline':       path = 'software_checking.csv'       online = 'offline'       pk_path = os.getcwd()       check_flag ='version'    elif sys.argv[1] =='--install-online':       path = 'software_checking.csv'       online = None       pk_path = None       check_flag ='not_version'    elif sys.argv[1] =='--install-offline':       path = 'software_checking.csv'       online = None       pk_path = None       #pk_path = os.getcwd()       check_flag ='not_version'    elif sys.argv[1] !='':                print 'need add parameter!\none of them: \n--with-version:check tools verison \n--force-install-online: not check tools force install online\n--force-install-offline:not check tools force install offline\n--install-online: check tools ,if not exist, install online\n--install-offline:check tools ,if not exist, install offline\n if have no parameter , check tools not exits'       return 0    csvtodictlist(lie,path)    #READ_ONLY = (select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR)    #p=select.poll()    #p.register(li,READ_ONLY)    #result = p.poll()    #if result == 0:     #  print'...',            #copy file to need dir cp -r i386/* /var/cache/yum/i386/    copy_cmd ='cp -r all/i386/* /var/cache/yum/i386/'    copy_code, copy_output =  subproc(copy_cmd)    if copy_code !=0 :       print 'copy dir error!!!:',copy_output       return 0    m = 0    error =0    warnning = 0    disable  = 0    errorlist = []    warnninglist = []    offcmdlist = []    disablelist = []    rtype = []    otype = []    dtype = []    while m

转载于:https://my.oschina.net/xxjbs001/blog/277277

你可能感兴趣的文章
oracle 按照中文排序
查看>>
免费实用的看图工具 Xee
查看>>
我的友情链接
查看>>
Linux 下sendmail 的加密与认证
查看>>
自我成长及沟通技巧
查看>>
【Practical API Design学习笔记】谨慎使用第三方API
查看>>
体验 Scala 2.12 支持的 Java 8 风格(SAM) Lambda
查看>>
Nicholas C. Zakas:我得到的最佳职业生涯建议
查看>>
openfire学习总结之插件
查看>>
UDT协议实现分析——UDT数据收发的可靠性保障
查看>>
谷歌将降低侵权网站的排名
查看>>
知识图谱的应用
查看>>
springboot 系列教程一:基础项目搭建
查看>>
Rxjava2 源码解析 (三)
查看>>
ios cocoapods 0.32.1 升级提示不行 要安装cocoapods-core
查看>>
匿名函数和闭包
查看>>
Overfitting
查看>>
Open vSwitch 简介
查看>>
Composer
查看>>
Go Little Book - 关于本书
查看>>