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

Posted wanghuixi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyton 获取系统安装软件的基本信息相关的知识,希望对你有一定的参考价值。

代码如下:

#!/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}
‘")
        soft_name_list = name_str.split("
")

        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("Names+:s+(.+)",soft_info):
                    self.name=re.findall("Names+:s+(.+)",soft_info)[0]
                else:
                    self.name = ‘‘
                if re.findall("Releases+:s+(.+)",soft_info):
                    self.release=re.findall("Releases+:s+(.+)",soft_info)[0]
                else:
                    self.release = ‘‘
                if re.findall("Vendors+:s+(.+)",soft_info):
                    self.version=re.findall("Versions+:s+(.+)",soft_info)[0]
                else:
                    self.version = ‘‘
                if re.findall("Vendors+:s+(.+)",soft_info):
                    self.vendor=re.findall("Vendors+:s+(.+)",soft_info)[0]
                else:
                    self.vendor = ‘‘
                if re.findall("Build Dates+:s+(.+)",soft_info):
                    self.buildDate =re.findall("Build Dates+: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("Groups+:s+(.+)", soft_info):
                    self.group=re.findall("Groups+:s+(.+)", soft_info)[0]
                else:
                    self.group = ‘‘
                if re.findall("Sizes+:s+(.+)", soft_info):
                    self.size=re.findall("Sizes+:s+(.+)", soft_info)[0]
                else:
                    self.size = ‘‘
                if re.findall("Signatures+:s+(.+)", soft_info):
                    self.signature=re.findall("Signatures+:s+(.+)", soft_info)[0]
                else:
                    self.signature = ‘‘
                if re.findall("Summarys+:s+(.+)", soft_info):
                    self.summary=re.findall("Summarys+:s+(.+)", soft_info)[0]
                else:
                    self.summary = ‘‘
                if re.findall("Descriptions+:s+(.+)", soft_info):
                    self.description=re.findall("Descriptions+: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 获取系统安装软件的基本信息的主要内容,如果未能解决你的问题,请参考以下文章

怎么在WINDOWS系统中安装Python

python BrickstorOS片段用于获取各种文件系统信息。

linux系统基本信息

Python学习记录——Ubuntu基本配置快捷键和系统启停命令行

为啥华为系统恢复eRecovery总是说获取安装包信息失败?

电脑管家(像qq,360管家)是怎么获取到系统中安装的软件列表和补丁列表(漏洞,已安装,未安装)用C#如何实现