Ticket #14087: namespace_package_pth.4.diff

File namespace_package_pth.4.diff, 16.6 KB (added by bhuztez, 8 years ago)

update patch for revision 17777

  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 8e83304..f31bcc5 100644
    a b def find_management_module(app_name): 
    3939    """
    4040    parts = app_name.split('.')
    4141    parts.append('management')
    42     parts.reverse()
    43     part = parts.pop()
    44     path = None
    45 
    46     # When using manage.py, the project module is added to the path,
    47     # loaded, then removed from the path. This means that
    48     # testproject.testapp.models can be loaded in future, even if
    49     # testproject isn't in the path. When looking for the management
    50     # module, we need look for the case where the project name is part
    51     # of the app_name but the project directory itself isn't on the path.
    52     try:
    53         f, path, descr = imp.find_module(part,path)
    54     except ImportError,e:
    55         if os.path.basename(os.getcwd()) != part:
    56             raise e
     42
     43    for i in range(len(parts), 0, -1):
     44        try:
     45            path = sys.modules['.'.join(parts[:i])].__path__
     46        except AttributeError:
     47            raise ImportError("No package named %s" % parts[i-1])
     48        except KeyError:
     49            continue
     50
     51        parts = parts[i:]
     52        parts.reverse()
     53        break
     54    else:
     55        parts.reverse()
     56        part = parts.pop()
     57        path = sys.path
     58
     59        # When using manage.py, the project module is added to the path,
     60        # loaded, then removed from the path. This means that
     61        # testproject.testapp.models can be loaded in future, even if
     62        # testproject isn't in the path. When looking for the management
     63        # module, we need look for the case where the project name is part
     64        # of the app_name but the project directory itself isn't on the path.
     65        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
     75        except ImportError,e:
     76            if os.path.basename(os.getcwd()) != part:
     77                raise e
    5778
    5879    while parts:
    5980        part = parts.pop()
    60         f, path, descr = imp.find_module(part, path and [path] or None)
    61     return path
     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
     91    return path[0]
    6292
    6393def load_command_class(app_name, name):
    6494    """
  • new file tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/nons_app_command1.py

    diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/__init__.py b/tests/regressiontests/admin_scripts/lib1/nons_app/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/management/__init__.py b/tests/regressiontests/admin_scripts/lib1/nons_app/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/__init__.py b/tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/nons_app_command1.py b/tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/nons_app_command1.py
    new file mode 100644
    index 0000000..a393663
    - +  
     1from django.core.management.base import BaseCommand
     2
     3class Command(BaseCommand):
     4    help = 'Test managment commands in non-namespaced app'
     5    requires_model_validation = False
     6    args = ''
     7
     8    def handle(self, *labels, **options):
     9        print 'EXECUTE:nons_app_command1'
  • new file tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py

    diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/models.py b/tests/regressiontests/admin_scripts/lib1/nons_app/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/__init__.py b/tests/regressiontests/admin_scripts/lib1/npapp/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/management.py b/tests/regressiontests/admin_scripts/lib1/npapp/management.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/models.py b/tests/regressiontests/admin_scripts/lib1/npapp/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py
    new file mode 100644
    index 0000000..32f26d8
    - +  
     1# http://packages.python.org/distribute/setuptools.html#namespace-packages
     2try:
     3    __import__('pkg_resources').declare_namespace(__name__)
     4except ImportError:
     5    from pkgutil import extend_path
     6    __path__ = extend_path(__path__, __name__)
  • new file tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py

    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py
    new file mode 100644
    index 0000000..32f26d8
    - +  
     1# http://packages.python.org/distribute/setuptools.html#namespace-packages
     2try:
     3    __import__('pkg_resources').declare_namespace(__name__)
     4except ImportError:
     5    from pkgutil import extend_path
     6    __path__ = extend_path(__path__, __name__)
  • new file tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/app1_command1.py

    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/app1_command1.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/app1_command1.py
    new file mode 100644
    index 0000000..2f479bb
    - +  
     1from django.core.management.base import BaseCommand
     2
     3class Command(BaseCommand):
     4    help = 'Test managment commands in namespaced apps'
     5    requires_model_validation = False
     6    args = ''
     7
     8    def handle(self, *labels, **options):
     9        print 'EXECUTE:app1_command1'
  • new file tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py

    diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/models.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py
    new file mode 100644
    index 0000000..32f26d8
    - +  
     1# http://packages.python.org/distribute/setuptools.html#namespace-packages
     2try:
     3    __import__('pkg_resources').declare_namespace(__name__)
     4except ImportError:
     5    from pkgutil import extend_path
     6    __path__ = extend_path(__path__, __name__)
  • new file tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py

    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py
    new file mode 100644
    index 0000000..32f26d8
    - +  
     1# http://packages.python.org/distribute/setuptools.html#namespace-packages
     2try:
     3    __import__('pkg_resources').declare_namespace(__name__)
     4except ImportError:
     5    from pkgutil import extend_path
     6    __path__ = extend_path(__path__, __name__)
  • new file tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/app2_command1.py

    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/app2_command1.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/app2_command1.py
    new file mode 100644
    index 0000000..b9e20a7
    - +  
     1from django.core.management.base import BaseCommand
     2
     3class Command(BaseCommand):
     4    help = 'Test managment commands in namespaced apps'
     5    requires_model_validation = False
     6    args = ''
     7
     8    def handle(self, *labels, **options):
     9        print 'EXECUTE:app2_command1'
  • new file tests/regressiontests/admin_scripts/lib3/_addsitedir.py

    diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/models.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib3/_addsitedir.py b/tests/regressiontests/admin_scripts/lib3/_addsitedir.py
    new file mode 100644
    index 0000000..9e264d2
    - +  
     1import os.path, site; site.addsitedir(os.path.dirname(__file__))
  • new file tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth

    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth b/tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth
    new file mode 100644
    index 0000000..1f31155
    - +  
     1import sys,new,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('exapps',)); ie = os.path.exists(os.path.join(p,'__init__.py')); m = not ie and sys.modules.setdefault('exapps',new.module('exapps')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)
  • new file tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/app3_command1.py

    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/__init__.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/__init__.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/__init__.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/app3_command1.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/app3_command1.py
    new file mode 100644
    index 0000000..97f5d33
    - +  
     1from django.core.management.base import BaseCommand
     2
     3class Command(BaseCommand):
     4    help = 'Test managment commands in namespaced apps'
     5    requires_model_validation = False
     6    args = ''
     7
     8    def handle(self, *labels, **options):
     9        print 'EXECUTE:app3_command1'
  • tests/regressiontests/admin_scripts/tests.py

    diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/models.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
    index 669c6e8..9ce60e8 100644
    a b class AdminScriptTestCase(unittest.TestCase): 
    9191    def run_test(self, script, args, settings_file=None, apps=None):
    9292        project_dir = os.path.dirname(test_dir)
    9393        base_dir = os.path.dirname(project_dir)
     94        lib1_dir = os.path.join(os.path.dirname(__file__), 'lib1')
     95        lib2_dir = os.path.join(os.path.dirname(__file__), 'lib2')
     96        lib3_dir = os.path.join(os.path.dirname(__file__), 'lib3')
    9497        ext_backend_base_dirs = self._ext_backend_paths()
    9598
    9699        # Remember the old environment
    class AdminScriptTestCase(unittest.TestCase): 
    108111            os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
    109112        elif 'DJANGO_SETTINGS_MODULE' in os.environ:
    110113            del os.environ['DJANGO_SETTINGS_MODULE']
    111         python_path = [project_dir, base_dir]
     114        python_path = [project_dir, base_dir, lib1_dir, lib2_dir, lib3_dir]
    112115        python_path.extend(ext_backend_base_dirs)
    113116        os.environ[python_path_var_name] = os.pathsep.join(python_path)
    114117
    class StartProject(LiveServerTestCase, AdminScriptTestCase): 
    15631566        self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir)
    15641567        self.assertFalse(os.path.exists(testproject_dir))
    15651568
     1569
     1570class NamespacePackagedApps(AdminScriptTestCase):
     1571    def setUp(self):
     1572        self.write_settings('settings.py', apps=['nons_app', 'nsapps.contrib.app1','nsapps.contrib.app2','exapps.app3'])
     1573        test_dir = os.path.dirname(os.path.dirname(__file__))
     1574        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
     1575        settings_file.write('import _addsitedir')
     1576        settings_file.close()
     1577
     1578    def tearDown(self):
     1579        self.remove_settings('settings.py')
     1580
     1581    def test_help(self):
     1582        out, err = self.run_manage(['help'])
     1583        self.assertNoOutput(err)
     1584        self.assertOutput(out, "nons_app_command1")
     1585        self.assertOutput(out, "app1_command1")
     1586        self.assertOutput(out, "app2_command1")
     1587        self.assertOutput(out, "app3_command1")
     1588
     1589    def test_nons_app(self):
     1590        args = ['nons_app_command1']
     1591        out, err = self.run_manage(args)
     1592        self.assertNoOutput(err)
     1593        self.assertOutput(out, "EXECUTE:nons_app_command1")
     1594
     1595    def test_nsapps(self):
     1596        args = ['app1_command1']
     1597        out, err = self.run_manage(args)
     1598        self.assertNoOutput(err)
     1599        self.assertOutput(out, "EXECUTE:app1_command1")
     1600
     1601        args = ['app2_command1']
     1602        out, err = self.run_manage(args)
     1603        self.assertNoOutput(err)
     1604        self.assertOutput(out, "EXECUTE:app2_command1")
     1605
     1606    def test_exapps(self):
     1607        args = ['app3_command1']
     1608        out, err = self.run_manage(args)
     1609        self.assertNoOutput(err)
     1610        self.assertOutput(out, "EXECUTE:app3_command1")
     1611
     1612class PreloadedNamespacePackagedApps(AdminScriptTestCase):
     1613    def setUp(self):
     1614        self.write_settings('settings.py', apps=['nsapps.contrib.app1','nsapps.contrib.app2'])
     1615        test_dir = os.path.dirname(os.path.dirname(__file__))
     1616        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
     1617        settings_file.write('import nsapps')
     1618        settings_file.close()
     1619       
     1620    def tearDown(self):
     1621        self.remove_settings('settings.py')
     1622
     1623    def test_help(self):
     1624        out, err = self.run_manage(['help'])
     1625        self.assertNoOutput(err)
     1626        self.assertOutput(out, "app1_command1")
     1627        self.assertOutput(out, "app2_command1")
     1628
     1629
     1630    def test_nsapps(self):
     1631        args = ['app1_command1']
     1632        out, err = self.run_manage(args)
     1633        self.assertNoOutput(err)
     1634        self.assertOutput(out, "EXECUTE:app1_command1")
     1635
     1636        args = ['app2_command1']
     1637        out, err = self.run_manage(args)
     1638        self.assertNoOutput(err)
     1639        self.assertOutput(out, "EXECUTE:app2_command1")
     1640
     1641
     1642class NonPackageManagementApps(AdminScriptTestCase):
     1643    def setUp(self):
     1644        self.write_settings('settings.py', apps=['npapp'])
     1645        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
     1646        settings_file.write('import npapp.management')
     1647        settings_file.close()
     1648
     1649    def tearDown(self):
     1650        self.remove_settings('settings.py')
     1651
     1652    def test_help(self):
     1653        out, err = self.run_manage(['help'])
     1654        self.assertNoOutput(err)
     1655
Back to Top