def get_stage_snaps(self) -> Set[str]: if not self.__stage_snaps: processor = GrammarProcessor( arch=self._arch, target_arch=self._target_arch, checker=repo.snaps.SnapPackage.is_valid_snap, ) self.__stage_snaps = set( processor.process(grammar=self._get_property("stage-snaps"))) return self.__stage_snaps
def get_build_environment(self) -> List[Dict[str, str]]: if not self.__build_environment: processor = GrammarProcessor( arch=self._arch, target_arch=self._target_arch, checker=lambda s: True, ) self.__build_environment = processor.process( grammar=self._get_property("build-environment"), ) return self.__build_environment
def get_build_packages(self) -> Set[str]: if not self.__build_packages: processor = GrammarProcessor( arch=self._arch, target_arch=self._target_arch, checker=self._repo.build_package_is_valid, ) self.__build_packages = set( processor.process( grammar=self._get_property("build-packages"), )) return self.__build_packages
def get_source(self) -> str: if not self.__source: # The grammar is array-based, even though we only support a single # source. processor = GrammarProcessor( arch=self._arch, target_arch=self._target_arch, checker=lambda s: True, ) source_array = processor.process(grammar=self._source_grammar) if len(source_array) > 0: self.__source = source_array.pop() return self.__source
def process_parts( *, parts_yaml_data: Dict[str, Any], arch: str, target_arch: str ) -> Dict[str, Any]: """Process grammar for parts. :param yaml_data: unprocessed snapcraft.yaml. :returns: process snapcraft.yaml. """ # TODO: make checker optional in craft-grammar. processor = GrammarProcessor( arch=arch, target_arch=target_arch, checker=lambda x: x == x, # pylint: disable=comparison-with-itself ) for part_name in parts_yaml_data: parts_yaml_data[part_name] = process_part( part_yaml_data=parts_yaml_data[part_name], processor=processor ) return parts_yaml_data
def process_part( *, part_yaml_data: Dict[str, Any], processor: GrammarProcessor ) -> Dict[str, Any]: """Process grammar for a given part.""" existing_keys = (key for key in _KEYS if key in part_yaml_data) for key in existing_keys: unprocessed_grammar = part_yaml_data[key] if key in _SCALAR_VALUES and isinstance(unprocessed_grammar, str): unprocessed_grammar = [unprocessed_grammar] processed_grammar = processor.process(grammar=unprocessed_grammar) if key in _SCALAR_VALUES and isinstance(processed_grammar, list): if processed_grammar: processed_grammar = processed_grammar[0] else: processed_grammar = None part_yaml_data[key] = processed_grammar return part_yaml_data
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """Grammar processor tests.""" from collections import namedtuple import pytest from craft_grammar import GrammarProcessor from snapcraft.parts.grammar import process_part, process_parts _PROCESSOR = GrammarProcessor( arch="amd64", target_arch="amd64", checker=lambda x: x == x, # pylint: disable=comparison-with-itself ) GrammarEntry = namedtuple("GrammarEntry", ["value", "expected"]) GRAMMAR_SCALAR_ENTRIES = [ # no grammar. GrammarEntry("entry", "entry"), # on arch match. GrammarEntry([{ "on amd64": "entry" }], "entry"), # on else match. GrammarEntry([{ "on arm64": "entry" }, {