Commits

Boris FELD committed b6ad6f9

Finish the renaming of datafiles to resources.
Change test_ressources.test_resources_open to avoid to install a complete distribution, create a fake one manually.

  • Participants
  • Parent commits 14b50b3

Comments (0)

Files changed (6)

 include
 bin
 nosetests.xml
+Distutils2.egg-info

File distutils2/_backport/pkgutil.py

 """Utilities to support packages."""
 
+import imp
+import sys
+
+from csv import reader as csv_reader
 import os
-import sys
-import imp
 import re
+from stat import ST_SIZE
+from types import ModuleType
 import warnings
-from csv import reader as csv_reader
-from types import ModuleType
-from stat import ST_SIZE
 
 try:
     from hashlib import md5
     """Make a trivial single-dispatch generic function"""
     registry = {}
 
-    def wrapper(*args, **kw):
+    def wrapper(*args, ** kw):
         ob = args[0]
         try:
             cls = ob.__class__
                     pass
                 mro = cls.__mro__[1:]
             except TypeError:
-                mro = object,   # must be an ExtensionClass or some such  :(
+                mro = object, # must be an ExtensionClass or some such  :(
         for t in mro:
             if t in registry:
-                return registry[t](*args, **kw)
+                return registry[t](*args, ** kw)
         else:
-            return func(*args, **kw)
+            return func(*args, ** kw)
     try:
         wrapper.__name__ = func.__name__
     except (TypeError, AttributeError):
 # PEP 376 Implementation #
 ##########################
 
-DIST_FILES = ('INSTALLER', 'METADATA', 'RECORD', 'REQUESTED', 'DATAFILES')
+DIST_FILES = ('INSTALLER', 'METADATA', 'RECORD', 'REQUESTED', 'RESOURCES')
 
 # Cache
 _cache_name = {}  # maps names to Distribution instances
 def clear_cache():
     """ Clears the internal cache. """
     global _cache_name, _cache_name_egg, _cache_path, _cache_path_egg, \
-           _cache_generated, _cache_generated_egg
+        _cache_generated, _cache_generated_egg
 
     _cache_name = {}
     _cache_name_egg = {}
             yield path, md5, size
 
     def get_resource_path(self, relative_path):
-        resources_file = self.get_distinfo_file('DATAFILES')
-        resources_reader = csv_reader(resources_file, delimiter = ',')
+        resources_file = self.get_distinfo_file('RESOURCES')
+        resources_reader = csv_reader(resources_file, delimiter=',')
         for relative, destination in resources_reader:
             if relative == relative_path:
                 return destination
-        raise KeyError('No data_file with relative path %s were installed' %
-                relative_path)
+        raise KeyError('No resource file with relative path %s were installed' %
+                       relative_path)
 
     def get_installed_files(self, local=False):
         """
             distinfo_dirname, path = path.split(os.sep)[-2:]
             if distinfo_dirname != self.path.split(os.sep)[-1]:
                 raise DistutilsError("Requested dist-info file does not "
-                    "belong to the %s distribution. '%s' was requested." \
-                    % (self.name, os.sep.join([distinfo_dirname, path])))
+                                     "belong to the %s distribution. '%s' was requested." \
+                                     % (self.name, os.sep.join([distinfo_dirname, path])))
 
         # The file must be relative
         if path not in DIST_FILES:
             raise DistutilsError("Requested an invalid dist-info file: "
-                "%s" % path)
+                                 "%s" % path)
 
         # Convert the relative path back to absolute
         path = os.path.join(self.path, path)
     metadata = None
     """A :class:`distutils2.metadata.DistributionMetadata` instance loaded with
     the distribution's ``METADATA`` file."""
-    _REQUIREMENT = re.compile( \
-        r'(?P<name>[-A-Za-z0-9_.]+)\s*' \
-        r'(?P<first>(?:<|<=|!=|==|>=|>)[-A-Za-z0-9_.]+)?\s*' \
-        r'(?P<rest>(?:\s*,\s*(?:<|<=|!=|==|>=|>)[-A-Za-z0-9_.]+)*)\s*' \
-        r'(?P<extras>\[.*\])?')
+    _REQUIREMENT = re.compile(\
+                              r'(?P<name>[-A-Za-z0-9_.]+)\s*' \
+                              r'(?P<first>(?:<|<=|!=|==|>=|>)[-A-Za-z0-9_.]+)?\s*' \
+                              r'(?P<rest>(?:\s*,\s*(?:<|<=|!=|==|>=|>)[-A-Za-z0-9_.]+)*)\s*' \
+                              r'(?P<extras>\[.*\])?')
 
     def __init__(self, path, display_warnings=False):
         self.path = path
                     else:
                         if match.group('extras'):
                             s = (('Distribution %s uses extra requirements '
-                                  'which are not supported in distutils') \
-                                         % (self.name))
+                                 'which are not supported in distutils') \
+                                 % (self.name))
                             warnings.warn(s)
                         name = match.group('name')
                         version = None
 
     def __eq__(self, other):
         return isinstance(other, EggInfoDistribution) and \
