From 8ed46bb7e563c18ecb78569b6f2a6d3a9dd66541 Mon Sep 17 00:00:00 2001 From: Dmitry Arkhipov Date: Thu, 9 Oct 2025 09:39:17 +0300 Subject: [PATCH] depinst option to reject cycles --- depinst/depinst.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/depinst/depinst.py b/depinst/depinst.py index 6dfba36..a851919 100644 --- a/depinst/depinst.py +++ b/depinst/depinst.py @@ -70,7 +70,7 @@ def module_for_header( h, x, gm ): return None -def scan_header_dependencies( f, x, gm, deps ): +def scan_header_dependencies( f, x, gm, deps, dep_path ): for line in f: @@ -89,8 +89,11 @@ def scan_header_dependencies( f, x, gm, deps ): vprint( 1, 'Adding dependency', mod ) deps[ mod ] = 0 + elif len(dep_path) > 1 and mod == dep_path[0]: + raise DependencyCycle('Dependency cycle detected: ' + '->'.join(dep_path) + '->' + mod) -def scan_directory( d, x, gm, deps ): + +def scan_directory( d, x, gm, deps, dep_path ): vprint( 1, 'Scanning directory', d ) @@ -108,20 +111,20 @@ def scan_directory( d, x, gm, deps ): if sys.version_info[0] < 3: with open( fn, 'r' ) as f: - scan_header_dependencies( f, x, gm, deps ) + scan_header_dependencies( f, x, gm, deps, dep_path ) else: with open( fn, 'r', encoding='latin-1' ) as f: - scan_header_dependencies( f, x, gm, deps ) + scan_header_dependencies( f, x, gm, deps, dep_path ) -def scan_module_dependencies( m, x, gm, deps, dirs ): +def scan_module_dependencies( m, x, gm, deps, dirs, dep_path ): vprint( 1, 'Scanning module', m ) for dir in dirs: - scan_directory( os.path.join( 'libs', m, dir ), x, gm, deps ) + scan_directory( os.path.join( 'libs', m, dir ), x, gm, deps, dep_path ) def read_exceptions(): @@ -197,7 +200,7 @@ def install_modules( modules, git_args ): raise Exception( "The command '%s' failed with exit code %d" % (command, r) ) -def install_module_dependencies( deps, x, gm, git_args ): +def install_module_dependencies( deps, x, gm, git_args, dep_path ): modules = [] @@ -214,11 +217,18 @@ def install_module_dependencies( deps, x, gm, git_args ): install_modules( modules, git_args ) for m in modules: - scan_module_dependencies( m, x, gm, deps, [ 'include', 'src' ] ) + next_dep_path = dep_path[:] + if dep_path: + next_dep_path.append(m) + scan_module_dependencies( m, x, gm, deps, [ 'include', 'src' ], next_dep_path ) return len( modules ) +class DependencyCycle(Exception): + pass + + if( __name__ == "__main__" ): parser = argparse.ArgumentParser( description='Installs the dependencies needed to test a Boost library.' ) @@ -230,6 +240,7 @@ def install_module_dependencies( deps, x, gm, git_args ): parser.add_argument( '-I', '--include', help="additional subdirectory to scan; can be repeated", metavar='DIR', action='append', default=[] ) parser.add_argument( '-g', '--git_args', help="additional arguments to `git submodule update`", default='', action='store' ) parser.add_argument( '-u', '--update', help='update before scanning', action='store_true' ) + parser.add_argument( '-C', '--reject-cycles', help='abort if has cyclical dependencies', action='store_true' ) parser.add_argument( 'library', help="name of library to scan ('libs/' will be prepended)" ) args = parser.parse_args() @@ -239,6 +250,7 @@ def install_module_dependencies( deps, x, gm, git_args ): vprint( 2, '-X:', args.exclude ) vprint( 2, '-I:', args.include ) vprint( 2, '-N:', args.ignore ) + vprint( 2, '-C:', args.reject_cycles ) x = read_exceptions() vprint( 2, 'Exceptions:', x ) @@ -269,7 +281,11 @@ def install_module_dependencies( deps, x, gm, git_args ): vprint( 1, 'Directories to scan:', *dirs ) - scan_module_dependencies( m, x, gm, deps, dirs ) + dep_path = [] + if args.reject_cycles: + dep_path.append(m) + + scan_module_dependencies( m, x, gm, deps, dirs, dep_path ) for dep in args.ignore: if dep in deps: @@ -278,5 +294,5 @@ def install_module_dependencies( deps, x, gm, git_args ): vprint( 2, 'Dependencies:', deps ) - while install_module_dependencies( deps, x, gm, args.git_args ): + while install_module_dependencies( deps, x, gm, args.git_args, dep_path ): pass