Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace PlatformMapper with a function #120

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion codebasin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import codebasin.source
import codebasin.util
import codebasin.walkers

warnings.warn(
"Calling codebasin package internals is deprecated. "
Expand Down
4 changes: 1 addition & 3 deletions codebasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 18 additions & 1 deletion codebasin/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 0 additions & 38 deletions codebasin/walkers/platform_mapper.py

This file was deleted.

4 changes: 1 addition & 3 deletions tests/basic_asm/test_basic_asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestBasicAsm(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/basic_fortran/test_basic_fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestBasicFortran(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/build-dir/test_build_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions tests/commented_directive/test_commented_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestCommentedDirective(unittest.TestCase):
Expand Down Expand Up @@ -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():
Expand Down
4 changes: 1 addition & 3 deletions tests/define/test_define.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestDefine(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/disjoint/test_disjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestDisjointCodebase(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/duplicates/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestDuplicates(unittest.TestCase):
Expand Down Expand Up @@ -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")


Expand Down
4 changes: 1 addition & 3 deletions tests/exclude/test_exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions tests/include/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/literals/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/macro_expansion/test_macro_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/multi_line/test_multi_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestMultiLine(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/nesting/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestNesting(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/once/test_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from codebasin import CodeBase, finder
from codebasin.walkers.platform_mapper import PlatformMapper


class TestOnce(unittest.TestCase):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions tests/operators/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
Loading