async def load_plugin_list(self): result = [] try: discovered_plugins = { name: importlib.import_module(name) for finder, name, ispkg in pkgutil.iter_modules() if name.startswith('cbpi') and len(name) > 4 } for key, module in discovered_plugins.items(): from importlib.metadata import version try: from importlib.metadata import (distribution, metadata, version) meta = metadata(key) result.append( {row: meta[row] for row in list(metadata(key))}) except Exception as e: logger.error("FAILED to load plugin {} ".format(key)) logger.error(e) except Exception as e: logger.error(e) return [] return result
def _get_importlib_metadata_metadata(package_name): # type: (str) -> None """Retrieve all the metadata for the given package.""" import sys try: import importlib_metadata except ImportError: import importlib.metadata as importlib_metadata # type: ignore import json result = dict(importlib_metadata.metadata(package_name).items()) # As metadata are encoded as email.message, it's not possible to implicitly expose information about which keys # keep multiple values and which are single-value. Let's explicitly maintain a list for metadata keys that # are arrays: # https://packaging.python.org/specifications/core-metadata keys = frozenset(( "Platform", "Supported-Platform", "Classifier", "Provides-Dist", "Requires-Dist", "Requires-External", "Project-URL", "Provides-Extra", ), ) for key in keys: value = importlib_metadata.metadata(package_name).get_all(key) if value: # Override the previous one (single value) with array. result[key] = value print(json.dumps(result)) sys.exit(0)
def get_about(self) -> AboutModel: # TODO - G.M - 2018-09-26 - Set version correctly return AboutModel( name="Tracim", version=metadata("tracim_backend")["Version"], build_version=self._config.BUILD_VERSION, datetime=datetime.datetime.now(), website=metadata("tracim_backend")["Home-page"], )
def test_metadata_for_this_package(self): md = metadata('egginfo-pkg') assert md['author'] == 'Steven Ma' assert md['LICENSE'] == 'Unknown' assert md['Name'] == 'egginfo-pkg' classifiers = md.get_all('Classifier') assert 'Topic :: Software Development :: Libraries' in classifiers
def test_as_json_egg_info(self): md = metadata('egginfo-pkg').json assert 'name' in md assert md['keywords'] == ['sample', 'package'] desc = md['description'] assert desc.startswith('Once upon a time\nThere was') assert len(md['classifier']) == 2
def test_as_json(self): md = metadata('distinfo-pkg').json assert 'name' in md assert md['keywords'] == ['sample', 'package'] desc = md['description'] assert desc.startswith('Once upon a time\nThere was') assert len(md['requires_dist']) == 2
def get(self, name, version=None): """return a dictionary with pypi project data""" try: md = metadata(name) return self.parse_metadata(md) except PackageNotFoundError: return self.getPipShow(name)
def get_lib_version() -> str: """Returns the current Library version Returns: str: The current Library version read from package metadata or an environment variable """ version = None try: sdk_metadata = metadata("testproject-robot-library") version = sdk_metadata["Version"] except PackageNotFoundError: # This is OK, it just means that there's no previously installed version available pass logging.debug(f"Version read from package metadata: {version}") if version is None: # we're not dealing with an installed package, build uses an environment variable version = os.getenv("TP_ROBOT_LIB_VERSION") if version is None: raise Exception("No Library version definition found in metadata or environment variable") logging.debug(f"Version read from environment variable: {version}") return version
def get_sdk_version() -> str: """Returns the current SDK version Returns: str: The current SDK version read from package metadata or an environment variable """ version = None try: sdk_metadata = metadata('testproject-python-sdk') version = sdk_metadata['Version'] except PackageNotFoundError: # This is OK, it just means that there's no previously installed version available pass logging.debug(f"Version read from package metadata: {version}") if version is None: # we're not dealing with an installed package, build uses an environment variable version = os.environ.get("TP_SDK_VERSION") if version is None: raise SdkException( "No SDK version definition found in metadata or environment variable" ) logging.debug(f"Version read from environment variable: {version}") return version
def test_metadata_for_this_package(self): md = importlib_metadata.metadata('importlib_metadata') assert md['author'] == 'Barry Warsaw' assert md['LICENSE'] == 'Apache Software License' assert md['Name'] == 'importlib-metadata' classifiers = md.get_all('Classifier') assert 'Topic :: Software Development :: Libraries' in classifiers
def get_requirement_version(package_name, dependency_name): """Get assigned version to a dependency in package requirements.""" package_name = package_name.replace('pollination.', 'pollination_') package_name = name_to_pollination(package_name) dependency_name = dependency_name.replace('_', '-') requirements = {} try: req = pkg_resources.get_distribution(package_name).get_metadata( 'requires.txt') except FileNotFoundError: # try to get it from meta data package_data = importlib_metadata.metadata(package_name) req_dists = package_data.get_all('Requires-Dist') or [] for package in req_dists: name, version = package.split(' (') version = \ version.replace('=', '').replace('>', '').replace('<', '') \ .replace(')', '').strip() requirements[name] = version else: for package in pkg_resources.parse_requirements(req): version = \ str(package.specifier).replace('=', '').replace('>', '').replace('<', '') requirements[package.project_name] = version assert dependency_name in requirements, \ f'{dependency_name} is not a requirement for {package_name}.' return requirements[dependency_name]
def _get_package_readme(package_name: str) -> str: package_name = name_to_pollination(package_name).replace('-', '_') package_data = importlib_metadata.metadata(package_name) long_description = package_data.get_payload() if not long_description.strip(): content = package_data.get('Description') long_description = [] for line in content.split('\n'): long_description.append(re.sub('^ ', '', line)) long_description = '\n'.join(long_description) return long_description
def get_metadata(module): module_path = module.__file__ for dist in pkg_resources.working_set: for path in get_dist_files(dist): if module_path == path: meta = importlib_metadata.metadata(dist.project_name) return metadata(name=meta['Name'], author=meta['Author'], author_email=meta['Author-email'], description=meta['Summary'], timestamp=str(datetime.now()), version=meta['Version'])
def register_packages(self, registerables): registerables = set(registerables) for registerable in registerables: if isinstance(registerable, Package): package = registerable elif hasattr(registerable, "export"): package = registerable.export else: raise BootstrapException( f"Expected an Aioli-type Python Package, or an aioli.Package, got: {registerable}" ) meta = None if package._auto_meta and hasattr(registerable, "__path__"): pyproject_file = "pyproject.toml" self.log.debug(f"Looking for project metadata [auto_meta]") pyproject_path = os.path.join(registerable.__path__[0], "..", pyproject_file) if os.path.exists(pyproject_path): # Meta from pyproject parser = configparser.ConfigParser() parser.read(pyproject_path) pyproject = parser["tool.poetry"] meta = { k: v.strip('"') for k, v in pyproject.items() if k in ["name", "description", "version"] } elif hasattr(registerable, "__name__"): # Meta from dist dist = dict(metadata(registerable.__name__)) meta = dict(name=dist.get("Name").replace("-", "_"), version=dist.get("Version"), description=dist.get("Summary")) elif package._meta: meta = package._meta else: raise BootstrapException( f"Unable to find metadata for {registerable}") try: package.meta = PackageMetadata().load(meta) except BootstrapException as e: self.log.exception(e) raise self.log.info("Attaching {name}/{version}".format(**package.meta)) config = self._config.get(package.meta["name"], {}) package.register(self._app, config) self.imported.append(package)
def extract_metadata() -> Mapping[str, str]: # Backport of Python 3.8's future importlib.metadata() metadata = importlib_metadata.metadata("procrastinate") return { "author": metadata["Author"], "email": metadata["Author-email"], "license": metadata["License"], "url": metadata["Home-page"], "version": metadata["Version"], }
def extract_metadata() -> Mapping[str, str]: # Backport of Python 3.8's future importlib.metadata() metadata = importlib_metadata.metadata("{{ cookiecutter.pypi_name }}") return { "author": metadata["Author"], "email": metadata["Author-email"], "license": metadata["License"], "url": metadata["Home-page"], "version": metadata["Version"], }
def get_package_dependencies(name: str) -> dict: """Extract dependencies from metadata of installed package""" requirements: typing.Dict[str, typing.Union[str, typing.List[str]]] = { 'name': name, 'content': [ f'{i[0]}: {i[1]}' for i in importlib_metadata.metadata( name).items() # type: ignore[attr-defined] if i[0] in ('Requires-Dist', 'Provides-Extra') ], } return requirements
def _get_package_owner(package_name: str) -> str: """Author field is used for package owner.""" package_name = name_to_pollination(package_name).replace('-', '_') package_data = importlib_metadata.metadata(package_name) owner = package_data.get('Author') assert owner, \ 'You must set the author of the package in setup.py to Pollination account owner' # ensure there is only one author owner = owner.strip() assert len(owner.split(',')) == 1, \ 'A Pollination package can only have one author. Use maintainer field for ' \ 'providing multiple maintainers.' return owner
def search_script(self, script): """ Search a script's contents for import statements and check if they're currently prevent in the list of all installed pip modules. :param script: string :return: void """ if self.import_statement.search(script): unique_found = [] for f_import in set(self.import_statement.findall(script)): # Try the package metadata lookup, if its not found its just local or builtin try: import_metadata = metadata(f_import) except PackageNotFoundError: try: import_metadata = metadata( self.tricky_package_map[f_import]) except KeyError: # if f_import is not in our tricky_package_map, it must be a local package, # so skip it continue # Check that the package isn't already accounted for name = import_metadata["Name"] # Shriver - previously this was checking installed packages # Thinking this prevents freeze_all from working correctly on a clean venv # Want it to be able to go from clean venv + import scan to a frozen req file # freeze.py uses any existing frozen file as constraints if name not in self.libraries_found: # and name in self.installed_packages: unique_found.append(name) for package_name in unique_found: # Shriver - see above # self.installed_packages.remove(package_name) self.libraries_found.append(package_name)
def _get_package_data(package_name: str) -> Dict: """Get package data as a dictionary.""" package_name = name_to_pollination(package_name).replace('-', '_') package_data = importlib_metadata.metadata(package_name) data = { 'name': package_data.get('Name').replace('pollination-', ''), 'description': package_data.get('Summary'), 'home': package_data.get('Home-page'), 'tag': _get_package_version(package_data), 'keywords': _get_package_keywords(package_data), 'maintainers': _get_package_maintainers(package_data), 'license': _get_package_license(package_data), 'icon': _get_package_icon(package_data), 'sources': _get_package_sources(package_data) } return data
def get_doc_url_for_provider(provider_name: str, provider_version: str) -> str: """Prepare link to Airflow Provider documentation.""" try: metadata_items = importlib_metadata.metadata(provider_name).get_all( "Project-URL") if isinstance(metadata_items, str): metadata_items = [metadata_items] if metadata_items: for item in metadata_items: if item.lower().startswith('documentation'): _, _, url = item.partition(",") if url: return url.strip() except importlib_metadata.PackageNotFoundError: pass # Fallback if provider is apache one if provider_name.startswith("apache-airflow"): return f'https://airflow.apache.org/docs/{provider_name}/{provider_version}/' return "https://airflow.apache.org/docs/apache-airflow-providers/index.html#creating-your-own-providers"
def _create_window(): window = tkinter.Tk() screen_w = window.winfo_screenwidth() screen_h = window.winfo_screenheight() w = (screen_w // 2) - 16 h = (screen_h // 2) - 16 x = (screen_w - w) // 2 y = (screen_h - h) // 2 window.geometry(f'{w}x{h}+{x}+{y}') window.minsize(w, h) meta = ilm.metadata(__package__) name = meta['name'] version = meta['version'] window.title(f'{name} {version}') return window
def get_packages_metadata(package_names): """ Get list of installed package's metadata :param list package_names: Install package names. :return: list of dict. Dict contains package name and license. :rtype: list """ results = [] for name in package_names: d = {"name": name} try: metadata = importlib_metadata.metadata(name) except importlib_metadata.api.PackageNotFoundError: continue else: d["version"] = metadata["Version"] d["license"] = metadata["License"] results.append(d) return results
def extract_metadata(package_name: str) -> Mapping[str, str]: # Backport of Python 3.8's future importlib.metadata() try: metadata = importlib_metadata.metadata(package_name) except importlib_metadata.PackageNotFoundError: return { "author": "-", "email": "-", "license": "-", "url": "-", "version": "0.0.0", } return { "author": metadata["Author"], "email": metadata["Author-email"], "license": metadata["License"], "url": metadata["Home-page"], "version": metadata["Version"], }
def plugins_list(self): result = [] print("") print(Fore.LIGHTYELLOW_EX, "List of active plugins", Style.RESET_ALL) print("") discovered_plugins = { name: importlib.import_module(name) for finder, name, ispkg in pkgutil.iter_modules() if name.startswith('cbpi') and len(name) > 4 } for key, module in discovered_plugins.items(): try: meta = metadata(key) result.append( dict(Name=meta["Name"], Version=meta["Version"], Author=meta["Author"], Homepage=meta["Home-page"], Summary=meta["Summary"])) except Exception as e: print(e) print(Fore.LIGHTGREEN_EX, tabulate(result, headers="keys"), Style.RESET_ALL)
def test_metadata_loads_egg_info(self): pkg_name = self.pkg_with_non_ascii_description_egg_info(self.site_dir) meta = metadata(pkg_name) assert meta['Description'] == 'pôrˈtend'
__all__ = ( "__title__", "__summary__", "__uri__", "__version__", "__author__", "__email__", "__license__", "__copyright__", ) __copyright__ = "Copyright 2021 David Vicente and individual contributors" import importlib_metadata metadata = importlib_metadata.metadata("fnvstring") __title__ = metadata["name"] __summary__ = metadata["summary"] __uri__ = metadata["home-page"] __version__ = metadata["version"] __author__ = metadata["author"] __email__ = metadata["author-email"] __license__ = metadata["license"] class Fvn64SaltedHasher: """Fnv64 Hasher with specific salt.""" salt: str = DEFAULT_SALT
"""Command-line interface.""" from typing import Optional import icontract import importlib_metadata import structlog_sentry_logger import typeguard import typer from dotenv import find_dotenv, load_dotenv __version__ = importlib_metadata.metadata( "{{cookiecutter.project_slug}}")["Version"] app = typer.Typer() # see `.env` for requisite environment variables load_dotenv(find_dotenv()) LOGGER = structlog_sentry_logger.get_logger() @typeguard.typechecked() @icontract.require(lambda value: value is None or value is True) def version_callback(value: Optional[bool], ) -> None: # pylint: disable=unsubscriptable-object """Provides a version option for the CLI""" if value: typer.echo(f"{{cookiecutter.project_slug}} Version: {__version__}") raise typer.Exit() @typeguard.typechecked() @app.command() def main(
def package_name_from_metadata(package: str) -> Optional[str]: if not is_std(package): with contextlib.suppress(PackageNotFoundError): return metadata(package)["Name"] return None
import sys import os from importlib_metadata import metadata # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath('../..')) sys.path.insert(0, os.path.abspath('../../scripts')) # -- General configuration ------------------------------------------------ # General information about the project. project = 'pyraliddemo' mdata = metadata(project) author = mdata['Author'] version = mdata['Version'] copyright = '2020, {}'.format(author) gh_user = '******' # If your documentation needs a minimal Sphinx version, state it here. needs_sphinx = '3.3' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.coverage',