diff --git a/codebasin/__init__.py b/codebasin/__init__.py index c1bd884..97a18a2 100644 --- a/codebasin/__init__.py +++ b/codebasin/__init__.py @@ -10,7 +10,6 @@ import codebasin.source import codebasin.util -import codebasin.walkers warnings.warn( "Calling codebasin package internals is deprecated. " diff --git a/codebasin/__main__.py b/codebasin/__main__.py index 32c2f78..64b76d5 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -12,7 +12,6 @@ import sys from codebasin import CodeBase, config, finder, report, util -from codebasin.walkers.platform_mapper import PlatformMapper log = logging.getLogger("codebasin") version = "1.2.0" @@ -331,8 +330,7 @@ def main(): stdout_handler.setLevel(log_level) # Count lines for platforms - platform_mapper = PlatformMapper(codebase) - setmap = platform_mapper.walk(state) + setmap = state.get_setmap(codebase) def report_enabled(name): if "all" in args.reports: diff --git a/codebasin/finder.py b/codebasin/finder.py index 86d59a3..1b2132e 100644 --- a/codebasin/finder.py +++ b/codebasin/finder.py @@ -12,8 +12,9 @@ from tqdm import tqdm -from codebasin import file_parser, platform, preprocessor +from codebasin import CodeBase, file_parser, platform, preprocessor from codebasin.language import FileLanguage +from codebasin.preprocessor import CodeNode from codebasin.walkers.tree_associator import TreeAssociator log = logging.getLogger(__name__) @@ -72,6 +73,22 @@ def get_map(self, fn): return None return self.maps[fn] + def get_setmap(self, codebase: CodeBase) -> dict[frozenset, int]: + """ + Returns + ------- + dict[frozenset, int] + The number of lines associated with each platform set. + """ + setmap = collections.defaultdict(int) + for fn in codebase: + tree = self.get_tree(fn) + association = self.get_map(fn) + for node in [n for n in tree.walk() if isinstance(n, CodeNode)]: + platform = frozenset(association[node]) + setmap[platform] += node.num_lines + return setmap + def find( rootdir, diff --git a/codebasin/walkers/platform_mapper.py b/codebasin/walkers/platform_mapper.py deleted file mode 100644 index 753a1ae..0000000 --- a/codebasin/walkers/platform_mapper.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (C) 2019-2024 Intel Corporation -# SPDX-License-Identifier: BSD-3-Clause - -import logging - -from codebasin.preprocessor import CodeNode, FileNode -from codebasin.walkers.tree_mapper import TreeMapper - -log = logging.getLogger("codebasin") - - -class PlatformMapper(TreeMapper): - """ - Specific TreeMapper that builds a mapping of nodes to platforms. - """ - - def __init__(self, codebase, _tree=None, _node_associations=None): - super().__init__(_tree, _node_associations) - self.codebase = codebase - self._null_set = frozenset([]) - - def _map_node(self, _node, _map): - """ - Map a specific node to its platform set, and descend into the - children nodes. - """ - # Do not map files that the user does not consider to be part of - # the codebase - if isinstance(_node, FileNode) and _node.filename not in self.codebase: - return - - if isinstance(_node, CodeNode): - association = _map[_node] - platform = frozenset(association) - self.line_map[platform] += _node.num_lines - - for child in _node.children: - self._map_node(child, _map) diff --git a/tests/basic_asm/test_basic_asm.py b/tests/basic_asm/test_basic_asm.py index a3bef31..202a080 100644 --- a/tests/basic_asm/test_basic_asm.py +++ b/tests/basic_asm/test_basic_asm.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestBasicAsm(unittest.TestCase): @@ -35,8 +34,7 @@ def test_yaml(self): ) configuration = {"CPU": entries} state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/basic_fortran/test_basic_fortran.py b/tests/basic_fortran/test_basic_fortran.py index e3a244a..29de5f4 100644 --- a/tests/basic_fortran/test_basic_fortran.py +++ b/tests/basic_fortran/test_basic_fortran.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestBasicFortran(unittest.TestCase): @@ -46,8 +45,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/build-dir/test_build_dir.py b/tests/build-dir/test_build_dir.py index 95b46ba..6626109 100644 --- a/tests/build-dir/test_build_dir.py +++ b/tests/build-dir/test_build_dir.py @@ -8,7 +8,6 @@ from pathlib import Path from codebasin import CodeBase, config, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestBuildDirectories(unittest.TestCase): @@ -64,8 +63,7 @@ def test_absolute_paths(self): expected_setmap = {frozenset(["one", "two"]): 1} state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap") def test_empty_platform(self): diff --git a/tests/commented_directive/test_commented_directive.py b/tests/commented_directive/test_commented_directive.py index 0ae12b9..dbb6210 100644 --- a/tests/commented_directive/test_commented_directive.py +++ b/tests/commented_directive/test_commented_directive.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestCommentedDirective(unittest.TestCase): @@ -50,8 +49,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) node_count = 1 for fn in state.get_filenames(): diff --git a/tests/define/test_define.py b/tests/define/test_define.py index b27aff5..5581ed7 100644 --- a/tests/define/test_define.py +++ b/tests/define/test_define.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestDefine(unittest.TestCase): @@ -45,8 +44,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/disjoint/test_disjoint.py b/tests/disjoint/test_disjoint.py index 1ec9b8c..0b7ecbe 100644 --- a/tests/disjoint/test_disjoint.py +++ b/tests/disjoint/test_disjoint.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestDisjointCodebase(unittest.TestCase): @@ -44,8 +43,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/duplicates/test_duplicates.py b/tests/duplicates/test_duplicates.py index 459f29e..7d0bc46 100644 --- a/tests/duplicates/test_duplicates.py +++ b/tests/duplicates/test_duplicates.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestDuplicates(unittest.TestCase): @@ -48,8 +47,7 @@ def test_duplicates(self): expected_setmap = {frozenset(["cpu"]): 1, frozenset(["gpu"]): 1} state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap") diff --git a/tests/exclude/test_exclude.py b/tests/exclude/test_exclude.py index b640e17..7fecfe3 100644 --- a/tests/exclude/test_exclude.py +++ b/tests/exclude/test_exclude.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, config, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestExclude(unittest.TestCase): @@ -28,8 +27,7 @@ def _get_setmap(self, excludes): "test": config.load_database(str(dbpath), str(self.rootdir)), } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) return setmap def test_exclude_nothing(self): diff --git a/tests/include/test_include.py b/tests/include/test_include.py index 65e38ad..27e298e 100644 --- a/tests/include/test_include.py +++ b/tests/include/test_include.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, config, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestInclude(unittest.TestCase): @@ -37,8 +36,7 @@ def test_include(self): } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/literals/test_literals.py b/tests/literals/test_literals.py index 0a8b28d..b7e387d 100644 --- a/tests/literals/test_literals.py +++ b/tests/literals/test_literals.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder, preprocessor -from codebasin.walkers.platform_mapper import PlatformMapper class TestLiterals(unittest.TestCase): @@ -43,8 +42,7 @@ def test_literals(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/macro_expansion/test_macro_expansion.py b/tests/macro_expansion/test_macro_expansion.py index 5ad699b..da4ff9c 100644 --- a/tests/macro_expansion/test_macro_expansion.py +++ b/tests/macro_expansion/test_macro_expansion.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder, platform, preprocessor -from codebasin.walkers.platform_mapper import PlatformMapper class TestMacroExpansion(unittest.TestCase): @@ -52,8 +51,7 @@ def test_macro_expansion(self): "GPU": gpu_entries, } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/multi_line/test_multi_line.py b/tests/multi_line/test_multi_line.py index 4b63a24..76a3b53 100644 --- a/tests/multi_line/test_multi_line.py +++ b/tests/multi_line/test_multi_line.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestMultiLine(unittest.TestCase): @@ -45,8 +44,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/nesting/test_nesting.py b/tests/nesting/test_nesting.py index 76966b9..32ae02a 100644 --- a/tests/nesting/test_nesting.py +++ b/tests/nesting/test_nesting.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestNesting(unittest.TestCase): @@ -46,8 +45,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/once/test_once.py b/tests/once/test_once.py index 0e5571a..c8e960b 100644 --- a/tests/once/test_once.py +++ b/tests/once/test_once.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder -from codebasin.walkers.platform_mapper import PlatformMapper class TestOnce(unittest.TestCase): @@ -45,8 +44,7 @@ def test_yaml(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap, diff --git a/tests/operators/test_operators.py b/tests/operators/test_operators.py index 7eba420..fb4011c 100644 --- a/tests/operators/test_operators.py +++ b/tests/operators/test_operators.py @@ -6,7 +6,6 @@ from pathlib import Path from codebasin import CodeBase, finder, platform, preprocessor -from codebasin.walkers.platform_mapper import PlatformMapper class TestOperators(unittest.TestCase): @@ -43,8 +42,7 @@ def test_operators(self): ], } state = finder.find(self.rootdir, codebase, configuration) - mapper = PlatformMapper(codebase) - setmap = mapper.walk(state) + setmap = state.get_setmap(codebase) self.assertDictEqual( setmap, self.expected_setmap,