Skip to content

Commit

Permalink
Merge pull request #120 from Pennycook/platform-mapper
Browse files Browse the repository at this point in the history
Replace PlatformMapper with a function
  • Loading branch information
Pennycook authored Nov 5, 2024
2 parents 3732c72 + af11fb7 commit 9231ff8
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 88 deletions.
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 @@ -88,6 +89,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")

def test_symlinks(self):
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

0 comments on commit 9231ff8

Please sign in to comment.