Package pyhai :: Package plugins :: Package linux
[hide private]
[frames] | no frames]

Source Code for Package pyhai.plugins.linux

 1  """ 
 2  @copyright: 2011 Mark LaPerriere 
 3   
 4  @license: 
 5      Licensed under the Apache License, Version 2.0 (the "License"); 
 6      you may not use this file except in compliance with the License. 
 7      You may obtain a copy of the License at 
 8   
 9      U{http://www.apache.org/licenses/LICENSE-2.0} 
10   
11      Unless required by applicable law or agreed to in writing, software 
12      distributed under the License is distributed on an "AS IS" BASIS, 
13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14      See the License for the specific language governing permissions and 
15      limitations under the License. 
16   
17  @summary: 
18      A installed packages auditing plugin that should work with CentOS 
19   
20  @author: Mark LaPerriere 
21  @contact: pyhai@mindmind.com 
22  @organization: Mind Squared Design / www.mindmind.com 
23  """ 
24  import abc 
25  import subprocess.Popen as Popen 
26  import subprocess.PIPE as PIPE 
27  import shlex 
28  import logging 
29   
30  # set some default logging behavior 
31  _logger = logging.getLogger(__name__) 
32  _logger.addHandler(logging.NullHandler()) 
33 34 -class PackageManagerBase(object):
35 """ 36 Base class for all the different linux package managers 37 """ 38 __metaclass__ = abc.ABCMeta 39 40 @abc.abstractmethod
41 - def list_packages(self):
42 """ 43 @return: A dictionary of package name keys and package version values 44 @rtype: C{dict(str: str)} 45 """ 46 pass
47
48 -class PackageManagerDpkg(PackageManagerBase):
49 """ 50 Gets listing of packages and versions using dpkg 51 """
52 - def list_packages(self):
53 software = {} 54 55 pkg_list_cmd = '/usr/bin/dpkg -l' 56 pkg_list_out = Popen(shlex.split(pkg_list_cmd), stdout=PIPE).stdout.read().splitlines() 57 _logger.debug('pkg_list command results [%s]: %s', pkg_list_cmd, pkg_list_out) 58 59 for line in pkg_list_out: 60 cols = line.strip().split() 61 if cols[0] == 'ii' and len(cols) >= 3: 62 software[cols[1]] = cols[2] 63 64 return software
65
66 -class PackageManagerRpm(PackageManagerBase):
67 """ 68 Gets listing of packages and versions using rpm 69 """
70 - def list_packages(self):
71 software = {} 72 73 pkg_list_cmd = '/bin/rpm -qa --queryformat="%{NAME} %{VERSION}\\n"' 74 pkg_list_out = Popen(shlex.split(pkg_list_cmd), stdout=PIPE).stdout.read().splitlines() 75 _logger.debug('pkg_list command results [%s]: %s', pkg_list_cmd, pkg_list_out) 76 77 for line in pkg_list_out: 78 cols = line.strip().split() 79 if len(cols) >= 2: 80 software[cols[0]] = cols[1] 81 82 return software
83