码迷,mamicode.com
首页 > 其他好文 > 详细

pyton 获取系统安装软件的基本信息

时间:2020-01-23 11:16:11      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:code   RKE   for   val   代码   self   pre   owa   run   

代码如下:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import re
import commands
import datetime
import threading
import sys
import Queue
from concurrent.futures import ThreadPoolExecutor
import time

from myDb import MyDb


class software(object):

    def __init__(self):
        super(software, self).__init__()
        print "init"
        self.name = ‘‘
        self.release = ‘‘
        self.version = ‘‘
        self.vendor = ‘‘
        self.buildDate = ‘‘
        self.sourceRPM = ‘‘
        self.installDate = ‘‘
        self.group = ‘‘
        self.size = ‘‘
        self.signature = ‘‘
        self.summary = ‘‘
        self.description = ‘‘

        self.soft_sum =0
        self.thread_max_num=10

        self.db = MyDb()
        self.db.open(./citosis.db)
        self.db.delete("delete from sys_software")
        self.insert_db_queue = Queue.Queue()
    def start(self):
        start = time.time()
        cmd = which rpm
        ret_str = commands.getoutput(cmd)
        rg = re.compile((\\/)+(usr)+(\\/)+(.*?)+(\\/)+(rpm), re.IGNORECASE | re.DOTALL)
        # ret_str ="/usr/22222bin/rpm"
        m = rg.search(ret_str)
        if m:
            self.get_soft_rpm()
        else:
            self.get_soft_dpkg()
        end = time.time()
        print(Running time: %s Seconds % (end - start))


    def get_soft_rpm(self):
        print "get_soft_rpm"

        name_str = commands.getoutput("rpm -qa --qf ‘%{name}\n‘")
        soft_name_list = name_str.split("\n")

        self.soft_sum = len(soft_name_list);
        print self.soft_sum
        print type(soft_name_list)
        insert_db_thd = threading.Thread(target=self.insert_db_thread,name=insert_db_thread)
        insert_db_thd.setDaemon(True)
        insert_db_thd.start()

        with ThreadPoolExecutor(max_workers=self.thread_max_num) as pool:
            results = pool.map(self.get_rpm_data, (soft_name_list[0:self.soft_sum/self.thread_max_num],
                                                   soft_name_list[self.soft_sum/self.thread_max_num:self.soft_sum/self.thread_max_num * 2],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 2:self.soft_sum/self.thread_max_num * 3],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 3:self.soft_sum/self.thread_max_num * 4],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 4:self.soft_sum/self.thread_max_num * 5],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 5:self.soft_sum/self.thread_max_num * 6],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 6:self.soft_sum/self.thread_max_num * 7],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 7:self.soft_sum/self.thread_max_num * 8],
                                                   soft_name_list[self.soft_sum/self.thread_max_num * 8:self.soft_sum/self.thread_max_num * 9],
                                                   soft_name_list[self.soft_sum / self.thread_max_num * 9:]
                                                   ))
            print(--------------)
            for r in results:
                print(r)
    def insert_db_thread(self):
        for num in range(1,self.soft_sum,1):
            # print self.insert_db_queue.get()
            self.db.insert(self.insert_db_queue.get())


    def get_rpm_data(self,soft_name_part):
        try:
            for soft_name in soft_name_part:
                cmd = "rpm -qi {} ".format(soft_name)
                soft_info = commands.getoutput(cmd)

                if re.findall("Name\s+\:\s+(.+)",soft_info):
                    self.name=re.findall("Name\s+\:\s+(.+)",soft_info)[0]
                else:
                    self.name = ‘‘
                if re.findall("Release\s+\:\s+(.+)",soft_info):
                    self.release=re.findall("Release\s+\:\s+(.+)",soft_info)[0]
                else:
                    self.release = ‘‘
                if re.findall("Vendor\s+\:\s+(.+)",soft_info):
                    self.version=re.findall("Version\s+\:\s+(.+)",soft_info)[0]
                else:
                    self.version = ‘‘
                if re.findall("Vendor\s+\:\s+(.+)",soft_info):
                    self.vendor=re.findall("Vendor\s+\:\s+(.+)",soft_info)[0]
                else:
                    self.vendor = ‘‘
                if re.findall("Build Date\s+\:\s+(.+)",soft_info):
                    self.buildDate =re.findall("Build Date\s+\:\s+(.+)",soft_info)[0]
                else:
                    self.buildDate  = ‘‘
                if re.findall("Install Date\:\s+(.+)", soft_info):
                    self.sourceRPM =re.findall("Install Date\:\s+(.+)", soft_info)[0]
                else:
                    self.sourceRPM  = ‘‘
                if re.findall("Install Date\:\s+(.+)", soft_info):
                    self.installDate=re.findall("Install Date\:\s+(.+)", soft_info)[0]
                else:
                    self.installDate = ‘‘
                if re.findall("Group\s+\:\s+(.+)", soft_info):
                    self.group=re.findall("Group\s+\:\s+(.+)", soft_info)[0]
                else:
                    self.group = ‘‘
                if re.findall("Size\s+\:\s+(.+)", soft_info):
                    self.size=re.findall("Size\s+\:\s+(.+)", soft_info)[0]
                else:
                    self.size = ‘‘
                if re.findall("Signature\s+\:\s+(.+)", soft_info):
                    self.signature=re.findall("Signature\s+\:\s+(.+)", soft_info)[0]
                else:
                    self.signature = ‘‘
                if re.findall("Summary\s+\:\s+(.+)", soft_info):
                    self.summary=re.findall("Summary\s+\:\s+(.+)", soft_info)[0]
                else:
                    self.summary = ‘‘
                if re.findall("Description\s+\:\s+(.+)", soft_info):
                    self.description=re.findall("Description\s+\:\s+(.+)", soft_info)[0]
                else:
                    self.description = ‘‘

                cmd = "insert into sys_software (sname,vendor,version,release,source,sgroup,size,signature,build_date,inst_date,summary,desc)"                       "values(‘{sname}‘, ‘{vendor}‘, ‘{version}‘, ‘{release}‘, ‘{source}‘, ‘{sgroup}‘, ‘{size}‘, ‘{signature}‘,"                       "‘{build_date}‘, ‘{inst_date}‘, ‘{summary}‘, ‘{desc}‘)".format(sname=self.name,
                                                                             vendor=self.vendor,
                                                                             version=self.version,
                                                                             release=self.release,
                                                                             source=self.buildDate,
                                                                             sgroup=self.group,
                                                                             size=self.size,
                                                                             signature=self.signature,
                                                                             build_date=self.buildDate,
                                                                             inst_date=self.installDate,
                                                                             summary=self.summary,
                                                                             desc=self.description)


                # self.db.insert(cmd)
                self.insert_db_queue.put_nowait(cmd)
        except Exception as e:
            print e
    def get_soft_dpkg(self):
        print "get_soft_dpkg"



if __name__ == __main__:
    print("main")
    s = software()
    s.start()

pyton 获取系统安装软件的基本信息

标签:code   RKE   for   val   代码   self   pre   owa   run   

原文地址:https://www.cnblogs.com/wanghuixi/p/12230258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!