def test_to_dict_is_a_copy(self): metadata = ExtractedMetadata(summary="summary") metadata_dict = metadata.to_dict() metadata_dict["summary"] = "edited summary" # Ensure the metadata cannot be edited with its dict self.assertThat(metadata.get_summary(), Equals("summary"))
def test_update_overwrite(self): metadata = ExtractedMetadata(summary="summary", description="description") metadata2 = ExtractedMetadata(description="new description") metadata.update(metadata2) self.assertThat(metadata.get_summary(), Equals("summary")) self.assertThat(metadata.get_description(), Equals("new description"))
def test_to_dict_is_a_copy(self): metadata = ExtractedMetadata(summary='summary') metadata_dict = metadata.to_dict() metadata_dict['summary'] = 'edited summary' # Ensure the metadata cannot be edited with its dict self.assertThat(metadata.get_summary(), Equals('summary'))
def test_to_dict_complete(self): metadata = ExtractedMetadata( summary='summary', description='description') self.assertThat(metadata.to_dict(), Equals({ 'summary': 'summary', 'description': 'description', }))
def test_to_dict_complete(self): metadata = ExtractedMetadata(summary="summary", description="description") self.assertThat( metadata.to_dict(), Equals({ "summary": "summary", "description": "description" }), )
def _adopt_info( config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata): metadata_dict = extracted_metadata.to_dict() for key, value in metadata_dict.items(): # desktop_file_ids are a special case that will be handled # after all the top level snapcraft.yaml keys. if key != 'desktop_file_ids' and key not in config_data: if key == 'icon': if _icon_file_exists() or not os.path.exists( str(value)): # Do not overwrite the icon file. continue config_data[key] = value if 'desktop_file_ids' in metadata_dict: for desktop_file_id in metadata_dict['desktop_file_ids']: app_name = _get_app_name_from_desktop_file_id( config_data, desktop_file_id) if app_name and not _desktop_file_exists(app_name): for xdg_data_dir in ('usr/local/share', 'usr/share'): desktop_file_path = os.path.join( xdg_data_dir, 'applications', desktop_file_id.replace('-', '/')) if os.path.exists(desktop_file_path): config_data['apps'][app_name]['desktop'] = ( desktop_file_path)
def _adopt_keys( config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata ) -> Set[str]: ignored_keys = set() metadata_dict = extracted_metadata.to_dict() # desktop_file_paths and common_ids are special cases that will be handled # after all the top level snapcraft.yaml keys. ignore = ("desktop_file_paths", "common_id") overrides = ((k, v) for k, v in metadata_dict.items() if k not in ignore) for key, value in overrides: if key not in config_data: if key == "icon": if _icon_file_exists() or not os.path.exists(str(value)): # Do not overwrite the icon file. continue config_data[key] = value else: ignored_keys.add(key) if "desktop_file_paths" in metadata_dict and "common_id" in metadata_dict: app_name = _get_app_name_from_common_id( config_data, str(metadata_dict["common_id"]) ) if app_name and not _desktop_file_exists(app_name): for desktop_file_path in metadata_dict["desktop_file_paths"]: if os.path.exists(desktop_file_path): config_data["apps"][app_name]["desktop"] = desktop_file_path break return ignored_keys
def _adopt_keys(config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata) -> Set[str]: ignored_keys = set() metadata_dict = extracted_metadata.to_dict() # desktop_file_paths and common_ids are special cases that will be handled # after all the top level snapcraft.yaml keys. ignore = ('desktop_file_paths', 'common_id') overrides = ((k, v) for k, v in metadata_dict.items() if k not in ignore) for key, value in overrides: if key not in config_data: if key == 'icon': if _icon_file_exists() or not os.path.exists(str(value)): # Do not overwrite the icon file. continue config_data[key] = value else: ignored_keys.add(key) if 'desktop_file_paths' in metadata_dict and 'common_id' in metadata_dict: app_name = _get_app_name_from_common_id( config_data, str(metadata_dict['common_id'])) if app_name and not _desktop_file_exists(app_name): for desktop_file_path in metadata_dict['desktop_file_paths']: if os.path.exists(desktop_file_path): config_data['apps'][app_name]['desktop'] = ( desktop_file_path) break return ignored_keys
def test_getters(self, tmp_work_path, prop, value): metadata = ExtractedMetadata(**{prop: value}) for p in self.properties: gotten = getattr(metadata, "get_{}".format(p))() if p == prop: assert gotten == value else: assert gotten is None
def test_update_merge(self): metadata = ExtractedMetadata(summary="summary") metadata2 = ExtractedMetadata(description="description") metadata.update(metadata2) self.assertThat(metadata.get_summary(), Equals("summary")) self.assertThat(metadata.get_description(), Equals("description"))
def test_update_merge(self): metadata = ExtractedMetadata(summary='summary') metadata2 = ExtractedMetadata(description='description') metadata.update(metadata2) self.assertThat(metadata.get_summary(), Equals('summary')) self.assertThat(metadata.get_description(), Equals('description'))
def test_getters(self): metadata = ExtractedMetadata(**{self.property: self.value}) for prop in self.properties: gotten = getattr(metadata, "get_{}".format(prop))() if prop == self.property: self.assertThat( gotten, Equals(self.value), "Expected {!r} getter to return {}".format( prop, self.value), ) else: self.assertThat( gotten, Equals(None), "Expected {!r} getter to return None".format(prop), )
def _adopt_keys( config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata, prime_dir: str, ) -> Set[str]: ignored_keys = set() metadata_dict = extracted_metadata.to_dict() # desktop_file_paths and common_ids are special cases that will be handled # after all the top level snapcraft.yaml keys. ignore = ("desktop_file_paths", "common_id") overrides = ((k, v) for k, v in metadata_dict.items() if k not in ignore) for key, value in overrides: if key in config_data: ignored_keys.add(key) else: if key == "icon": # Extracted appstream icon paths will be relative to prime. icon = pathlib.Path(prime_dir, str(value)) if not icon.exists(): # Cannot find icon, ignore silently. continue if _find_icon_file() is None: # Already have icon file, do not overwrite. continue config_data[key] = value if "desktop_file_paths" in metadata_dict and "common_id" in metadata_dict: app_name = _get_app_name_from_common_id( config_data, str(metadata_dict["common_id"])) if app_name and not _desktop_file_exists(app_name): for desktop_file_path in [ os.path.join(prime_dir, d) for d in metadata_dict["desktop_file_paths"] ]: if os.path.exists(desktop_file_path): config_data["apps"][app_name][ "desktop"] = desktop_file_path break return ignored_keys
def _adopt_info(config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata): metadata_dict = extracted_metadata.to_dict() for key, value in metadata_dict.items(): # desktop_file_paths are a special case that will be handled # after all the top level snapcraft.yaml keys. if key != 'desktop_file_paths' and key not in config_data: if key == 'icon': if _icon_file_exists() or not os.path.exists(str(value)): # Do not overwrite the icon file. continue config_data[key] = value if 'desktop_file_paths' in metadata_dict and 'common_id' in metadata_dict: app_name = _get_app_name_from_common_id( config_data, str(metadata_dict['common_id'])) if app_name and not _desktop_file_exists(app_name): for desktop_file_path in metadata_dict['desktop_file_paths']: if os.path.exists(desktop_file_path): config_data['apps'][app_name]['desktop'] = ( desktop_file_path) break
def _adopt_keys( config_data: Dict[str, Any], extracted_metadata: _metadata.ExtractedMetadata, prime_dir: str, ) -> Set[str]: ignored_keys = set() metadata_dict = extracted_metadata.to_dict() # desktop_file_paths and common_ids are special cases that will be handled # after all the top level snapcraft.yaml keys. ignore = ("desktop_file_paths", "common_id") overrides = ((k, v) for k, v in metadata_dict.items() if k not in ignore) for key, value in overrides: if key not in config_data: if key == "icon": # Extracted appstream icon paths will be relative to the runtime tree, # so rebase it on the snap source root. value = os.path.join(prime_dir, str(value)) if _icon_file_exists() or not os.path.exists(str(value)): # Do not overwrite the icon file. continue config_data[key] = value else: ignored_keys.add(key) if "desktop_file_paths" in metadata_dict and "common_id" in metadata_dict: app_name = _get_app_name_from_common_id( config_data, str(metadata_dict["common_id"])) if app_name and not _desktop_file_exists(app_name): for desktop_file_path in [ os.path.join(prime_dir, d) for d in metadata_dict["desktop_file_paths"] ]: if os.path.exists(desktop_file_path): config_data["apps"][app_name][ "desktop"] = desktop_file_path break return ignored_keys
def parse_and_reformat(self, extracted_metadata: _metadata.ExtractedMetadata): self._parser = configparser.ConfigParser(interpolation=None) # mypy type checking ignored, see https://github.com/python/mypy/issues/506 self._parser.optionxform = str # type: ignore self._parser.read(self._path, encoding="utf-8") section = "Desktop Entry" if section not in self._parser.sections(): raise errors.InvalidDesktopFileError( self._filename, "missing 'Desktop Entry' section" ) if "Exec" not in self._parser[section]: raise errors.InvalidDesktopFileError(self._filename, "missing 'Exec' key") # XXX: do we want to allow more parameters for Exec? if self._name == self._snap_name: exec_value = "{} %U".format(self._name) else: exec_value = "{}.{} %U".format(self._snap_name, self._name) self._parser[section]["Exec"] = exec_value if "Icon" in self._parser[section]: icon = self._parser[section]["Icon"] # Extracted metadata (e.g. from the AppStream) can override the # icon location. if extracted_metadata: metadata_icon = extracted_metadata.get_icon() if metadata_icon is not None: icon = metadata_icon icon = icon.lstrip("/") if icon.startswith("${SNAP}/") and os.path.exists( os.path.join(self._prime_dir, icon[8:]) ): self._parser[section]["Icon"] = icon elif os.path.exists(os.path.join(self._prime_dir, icon)): self._parser[section]["Icon"] = "${{SNAP}}/{}".format(icon) else: logger.warning( "Icon {} specified in desktop file {} not found " "in prime directory".format(icon, self._filename) )
def test_not_eq(self): metadata1 = ExtractedMetadata(summary="summary") metadata2 = ExtractedMetadata(description="description") self.assertThat(metadata1, Not(Equals(metadata2)))
def test_to_dict_partial(self): metadata = ExtractedMetadata(summary='summary') self.assertThat(metadata.to_dict(), Equals({'summary': 'summary'}))
def test_to_dict_complete(self): metadata = ExtractedMetadata(summary="summary", description="description") self.assertThat( metadata.to_dict(), Equals({"summary": "summary", "description": "description"}), )
def test_to_dict_partial(self): metadata = ExtractedMetadata(summary="summary") self.assertThat(metadata.to_dict(), Equals({"summary": "summary"}))
def test_overlap(self): metadata = ExtractedMetadata(summary="summary", description="description") metadata2 = ExtractedMetadata(description="new description") self.assertThat(metadata.overlap(metadata2), Equals({"description"}))
def test_init(self): metadata = ExtractedMetadata(summary="summary") self.assertThat(metadata.get_summary(), Equals("summary")) self.assertThat(metadata.get_description(), Equals(None))
def test_eq(self): metadata1 = ExtractedMetadata(summary="summary") metadata2 = ExtractedMetadata(summary="summary") self.assertThat(metadata1, Equals(metadata2))
def test_len(self): metadata = ExtractedMetadata(version="version") self.assertThat(len(metadata), Equals(1)) metadata = ExtractedMetadata(summary="summary", version="version") self.assertThat(len(metadata), Equals(2))
def test_empty_eq(self): self.assertThat(ExtractedMetadata(), Equals(ExtractedMetadata()))