def PythonEgg(glob, name=None): """Refers to pre-built Python eggs in the file system. (To instead fetch eggs in a ``pip``/``easy_install`` way, use ``python_requirement``) E.g., ``egg(name='foo', glob='foo-0.1-py2.6.egg')`` would pick up the file ``foo-0.1-py2.6.egg`` from the ``BUILD`` file's directory; targets could depend on it by name ``foo``. :param string glob: File glob pattern. :param string name: Target name; by default uses the egg's project name. """ # TODO(John Sirois): Rationalize with globs handling in ParseContext eggs = fsglob(ParseContext.path(glob)) requirements = set() for egg in eggs: if os.path.isdir(egg): metadata = PathMetadata(egg, os.path.join(egg, 'EGG-INFO')) else: metadata = EggMetadata(zipimporter(egg)) dist = Distribution.from_filename(egg, metadata=metadata) requirements.add(dist.as_requirement()) if len(requirements) > 1: raise ValueError( 'Got multiple egg versions! => {}'.format(requirements)) return PythonRequirement(str(requirements.pop()), name=name)
def dist_from_egg(egg_path): if os.path.isdir(egg_path): metadata = PathMetadata(egg_path, os.path.join(egg_path, 'EGG-INFO')) else: # Assume it's a file or an internal egg metadata = EggMetadata(zipimporter(egg_path)) return Distribution.from_filename(egg_path, metadata=metadata)
def add_egg(self, egg): """ helper for add_distribution """ metadata = EggMetadata(zipimporter(egg)) dist = Distribution.from_filename(egg, metadata) self.add_distribution(dist) self.add_requirement(dist.as_requirement(), dynamic=False, repo=None)
def egg_distribution(egg_path): # from setuptools.command.easy_install.easy_install.egg_distribution - # it's an instance method even though it doesn't need to be if os.path.isdir(egg_path): metadata = PathMetadata(egg_path, os.path.join(egg_path, 'EGG-INFO')) else: metadata = EggMetadata(zipimport.zipimporter(egg_path)) return Distribution.from_filename(egg_path, metadata=metadata)
async def get_package_requires(package, organization, team, type, cached=None): if not package.endswith('.egg') or not await is_zipfile(package): logger.error(f'{package} is not an .egg file') return None packages = [] dist = Distribution.from_filename(package, metadata=EggMetadata( zipimport.zipimporter(package))) for r in dist.requires(): name, constraint = VERSION_CHECK(str(r)).group('name', 'constraint') version_range = parse_constraint(constraint) package = await Package.find_one({ 'py_packages': name, 'organization': organization.pk, 'team': team.pk if team else None, 'package_type': type }) ver = meet_version(await package.versions, version_range) if package else None if not package or not ver: package = await Package.find_one({ 'py_packages': name, 'organization': organization.pk, 'team': None, 'package_type': type }) ver = meet_version(await package.versions, version_range) if package else None if not package or not ver: package = await Package.find_one({ 'py_packages': name, 'organization': None, 'team': None, 'package_type': type }) ver = meet_version(await package.versions, version_range) if package else None if not package or not ver: logger.error( f'package {name} not found or version requirement not meet: {version_range}' ) return None packages.append((package, ver)) if package in cached: cached[package] = cached[package].intersect(version_range) else: cached[package] = version_range return packages
def distribution_from_path(cls, location, location_base=None): """Returns a Distribution given a location. If the distribution name should be based off a different egg name than described by the location, supply location_base as an alternate name, e.g. DistributionHelper.distribution_from_path('/path/to/wrong_package_name-3.2.1', 'right_package_name-1.2.3.egg') """ location_base = location_base or os.path.basename(location) if os.path.isdir(location): metadata = PathMetadata(location, os.path.join(location, 'EGG-INFO')) else: from zipimport import zipimporter metadata = EggMetadata(zipimporter(location)) return Distribution.from_location(location, location_base, metadata=metadata)
def PythonEgg(egg_glob): eggs = glob.glob(egg_glob) requirements = set() for egg in eggs: if os.path.isdir(egg): metadata = PathMetadata(egg, os.path.join(egg, 'EGG-INFO')) else: metadata = EggMetadata(EggZipImporter(egg)) dist = Distribution.from_filename(egg, metadata=metadata) requirements.add(dist.as_requirement()) if len(requirements) > 1: raise ValueError('Got multiple egg versions! => %s' % requirements) return PythonRequirement(str(requirements.pop()))
def get_package_requires(package, organization, team, type): if not package.endswith('.egg') or not zipfile.is_zipfile(package): current_app.logger.error(f'{package} is not an .egg file') return None packages = [] dist = Distribution.from_filename(package, metadata=EggMetadata(zipimport.zipimporter(package))) for r in dist.requires(): name, compare, version = VERSION_CHECK(str(r)).group('name', 'compare', 'version') package = Package.objects(py_packages=name, organization=organization, team=team, package_type=type).first() ver = meet_version(package.versions, compare, version) if package else None if not package or not ver: package = Package.objects(py_packages=name, organization=organization, team=None, package_type=type).first() ver = meet_version(package.versions, compare, version) if package else None if not package or not ver: package = Package.objects(py_packages=name, organization=None, team=None, package_type=type).first() ver = meet_version(package.versions, compare, version) if package else None if not package or not ver: current_app.logger.error(f'package {name} not found or version requirement not meet: {compare}{version}') return None packages.append((package, ver)) return packages
def from_egg(egg_path): from twitter.common.python.importer import EggZipImporter from pkg_resources import EggMetadata, Distribution metadata = EggMetadata(EggZipImporter(egg_path)) return Distribution.from_filename(egg_path, metadata)