Skip to content

Commit

Permalink
machinefile: Make fully typesafe
Browse files Browse the repository at this point in the history
With the use of `typing_extensions.TypeAlias` we can get recursive
types, which solves most of the issues we had with this file.
  • Loading branch information
dcbaker committed Sep 3, 2024
1 parent 316758f commit c995558
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
28 changes: 16 additions & 12 deletions mesonbuild/machinefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from .mesonlib import MesonException

if T.TYPE_CHECKING:
from typing_extensions import TypeAlias

from .coredata import StrOrBytesPath

SectionT: TypeAlias = T.Union[str, int, bool, T.List[str], T.List['SectionT']]


class CmdLineFileParser(configparser.ConfigParser):
def __init__(self) -> None:
Expand All @@ -32,8 +36,8 @@ def optionxform(self, optionstr: str) -> str:
class MachineFileParser():
def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
self.parser = CmdLineFileParser()
self.constants: T.Dict[str, T.Union[str, bool, int, T.List[str]]] = {'True': True, 'False': False}
self.sections: T.Dict[str, T.Dict[str, T.Union[str, bool, int, T.List[str]]]] = {}
self.constants: T.Dict[str, SectionT] = {'True': True, 'False': False}
self.sections: T.Dict[str, T.Dict[str, SectionT]] = {}

for fname in filenames:
try:
Expand All @@ -58,9 +62,9 @@ def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
continue
self.sections[s] = self._parse_section(s)

def _parse_section(self, s: str) -> T.Dict[str, T.Union[str, bool, int, T.List[str]]]:
def _parse_section(self, s: str) -> T.Dict[str, SectionT]:
self.scope = self.constants.copy()
section: T.Dict[str, T.Union[str, bool, int, T.List[str]]] = {}
section: T.Dict[str, SectionT] = {}
for entry, value in self.parser.items(s):
if ' ' in entry or '\t' in entry or "'" in entry or '"' in entry:
raise MesonException(f'Malformed variable name {entry!r} in machine file.')
Expand All @@ -79,7 +83,7 @@ def _parse_section(self, s: str) -> T.Dict[str, T.Union[str, bool, int, T.List[s
self.scope[entry] = res
return section

def _evaluate_statement(self, node: mparser.BaseNode) -> T.Union[str, bool, int, T.List[str]]:
def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
if isinstance(node, (mparser.StringNode)):
return node.value
elif isinstance(node, mparser.BooleanNode):
Expand All @@ -89,28 +93,28 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> T.Union[str, bool, int,
elif isinstance(node, mparser.ParenthesizedNode):
return self._evaluate_statement(node.inner)
elif isinstance(node, mparser.ArrayNode):
# TODO: This is where recursive types would come in handy
return [self._evaluate_statement(arg) for arg in node.args.arguments]
elif isinstance(node, mparser.IdNode):
return self.scope[node.value]
elif isinstance(node, mparser.ArithmeticNode):
l = self._evaluate_statement(node.left)
r = self._evaluate_statement(node.right)
if node.operation == 'add':
if (isinstance(l, str) and isinstance(r, str)) or \
(isinstance(l, list) and isinstance(r, list)):
if isinstance(l, str) and isinstance(r, str):
return l + r
if isinstance(l, list) and isinstance(r, list):
return l + r
elif node.operation == 'div':
if isinstance(l, str) and isinstance(r, str):
return os.path.join(l, r)
raise MesonException('Unsupported node type')

def parse_machine_files(filenames: T.List[str], sourcedir: str):
def parse_machine_files(filenames: T.List[str], sourcedir: str) -> T.Dict[str, T.Dict[str, SectionT]]:
parser = MachineFileParser(filenames, sourcedir)
return parser.sections


class MachineFileStore:
def __init__(self, native_files, cross_files, source_dir):
self.native = MachineFileParser(native_files if native_files is not None else [], source_dir).sections
self.cross = MachineFileParser(cross_files if cross_files is not None else [], source_dir).sections
def __init__(self, native_files: T.Optional[T.List[str]], cross_files: T.Optional[T.List[str]], source_dir: str):
self.native = parse_machine_files(native_files if native_files is not None else [], source_dir)
self.cross = parse_machine_files(cross_files if cross_files is not None else [], source_dir)
3 changes: 3 additions & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2024 Intel Corporation

from pathlib import Path
import argparse
Expand Down Expand Up @@ -38,6 +40,7 @@
'mesonbuild/interpreter/mesonmain.py',
'mesonbuild/interpreter/interpreterobjects.py',
'mesonbuild/interpreter/type_checking.py',
'mesonbuild/machinefile.py',
'mesonbuild/mcompile.py',
'mesonbuild/mdevenv.py',
'mesonbuild/utils/core.py',
Expand Down

0 comments on commit c995558

Please sign in to comment.