-               self.path == other.path
+            self.path == other.path
 
     # See http://docs.python.org/reference/datamodel#object.__hash__
     __hash__ = object.__hash__
                 yield dist
 
 
-def get_distribution(name, use_egg_info=False, paths=sys.path):
+def get_distribution(name, use_egg_info=False, paths=None):
     """
     Scans all elements in ``sys.path`` and looks for all directories
     ending with ``.dist-info``. Returns a :class:`Distribution`
 
     :rtype: :class:`Distribution` or :class:`EggInfoDistribution` or None
     """
+    if paths == None:
+        paths = sys.path
+
     if not _cache_enabled:
         for dist in _yield_distributions(True, use_egg_info, paths):
             if dist.name == name:
                     predicate = VersionPredicate(obs)
                 except ValueError:
                     raise DistutilsError(('Distribution %s has ill formed' +
-                                          ' obsoletes field') % (dist.name,))
+                                         ' obsoletes field') % (dist.name,))
                 if name == o_components[0] and predicate.match(version):
                     yield dist
                     break
                 p_name, p_ver = p_components
                 if len(p_ver) < 2 or p_ver[0] != '(' or p_ver[-1] != ')':
                     raise DistutilsError(('Distribution %s has invalid ' +
-                                          'provides field: %s') \
-                                           % (dist.name, p))
+                                         'provides field: %s') \
+                                         % (dist.name, p))
                 p_ver = p_ver[1:-1]  # trim off the parenthesis
                 if p_name == name and predicate.match(p_ver):
                     yield dist
             yield dist
 
 def resource_path(distribution_name, relative_path):
-     dist = get_distribution(distribution_name)
-     if dist != None:
-         return dist.get_resource_path(relative_path)
-     raise LookupError('No distribution named %s is installed.' %
-                    distribution_name)
+    dist = get_distribution(distribution_name)
+    if dist != None:
+        return dist.get_resource_path(relative_path)
+    raise LookupError('No distribution named %s is installed.' %
+                      distribution_name)
 
-def resource_open(distribution_name, relative_path, *args, **kwargs):
-    file = open(resource_path(distribution_name, relative_path), *args,
-                **kwargs)
+def resource_open(distribution_name, relative_path, * args, ** kwargs):
+    file = open(resource_path(distribution_name, relative_path), * args,
+                ** kwargs)
     return file

File distutils2/_backport/tests/fake_dists/babar-0.1.dist-info/DATAFILES

-babar.png,babar.png
-babar.cfg,babar.cfg

File distutils2/_backport/tests/fake_dists/babar-0.1.dist-info/RESOURCES

+babar.png,babar.png
+babar.cfg,babar.cfg

File distutils2/command/install_distinfo.py

 
 # This file was created from the code for the former command install_egg_info
 
-import os
 import csv
-import re
-from distutils2.command.cmd import Command
 from distutils2 import logger
 from distutils2._backport.shutil import rmtree
+from distutils2.command.cmd import Command
+import os
+import re
 try:
     import hashlib
 except ImportError:
         ('no-record', None,
          "do not generate a RECORD file"),
         ('no-resources', None,
-         "do not generate a DATAFILES list installed file")
+         "do not generate a RESSOURCES list installed file")
     ]
 
     boolean_options = ['requested', 'no-record', 'no-resources']
         metadata = self.distribution.metadata
 
         basename = "%s-%s.dist-info" % (
-            to_filename(safe_name(metadata['Name'])),
-            to_filename(safe_version(metadata['Version'])),
-        )
+                                        to_filename(safe_name(metadata['Name'])),
+                                        to_filename(safe_version(metadata['Version'])),
+                                        )
 
         self.distinfo_dir = os.path.join(self.distinfo_dir, basename)
         self.outputs = []
             if not self.no_resources:
                 install_data = self.get_finalized_command('install_data')
                 if install_data.get_resources_out() != []:
-                    resources_path = os.path.join(self.distinfo_dir, 'DATAFILES')
+                    resources_path = os.path.join(self.distinfo_dir,
+                                                  'RESOURCES')
                     logger.info('creating %s', resources_path)
                     f = open(resources_path, 'wb')
                     try:

File distutils2/tests/test_resources.py

 # -*- encoding: utf-8 -*-
 """Tests for distutils.data."""
-import os
+import pkgutil
 import sys
 
-from distutils2.tests import unittest, support, run_unittest
+from distutils2._backport.pkgutil import resource_open
+from distutils2._backport.pkgutil import resource_path
+from distutils2._backport.pkgutil import disable_cache
+from distutils2._backport.pkgutil import enable_cache
+from distutils2.command.install_dist import install_dist
+from distutils2.resources import resources_dests
+from distutils2.tests import run_unittest
+from distutils2.tests import unittest
 from distutils2.tests.test_util import GlobTestCaseBase
