def get_metadata(path, pkg_name=None): if pkg_name: pkg_name = packaging.utils.canonicalize_name(pkg_name) egg_dir = next(iter(find_egginfo(path, pkg_name=pkg_name)), None) if egg_dir is not None: import pkg_resources egg_dir = os.path.abspath(egg_dir.path) base_dir = os.path.dirname(egg_dir) path_metadata = pkg_resources.PathMetadata(base_dir, egg_dir) dist = next( iter(pkg_resources.distributions_from_metadata(path_metadata.egg_info)), None, ) if dist: requires = dist.requires() dep_map = dist._build_dep_map() deps = [] for k in dep_map.keys(): if k is None: deps.extend(dep_map.get(k)) continue else: _deps = dep_map.get(k) k = k.replace(":", "; ") _deps = [ pkg_resources.Requirement.parse("{0}{1}".format(str(req), k)) for req in _deps ] deps.extend(_deps) return { "name": dist.project_name, "version": dist.version, "requires": requires, }
def get_metadata(path, pkg_name=None): if pkg_name: pkg_name = packaging.utils.canonicalize_name(pkg_name) egg_dir = next(iter(find_egginfo(path, pkg_name=pkg_name)), None) if egg_dir is not None: import pkg_resources egg_dir = os.path.abspath(egg_dir.path) base_dir = os.path.dirname(egg_dir) path_metadata = pkg_resources.PathMetadata(base_dir, egg_dir) dist = next( iter( pkg_resources.distributions_from_metadata( path_metadata.egg_info)), None, ) if dist: try: requires = dist.requires() except exception: requires = [] try: dep_map = dist._build_dep_map() except Exception: dep_map = {} deps = [] extras = {} for k in dep_map.keys(): if k is None: deps.extend(dep_map.get(k)) continue else: extra = None _deps = dep_map.get(k) if k.startswith(":python_version"): marker = k.replace(":", "; ") else: marker = "" extra = "{0}".format(k) _deps = [ pkg_resources.Requirement.parse("{0}{1}".format( str(req), marker)) for req in _deps ] if extra: extras[extra] = _deps else: deps.extend(_deps) return { "name": dist.project_name, "version": dist.version, "requires": requires, "extras": extras }
def from_directory(site_packages): pkgs = [] for dir in os.listdir(site_packages): if dir.endswith('.dist-info'): dist_info = os.path.join(site_packages, dir) dists = pkg_resources.distributions_from_metadata(dist_info) for dist in dists: pkg = Package.from_distribution(dist) pkg.deps = sorted( (Dependency(x, pkg) for x in dist.requires()), key=lambda x: str(x)) pkgs.append(pkg) return PackageGraph(sorted(pkgs))
def make_distribution_no_version(tmpdir, basename): """ Create a distribution directory with no file containing the version. """ # Convert the LocalPath object to a string before joining. dist_dir = os.path.join(str(tmpdir), basename) os.mkdir(dist_dir) # Make the directory non-empty so distributions_from_metadata() # will detect it and yield it. touch_file(os.path.join(dist_dir, 'temp.txt')) dists = list(pkg_resources.distributions_from_metadata(dist_dir)) assert len(dists) == 1 dist, = dists return dist, dist_dir
def make_distribution_no_version(tmpdir, basename): """ Create a distribution directory with no file containing the version. """ # Convert the LocalPath object to a string before joining. dist_dir = os.path.join(str(tmpdir), basename) os.mkdir(dist_dir) # Make the directory non-empty so distributions_from_metadata() # will detect it and yield it. touch_file(os.path.join(dist_dir, 'temp.txt')) dists = list(pkg_resources.distributions_from_metadata(dist_dir)) assert len(dists) == 1 dist, = dists return dist, dist_dir
def make_test_distribution(metadata_path, metadata): """ Make a test Distribution object, and return it. :param metadata_path: the path to the metadata file that should be created. This should be inside a distribution directory that should also be created. For example, an argument value might end with "<project>.dist-info/METADATA". :param metadata: the desired contents of the metadata file, as bytes. """ dist_dir = os.path.dirname(metadata_path) os.mkdir(dist_dir) with open(metadata_path, 'wb') as f: f.write(metadata) dists = list(pkg_resources.distributions_from_metadata(dist_dir)) dist, = dists return dist
def make_distribution_no_version(tmpdir, basename): """ Create a distribution directory with no file containing the version. """ dist_dir = tmpdir / basename dist_dir.ensure_dir() # Make the directory non-empty so distributions_from_metadata() # will detect it and yield it. dist_dir.join('temp.txt').ensure() if sys.version_info < (3, 6): dist_dir = str(dist_dir) dists = list(pkg_resources.distributions_from_metadata(dist_dir)) assert len(dists) == 1 dist, = dists return dist, dist_dir
def get_metadata(path, pkg_name=None, metadata_type=None): # type: (S, Optional[S], Optional[S]) -> Dict[S, Union[S, List[RequirementType], Dict[S, RequirementType]]] metadata_dirs = [] wheel_allowed = metadata_type == "wheel" or metadata_type is None egg_allowed = metadata_type == "egg" or metadata_type is None egg_dir = next(iter(find_egginfo(path, pkg_name=pkg_name)), None) dist_dir = next(iter(find_distinfo(path, pkg_name=pkg_name)), None) if dist_dir and wheel_allowed: metadata_dirs.append(dist_dir) if egg_dir and egg_allowed: metadata_dirs.append(egg_dir) matched_dir = next(iter(d for d in metadata_dirs if d is not None), None) metadata_dir = None base_dir = None if matched_dir is not None: import pkg_resources metadata_dir = os.path.abspath(matched_dir.path) base_dir = os.path.dirname(metadata_dir) dist = None distinfo_dist = None egg_dist = None if wheel_allowed and dist_dir is not None: distinfo_dist = next( iter(pkg_resources.find_distributions(base_dir)), None) if egg_allowed and egg_dir is not None: path_metadata = pkg_resources.PathMetadata(base_dir, metadata_dir) egg_dist = next( iter( pkg_resources.distributions_from_metadata( path_metadata.egg_info)), None, ) dist = next( iter(d for d in (distinfo_dist, egg_dist) if d is not None), None) if dist is not None: return get_metadata_from_dist(dist) return {}