def _uninstall(self, target: str) -> Optional[ExitStatus]: try: distribution = importlib_metadata.distribution(target) except importlib_metadata.PackageNotFoundError: return self.fail('uninstall', target, 'package is not installed') base_dir = Path(distribution.locate_file('.')).resolve() if self.dir not in base_dir.parents: # If the package is installed somewhere else (e.g on the site packages # of the real python interpreter), than that means this package is not # installed through us. return self.fail( 'uninstall', target, 'package is not installed through httpie plugins' ' interface') files = distribution.files if files is None: return self.fail('uninstall', target, 'couldn\'t locate the package') # TODO: Consider handling failures here (e.g if it fails, # just rever the operation and leave the site-packages # in a proper shape). for file in files: with suppress(FileNotFoundError): os.unlink(distribution.locate_file(file)) metadata_path = getattr(distribution, '_path', None) if (metadata_path and metadata_path.exists() and not any(metadata_path.iterdir())): metadata_path.rmdir() self.env.stdout.write(f'Successfully uninstalled {target}\n')
def get_plugin(self, target: str) -> importlib_metadata.Distribution: with enable_plugins(self.environment.config.plugins_dir): return importlib_metadata.distribution(target)