-from distutils2.resources import resources_dests
-from distutils2.command.install_dist import install_dist
-from distutils2._backport.pkgutil import resource_open, resource_path
+import os
+import tempfile
 
 
 class DataFilesTestCase(GlobTestCaseBase):
     def test_simple_glob(self):
         rules = [('', '*.tpl', '{data}')]
         spec  = {'coucou.tpl': '{data}/coucou.tpl',
-                 'Donotwant': None}
+            'Donotwant': None}
         self.assertRulesMatch(rules, spec)
 
     def test_multiple_match(self):
         rules = [('scripts', '*.bin', '{appdata}'),
-                 ('scripts', '*', '{appscript}')]
+            ('scripts', '*', '{appscript}')]
         spec  = {'scripts/script.bin': '{appscript}/script.bin',
-                 'Babarlikestrawberry': None}
+            '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}
+            '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}
+            'scripts/script.sh':  '{appscript}/script.sh',
+            'Babarlikestrawberry': None}
         self.assertRulesMatch(rules, spec)
 
     def test_set_match_exclude(self):
         rules = [('scripts', '*', '{appscript}'),
-                 ('', '**/*.sh', None)]
+            ('', '**/*.sh', None)]
         spec  = {'scripts/scripts.bin': '{appscript}/scripts.bin',
-                 'scripts/script.sh':  None,
-                 'Babarlikestrawberry': None}
+            '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',
-                 'Babarlikestrawberry': None}
+            'Babarlikestrawberry': None}
         tempdir = self.build_files_tree(spec)
         self.assertRaises(NotImplementedError, resources_dests, tempdir, rules)
 
     def test_recursive_glob(self):
         rules = [('', '**/*.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}
+            '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'),
+            ('mailman/database/schemas/', '*', '{appdata}/schemas'),
             ('', '**/*.tpl', '{appdata}/templates'),
             ('', 'developer-docs/**/*.txt', '{doc}'),
             ('', 'README', '{doc}'),
         self.assertRulesMatch(rules, spec)
 
     def test_resource_open(self):
-        from distutils2._backport.sysconfig import _SCHEMES as sysconfig_SCHEMES
-        from distutils2._backport.sysconfig import _get_default_scheme
-            #dirty but hit marmoute
 
-        tempdir = self.mkdtemp()
-        
-        old_scheme = sysconfig_SCHEMES
 
-        sysconfig_SCHEMES.set(_get_default_scheme(), 'config',
-            tempdir)
+        #Create a fake-dist
+        temp_site_packages = tempfile.mkdtemp()
 
-        pkg_dir, dist = self.create_dist()
-        dist_name = dist.metadata['name']
+        dist_name = 'test'
+        dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info')
+        os.mkdir(dist_info)
 
-        test_path = os.path.join(pkg_dir, 'test.cfg')
-        self.write_file(test_path, 'Config')
-        dist.data_files = {test_path : '{config}/test.cfg'}
+        metadata_path = os.path.join(dist_info, 'METADATA')
+        resources_path = os.path.join(dist_info, 'RESOURCES')
 
-        cmd = install_dist(dist)
-        cmd.install_dir = os.path.join(pkg_dir, 'inst')
-        content = 'Config'        
-        
-        cmd.ensure_finalized()
-        cmd.run()
+        metadata_file = open(metadata_path, 'w')
 
-        cfg_dest = os.path.join(tempdir, 'test.cfg')
+        metadata_file.write(
+"""Metadata-Version: 1.2
+Name: test
+Version: 0.1
+Summary: test
+Author: me
+        """)
 
-        self.assertEqual(resource_path(dist_name, test_path), cfg_dest)
-        self.assertRaises(KeyError, lambda: resource_path(dist_name, 'notexis'))
+        metadata_file.close()
+
+        test_path = 'test.cfg'
+
+        _, test_resource_path = tempfile.mkstemp()
+
+        test_resource_file = open(test_resource_path, 'w')
+
+        content = 'Config'
+        test_resource_file.write(content)
+        test_resource_file.close()
+
+        resources_file = open(resources_path, 'w')
+
+        resources_file.write("""%s,%s""" % (test_path, test_resource_path))
+        resources_file.close()
+
+        #Add fake site-packages to sys.path to retrieve fake dist
+        old_sys_path = sys.path
+        sys.path.insert(0, temp_site_packages)
+
+        #Force pkgutil to rescan the sys.path
+        disable_cache()
+
+        #Try to retrieve resources paths and files
+        self.assertEqual(resource_path(dist_name, test_path), test_resource_path)
+        self.assertRaises(KeyError, resource_path, dist_name, 'notexis')
 
         self.assertEqual(resource_open(dist_name, test_path).read(), content)
-        self.assertRaises(KeyError, lambda: resource_open(dist_name, 'notexis'))
-        
-        sysconfig_SCHEMES = old_scheme
+        self.assertRaises(KeyError, resource_open, dist_name, 'notexis')
+
+        sys.path = old_sys_path
+
+        enable_cache()
 
 def test_suite():
     return unittest.makeSuite(DataFilesTestCase)