Better for tests and api usability.
from collect import prepare_search_dirs, parse_revdep_config, collect_libraries_from_dir, collect_binaries_from_dir
from assign import assign_packages
from cache import save_cache
-from settings import SETTINGS
-
-def prepare_checks(files_to_check, libraries, bits):
+def prepare_checks(files_to_check, libraries, bits, cmd_max_args):
''' Calls scanelf for all files_to_check, then returns found libraries and dependencies
'''
dependencies = [] # list of lists of files (from file_to_check) that uses
# library (for dependencies[id] and libs[id] => id==id)
- for line in scan(['-M', str(bits), '-nBF', '%F %n'], files_to_check, SETTINGS['CMD_MAX_ARGS']):
+ for line in scan(['-M', str(bits), '-nBF', '%F %n'], files_to_check, cmd_max_args):
#call_program(['scanelf', '-M', str(bits), '-nBF', '%F %n',]+files_to_check).strip().split('\n'):
r = line.strip().split(' ')
if len(r) < 2: # no dependencies?
return broken_pathes
-def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=None, binaries=None, _libs_to_check=set()):
+def analyse(settings=None, logger=None, libraries=None, la_libraries=None,
+ libraries_links=None, binaries=None, _libs_to_check=set()):
"""Main program body. It will collect all info and determine the
pkgs needing rebuilding.
#TODO: add partial cache (for ex. only libraries) when found for some reason
logger.warn(green(' * ') + bold('Collecting system binaries and libraries'))
- bin_dirs, lib_dirs = prepare_search_dirs(logger)
+ bin_dirs, lib_dirs = prepare_search_dirs(logger, settings)
- masked_dirs, masked_files, ld = parse_revdep_config()
+ masked_dirs, masked_files, ld = parse_revdep_config(settings['REVDEP_CONFDIR'])
lib_dirs = lib_dirs.union(ld)
bin_dirs = bin_dirs.union(ld)
masked_dirs = masked_dirs.union(set(['/lib/modules', '/lib32/modules', '/lib64/modules',]))
libraries, la_libraries, libraries_links, symlink_pairs = collect_libraries_from_dir(lib_dirs, masked_dirs, logger)
binaries = collect_binaries_from_dir(bin_dirs, masked_dirs, logger)
- if SETTINGS['USE_TMP_FILES']:
+ if settings['USE_TMP_FILES']:
save_cache(to_save={'libraries':libraries, 'la_libraries':la_libraries, 'libraries_links':libraries_links, 'binaries':binaries})
for av_bits in glob.glob('/lib[0-9]*') or ('/lib32',):
bits = int(av_bits[4:])
- _libraries = scan(['-M', str(bits), '-BF', '%F'], libraries+libraries_links, SETTINGS['CMD_MAX_ARGS'])
+ _libraries = scan(['-M', str(bits), '-BF', '%F'], libraries+libraries_links, settings['CMD_MAX_ARGS'])
#call_program(['scanelf', '-M', str(bits), '-BF', '%F',] + libraries+libraries_links).strip().split('\n')
- found_libs, dependencies = prepare_checks(libs_and_bins, _libraries, bits)
+ found_libs, dependencies = prepare_checks(libs_and_bins, _libraries, bits, settings['CMD_MAX_ARGS'])
broken = find_broken(found_libs, _libraries, _libs_to_check)
broken_la = extract_dependencies_from_la(la_libraries, _libraries, _libs_to_check, logger)
logger.warn(green(' * ') + bold('Assign files to packages'))
- return assign_packages(broken_pathes, logger)
+ return assign_packages(broken_pathes, logger, settings)
import os
import re
-import logging
import portage
from portage import portdb
from portage.output import bold, red, blue, yellow, green, nocolor
-from settings import SETTINGS
-def assign_packages(broken, logger=logging):
+def assign_packages(broken, logger, settings):
''' Finds and returns packages that owns files placed in broken.
Broken is list of files
'''
assigned = set()
- for group in os.listdir(SETTINGS['PKG_DIR']):
- for pkg in os.listdir(SETTINGS['PKG_DIR'] + group):
- f = SETTINGS['PKG_DIR'] + group + '/' + pkg + '/CONTENTS'
+ for group in os.listdir(settings['PKG_DIR']):
+ for pkg in os.listdir(settings['PKG_DIR'] + group):
+ f = settings['PKG_DIR'] + group + '/' + pkg + '/CONTENTS'
if os.path.exists(f):
try:
with open(f, 'r') as cnt:
import time
import logging
from portage.output import red
-from settings import SETTINGS
+from settings import DEFAULTS
-def read_cache(temp_path=SETTINGS['DEFAULT_TMP_DIR']):
+def read_cache(temp_path=DEFAULTS['DEFAULT_TMP_DIR']):
''' Reads cache information needed by analyse function.
This function does not checks if files exists nor timestamps,
check_temp_files should be called first
return (ret['libraries'], ret['la_libraries'], ret['libraries_links'], ret['binaries'])
-def save_cache(logger=logging, to_save={}, temp_path=SETTINGS['DEFAULT_TMP_DIR']):
+def save_cache(logger=logging, to_save={}, temp_path=DEFAULTS['DEFAULT_TMP_DIR']):
''' Tries to store caching information.
@param logger
@param to_save have to be dict with keys: libraries, la_libraries, libraries_links and binaries
-def check_temp_files(temp_path=SETTINGS['DEFAULT_TMP_DIR'], max_delay=3600):
+def check_temp_files(temp_path=DEFAULTS['DEFAULT_TMP_DIR'], max_delay=3600):
''' Checks if temporary files from previous run are still available
and if they aren't too old
@param temp_path is directory, where temporary files should be found
import logging
import portage
from portage.output import bold, red, blue, yellow, green, nocolor
-from settings import SETTINGS
-def parse_conf(conf_file=SETTINGS['DEFAULT_LD_FILE'], visited=None, logger=logging):
+def parse_conf(conf_file, visited=None, logger=None):
''' Parses supplied conf_file for libraries pathes.
conf_file is file or files to parse
visited is set of files already parsed
return lib_dirs
-def prepare_search_dirs(logger=logging):
+def prepare_search_dirs(logger, settings):
''' Lookup for search dirs. Returns tuple with two lists,
(list_of_bin_dirs, list_of_lib_dirs)
'''
lib_dirs = set(['/lib', '/usr/lib', ])
#try:
- with open(os.path.join(portage.root, SETTINGS['DEFAULT_ENV_FILE']), 'r') as f:
+ with open(os.path.join(portage.root, settings['DEFAULT_ENV_FILE']), 'r') as f:
for line in f.readlines():
line = line.strip()
m = re.match("^export (ROOT)?PATH='([^']+)'", line)
#except EnvironmentError:
#logger.debug(yellow('Could not open file %s' % f))
- lib_dirs = parse_conf(logger=logger)
+ lib_dirs = parse_conf(settings['DEFAULT_LD_FILE'], logger=logger)
return (bin_dirs, lib_dirs)
-def parse_revdep_config():
+def parse_revdep_config(revdep_confdir):
''' Parses all files under /etc/revdep-rebuild/ and returns
tuple of: (masked_dirs, masked_files, search_dirs)'''
masked_files = set()
#TODO: remove hard-coded path
- for f in os.listdir(SETTINGS['REVDEP_CONFDIR']):
+ for f in os.listdir(revdep_confdir):
for line in open(os.path.join('/etc/revdep-rebuild', f)):
line = line.strip()
if not line.startswith('#'): #first check for comment, we do not want to regex all lines
return (masked_dirs, masked_files, search_dirs)
-def collect_libraries_from_dir(dirs, mask, logger=logging):
+def collect_libraries_from_dir(dirs, mask, logger):
''' Collects all libraries from specified list of directories.
mask is list of pathes, that are ommited in scanning, can be eighter single file or entire directory
Returns tuple composed of: list of libraries, list of symlinks, and toupe with pair
#!/usr/bin/python
import os
+import sys
import portage
-SETTINGS = {
+DEFAULTS = {
'DEFAULT_LD_FILE': os.path.join(portage.root, 'etc/ld.so.conf'),
'DEFAULT_ENV_FILE': os.path.join(portage.root, 'etc/profile.env'),
'REVDEP_CONFDIR': os.path.join(portage.root, 'etc/revdep-rebuild/'),
# can be set True from the cli with the --no-pretend option
'NO_PRETEND': False,
'VERBOSITY': 1,
- }
+
+ 'quiet': False,
+ 'nocolor': False,
+ 'library': set(),
+ 'no-progress': False,
+ 'debug': False,
+ 'no-ld-path': False,
+ 'no-order': False,
+ 'pass_through_options': '',
+ 'stdout': sys.stdout,
+ 'stdin': sys.stdin,
+ 'stderr': sys.stderr
+ }