migrate away from a global SETTINGS usage.
authordol-sen <brian.dolbec@gmail.com>
Mon, 25 Apr 2011 06:18:44 +0000 (23:18 -0700)
committerPaul Varner <fuzzyray@gentoo.org>
Tue, 12 Jul 2011 21:29:01 +0000 (16:29 -0500)
Better for tests and api usability.

pym/gentoolkit/revdep_rebuild/analyse.py
pym/gentoolkit/revdep_rebuild/assign.py
pym/gentoolkit/revdep_rebuild/cache.py
pym/gentoolkit/revdep_rebuild/collect.py
pym/gentoolkit/revdep_rebuild/settings.py

index a9ac753bac2a1b45166ae8e2b295eb26b0fd8b8c..5ee0c7beff228c677a89faab57b7575b75846077 100644 (file)
@@ -11,11 +11,9 @@ from stuff import scan
 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
        '''
 
@@ -23,7 +21,7 @@ def prepare_checks(files_to_check, libraries, bits):
        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?
@@ -116,7 +114,8 @@ def main_checks(found_libs, broken, dependencies, logger):
        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.
 
@@ -132,9 +131,9 @@ def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=N
                #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',]))
@@ -143,7 +142,7 @@ def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=N
                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})
 
 
@@ -170,10 +169,10 @@ def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=N
 
        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)
@@ -186,7 +185,7 @@ def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=N
 
        logger.warn(green(' * ') + bold('Assign files to packages'))
 
-       return assign_packages(broken_pathes, logger)
+       return assign_packages(broken_pathes, logger, settings)
 
 
 
index c4ae75074bd4e6fcfc36e4c77335688f7e8a01e6..9eb177da1b8e3fe23b1363f46bc836293e69c4d9 100644 (file)
@@ -2,21 +2,19 @@
 
 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:
index 616ef790de37d2b1e22d7c4e86a7c3fe97ec5f09..e1a8e11458c57f4a28722b57378b34ce5b1c3622 100644 (file)
@@ -4,10 +4,10 @@ import os
 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
@@ -29,7 +29,7 @@ def read_cache(temp_path=SETTINGS['DEFAULT_TMP_DIR']):
        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
@@ -53,7 +53,7 @@ def save_cache(logger=logging, to_save={}, temp_path=SETTINGS['DEFAULT_TMP_DIR']
 
 
 
-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
index 613749aa001434ec72a346dfdf584dff11ffed24..735857b16ff93d8a251619da59ee4fb79523094f 100644 (file)
@@ -7,10 +7,9 @@ import stat
 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
@@ -54,7 +53,7 @@ def parse_conf(conf_file=SETTINGS['DEFAULT_LD_FILE'], visited=None, logger=loggi
        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)
        '''
@@ -63,7 +62,7 @@ def prepare_search_dirs(logger=logging):
        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)
@@ -72,11 +71,11 @@ def prepare_search_dirs(logger=logging):
        #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)'''
 
@@ -85,7 +84,7 @@ def parse_revdep_config():
        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
@@ -110,7 +109,7 @@ def parse_revdep_config():
        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
index b47cb3e1dca53b5f8228cce5566c7d47a1f66c84..7747ac2ca3084ae4ea356cc13ad57681ba1cd3df 100644 (file)
@@ -1,9 +1,10 @@
 #!/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/'),
@@ -23,4 +24,16 @@ SETTINGS = {
                                # 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
+               }