def test_requires_dist(self): fields = {'name': 'project', 'version': '1.0', 'requires_dist': ['other', 'another (==1.0)']} metadata = Metadata(mapping=fields) self.assertEqual(metadata['Requires-Dist'], ['other', 'another (==1.0)']) self.assertEqual(metadata['Metadata-Version'], '1.2') self.assertNotIn('Provides', metadata) self.assertEqual(metadata['Requires-Dist'], ['other', 'another (==1.0)']) self.assertNotIn('Obsoletes', metadata) # make sure write_file uses one RFC 822 header per item fp = StringIO() metadata.write_file(fp) lines = fp.getvalue().split('\n') self.assertIn('Requires-Dist: other', lines) self.assertIn('Requires-Dist: another (==1.0)', lines) # test warnings for invalid version constraints # XXX this would cause no warnings if we used update (or the mapping # argument of the constructor), see comment in Metadata.update metadata = Metadata() metadata['Requires-Dist'] = 'Funky (Groovie)' metadata['Requires-Python'] = '1-4' self.assertEqual(len(self.get_logs()), 2) # test multiple version matches metadata = Metadata() # XXX check PEP and see if 3 == 3.0 metadata['Requires-Python'] = '>=2.6, <3.0' metadata['Requires-Dist'] = ['Foo (>=2.6, <3.0)'] self.assertEqual(self.get_logs(), [])
def test_check_name_strict(self): metadata = Metadata() metadata['Version'] = '1.0' metadata['Home-page'] = 'http://pypi.python.org' metadata['Author'] = 'Monty Python' metadata.docutils_support = False self.assertRaises(MetadataMissingError, metadata.check, strict=True)
def test_upload(self): "Test upload" self.check_pypi_server_available() self.check_testdist_available() if self.run_test_server: self.remove_package(self.dist_project, self.dist_version) d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() md.update(self.get_index_metadata(data)) #import pdb; pdb.set_trace() self.index.gpg_home = os.path.join(HERE, 'keys') try: zip_name = os.path.join(HERE, '%s.zip' % self.testdir) self.assertRaises(DistlibException, self.index.upload_file, md, 'random-' + zip_name, 'Test User', 'tuser') response = self.index.upload_file(md, zip_name, 'Test User', 'tuser') self.assertEqual(response.code, 200) if self.run_test_server: fn = os.path.join(HERE, 'packages', os.path.basename(zip_name)) self.assertTrue(os.path.exists(fn)) except HTTPError as e: # Treat as success if it already exists if e.getcode() != 400 or 'already exists' not in e.msg: raise
def test_write(self): dfn = self.temp_filename() # Read legacy, write new sfn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', LEGACY_METADATA_FILENAME) md = Metadata(path=sfn) md.write(path=dfn) with codecs.open(dfn, 'r', 'utf-8') as f: data = json.load(f) self.assertEqual( data, { 'metadata_version': '2.0', 'generator': 'distlib (%s)' % __version__, 'name': 'choxie', 'version': '2.0.0.9', 'license': 'BSD', 'summary': 'Chocolate with a kick!', 'description': 'Chocolate with a longer kick!', 'provides': ['truffles (1.0)', 'choxie (2.0.0.9)'], 'run_requires': [{ 'requires': ['towel-stuff (0.1)', 'nut'] }], 'keywords': [], }) # Write legacy, compare with original md.write(path=dfn, legacy=True) nmd = Metadata(path=dfn) d1 = md.todict() d2 = nmd.todict() self.assertEqual(d1, d2)
def test_check_homepage(self): metadata = Metadata() metadata['Version'] = '1.0' metadata['Name'] = 'vimpdb' metadata['Author'] = 'Monty Python' metadata.docutils_support = False missing, warnings = metadata.check() self.assertEqual(missing, ['Home-page'])
def test_check_author(self): metadata = Metadata() metadata['Version'] = '1.0' metadata['Name'] = 'vimpdb' metadata['Home-page'] = 'http://pypi.python.org' metadata.docutils_support = False missing, warnings = metadata.check() self.assertEqual(missing, ['Author'])
def test_fullname(self): md = Metadata() md['Name'] = 'a b c' md['Version'] = '1 0 0' s = md.get_fullname() self.assertEqual(s, 'a b c-1 0 0') s = md.get_fullname(True) self.assertEqual(s, 'a-b-c-1.0.0')
def test_description_invalid_rst(self): # make sure bad rst is well handled in the description attribute metadata = Metadata() description = ':funkie:`str`' # mimic Sphinx-specific markup metadata['description'] = description missing, warnings = metadata.check(restructuredtext=True) warning = warnings[0][1] self.assertIn('funkie', warning)
def process_egg(egg): logger.debug(egg) pkginfo = os.path.join(egg, 'PKG-INFO') toplevel = os.path.join(egg, 'top_level.txt') if os.path.exists(pkginfo): metadata = Metadata(path=pkginfo) logger.debug(metadata.todict()) return metadata
def test_check_matchers(self): metadata = Metadata() metadata['Version'] = 'rr' metadata['Name'] = 'vimpdb' metadata['Home-page'] = 'http://pypi.python.org' metadata['Author'] = 'Monty Python' metadata['Requires-dist'] = ['Foo (a)'] metadata['Obsoletes-dist'] = ['Foo (a)'] metadata['Provides-dist'] = ['Foo (a)'] missing, warnings = metadata.check() self.assertEqual(len(warnings), 4)
def test_register(self): "Test registration" self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() self.assertRaises(MetadataMissingError, self.index.register, md) md['Name'] = self.dist_project self.assertRaises(MetadataMissingError, self.index.register, md) md.update(self.get_index_metadata(data)) response = self.index.register(md) self.assertEqual(response.code, 200)
def test_add_requirements(self): md = Metadata() md.name = 'bar' md.version = '0.5' md.add_requirements(['foo (0.1.2)']) self.assertEqual(md.run_requires, [{'requires': ['foo (0.1.2)']}]) fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', LEGACY_METADATA_FILENAME) md = Metadata(path=fn) md.add_requirements(['foo (0.1.2)']) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut', 'foo (0.1.2)']))
def test_register(self): "Test registration" self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() self.assertRaises(MetadataMissingError, self.index.register, md) md.name = self.dist_project self.assertRaises(MetadataMissingError, self.index.register, md) md.version = data['version'] md.summary = data['summary'] response = self.index.register(md) self.assertEqual(response.code, 200)
def test_instantiation(self): PKG_INFO = os.path.join(HERE, 'PKG-INFO') f = codecs.open(PKG_INFO, 'r', encoding='utf-8') try: contents = f.read() finally: f.close() fp = StringIO(contents) m = Metadata() self.assertRaises(MetadataUnrecognizedVersionError, m.items) m = Metadata(PKG_INFO) self.assertEqual(len(m.items()), 22) m = Metadata(fileobj=fp) self.assertEqual(len(m.items()), 22) m = Metadata(mapping=dict(name='Test', version='1.0')) self.assertEqual(len(m.items()), 17) d = dict(m.items()) self.assertRaises(TypeError, Metadata, PKG_INFO, fileobj=fp) self.assertRaises(TypeError, Metadata, PKG_INFO, mapping=d) self.assertRaises(TypeError, Metadata, fileobj=fp, mapping=d) self.assertRaises(TypeError, Metadata, PKG_INFO, mapping=m, fileobj=fp)
def test_register(self): "Test registration" self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() self.assertRaises(MetadataMissingError, self.index.register, md) md.name = self.dist_project self.assertRaises(MetadataMissingError, self.index.register, md) md.version = data['version'] md.summary = data['summary'] response = self.index.register(md) self.assertEqual(response.code, 200)
def test_write(self): dfn = self.temp_filename() # Read legacy, write new sfn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', 'METADATA') md = Metadata(path=sfn) md.write(path=dfn) with codecs.open(dfn, 'r', 'utf-8') as f: data = json.load(f) self.assertEqual(data, { 'metadata_version': '2.0', 'generator': 'distlib (%s)' % __version__, 'name': 'choxie', 'version': '2.0.0.9', 'license': 'BSD', 'summary': 'Chocolate with a kick!', 'description': 'Chocolate with a longer kick!', 'provides': ['truffles (1.0)', 'choxie (2.0.0.9)'], 'run_requires': [{'requires': ['towel-stuff (0.1)', 'nut']}], 'keywords': [], }) # Write legacy, compare with original md.write(path=dfn, legacy=True) nmd = Metadata(path=dfn) d1 = md.todict() d2 = nmd.todict() self.assertEqual(d1, d2)
def test_description_folding(self): # make sure the indentation is preserved out = StringIO() desc = dedent("""\ example:: We start here and continue here and end here. """) metadata = Metadata() metadata['description'] = desc metadata.write_file(out) folded_desc = desc.replace('\n', '\n' + (7 * ' ') + '|') self.assertIn(folded_desc, out.getvalue())
def test_upload_documentation(self): "Test upload of documentation" raise unittest.SkipTest('Skipped, as pythonhosted.org is being ' 'de-emphasised and this functionality may ' 'no longer be available') self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata(mapping=data) d = os.path.join(d, 'doc') # Non-existent directory self.assertRaises(DistlibException, self.index.upload_documentation, md, d + '-random') # Directory with no index.html self.assertRaises(DistlibException, self.index.upload_documentation, md, HERE) response = self.index.upload_documentation(md, d) self.assertEqual(response.code, 200) if not self.run_test_server: url = 'http://packages.python.org/%s/' % self.dist_project response = urlopen(url) self.assertEqual(response.code, 200) data = response.read() expected = b'This is dummy documentation' self.assertIn(expected, data)
def test_metadata_read_write(self): PKG_INFO = os.path.join(HERE, 'PKG-INFO') metadata = Metadata(PKG_INFO) out = StringIO() metadata.write_file(out) out.seek(0) res = Metadata() res.read_file(out) self.assertEqual(metadata.values(), res.values())
def convert_egg_info(libdir, prefix): files = os.listdir(libdir) ei = list(filter(lambda d: d.endswith('.egg-info'), files))[0] olddn = os.path.join(libdir, ei) di = EGG_INFO_RE.sub('.dist-info', ei) newdn = os.path.join(libdir, di) os.rename(olddn, newdn) files = os.listdir(newdn) for oldfn in files: pn = os.path.join(newdn, oldfn) if oldfn == 'PKG-INFO': md = Metadata(path=pn) mn = os.path.join(newdn, METADATA_FILENAME) md.write(mn) os.remove(pn) manifest = Manifest(os.path.dirname(libdir)) manifest.findall() dp = DistributionPath([libdir]) dist = next(dp.get_distributions()) dist.write_installed_files(manifest.allfiles, prefix)
def convert_egg_info(libdir, prefix): files = os.listdir(libdir) ei = list(filter(lambda d: d.endswith('.egg-info'), files))[0] olddn = os.path.join(libdir, ei) di = EGG_INFO_RE.sub('.dist-info', ei) newdn = os.path.join(libdir, di) os.rename(olddn, newdn) files = os.listdir(newdn) for oldfn in files: pn = os.path.join(newdn, oldfn) if oldfn == 'PKG-INFO': md = Metadata(path=pn) mn = os.path.join(newdn, METADATA_FILENAME) md.write(mn) os.remove(pn) manifest = Manifest(os.path.dirname(libdir)) manifest.findall() dp = DistributionPath([libdir]) dist = next(dp.get_distributions()) dist.write_installed_files(manifest.allfiles, prefix)
def test_add_requirements(self): md = Metadata() md.name = 'bar' md.version = '0.5' md.add_requirements(['foo (0.1.2)']) self.assertEqual(md.run_requires, [{ 'requires': ['foo (0.1.2)']}]) fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', 'METADATA') md = Metadata(path=fn) md.add_requirements(['foo (0.1.2)']) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut', 'foo (0.1.2)']))
def test_project_url(self): metadata = Metadata() metadata['Project-URL'] = [('one', 'http://ok')] self.assertEqual(metadata['Project-URL'], [('one', 'http://ok')]) metadata.set_metadata_version() self.assertEqual(metadata['Metadata-Version'], '1.2') # make sure this particular field is handled properly when written fp = StringIO() metadata.write_file(fp) self.assertIn('Project-URL: one,http://ok', fp.getvalue().split('\n')) fp.seek(0) metadata = Metadata() metadata.read_file(fp) self.assertEqual(metadata['Project-Url'], [('one', 'http://ok')])
def test_upload_documentation(self): "Test upload of documentation" self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() md.update(self.get_index_metadata(data)) d = os.path.join(d, 'doc') # Non-existent directory self.assertRaises(DistlibException, self.index.upload_documentation, md, d+'-random') # Directory with no index.html self.assertRaises(DistlibException, self.index.upload_documentation, md, HERE) response = self.index.upload_documentation(md, d) self.assertEqual(response.code, 200) if not self.run_test_server: url = 'http://packages.python.org/%s/' % self.dist_project response = urlopen(url) self.assertEqual(response.code, 200) data = response.read() expected = b'This is dummy documentation' self.assertIn(expected, data)
def test_read_metadata(self): fields = {'name': 'project', 'version': '1.0', 'description': 'desc', 'summary': 'xxx', 'download_url': 'http://example.com', 'keywords': ['one', 'two'], 'requires_dist': ['foo']} metadata = Metadata(mapping=fields) PKG_INFO = StringIO() metadata.write_file(PKG_INFO) PKG_INFO.seek(0) metadata = Metadata(fileobj=PKG_INFO) self.assertEqual(metadata['name'], 'project') self.assertEqual(metadata['version'], '1.0') self.assertEqual(metadata['summary'], 'xxx') self.assertEqual(metadata['download_url'], 'http://example.com') self.assertEqual(metadata['keywords'], ['one', 'two']) self.assertEqual(metadata['platform'], []) self.assertEqual(metadata['obsoletes'], []) self.assertEqual(metadata['requires-dist'], ['foo'])
def _make_distributions(metadata_list, locator_url): from distlib.database import Distribution from distlib.metadata import Metadata distributions = [] url = locator_url + "/packages/" for md in metadata_list: try: wheel = md["wheel"] except KeyError: pass else: md["source_url"] = url + wheel del md["wheel"] distributions.append(Distribution(Metadata(mapping=md))) return distributions
def convert_egg_info(libdir, prefix, options): files = os.listdir(libdir) ei = list(filter(lambda d: d.endswith('.egg-info'), files))[0] olddn = os.path.join(libdir, ei) di = EGG_INFO_RE.sub('.dist-info', ei) newdn = os.path.join(libdir, di) os.rename(olddn, newdn) if options.compatible: renames = {} else: renames = { 'entry_points.txt': 'EXPORTS', } excludes = set([ 'SOURCES.txt', # of no interest in/post WHEEL 'installed-files.txt', # replaced by RECORD, so not needed 'requires.txt', # added to METADATA, so not needed 'PKG-INFO', # replaced by METADATA 'not-zip-safe', # not applicable ]) files = os.listdir(newdn) metadata = mdname = reqts = None for oldfn in files: pn = os.path.join(newdn, oldfn) if oldfn in renames: os.rename(pn, os.path.join(newdn, renames[oldfn])) else: if oldfn == 'requires.txt': with open(pn, 'r') as f: reqts = get_requirements(f.read()) elif oldfn == 'PKG-INFO': metadata = Metadata(path=pn) pd = get_package_data(metadata.name, metadata.version) metadata = Metadata(mapping=pd['index-metadata']) mdname = os.path.join(newdn, 'pydist.json') if oldfn in excludes or not options.compatible: os.remove(pn) if metadata: # Use Metadata 1.2 or later metadata.provides += ['%s (%s)' % (metadata.name, metadata.version)] # Update if not set up by get_package_data if reqts and not metadata.run_requires: metadata.dependencies = reqts metadata.write(path=mdname) manifest = Manifest(os.path.dirname(libdir)) manifest.findall() paths = manifest.allfiles dp = DistributionPath([libdir]) dist = next(dp.get_distributions()) dist.write_installed_files(paths, prefix)
def make_metadata(project_dir): md = Metadata() md.name = input('name >>> ') md.version = input('version >>> ') md.summary = input('summary >>> ') dist_path = os.path.join(project_dir, 'dist-info') if not os.path.exists(dist_path): os.makedirs(dist_path) md.write(path=os.path.join(dist_path, 'pydist.json')) return md
def test_get_file(self): # Create a fake dist temp_site_packages = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_site_packages) dist_name = 'test' dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info') os.mkdir(dist_info) metadata_path = os.path.join(dist_info, 'pydist.json') resources_path = os.path.join(dist_info, 'RESOURCES') md = Metadata() md.name = 'test' md.version = '0.1' md.summary = 'test' md.write(path=metadata_path) test_path = 'test.cfg' fd, test_resource_path = tempfile.mkstemp() os.close(fd) self.addCleanup(os.remove, test_resource_path) fp = open(test_resource_path, 'w') try: fp.write('Config') finally: fp.close() fp = open(resources_path, 'w') try: fp.write('%s,%s' % (test_path, test_resource_path)) finally: fp.close() # Add fake site-packages to sys.path to retrieve fake dist self.addCleanup(sys.path.remove, temp_site_packages) sys.path.insert(0, temp_site_packages) # Try to retrieve resources paths and files d = DistributionPath() self.assertEqual(d.get_file_path(dist_name, test_path), test_resource_path) self.assertRaises(KeyError, d.get_file_path, dist_name, 'i-dont-exist')
def test_description(self): content = self.get_file_contents('PKG-INFO') metadata = Metadata() metadata.read_file(StringIO(content)) # see if we can read the description now DESC = os.path.join(HERE, 'LONG_DESC.txt') f = open(DESC) try: wanted = f.read() finally: f.close() self.assertEqual(wanted, metadata['Description']) # save the file somewhere and make sure we can read it back out = StringIO() metadata.write_file(out) out.seek(0) out.seek(0) metadata = Metadata() metadata.read_file(out) self.assertEqual(wanted, metadata['Description'])
def test_metadata_markers(self): # see if we can be platform-aware content = self.get_file_contents('PKG-INFO') metadata = Metadata(platform_dependent=True) metadata.read_file(StringIO(content)) self.assertEqual(metadata['Requires-Dist'], ['bar']) metadata['Name'] = "baz; sys.platform == 'blah'" # FIXME is None or 'UNKNOWN' correct here? # where is that documented? self.assertEqual(metadata['Name'], None) # test with context context = {'sys.platform': 'okook'} metadata = Metadata(platform_dependent=True, execution_context=context) metadata.read_file(StringIO(content)) self.assertEqual(metadata['Requires-Dist'], ['foo'])
def test_get_file(self): # Create a fake dist temp_site_packages = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_site_packages) dist_name = 'test' dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info') os.mkdir(dist_info) metadata_path = os.path.join(dist_info, 'pydist.json') resources_path = os.path.join(dist_info, 'RESOURCES') md = Metadata() md.name = 'test' md.version = '0.1' md.summary = 'test' md.write(path=metadata_path) test_path = 'test.cfg' fd, test_resource_path = tempfile.mkstemp() os.close(fd) self.addCleanup(os.remove, test_resource_path) fp = open(test_resource_path, 'w') try: fp.write('Config') finally: fp.close() fp = open(resources_path, 'w') try: fp.write('%s,%s' % (test_path, test_resource_path)) finally: fp.close() # Add fake site-packages to sys.path to retrieve fake dist self.addCleanup(sys.path.remove, temp_site_packages) sys.path.insert(0, temp_site_packages) # Try to retrieve resources paths and files d = DistributionPath() self.assertEqual(d.get_file_path(dist_name, test_path), test_resource_path) self.assertRaises(KeyError, d.get_file_path, dist_name, 'i-dont-exist')
def test_upload(self): "Test upload" self.check_pypi_server_available() self.check_testdist_available() if self.run_test_server: self.remove_package(self.dist_project, self.dist_version) d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata(mapping=data) self.index.gpg_home = os.path.join(HERE, 'keys') try: zip_name = os.path.join(HERE, '%s.zip' % self.testdir) self.assertRaises(DistlibException, self.index.upload_file, md, 'random-' + zip_name, 'Test User', 'tuser') response = self.index.upload_file(md, zip_name, 'Test User', 'tuser') self.assertEqual(response.code, 200) if self.run_test_server: fn = os.path.join(HERE, 'packages', os.path.basename(zip_name)) self.assertTrue(os.path.exists(fn)) except HTTPError as e: # Treat as success if it already exists if e.getcode() != 400 or 'already exists' not in e.msg: raise
def test_get_file(self): # Create a fake dist temp_site_packages = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_site_packages) dist_name = "test" dist_info = os.path.join(temp_site_packages, "test-0.1.dist-info") os.mkdir(dist_info) metadata_path = os.path.join(dist_info, "pydist.json") resources_path = os.path.join(dist_info, "RESOURCES") md = Metadata() md.name = "test" md.version = "0.1" md.summary = "test" md.write(path=metadata_path) test_path = "test.cfg" fd, test_resource_path = tempfile.mkstemp() os.close(fd) self.addCleanup(os.remove, test_resource_path) fp = open(test_resource_path, "w") try: fp.write("Config") finally: fp.close() fp = open(resources_path, "w") try: fp.write("%s,%s" % (test_path, test_resource_path)) finally: fp.close() # Add fake site-packages to sys.path to retrieve fake dist self.addCleanup(sys.path.remove, temp_site_packages) sys.path.insert(0, temp_site_packages) # Try to retrieve resources paths and files d = DistributionPath() self.assertEqual(d.get_file_path(dist_name, test_path), test_resource_path) self.assertRaises(KeyError, d.get_file_path, dist_name, "i-dont-exist")
def wheel_modifier_ver(self, path_map): mdpath = path_map["dummy-0.1.dist-info/pydist.json"] md = Metadata(path=mdpath) md.version = "0.1+123" md.write(path=mdpath) return True
def wheel_modifier(self, path_map): mdpath = path_map["dummy-0.1.dist-info/pydist.json"] md = Metadata(path=mdpath) md.add_requirements(["numpy"]) md.write(path=mdpath) return True
def wheel_modifier(self, path_map): mdpath = path_map['dummy-0.1.dist-info/pydist.json'] md = Metadata(path=mdpath) md.add_requirements(['numpy']) md.write(path=mdpath) return True
def test_requirements(self): fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) self.assertEqual(md.meta_requires, [{'requires': ['bar (1.0)']}]) r = md.get_requirements(md.run_requires) self.assertEqual(r, ['foo']) r = md.get_requirements(md.run_requires, extras=['certs']) self.assertEqual(r, ['foo', 'certifi (0.0.8)']) r = md.get_requirements(md.run_requires, extras=['certs', 'ssl']) if sys.platform != 'win32': self.assertEqual(r, ['foo', 'certifi (0.0.8)']) else: self.assertEqual( set(r), set(['foo', 'certifi (0.0.8)', 'wincertstore (0.1)'])) for ver in ('2.5', '2.4'): env = {'python_version': ver} r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) if sys.platform != 'win32': self.assertEqual(set(r), set(['foo', 'certifi (0.0.8)', 'ssl (1.16)'])) elif ver == '2.4': self.assertEqual( set(r), set([ 'certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)', 'foo', 'ctypes (1.0.2)' ])) else: self.assertEqual( set(r), set([ 'certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)', 'foo' ])) env['sys_platform'] = 'win32' r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) self.assertEqual( set(r), set([ 'foo', 'certifi (0.0.8)', 'ssl (1.16)', 'ctypes (1.0.2)', 'wincertstore (0.1)' ])) env['python_version'] = '2.5' r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) self.assertEqual( set(r), set(['foo', 'certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)'])) r = md.get_requirements(md.run_requires, extras=[':test:']) self.assertEqual(r, ['foo', 'nose']) r = md.get_requirements(md.run_requires, extras=[':test:', 'udp']) self.assertEqual(set(r), set(['foo', 'nose', 'nose-udp'])) self.assertEqual( md.dependencies, { 'provides': ['foobar (0.1)'], 'meta_requires': [{ 'requires': ['bar (1.0)'] }], 'extras': ['ssl', 'certs'], 'build_requires': [], 'test_requires': [{ 'requires': ['nose'], }, { 'requires': ['nose-udp'], 'extra': 'udp', }], 'run_requires': [{ 'requires': ['foo'] }, { 'requires': ['certifi (0.0.8)'], 'extra': 'certs', }, { 'requires': ['wincertstore (0.1)'], 'extra': 'ssl', 'environment': "sys_platform=='win32'", }, { 'requires': ['ctypes (1.0.2)'], 'extra': 'ssl', 'environment': "sys_platform=='win32' and " "python_version=='2.4'", }, { 'requires': ['ssl (1.16)'], 'extra': 'ssl', 'environment': "python_version in '2.4, 2.5'", }] })
def wheel_modifier_ver(self, path_map): mdpath = path_map['dummy-0.1.dist-info/pydist.json'] md = Metadata(path=mdpath) md.version = '0.1+123' md.write(path=mdpath) return True
def test_init(self): "Test initialisation" md = Metadata() self.assertIsNone(md._legacy) self.assertRaises(MetadataMissingError, md.validate) md.name = 'dummy' self.assertRaises(MetadataMissingError, md.validate) md.version = '0.1' self.assertRaises(MetadataMissingError, md.validate) md.summary = 'Summary' md.validate() self.assertEqual(md.name, 'dummy') self.assertEqual(md.version, '0.1') # Initialise from mapping md = Metadata(mapping={ 'metadata_version': '2.0', 'name': 'foo', 'version': '0.3.4', 'summary': 'Summary', }) md.validate() self.assertEqual(md.name, 'foo') self.assertEqual(md.version, '0.3.4') self.assertEqual(md.run_requires, []) self.assertEqual(md.meta_requires, []) self.assertEqual(md.provides, ['foo (0.3.4)']) # Initialise from legacy metadata fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', 'METADATA') md = Metadata(path=fn) md.validate() self.assertIsNotNone(md._legacy) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut'])) self.assertEqual(md.metadata_version, '1.2') self.assertEqual(md.version, '2.0.0.9') self.assertEqual(md.meta_requires, []) self.assertEqual(set(md.provides), set(['choxie (2.0.0.9)', 'truffles (1.0)'])) # Initialise from new metadata fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNone(md._legacy) self.assertEqual(md.metadata_version, '2.0') self.assertEqual(md.name, 'foobar') self.assertEqual(md.version, '0.1') self.assertEqual(md.provides, ['foobar (0.1)'])
def test_valid(self): """ Tests to check that missing and invalid metadata is caught. """ md = Metadata() self.assertRaises(MetadataMissingError, md.validate) try: md.name = 'Foo Bar' except MetadataInvalidError: pass md.name = 'foo_bar' # Name now OK, but version and summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.version = '1.0a' except MetadataInvalidError: pass md.version = '1.0' # Name and version now OK, but summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.summary = '' except MetadataInvalidError: pass try: md.summary = ' ' * 2048 except MetadataInvalidError: pass md.summary = ' ' * 2047 md.validate() md.summary = ' ' md.validate()
def wheel_modifier(self, path_map): mdpath = path_map['dummy-0.1.dist-info/%s' % LEGACY_METADATA_FILENAME] md = Metadata(path=mdpath) md.add_requirements(['numpy']) md.write(path=mdpath, legacy=True) return True
def test_valid(self): """ Tests to check that missing and invalid metadata is caught. """ md = Metadata() self.assertRaises(MetadataMissingError, md.validate) try: md.name = 'Foo Bar' except MetadataInvalidError: pass md.name = 'foo_bar' # Name now OK, but version and summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.version = '1.0a' except MetadataInvalidError: pass md.version = '1.0' # Name and version now OK, but summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.summary = '' except MetadataInvalidError: pass try: md.summary = ' ' * 2048 except MetadataInvalidError: pass md.summary = ' ' * 2047 md.validate() md.summary = ' ' md.validate()
def wheel_modifier_ver(self, path_map): mdpath = path_map['dummy-0.1.dist-info/%s' % LEGACY_METADATA_FILENAME] md = Metadata(path=mdpath) md.version = '0.1+123' md.write(path=mdpath, legacy=True) return True
def test_requirements(self): fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) self.assertEqual(md.meta_requires, [{'requires': ['bar (1.0)']}]) r = md.get_requirements(md.run_requires) self.assertEqual(r, ['foo']) r = md.get_requirements(md.run_requires, extras=['certs']) self.assertEqual(r, ['foo', 'certifi (0.0.8)']) r = md.get_requirements(md.run_requires, extras=['certs', 'ssl']) if sys.platform != 'win32': self.assertEqual(r, ['foo', 'certifi (0.0.8)']) else: self.assertEqual(set(r), set(['foo', 'certifi (0.0.8)', 'wincertstore (0.1)'])) for ver in ('2.5', '2.4'): env = {'python_version': ver} r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) if sys.platform != 'win32': self.assertEqual(set(r), set(['foo', 'certifi (0.0.8)', 'ssl (1.16)'])) elif ver == '2.4': self.assertEqual(set(r), set(['certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)', 'foo', 'ctypes (1.0.2)'])) else: self.assertEqual(set(r), set(['certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)', 'foo'])) env['sys_platform'] = 'win32' r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) self.assertEqual(set(r), set(['foo', 'certifi (0.0.8)', 'ssl (1.16)', 'ctypes (1.0.2)', 'wincertstore (0.1)'])) env['python_version'] = '2.5' r = md.get_requirements(md.run_requires, extras=['certs', 'ssl'], env=env) self.assertEqual(set(r), set(['foo', 'certifi (0.0.8)', 'ssl (1.16)', 'wincertstore (0.1)'])) r = md.get_requirements(md.run_requires, extras=[':test:']) self.assertEqual(r, ['foo', 'nose']) r = md.get_requirements(md.run_requires, extras=[':test:', 'udp']) self.assertEqual(set(r), set(['foo', 'nose', 'nose-udp'])) self.assertEqual(md.dependencies, { 'provides': ['foobar (0.1)'], 'meta_requires': [ { 'requires': ['bar (1.0)'] } ], 'extras': ['ssl', 'certs'], 'build_requires': [], 'test_requires': [ { 'requires': ['nose'], }, { 'requires': ['nose-udp'], 'extra': 'udp', } ], 'run_requires': [ { 'requires': ['foo'] }, { 'requires': ['certifi (0.0.8)'], 'extra': 'certs', }, { 'requires': ['wincertstore (0.1)'], 'extra': 'ssl', 'environment': "sys_platform=='win32'", }, { 'requires': ['ctypes (1.0.2)'], 'extra': 'ssl', 'environment': "sys_platform=='win32' and " "python_version=='2.4'", }, { 'requires': ['ssl (1.16)'], 'extra': 'ssl', 'environment': "python_version in '2.4, 2.5'", } ] })
def test_init(self): "Test initialisation" md = Metadata() self.assertIsNone(md._legacy) self.assertRaises(MetadataMissingError, md.validate) md.name = 'dummy' self.assertRaises(MetadataMissingError, md.validate) md.version = '0.1' self.assertRaises(MetadataMissingError, md.validate) md.summary = 'Summary' md.validate() self.assertEqual(md.name, 'dummy') self.assertEqual(md.version, '0.1') # Initialise from mapping md = Metadata( mapping={ 'metadata_version': '2.0', 'name': 'foo', 'version': '0.3.4', 'summary': 'Summary', }) md.validate() self.assertEqual(md.name, 'foo') self.assertEqual(md.version, '0.3.4') self.assertEqual(md.run_requires, []) self.assertEqual(md.meta_requires, []) self.assertEqual(md.provides, ['foo (0.3.4)']) # Initialise from legacy metadata fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', LEGACY_METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNotNone(md._legacy) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut'])) self.assertEqual(md.metadata_version, '1.2') self.assertEqual(md.version, '2.0.0.9') self.assertEqual(md.meta_requires, []) self.assertEqual(set(md.provides), set(['choxie (2.0.0.9)', 'truffles (1.0)'])) # Initialise from new metadata fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNone(md._legacy) self.assertEqual(md.metadata_version, '2.0') self.assertEqual(md.name, 'foobar') self.assertEqual(md.version, '0.1') self.assertEqual(md.provides, ['foobar (0.1)'])
def test_fields(self): md = Metadata() self.assertTrue(md.is_multi_field('Requires-Dist')) self.assertFalse(md.is_multi_field('Name')) self.assertTrue(md.is_field('Obsoleted-By')) self.assertFalse(md.is_field('Frobozz'))