a resource module for 2 functions is overkill. database is the right place for those
This commit is contained in:
parent
72e58651b2
commit
a17d8883fd
|
@ -18,6 +18,7 @@ __all__ = [
|
|||
'get_distributions', 'get_distribution', 'get_file_users',
|
||||
'provides_distribution', 'obsoletes_distribution',
|
||||
'enable_cache', 'disable_cache', 'clear_cache',
|
||||
'get_file_path', 'get_file'
|
||||
]
|
||||
|
||||
|
||||
|
@ -627,3 +628,17 @@ def get_file_users(path):
|
|||
for dist in get_distributions():
|
||||
if dist.uses(path):
|
||||
yield dist
|
||||
|
||||
|
||||
def get_file_path(distribution_name, relative_path):
|
||||
"""Return the path to a resource file."""
|
||||
dist = get_distribution(distribution_name)
|
||||
if dist != None:
|
||||
return dist.get_resource_path(relative_path)
|
||||
raise LookupError('no distribution named %r found' % distribution_name)
|
||||
|
||||
|
||||
def get_file(distribution_name, relative_path, *args, **kwargs):
|
||||
"""Open and return a resource file."""
|
||||
return open(get_file_path(distribution_name, relative_path),
|
||||
*args, **kwargs)
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
"""Data file path abstraction.
|
||||
|
||||
Functions in this module use sysconfig to find the paths to the resource
|
||||
files registered in project's setup.cfg file. See the documentation for
|
||||
more information.
|
||||
"""
|
||||
# TODO write that documentation
|
||||
|
||||
from packaging.database import get_distribution
|
||||
|
||||
__all__ = ['get_file_path', 'get_file']
|
||||
|
||||
|
||||
def get_file_path(distribution_name, relative_path):
|
||||
"""Return the path to a resource file."""
|
||||
dist = get_distribution(distribution_name)
|
||||
if dist != None:
|
||||
return dist.get_resource_path(relative_path)
|
||||
raise LookupError('no distribution named %r found' % distribution_name)
|
||||
|
||||
|
||||
def get_file(distribution_name, relative_path, *args, **kwargs):
|
||||
"""Open and return a resource file."""
|
||||
return open(get_file_path(distribution_name, relative_path),
|
||||
*args, **kwargs)
|
|
@ -8,16 +8,21 @@ import zipfile
|
|||
import tempfile
|
||||
from os.path import relpath # separate import for backport concerns
|
||||
from hashlib import md5
|
||||
from textwrap import dedent
|
||||
|
||||
from packaging.tests import unittest
|
||||
from packaging.tests.test_util import GlobTestCaseBase
|
||||
from packaging.tests.support import requires_zlib
|
||||
|
||||
from packaging.config import get_resources_dests
|
||||
from packaging.errors import PackagingError
|
||||
from packaging.metadata import Metadata
|
||||
from packaging.tests import unittest, run_unittest, support, TESTFN
|
||||
from packaging.tests.support import requires_zlib
|
||||
|
||||
from packaging.database import (
|
||||
Distribution, EggInfoDistribution, get_distribution, get_distributions,
|
||||
provides_distribution, obsoletes_distribution, get_file_users,
|
||||
enable_cache, disable_cache, distinfo_dirname, _yield_distributions)
|
||||
enable_cache, disable_cache, distinfo_dirname, _yield_distributions,
|
||||
get_file, get_file_path)
|
||||
|
||||
# TODO Add a test for getting a distribution provided by another distribution
|
||||
# TODO Add a test for absolute pathed RECORD items (e.g. /etc/myapp/config.ini)
|
||||
|
@ -504,12 +509,160 @@ class TestDatabase(support.LoggingCatcher,
|
|||
checkLists(dists + eggs, found)
|
||||
|
||||
|
||||
class DataFilesTestCase(GlobTestCaseBase):
|
||||
|
||||
def assertRulesMatch(self, rules, spec):
|
||||
tempdir = self.build_files_tree(spec)
|
||||
expected = self.clean_tree(spec)
|
||||
result = get_resources_dests(tempdir, rules)
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
def clean_tree(self, spec):
|
||||
files = {}
|
||||
for path, value in spec.items():
|
||||
if value is not None:
|
||||
files[path] = value
|
||||
return files
|
||||
|
||||
def test_simple_glob(self):
|
||||
rules = [('', '*.tpl', '{data}')]
|
||||
spec = {'coucou.tpl': '{data}/coucou.tpl',
|
||||
'Donotwant': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_multiple_match(self):
|
||||
rules = [('scripts', '*.bin', '{appdata}'),
|
||||
('scripts', '*', '{appscript}')]
|
||||
spec = {'scripts/script.bin': '{appscript}/script.bin',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match(self):
|
||||
rules = [('scripts', '*.{bin,sh}', '{appscript}')]
|
||||
spec = {'scripts/script.bin': '{appscript}/script.bin',
|
||||
'scripts/babar.sh': '{appscript}/babar.sh',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match_multiple(self):
|
||||
rules = [('scripts', 'script{s,}.{bin,sh}', '{appscript}')]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripts/script.sh': '{appscript}/script.sh',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match_exclude(self):
|
||||
rules = [('scripts', '*', '{appscript}'),
|
||||
('', os.path.join('**', '*.sh'), None)]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripts/script.sh': None,
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_glob_in_base(self):
|
||||
rules = [('scrip*', '*.bin', '{appscript}')]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripouille/babar.bin': '{appscript}/babar.bin',
|
||||
'scriptortu/lotus.bin': '{appscript}/lotus.bin',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_recursive_glob(self):
|
||||
rules = [('', os.path.join('**', '*.bin'), '{binary}')]
|
||||
spec = {'binary0.bin': '{binary}/binary0.bin',
|
||||
'scripts/binary1.bin': '{binary}/scripts/binary1.bin',
|
||||
'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin',
|
||||
'you/kill/pandabear.guy': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_final_exemple_glob(self):
|
||||
rules = [
|
||||
('mailman/database/schemas/', '*', '{appdata}/schemas'),
|
||||
('', os.path.join('**', '*.tpl'), '{appdata}/templates'),
|
||||
('', os.path.join('developer-docs', '**', '*.txt'), '{doc}'),
|
||||
('', 'README', '{doc}'),
|
||||
('mailman/etc/', '*', '{config}'),
|
||||
('mailman/foo/', os.path.join('**', 'bar', '*.cfg'), '{config}/baz'),
|
||||
('mailman/foo/', os.path.join('**', '*.cfg'), '{config}/hmm'),
|
||||
('', 'some-new-semantic.sns', '{funky-crazy-category}'),
|
||||
]
|
||||
spec = {
|
||||
'README': '{doc}/README',
|
||||
'some.tpl': '{appdata}/templates/some.tpl',
|
||||
'some-new-semantic.sns':
|
||||
'{funky-crazy-category}/some-new-semantic.sns',
|
||||
'mailman/database/mailman.db': None,
|
||||
'mailman/database/schemas/blah.schema':
|
||||
'{appdata}/schemas/blah.schema',
|
||||
'mailman/etc/my.cnf': '{config}/my.cnf',
|
||||
'mailman/foo/some/path/bar/my.cfg':
|
||||
'{config}/hmm/some/path/bar/my.cfg',
|
||||
'mailman/foo/some/path/other.cfg':
|
||||
'{config}/hmm/some/path/other.cfg',
|
||||
'developer-docs/index.txt': '{doc}/developer-docs/index.txt',
|
||||
'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt',
|
||||
}
|
||||
self.maxDiff = None
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
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, 'METADATA')
|
||||
resources_path = os.path.join(dist_info, 'RESOURCES')
|
||||
|
||||
with open(metadata_path, 'w') as fp:
|
||||
fp.write(dedent("""\
|
||||
Metadata-Version: 1.2
|
||||
Name: test
|
||||
Version: 0.1
|
||||
Summary: test
|
||||
Author: me
|
||||
"""))
|
||||
|
||||
test_path = 'test.cfg'
|
||||
|
||||
fd, test_resource_path = tempfile.mkstemp()
|
||||
os.close(fd)
|
||||
self.addCleanup(os.remove, test_resource_path)
|
||||
|
||||
with open(test_resource_path, 'w') as fp:
|
||||
fp.write('Config')
|
||||
|
||||
with open(resources_path, 'w') as fp:
|
||||
fp.write('%s,%s' % (test_path, test_resource_path))
|
||||
|
||||
# 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)
|
||||
|
||||
# Force packaging.database to rescan the sys.path
|
||||
self.addCleanup(enable_cache)
|
||||
disable_cache()
|
||||
|
||||
# Try to retrieve resources paths and files
|
||||
self.assertEqual(get_file_path(dist_name, test_path),
|
||||
test_resource_path)
|
||||
self.assertRaises(KeyError, get_file_path, dist_name, 'i-dont-exist')
|
||||
|
||||
with get_file(dist_name, test_path) as fp:
|
||||
self.assertEqual(fp.read(), 'Config')
|
||||
self.assertRaises(KeyError, get_file, dist_name, 'i-dont-exist')
|
||||
|
||||
|
||||
def test_suite():
|
||||
suite = unittest.TestSuite()
|
||||
load = unittest.defaultTestLoader.loadTestsFromTestCase
|
||||
suite.addTest(load(TestDistribution))
|
||||
suite.addTest(load(TestEggInfoDistribution))
|
||||
suite.addTest(load(TestDatabase))
|
||||
suite.addTest(load(DataFilesTestCase))
|
||||
return suite
|
||||
|
||||
|
||||
|
|
|
@ -1,167 +0,0 @@
|
|||
"""Tests for packaging.resources."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import tempfile
|
||||
from textwrap import dedent
|
||||
from packaging.config import get_resources_dests
|
||||
from packaging.database import disable_cache, enable_cache
|
||||
from packaging.resources import get_file, get_file_path
|
||||
|
||||
from packaging.tests import unittest
|
||||
from packaging.tests.test_util import GlobTestCaseBase
|
||||
|
||||
|
||||
class DataFilesTestCase(GlobTestCaseBase):
|
||||
|
||||
def assertRulesMatch(self, rules, spec):
|
||||
tempdir = self.build_files_tree(spec)
|
||||
expected = self.clean_tree(spec)
|
||||
result = get_resources_dests(tempdir, rules)
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
def clean_tree(self, spec):
|
||||
files = {}
|
||||
for path, value in spec.items():
|
||||
if value is not None:
|
||||
files[path] = value
|
||||
return files
|
||||
|
||||
def test_simple_glob(self):
|
||||
rules = [('', '*.tpl', '{data}')]
|
||||
spec = {'coucou.tpl': '{data}/coucou.tpl',
|
||||
'Donotwant': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_multiple_match(self):
|
||||
rules = [('scripts', '*.bin', '{appdata}'),
|
||||
('scripts', '*', '{appscript}')]
|
||||
spec = {'scripts/script.bin': '{appscript}/script.bin',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match(self):
|
||||
rules = [('scripts', '*.{bin,sh}', '{appscript}')]
|
||||
spec = {'scripts/script.bin': '{appscript}/script.bin',
|
||||
'scripts/babar.sh': '{appscript}/babar.sh',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match_multiple(self):
|
||||
rules = [('scripts', 'script{s,}.{bin,sh}', '{appscript}')]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripts/script.sh': '{appscript}/script.sh',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_set_match_exclude(self):
|
||||
rules = [('scripts', '*', '{appscript}'),
|
||||
('', os.path.join('**', '*.sh'), None)]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripts/script.sh': None,
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_glob_in_base(self):
|
||||
rules = [('scrip*', '*.bin', '{appscript}')]
|
||||
spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
|
||||
'scripouille/babar.bin': '{appscript}/babar.bin',
|
||||
'scriptortu/lotus.bin': '{appscript}/lotus.bin',
|
||||
'Babarlikestrawberry': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_recursive_glob(self):
|
||||
rules = [('', os.path.join('**', '*.bin'), '{binary}')]
|
||||
spec = {'binary0.bin': '{binary}/binary0.bin',
|
||||
'scripts/binary1.bin': '{binary}/scripts/binary1.bin',
|
||||
'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin',
|
||||
'you/kill/pandabear.guy': None}
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
def test_final_exemple_glob(self):
|
||||
rules = [
|
||||
('mailman/database/schemas/', '*', '{appdata}/schemas'),
|
||||
('', os.path.join('**', '*.tpl'), '{appdata}/templates'),
|
||||
('', os.path.join('developer-docs', '**', '*.txt'), '{doc}'),
|
||||
('', 'README', '{doc}'),
|
||||
('mailman/etc/', '*', '{config}'),
|
||||
('mailman/foo/', os.path.join('**', 'bar', '*.cfg'), '{config}/baz'),
|
||||
('mailman/foo/', os.path.join('**', '*.cfg'), '{config}/hmm'),
|
||||
('', 'some-new-semantic.sns', '{funky-crazy-category}'),
|
||||
]
|
||||
spec = {
|
||||
'README': '{doc}/README',
|
||||
'some.tpl': '{appdata}/templates/some.tpl',
|
||||
'some-new-semantic.sns':
|
||||
'{funky-crazy-category}/some-new-semantic.sns',
|
||||
'mailman/database/mailman.db': None,
|
||||
'mailman/database/schemas/blah.schema':
|
||||
'{appdata}/schemas/blah.schema',
|
||||
'mailman/etc/my.cnf': '{config}/my.cnf',
|
||||
'mailman/foo/some/path/bar/my.cfg':
|
||||
'{config}/hmm/some/path/bar/my.cfg',
|
||||
'mailman/foo/some/path/other.cfg':
|
||||
'{config}/hmm/some/path/other.cfg',
|
||||
'developer-docs/index.txt': '{doc}/developer-docs/index.txt',
|
||||
'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt',
|
||||
}
|
||||
self.maxDiff = None
|
||||
self.assertRulesMatch(rules, spec)
|
||||
|
||||
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, 'METADATA')
|
||||
resources_path = os.path.join(dist_info, 'RESOURCES')
|
||||
|
||||
with open(metadata_path, 'w') as fp:
|
||||
fp.write(dedent("""\
|
||||
Metadata-Version: 1.2
|
||||
Name: test
|
||||
Version: 0.1
|
||||
Summary: test
|
||||
Author: me
|
||||
"""))
|
||||
|
||||
test_path = 'test.cfg'
|
||||
|
||||
fd, test_resource_path = tempfile.mkstemp()
|
||||
os.close(fd)
|
||||
self.addCleanup(os.remove, test_resource_path)
|
||||
|
||||
with open(test_resource_path, 'w') as fp:
|
||||
fp.write('Config')
|
||||
|
||||
with open(resources_path, 'w') as fp:
|
||||
fp.write('%s,%s' % (test_path, test_resource_path))
|
||||
|
||||
# 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)
|
||||
|
||||
# Force packaging.database to rescan the sys.path
|
||||
self.addCleanup(enable_cache)
|
||||
disable_cache()
|
||||
|
||||
# Try to retrieve resources paths and files
|
||||
self.assertEqual(get_file_path(dist_name, test_path),
|
||||
test_resource_path)
|
||||
self.assertRaises(KeyError, get_file_path, dist_name, 'i-dont-exist')
|
||||
|
||||
with get_file(dist_name, test_path) as fp:
|
||||
self.assertEqual(fp.read(), 'Config')
|
||||
self.assertRaises(KeyError, get_file, dist_name, 'i-dont-exist')
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.makeSuite(DataFilesTestCase)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(defaultTest='test_suite')
|
Loading…
Reference in New Issue