def check_installed(name: str = __package__) -> bool: try: from importlib.metadata import Distribution, PackageNotFoundError # noqa except ImportError: try: from importlib_metadata import Distribution, PackageNotFoundError # noqa except ImportError: # Should not get here EVER, but just in case lets just try checking pip freeze instead result, output = subprocess.getstatusoutput( [f"{sys.executable} -m pip freeze"]) if result != 0: # Not Ok return False is_installed = name in [ requirement.split("==")[0] for requirement in output.splitlines() ] if name != "sickchill" or (name == "sickchill" and is_installed): logger.debug(f"{name} installed: {is_installed}") return is_installed try: Distribution.from_name(name) logger.debug(f"{name} installed: True") except PackageNotFoundError: if name != "sickchill": logger.debug(f"{name} installed: False") return False return True
def test_package_not_found_mentions_metadata(self): # When a package is not found, that could indicate that the # packgae is not installed or that it is installed without # metadata. Ensure the exception mentions metadata to help # guide users toward the cause. See #124. with self.assertRaises(PackageNotFoundError) as ctx: Distribution.from_name('does-not-exist') assert "metadata" in str(ctx.exception)
def check_installed(): try: from importlib.metadata import Distribution, PackageNotFoundError except ImportError: from importlib_metadata import Distribution, PackageNotFoundError try: Distribution.from_name('sickchill') except PackageNotFoundError: return False return True
def test_coherence_of_get_modules() -> None: for package in PACKAGES: dist = Distribution.from_name(package) lhs = _get_modules_by_toplevel_txt(dist) rhs = _get_modules_by_files(dist) if lhs is not None: assert set(lhs) == set(rhs)
def __init__(self, config: ChrispileConfig): pkg = Distribution.from_name(__package__) eps = [ep for ep in pkg.entry_points if ep.group == 'console_scripts'] self.program_name = eps[0].name jinja_env = Environment(loader=PackageLoader(__package__, 'templates')) self.template = jinja_env.get_template('exec.sh') self.config = config
def __init__(cls, name, bases, d): if 'PACKAGE' in d: # interrogate setup.py to automatically fill in some # class attributes for the subclass autofill = [ 'AUTHORS', 'DESCRIPTION', 'LICENSE', 'DOCUMENTATION', 'VERSION' ] for attr in autofill: if attr in d: raise ValueError( 'Do not manually set value value for ' f'"{attr}" when "PACKAGE={d["PACKAGE"]}" is declared') pkg = Distribution.from_name(d['PACKAGE']) setup = pkg.metadata if 'TITLE' not in d: cls.TITLE = setup['name'] d['TITLE'] = cls.TITLE cls.AUTHORS = f'{setup["author"]} <{setup["author-email"]}>' d['AUTHORS'] = cls.AUTHORS cls.DESCRIPTION = setup['summary'] d['DESCRIPTION'] = cls.DESCRIPTION cls.LICENSE = setup['license'] d['LICENSE'] = cls.LICENSE cls.DOCUMENTATION = setup['home-page'] d['DOCUMENTATION'] = cls.DOCUMENTATION cls.VERSION = setup['version'] d['VERSION'] = cls.VERSION if 'SELFEXEC' not in d: eps = [ ep for ep in pkg.entry_points if ep.group == 'console_scripts' ] if eps: if len(eps) > 1: # multiple console_scripts found but maybe # they're just the same thing different_scripts = [ ep for ep in eps if ep.value != eps[0].value ] if different_scripts: raise ValueError( 'SELFEXEC not defined and more than one ' 'console_scripts found') cls.SELFEXEC = eps[0].name d['SELFEXEC'] = cls.SELFEXEC cls.EXECSHELL = sys.executable d['EXECSHELL'] = cls.EXECSHELL script_location = shutil.which(cls.SELFEXEC) if not script_location: raise EnvironmentError( cls.SELFEXEC + ' not found in PATH - check your SELFEXEC') cls.SELFPATH = os.path.dirname(script_location) d['SELFPATH'] = cls.SELFPATH script_location = os.path.join(cls.SELFPATH, cls.SELFEXEC) if not os.path.isfile(script_location): raise EnvironmentError( script_location + ' not found - check your SELFPATH, SELFEXEC') # class variables to be enforced in the subclasses attrs = [ 'DESCRIPTION', 'TYPE', 'TITLE', 'LICENSE', 'AUTHORS', 'VERSION', 'SELFPATH', 'SELFEXEC', 'EXECSHELL' ] for attr in attrs: if attr not in d: raise ValueError( f"Class {name} doesn't define {attr} class variable") if type(d[attr]) is not str: raise ValueError(f'{attr} ({type(attr)}) must be a string') if 'OUTPUT_META_DICT' not in d: raise ValueError(f"Class {name} doesn't define OUTPUT_META_DICT") if type(d['OUTPUT_META_DICT']) is not dict: raise ValueError('OUTPUT_META_DICT must be dict') type.__init__(cls, name, bases, d)
def test_for_name_does_not_exist(self): with self.assertRaises(PackageNotFoundError): Distribution.from_name('does-not-exist')
def test_retrieves_version_of_self(self): dist = Distribution.from_name('distinfo-pkg') assert isinstance(dist.version, str) assert re.match(self.version_pattern, dist.version)
def _get_modules(project_name: str) -> List[str]: dist = Distribution.from_name(project_name) return _get_modules_by_toplevel_txt(dist) or _get_modules_by_files(dist)
def list_engines(): entrypoints = ( entry_point for entry_point in Distribution.from_name("xarray").entry_points if entry_point.module == "xarray.backends") return build_engines(entrypoints)
def test_invalid_inputs_to_from_name(self, name): with self.assertRaises(Exception): Distribution.from_name(name)