Ticket #8280: zip_egg_fixed.diff

File zip_egg_fixed.diff, 10.1 KB (added by bhuztez, 13 years ago)

zip_egg_fixed from #14087, namespace_package_pth.5.diff should be applied before this patch

  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 1214a82..88d3945 100644
    a b import os  
    33import sys
    44from optparse import OptionParser, NO_DEFAULT
    55import imp
     6import pkgutil
    67import warnings
    78
    89from django.core.management.base import BaseCommand, CommandError, handle_default_options
    910from django.core.management.color import color_style
    10 from django.utils.importlib import import_module
     11from django.utils.importlib import import_module, find_package_path
    1112
    1213# For backwards compatibility: get_version() used to be in this module.
    1314from django import get_version
    def find_commands(management_dir):  
    2324
    2425    Returns an empty list if no commands are defined.
    2526    """
    26     command_dir = os.path.join(management_dir, 'commands')
    2727    try:
    28         return [f[:-3] for f in os.listdir(command_dir)
    29                 if not f.startswith('_') and f.endswith('.py')]
    30     except OSError:
     28        commands_dir = find_package_path('commands', [management_dir])[0]
     29        return [name for loader,name,ispkg in pkgutil.iter_modules([commands_dir])
     30                if not name.startswith('_') ]
     31    except ImportError:
    3132        return []
    3233
    3334def find_management_module(app_name):
    def find_management_module(app_name):  
    5455    else:
    5556        parts.reverse()
    5657        part = parts.pop()
    57         path = sys.path
     58        path = None
    5859
    5960        # When using manage.py, the project module is added to the path,
    6061        # loaded, then removed from the path. This means that
    def find_management_module(app_name):  
    6364        # module, we need look for the case where the project name is part
    6465        # of the app_name but the project directory itself isn't on the path.
    6566        try:
    66             next_path = []
    67             for p in path:
    68                 try:
    69                     next_path.append(imp.find_module(part, [p])[1])
    70                 except ImportError:
    71                     pass
    72             if not next_path:
    73                 raise ImportError("No module named %s" % part)
    74             path = next_path
     67            path = find_package_path(part, path)
    7568        except ImportError,e:
    7669            if os.path.basename(os.getcwd()) != part:
    7770                raise e
    7871
    7972    while parts:
    8073        part = parts.pop()
    81         next_path = []
    82         for p in path:
    83             try:
    84                 next_path.append(imp.find_module(part, [p])[1])
    85             except ImportError:
    86                 pass
    87         if not next_path:
    88             raise ImportError("No module named %s" % part)
    89         path = next_path
    90 
     74        path = find_package_path(part, path)
    9175    return path[0]
    9276
    9377def load_command_class(app_name, name):
  • django/utils/importlib.py

    diff --git a/django/utils/importlib.py b/django/utils/importlib.py
    index ef4d0e4..f53abd9 100644
    a b  
    11# Taken from Python 2.7 with permission from/by the original author.
     2import os
    23import sys
     4import imp
     5import pkgutil
     6import warnings
    37
    48def _resolve_name(name, package, level):
    59    """Return the absolute name of the module to be imported."""
    def import_module(name, package=None):  
    3438        name = _resolve_name(name[level:], package, level)
    3539    __import__(name)
    3640    return sys.modules[name]
     41
     42
     43def find_package_path(name, path=None):
     44    """Finds search path for package with given name.
     45
     46    The 'path' argument defaults to ``sys.path``.
     47
     48    Raises ImportError if no search path could be found.
     49    """
     50    if path is None:
     51        path = sys.path
     52
     53    results = []
     54
     55    for path_item in path:
     56        importer = get_importer(path_item)
     57
     58        if importer is None:
     59            continue
     60
     61        try:
     62            loader = importer.find_module(name)
     63
     64            if loader is not None:
     65
     66                if not hasattr(loader, 'is_package'):
     67                    warnings.warn(
     68                        "Django cannot find search path for package '%s' ",
     69                        "under '%s', because the loader returned by '%s' does ",
     70                        "not implement 'is_package' method."%(
     71                            name,
     72                            path_item,
     73                            importer.__class__.__name__))
     74                    continue
     75
     76                if not hasattr(loader, 'get_filename'):
     77                    warnings.warn(
     78                        "Django cannot find search path for package '%s' ",
     79                        "under '%s', because the loader returned by '%s' does ",
     80                        "not implement 'get_filename' method."%(
     81                            name,
     82                            path_item,
     83                            importer.__class__.__name__))
     84                    continue
     85
     86                if loader.is_package(name):
     87                    results.append(os.path.dirname(loader.get_filename(name)))
     88        except ImportError:
     89            pass
     90
     91    if not results:
     92        raise ImportError("No package named %s" % name)
     93
     94    return results
     95
     96
     97get_importer = pkgutil.get_importer
     98
     99try:
     100    import zipimport
     101
     102    if hasattr(zipimport.zipimporter, 'get_filename'):
     103        class ZipImporter(zipimport.zipimporter):
     104            def get_filename(self, fullname):
     105                archivepath = os.path.join(self.archive, self.prefix)
     106                if self.is_package(fullname):
     107                    return os.path.join(archivepath, fullname, '__init__.py')
     108
     109                return os.path.join(archivepath, fullname + '.py')
     110
     111        def get_importer(path_item):
     112            importer = pkgutil.get_importer(path_item)
     113
     114            if isinstance(importer, zipimport.zipimporter):
     115                archivepath = os.path.join(importer.archive, importer.prefix)
     116                importer = ZipImporter(os.path.dirname(archivepath))
     117
     118            return importer
     119
     120except ImportError:
     121    pass
     122
     123
  • new file tests/regressiontests/admin_scripts/lib3/egg_module.pth

    diff --git a/tests/regressiontests/admin_scripts/lib3/egg_module.pth b/tests/regressiontests/admin_scripts/lib3/egg_module.pth
    new file mode 100644
    index 0000000..9367ab5
    - +  
     1test_egg.egg
  • new file tests/regressiontests/admin_scripts/lib3/test_egg.egg

    diff --git a/tests/regressiontests/admin_scripts/lib3/test_egg.egg b/tests/regressiontests/admin_scripts/lib3/test_egg.egg
    new file mode 100644
    index 0000000000000000000000000000000000000000..d9e57de006a3d602515293f8e980bf3a16fb42c0
    GIT binary patch
    literal 2210
    zcmWIWW@Zs#W&na+g#z{<8U{Fl3|Dt|T~9wZfBgWcG7g3{KxHDAuKle5O78$-K~!ZB
    z^|5C+avd@dV0&QeKT+IcWwzNt$AHBOuWGiW^E|0~S|nLhZ9Ow6Z`b?h&-X7l#KHBh
    zGf2q)-n(7*SQNg9C6^pYKAZ6`^S*BSyQv&cUcXhIbLgw+nTectc?6~3uC9yNw%@sF
    z%ItZU8-gRR@<p(DZr01@&6oUg*2%XH)tT;p3*R&VooCL-z#xL`%wYe}AZOQLy^@NO
    zseK1QE_HeT&--$}fT-Y_2pj#SlFkL2Ia9R1z3&YW$hEDwHMjD;fuxnn<YS_977M&R
    zu%dHMh3c<Wrx~B0zcgk0r=s4kFE_0Ic06}8$L(iDarse;`D34maV9DL^?37iT78$4
    zu;IH0CoB42FDd2_n0t1<bS;nS%|HFW4h3s`69&1M3mpESfSt^C-3aJd1`w7-c5zB-
    zL26z~YF=_>d`@Owb}`85T&Q|ip0Nz)2I>J}F=V|Z`33Pgsb#4-AWf<1>G8SwDWy57
    zXxfpjMv2p^%mUrw#59O{NK!eBmQ+AiA`Av67Koo#6`-amkh_o+#>Z#oWtPOp>lIYO
    zVvd0U-EcHLxrup+>8ZJ?c_m2p+{#0>M-kN?Bt1k~DUVZoa(*t*u#{pvc54yVjKvWg
    z3_F2gHQm(j`*xsy9$*5{C2S5TD4{L_2BB}CcONJzf46gee`Y}-UxUeP$C<(gLc3RV
    zY9Cwi_n@qWz2AwZxYCuY=4Gw2oV2^}(@yDPS8>aY?k=--b8BlHne^Ic|6BWKH)7Jy
    zXsq#?eWx}ep56KFx(QD9DgGB`hCMj6_@itPSErDB#zVor9U@z^*FXBlA!>Exjl$u{
    zsuuH{{dm6}Ulb7>vsbSqJM5*w#jS~~)~!DGw}|g^*kb!TX4*Er&D*q1x2SHj|9<>`
    zaDBt~!)B*vi6f<AwA2F1vq-50m~2yXiot1xkx7IZcc}%`1_S~P2OL2(QW*x*hP@O6
    zDP~|$XxP%I4y2K^LCUlMZ&dB*r3+X$So=9-?MP(}#01>A9brNPF#h2Vz?S#XjY7}-
    z2&48O8--F7pqqhS0wB!zjcf+2JV1m8W}XEV2oL~@1#Kpjcto}rTLA&l2#SXgRE;Rv
    z1{N8ZmV*im2mo2$focX)QGo0YYy}9!43Ina;536sCxHqS2mm?hH%{Y_iWiXoAV~$?
    zZJ+`M0zhuF09GEznFLWh!VSafNbCg;!rTVJ=Asrq$icY=OHM`@-Nb_GWJJDZWdo_=
    P1Hv0X>$d>aF)#oC`tnrE
  • tests/regressiontests/admin_scripts/tests.py

    literal 0
    HcmV?d00001
    
    diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
    index 45852ba..1240ca4 100644
    a b class AdminScriptTestCase(unittest.TestCase):  
    9393        lib1_dir = os.path.join(os.path.dirname(__file__), 'lib1')
    9494        lib2_dir = os.path.join(os.path.dirname(__file__), 'lib2')
    9595        lib3_dir = os.path.join(os.path.dirname(__file__), 'lib3')
     96        eggs_dir = os.path.join(os.path.dirname(__file__), 'eggs')
    9697        ext_backend_base_dirs = self._ext_backend_paths()
    9798
    9899        # Remember the old environment
    class StartProject(LiveServerTestCase, AdminScriptTestCase):  
    15581559
    15591560class NamespacePackagedApps(AdminScriptTestCase):
    15601561    def setUp(self):
    1561         self.write_settings('settings.py', apps=['nons_app', 'nsapps.contrib.app1','nsapps.contrib.app2','exapps.app3'])
    1562         test_dir = os.path.dirname(os.path.dirname(__file__))
     1562        self.write_settings('settings.py', apps=['nons_app', 'nsapps.contrib.app1','nsapps.contrib.app2','exapps.app3', 'egg_module'])
    15631563        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
    15641564        settings_file.write('import _addsitedir')
    15651565        settings_file.close()
    class NamespacePackagedApps(AdminScriptTestCase):  
    15741574        self.assertOutput(out, "app1_command1")
    15751575        self.assertOutput(out, "app2_command1")
    15761576        self.assertOutput(out, "app3_command1")
     1577        self.assertOutput(out, "egg_command")
    15771578
    15781579    def test_nons_app(self):
    15791580        args = ['nons_app_command1']
    class NamespacePackagedApps(AdminScriptTestCase):  
    15981599        self.assertNoOutput(err)
    15991600        self.assertOutput(out, "EXECUTE:app3_command1")
    16001601
     1602    def test_exapps(self):
     1603        args = ['egg_command']
     1604        out, err = self.run_manage(args)
     1605        self.assertNoOutput(err)
     1606        self.assertOutput(out, "EXECUTE:egg_command")
     1607
     1608
    16011609class PreloadedNamespacePackagedApps(AdminScriptTestCase):
    16021610    def setUp(self):
    16031611        self.write_settings('settings.py', apps=['nsapps.contrib.app1','nsapps.contrib.app2'])
    1604         test_dir = os.path.dirname(os.path.dirname(__file__))
    16051612        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
    16061613        settings_file.write('import nsapps')
    16071614        settings_file.close()
Back to Top