Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from flexmock import flexmock
try:
import xmlrpclib
except ImportError:
import xmlrpc.client as xmlrpclib
from pyp2rpm.package_getters import LocalFileGetter, PypiDownloader, get_url
from pyp2rpm.exceptions import MissingUrlException, NoSuchPackageException
from pyp2rpm import settings
tests_dir = os.path.split(os.path.abspath(__file__))[0]
class TestPackageGetters(object):
client = xmlrpclib.ServerProxy(settings.PYPI_URL)
@pytest.mark.parametrize(('name', 'version', 'wheel', 'hf', 'expected_url', 'expected_md5'), [
('setuptools', '18.3.1', False, False,
'https://files.pythonhosted.org/packages/source/s/setuptools/setuptools-18.3.1.tar.gz',
'748187b93152fa60287dfb896837fd7c'),
('setuptools', '18.3.1', True, False,
'https://files.pythonhosted.org/packages/source/s/setuptools/setuptools-18.3.1-py2.py3-none-any.whl',
'a21a4d02d0bab2eac499cca72faeb076'),
('setuptools', '18.3.1', False, True,
'https://files.pythonhosted.org/packages/86/8a/c4666b05c74e840eb9b09d28f4e7ae76fc9075e8c653d0eb4d265a5b49d9/setuptools-18.3.1.tar.gz',
'748187b93152fa60287dfb896837fd7c'),
('pypandoc', '1.1.3', False, False,
'https://files.pythonhosted.org/packages/source/p/pypandoc/pypandoc-1.1.3.zip',
'771f376bf9c936a90159cd94235998c2'),
])
@pytest.mark.webtest
def save_dir_init(self, save_dir):
self.save_dir = save_dir or settings.DEFAULT_PKG_SAVE_PATH
if self.save_dir == settings.DEFAULT_PKG_SAVE_PATH:
self.save_dir += '/SOURCES'
if not os.path.exists(self.save_dir):
if self.save_dir != (settings.DEFAULT_PKG_SAVE_PATH + '/SOURCES'):
os.makedirs(self.save_dir)
else:
try:
subprocess.Popen(
'rpmdev-setuptree', stdout=subprocess.PIPE)
logger.info("Using rpmdevtools package to make rpmbuild "
"folders tree.")
except OSError:
self.save_dir = '/tmp'
# pyp2rpm can work without rpmdevtools
logger.warning("Package rpmdevtools is missing , using "
"default folder: {0} to store {1}.".format(
def save_dir_init(self, save_dir):
self.save_dir = save_dir or settings.DEFAULT_PKG_SAVE_PATH
if self.save_dir == settings.DEFAULT_PKG_SAVE_PATH:
self.save_dir += '/SOURCES'
if not os.path.exists(self.save_dir):
if self.save_dir != (settings.DEFAULT_PKG_SAVE_PATH + '/SOURCES'):
os.makedirs(self.save_dir)
else:
try:
subprocess.Popen(
'rpmdev-setuptree', stdout=subprocess.PIPE)
logger.info("Using rpmdevtools package to make rpmbuild "
"folders tree.")
except OSError:
self.save_dir = '/tmp'
# pyp2rpm can work without rpmdevtools
logger.warning("Package rpmdevtools is missing , using "
def runtime_deps(self): # install_requires
"""Returns list of runtime dependencies of the package specified in
setup.py.
Dependencies are in RPM SPECFILE format - see dependency_to_rpm()
for details, but names are already transformed according to
current distro.
Returns:
list of runtime dependencies of the package
"""
use_rich_deps = self.distro not in settings.RPM_RICH_DEP_BLACKLIST
install_requires = self.metadata['install_requires']
if self.metadata[
'entry_points'] and 'setuptools' not in install_requires:
install_requires.append('setuptools') # entrypoints
return sorted(self.name_convert_deps_list(deps_from_pyp_format(
install_requires, runtime=True, use_rich_deps=use_rich_deps)))
def _stripped_name_version(self):
"""Returns filename stripped of the suffix.
Returns:
Filename stripped of the suffix (extension).
"""
# we don't use splitext, because on "a.tar.gz" it returns ("a.tar",
# "gz")
filename = os.path.basename(self.local_file)
for archive_suffix in settings.ARCHIVE_SUFFIXES:
if filename.endswith(archive_suffix):
return filename.rstrip('{0}'.format(archive_suffix))
# if for cycle is exhausted it means no suffix was found
else:
raise exceptions.UnknownArchiveFormatException(
'Unkown archive format of file {0}.'.format(filename))
def license_from_trove(trove):
"""Finds out license from list of trove classifiers.
Args:
trove: list of trove classifiers
Returns:
Fedora name of the package license or empty string, if no licensing
information is found in trove classifiers.
"""
license = []
for classifier in trove:
if 'License' in classifier:
stripped = classifier.strip()
# if taken from EGG-INFO, begins with Classifier:
stripped = stripped[stripped.find('License'):]
if stripped in settings.TROVE_LICENSES:
license.append(settings.TROVE_LICENSES[stripped])
return ' and '.join(license)
data.update_attr('source0', self.archive.name)
return data
try:
release_data = client.release_data(self.name, self.version)
except BaseException:
logger.warning("Some kind of error while communicating with "
"client: {0}.".format(client), exc_info=True)
return data
try:
url, md5_digest = get_url(client, self.name, self.version)
except exc.MissingUrlException:
url, md5_digest = ('FAILED TO EXTRACT FROM PYPI',
'FAILED TO EXTRACT FROM PYPI')
data_dict = {'source0': url, 'md5': md5_digest}
for data_field in settings.PYPI_USABLE_DATA:
data_dict[data_field] = release_data.get(data_field, '')
# we usually get better license representation from trove classifiers
data_dict["license"] = license_from_trove(release_data.get(
'classifiers', ''))
data.set_from(data_dict, update=True)
return data
return inner