1999-10-01 15:15:25 +08:00
#############################################################################
# File : rpmlint.py
# Package : rpmlint
# Author : Frederic Lepied
# Created on : Mon Sep 27 19:20:18 1999
# Version : $Id$
# Purpose : main entry point: process options, load the checks and run
# the checks.
#############################################################################
import sys
import AbstractCheck
import imp
import getopt
import Pkg
1999-10-16 19:14:24 +08:00
import Config
import os
2003-01-17 06:30:27 +08:00
import sys
2001-06-07 00:32:31 +08:00
import stat
2001-07-16 01:07:24 +08:00
import rpm
1999-11-16 02:28:50 +08:00
from Filter import *
1999-10-01 15:15:25 +08:00
2002-04-25 04:04:25 +08:00
version = ' @VERSION@ '
policy = None
1999-10-01 15:15:25 +08:00
# Print usage information
def usage ( name ) :
2001-07-16 01:07:24 +08:00
print ' usage: ' , name , ' [<options>] <rpm files> '
print ' options in: '
2003-01-17 06:30:27 +08:00
print ' \t [-i|--info] \n \t [-c|--check <check>] \n \t [-a|--all] \n \t [-C|--checkdir <checkdir>] \n \t [-h|--help] \n \t [-v|--verbose] \n \t [-E|--extractdir <dir>] \n \t [-p|--profile] \n \t [-V|--version] \n \t [-n|--noexception] \n \t [-P|--policy <policy>] \n \t [-f|--file <config file to use instead of ~/.rpmlintrc>] '
1999-10-01 15:15:25 +08:00
# Print version information
def printVersion ( ) :
2003-08-05 17:59:20 +08:00
print ' rpmlint version ' , version , ' Copyright (C) 1999-2003 Frederic Lepied, MandrakeSoft '
1999-10-01 15:15:25 +08:00
# Load a python module from its file name
def loadCheck ( name ) :
1999-10-16 19:14:24 +08:00
( file , pathname , description ) = imp . find_module ( name , Config . checkDirs ( ) )
1999-10-01 15:15:25 +08:00
imp . load_module ( name , file , pathname , description )
1999-10-16 19:14:24 +08:00
# Load a file
def loadFile ( name ) :
file = os . fdopen ( os . open ( os . path . expanduser ( name ) , os . O_RDONLY ) )
2001-07-16 01:07:24 +08:00
imp . load_source ( ' ' , name , file )
2002-02-10 08:47:28 +08:00
file . close ( )
1999-10-16 19:14:24 +08:00
1999-10-01 15:15:25 +08:00
#############################################################################
# main program
#############################################################################
2001-07-16 01:07:24 +08:00
def main ( ) :
# Load all the tests
for c in Config . allChecks ( ) :
loadCheck ( c )
2003-04-23 21:32:46 +08:00
pkg = None
2001-07-16 01:07:24 +08:00
try :
# Loop over all file names given in arguments
for f in args :
try :
try :
st = os . stat ( f )
if not stat . S_ISREG ( st [ stat . ST_MODE ] ) :
raise OSError
pkg = Pkg . Pkg ( f , extract_dir )
except OSError :
pkg = Pkg . InstalledPkg ( f )
except :
sys . stderr . write ( ' Error while reading ' + f + ' \n ' )
pkg = None
continue
2003-04-23 21:32:46 +08:00
runChecks ( pkg )
2001-07-16 01:07:24 +08:00
# if requested, scan all the installed packages
if all :
2003-04-23 21:32:46 +08:00
if Pkg . v42 :
ts = rpm . TransactionSet ( ' / ' )
for item in ts . IDTXload ( ) :
pkg = Pkg . InstalledPkg ( item [ 1 ] [ rpm . RPMTAG_NAME ] , item [ 1 ] )
runChecks ( pkg )
else :
db = rpm . opendb ( )
idx = db . firstkey ( )
while idx :
pkg = Pkg . InstalledPkg ( db [ idx ] [ rpm . RPMTAG_NAME ] , db [ idx ] )
runChecks ( pkg )
idx = db . nextkey ( idx )
del db
2001-07-16 01:07:24 +08:00
2003-04-23 21:32:46 +08:00
finally :
pkg and pkg . cleanup ( )
2001-07-16 01:07:24 +08:00
2003-04-23 21:32:46 +08:00
def runChecks ( pkg ) :
2001-07-16 01:07:24 +08:00
2003-04-23 21:32:46 +08:00
if verbose :
printInfo ( pkg , ' checking ' )
for c in AbstractCheck . AbstractCheck . checks :
c . check ( pkg )
pkg . cleanup ( )
1999-10-01 15:15:25 +08:00
2001-07-16 01:07:24 +08:00
#############################################################################
#
#############################################################################
1999-10-01 15:15:25 +08:00
sys . argv [ 0 ] = os . path . basename ( sys . argv [ 0 ] )
# parse options
try :
2002-04-25 04:04:25 +08:00
( opt , args ) = getopt . getopt ( sys . argv [ 1 : ] ,
2003-01-17 06:30:27 +08:00
' ic:C:hVvp:anP:E:f: ' ,
2002-04-25 04:04:25 +08:00
[ ' info ' ,
' check= ' ,
' checkdir= ' ,
' help ' ,
' version ' ,
' verbose ' ,
' profile ' ,
' all ' ,
' noexception ' ,
' policy= '
2003-01-17 06:30:27 +08:00
' extractdir= ' ,
' file= ' ,
] )
1999-10-01 15:15:25 +08:00
except getopt . error :
2001-07-16 01:07:24 +08:00
print ' bad option '
1999-10-01 15:15:25 +08:00
usage ( sys . argv [ 0 ] )
sys . exit ( 1 )
# process options
2001-07-16 01:07:24 +08:00
checkdir = ' /usr/share/rpmlint '
1999-10-01 15:15:25 +08:00
verbose = 0
2001-07-16 01:07:24 +08:00
extract_dir = Config . getOption ( ' ExtractDir ' , ' /tmp ' )
prof = 0
all = 0
2003-01-17 06:30:27 +08:00
conf_file = ' ~/.rpmlintrc '
1999-10-01 15:15:25 +08:00
1999-10-16 19:14:24 +08:00
# load global config file
try :
2001-07-16 01:07:24 +08:00
loadFile ( ' /etc/rpmlint/config ' )
1999-10-16 19:14:24 +08:00
except OSError :
pass
# process command line options
1999-10-01 15:15:25 +08:00
for o in opt :
2001-07-16 01:07:24 +08:00
if o [ 0 ] == ' -c ' or o [ 0 ] == ' --check ' :
1999-10-16 19:14:24 +08:00
Config . addCheck ( o [ 1 ] )
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -i ' or o [ 0 ] == ' --info ' :
2001-05-26 06:19:05 +08:00
Config . info = 1
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -h ' or o [ 0 ] == ' --help ' :
1999-10-01 15:15:25 +08:00
usage ( sys . argv [ 0 ] )
sys . exit ( 0 )
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -C ' or o [ 0 ] == ' --checkdir ' :
1999-10-16 19:14:24 +08:00
Config . addCheckDir ( o [ 1 ] )
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -v ' or o [ 0 ] == ' --verbose ' :
1999-10-01 15:15:25 +08:00
verbose = 1
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -V ' or o [ 0 ] == ' --version ' :
1999-10-01 15:15:25 +08:00
printVersion ( )
sys . exit ( 0 )
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -E ' or o [ 0 ] == ' --extractdir ' :
1999-10-01 15:15:25 +08:00
extract_dir = o [ 1 ]
2000-08-16 17:25:23 +08:00
Config . setOption ( ' ExtractDir ' , extract_dir )
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -p ' or o [ 0 ] == ' --profile ' :
prof = o [ 1 ]
2001-09-29 00:51:49 +08:00
elif o [ 0 ] == ' -n ' or o [ 0 ] == ' --noexception ' :
Config . no_exception = 1
2002-04-25 04:04:25 +08:00
elif o [ 0 ] == ' -P ' or o [ 0 ] == ' --policy ' :
policy = o [ 1 ]
2001-07-16 01:07:24 +08:00
elif o [ 0 ] == ' -a ' or o [ 0 ] == ' --all ' :
all = 1
2003-01-17 06:30:27 +08:00
elif o [ 0 ] == ' -f ' or o [ 0 ] == ' --file ' :
conf_file = o [ 1 ]
1999-10-01 15:15:25 +08:00
else :
2001-07-16 01:07:24 +08:00
print ' unknown option ' , o
1999-10-01 15:15:25 +08:00
2003-01-17 06:30:27 +08:00
# load user config file
try :
loadFile ( conf_file )
except OSError :
pass
except :
sys . stderr . write ( ' Error loading %s , skipping \n ' % conf_file )
2002-04-25 04:04:25 +08:00
policy and Config . load_policy ( policy )
1999-10-01 15:15:25 +08:00
# if no argument print usage
2001-07-16 01:07:24 +08:00
if args == [ ] and not all :
1999-10-01 15:15:25 +08:00
usage ( sys . argv [ 0 ] )
sys . exit ( 0 )
2001-07-16 01:07:24 +08:00
if prof :
import profile
import pstats
profile . run ( ' main() ' , prof )
p = pstats . Stats ( prof )
p . print_stats ( ' time ' ) . print_stats ( 20 )
else :
main ( )
1999-10-01 15:15:25 +08:00
# rpmlint.py ends here