Ticket #14087: namespace_package_pth.2.diff

File namespace_package_pth.2.diff, 15.6 KB (added by bhuztez, 4 years ago)

update patch for revision 17517

  • 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/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 dd7e4b3..aa56c45 100644
    a b class AdminScriptTestCase(unittest.TestCase): 
    9090    def run_test(self, script, args, settings_file=None, apps=None):
    9191        project_dir = os.path.dirname(test_dir)
    9292        base_dir = os.path.dirname(project_dir)
     93        lib1_dir = os.path.join(os.path.dirname(__file__), 'lib1')
     94        lib2_dir = os.path.join(os.path.dirname(__file__), 'lib2')
     95        lib3_dir = os.path.join(os.path.dirname(__file__), 'lib3')
    9396        ext_backend_base_dirs = self._ext_backend_paths()
    9497
    9598        # Remember the old environment
    class AdminScriptTestCase(unittest.TestCase): 
    107110            os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
    108111        elif 'DJANGO_SETTINGS_MODULE' in os.environ:
    109112            del os.environ['DJANGO_SETTINGS_MODULE']
    110         python_path = [project_dir, base_dir]
     113        python_path = [project_dir, base_dir, lib1_dir, lib2_dir, lib3_dir]
    111114        python_path.extend(ext_backend_base_dirs)
    112115        os.environ[python_path_var_name] = os.pathsep.join(python_path)
    113116
    class StartProject(LiveServerTestCase, AdminScriptTestCase): 
    15331536            with open(os.path.join(base_path, f)) as fh:
    15341537                self.assertEqual(fh.read(),
    15351538                    '# some file for customtestproject test project')
     1539
     1540class NamespacePackagedApps(AdminScriptTestCase):
     1541    def setUp(self):
     1542        self.write_settings('settings.py', apps=['nons_app', 'nsapps.contrib.app1','nsapps.contrib.app2','exapps.app3'])
     1543        test_dir = os.path.dirname(os.path.dirname(__file__))
     1544        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
     1545        settings_file.write('import _addsitedir')
     1546        settings_file.close()
     1547
     1548    def tearDown(self):
     1549        self.remove_settings('settings.py')
     1550
     1551    def test_help(self):
     1552        out, err = self.run_manage(['help'])
     1553        self.assertNoOutput(err)
     1554        self.assertOutput(out, "nons_app_command1")
     1555        self.assertOutput(out, "app1_command1")
     1556        self.assertOutput(out, "app2_command1")
     1557        self.assertOutput(out, "app3_command1")
     1558
     1559    def test_nons_app(self):
     1560        args = ['nons_app_command1']
     1561        out, err = self.run_manage(args)
     1562        self.assertNoOutput(err)
     1563        self.assertOutput(out, "EXECUTE:nons_app_command1")
     1564
     1565    def test_nsapps(self):
     1566        args = ['app1_command1']
     1567        out, err = self.run_manage(args)
     1568        self.assertNoOutput(err)
     1569        self.assertOutput(out, "EXECUTE:app1_command1")
     1570
     1571        args = ['app2_command1']
     1572        out, err = self.run_manage(args)
     1573        self.assertNoOutput(err)
     1574        self.assertOutput(out, "EXECUTE:app2_command1")
     1575
     1576    def test_exapps(self):
     1577        args = ['app3_command1']
     1578        out, err = self.run_manage(args)
     1579        self.assertNoOutput(err)
     1580        self.assertOutput(out, "EXECUTE:app3_command1")
     1581
     1582class PreloadedNamespacePackagedApps(AdminScriptTestCase):
     1583    def setUp(self):
     1584        self.write_settings('settings.py', apps=['nsapps.contrib.app1','nsapps.contrib.app2'])
     1585        test_dir = os.path.dirname(os.path.dirname(__file__))
     1586        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
     1587        settings_file.write('import nsapps')
     1588        settings_file.close()
     1589       
     1590    def tearDown(self):
     1591        self.remove_settings('settings.py')
     1592
     1593    def test_help(self):
     1594        out, err = self.run_manage(['help'])
     1595        self.assertNoOutput(err)
     1596        self.assertOutput(out, "app1_command1")
     1597        self.assertOutput(out, "app2_command1")
     1598
     1599
     1600    def test_nsapps(self):
     1601        args = ['app1_command1']
     1602        out, err = self.run_manage(args)
     1603        self.assertNoOutput(err)
     1604        self.assertOutput(out, "EXECUTE:app1_command1")
     1605
     1606        args = ['app2_command1']
     1607        out, err = self.run_manage(args)
     1608        self.assertNoOutput(err)
     1609        self.assertOutput(out, "EXECUTE:app2_command1")
     1610
Back to Top