Code

Ticket #14087: namespace_package_pth.5.diff

File namespace_package_pth.5.diff, 16.6 KB (added by bhuztez, 2 years ago)

remove trailing whitespaces

Line 
1diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
2index 8f32898..1214a82 100644
3--- a/django/core/management/__init__.py
4+++ b/django/core/management/__init__.py
5@@ -39,26 +39,56 @@ def find_management_module(app_name):
6     """
7     parts = app_name.split('.')
8     parts.append('management')
9-    parts.reverse()
10-    part = parts.pop()
11-    path = None
12-
13-    # When using manage.py, the project module is added to the path,
14-    # loaded, then removed from the path. This means that
15-    # testproject.testapp.models can be loaded in future, even if
16-    # testproject isn't in the path. When looking for the management
17-    # module, we need look for the case where the project name is part
18-    # of the app_name but the project directory itself isn't on the path.
19-    try:
20-        f, path, descr = imp.find_module(part,path)
21-    except ImportError,e:
22-        if os.path.basename(os.getcwd()) != part:
23-            raise e
24+
25+    for i in range(len(parts), 0, -1):
26+        try:
27+            path = sys.modules['.'.join(parts[:i])].__path__
28+        except AttributeError:
29+            raise ImportError("No package named %s" % parts[i-1])
30+        except KeyError:
31+            continue
32+
33+        parts = parts[i:]
34+        parts.reverse()
35+        break
36+    else:
37+        parts.reverse()
38+        part = parts.pop()
39+        path = sys.path
40+
41+        # When using manage.py, the project module is added to the path,
42+        # loaded, then removed from the path. This means that
43+        # testproject.testapp.models can be loaded in future, even if
44+        # testproject isn't in the path. When looking for the management
45+        # module, we need look for the case where the project name is part
46+        # of the app_name but the project directory itself isn't on the path.
47+        try:
48+            next_path = []
49+            for p in path:
50+                try:
51+                    next_path.append(imp.find_module(part, [p])[1])
52+                except ImportError:
53+                    pass
54+            if not next_path:
55+                raise ImportError("No module named %s" % part)
56+            path = next_path
57+        except ImportError,e:
58+            if os.path.basename(os.getcwd()) != part:
59+                raise e
60 
61     while parts:
62         part = parts.pop()
63-        f, path, descr = imp.find_module(part, path and [path] or None)
64-    return path
65+        next_path = []
66+        for p in path:
67+            try:
68+                next_path.append(imp.find_module(part, [p])[1])
69+            except ImportError:
70+                pass
71+        if not next_path:
72+            raise ImportError("No module named %s" % part)
73+        path = next_path
74+
75+    return path[0]
76 
77 def load_command_class(app_name, name):
78     """
79diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/__init__.py b/tests/regressiontests/admin_scripts/lib1/nons_app/__init__.py
80new file mode 100644
81index 0000000..e69de29
82diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/management/__init__.py b/tests/regressiontests/admin_scripts/lib1/nons_app/management/__init__.py
83new file mode 100644
84index 0000000..e69de29
85diff --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
86new file mode 100644
87index 0000000..e69de29
88diff --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
89new file mode 100644
90index 0000000..a393663
91--- /dev/null
92+++ b/tests/regressiontests/admin_scripts/lib1/nons_app/management/commands/nons_app_command1.py
93@@ -0,0 +1,9 @@
94+from django.core.management.base import BaseCommand
95+
96+class Command(BaseCommand):
97+    help = 'Test managment commands in non-namespaced app'
98+    requires_model_validation = False
99+    args = ''
100+
101+    def handle(self, *labels, **options):
102+        print 'EXECUTE:nons_app_command1'
103diff --git a/tests/regressiontests/admin_scripts/lib1/nons_app/models.py b/tests/regressiontests/admin_scripts/lib1/nons_app/models.py
104new file mode 100644
105index 0000000..e69de29
106diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/__init__.py b/tests/regressiontests/admin_scripts/lib1/npapp/__init__.py
107new file mode 100644
108index 0000000..e69de29
109diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/management.py b/tests/regressiontests/admin_scripts/lib1/npapp/management.py
110new file mode 100644
111index 0000000..e69de29
112diff --git a/tests/regressiontests/admin_scripts/lib1/npapp/models.py b/tests/regressiontests/admin_scripts/lib1/npapp/models.py
113new file mode 100644
114index 0000000..e69de29
115diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py
116new file mode 100644
117index 0000000..32f26d8
118--- /dev/null
119+++ b/tests/regressiontests/admin_scripts/lib1/nsapps/__init__.py
120@@ -0,0 +1,6 @@
121+# http://packages.python.org/distribute/setuptools.html#namespace-packages
122+try:
123+    __import__('pkg_resources').declare_namespace(__name__)
124+except ImportError:
125+    from pkgutil import extend_path
126+    __path__ = extend_path(__path__, __name__)
127diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py
128new file mode 100644
129index 0000000..32f26d8
130--- /dev/null
131+++ b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/__init__.py
132@@ -0,0 +1,6 @@
133+# http://packages.python.org/distribute/setuptools.html#namespace-packages
134+try:
135+    __import__('pkg_resources').declare_namespace(__name__)
136+except ImportError:
137+    from pkgutil import extend_path
138+    __path__ = extend_path(__path__, __name__)
139diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/__init__.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/__init__.py
140new file mode 100644
141index 0000000..e69de29
142diff --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
143new file mode 100644
144index 0000000..e69de29
145diff --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
146new file mode 100644
147index 0000000..e69de29
148diff --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
149new file mode 100644
150index 0000000..2f479bb
151--- /dev/null
152+++ b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/management/commands/app1_command1.py
153@@ -0,0 +1,9 @@
154+from django.core.management.base import BaseCommand
155+
156+class Command(BaseCommand):
157+    help = 'Test managment commands in namespaced apps'
158+    requires_model_validation = False
159+    args = ''
160+
161+    def handle(self, *labels, **options):
162+        print 'EXECUTE:app1_command1'
163diff --git a/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/models.py b/tests/regressiontests/admin_scripts/lib1/nsapps/contrib/app1/models.py
164new file mode 100644
165index 0000000..e69de29
166diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py
167new file mode 100644
168index 0000000..32f26d8
169--- /dev/null
170+++ b/tests/regressiontests/admin_scripts/lib2/nsapps/__init__.py
171@@ -0,0 +1,6 @@
172+# http://packages.python.org/distribute/setuptools.html#namespace-packages
173+try:
174+    __import__('pkg_resources').declare_namespace(__name__)
175+except ImportError:
176+    from pkgutil import extend_path
177+    __path__ = extend_path(__path__, __name__)
178diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py
179new file mode 100644
180index 0000000..32f26d8
181--- /dev/null
182+++ b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/__init__.py
183@@ -0,0 +1,6 @@
184+# http://packages.python.org/distribute/setuptools.html#namespace-packages
185+try:
186+    __import__('pkg_resources').declare_namespace(__name__)
187+except ImportError:
188+    from pkgutil import extend_path
189+    __path__ = extend_path(__path__, __name__)
190diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/__init__.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/__init__.py
191new file mode 100644
192index 0000000..e69de29
193diff --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
194new file mode 100644
195index 0000000..e69de29
196diff --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
197new file mode 100644
198index 0000000..e69de29
199diff --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
200new file mode 100644
201index 0000000..b9e20a7
202--- /dev/null
203+++ b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/management/commands/app2_command1.py
204@@ -0,0 +1,9 @@
205+from django.core.management.base import BaseCommand
206+
207+class Command(BaseCommand):
208+    help = 'Test managment commands in namespaced apps'
209+    requires_model_validation = False
210+    args = ''
211+
212+    def handle(self, *labels, **options):
213+        print 'EXECUTE:app2_command1'
214diff --git a/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/models.py b/tests/regressiontests/admin_scripts/lib2/nsapps/contrib/app2/models.py
215new file mode 100644
216index 0000000..e69de29
217diff --git a/tests/regressiontests/admin_scripts/lib3/_addsitedir.py b/tests/regressiontests/admin_scripts/lib3/_addsitedir.py
218new file mode 100644
219index 0000000..9e264d2
220--- /dev/null
221+++ b/tests/regressiontests/admin_scripts/lib3/_addsitedir.py
222@@ -0,0 +1 @@
223+import os.path, site; site.addsitedir(os.path.dirname(__file__))
224diff --git a/tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth b/tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth
225new file mode 100644
226index 0000000..1f31155
227--- /dev/null
228+++ b/tests/regressiontests/admin_scripts/lib3/exapps-nspkg.pth
229@@ -0,0 +1 @@
230+import 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)
231diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/__init__.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/__init__.py
232new file mode 100644
233index 0000000..e69de29
234diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/__init__.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/__init__.py
235new file mode 100644
236index 0000000..e69de29
237diff --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
238new file mode 100644
239index 0000000..e69de29
240diff --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
241new file mode 100644
242index 0000000..97f5d33
243--- /dev/null
244+++ b/tests/regressiontests/admin_scripts/lib3/exapps/app3/management/commands/app3_command1.py
245@@ -0,0 +1,9 @@
246+from django.core.management.base import BaseCommand
247+
248+class Command(BaseCommand):
249+    help = 'Test managment commands in namespaced apps'
250+    requires_model_validation = False
251+    args = ''
252+
253+    def handle(self, *labels, **options):
254+        print 'EXECUTE:app3_command1'
255diff --git a/tests/regressiontests/admin_scripts/lib3/exapps/app3/models.py b/tests/regressiontests/admin_scripts/lib3/exapps/app3/models.py
256new file mode 100644
257index 0000000..e69de29
258diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
259index 68ebda0..45852ba 100644
260--- a/tests/regressiontests/admin_scripts/tests.py
261+++ b/tests/regressiontests/admin_scripts/tests.py
262@@ -90,6 +90,9 @@ class AdminScriptTestCase(unittest.TestCase):
263     def run_test(self, script, args, settings_file=None, apps=None):
264         project_dir = os.path.dirname(test_dir)
265         base_dir = os.path.dirname(project_dir)
266+        lib1_dir = os.path.join(os.path.dirname(__file__), 'lib1')
267+        lib2_dir = os.path.join(os.path.dirname(__file__), 'lib2')
268+        lib3_dir = os.path.join(os.path.dirname(__file__), 'lib3')
269         ext_backend_base_dirs = self._ext_backend_paths()
270 
271         # Remember the old environment
272@@ -107,7 +110,7 @@ class AdminScriptTestCase(unittest.TestCase):
273             os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
274         elif 'DJANGO_SETTINGS_MODULE' in os.environ:
275             del os.environ['DJANGO_SETTINGS_MODULE']
276-        python_path = [project_dir, base_dir]
277+        python_path = [project_dir, base_dir, lib1_dir, lib2_dir, lib3_dir]
278         python_path.extend(ext_backend_base_dirs)
279         os.environ[python_path_var_name] = os.pathsep.join(python_path)
280 
281@@ -1552,3 +1555,89 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
282         self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir)
283         self.assertFalse(os.path.exists(testproject_dir))
284 
285+
286+class NamespacePackagedApps(AdminScriptTestCase):
287+    def setUp(self):
288+        self.write_settings('settings.py', apps=['nons_app', 'nsapps.contrib.app1','nsapps.contrib.app2','exapps.app3'])
289+        test_dir = os.path.dirname(os.path.dirname(__file__))
290+        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
291+        settings_file.write('import _addsitedir')
292+        settings_file.close()
293+
294+    def tearDown(self):
295+        self.remove_settings('settings.py')
296+
297+    def test_help(self):
298+        out, err = self.run_manage(['help'])
299+        self.assertNoOutput(err)
300+        self.assertOutput(out, "nons_app_command1")
301+        self.assertOutput(out, "app1_command1")
302+        self.assertOutput(out, "app2_command1")
303+        self.assertOutput(out, "app3_command1")
304+
305+    def test_nons_app(self):
306+        args = ['nons_app_command1']
307+        out, err = self.run_manage(args)
308+        self.assertNoOutput(err)
309+        self.assertOutput(out, "EXECUTE:nons_app_command1")
310+
311+    def test_nsapps(self):
312+        args = ['app1_command1']
313+        out, err = self.run_manage(args)
314+        self.assertNoOutput(err)
315+        self.assertOutput(out, "EXECUTE:app1_command1")
316+
317+        args = ['app2_command1']
318+        out, err = self.run_manage(args)
319+        self.assertNoOutput(err)
320+        self.assertOutput(out, "EXECUTE:app2_command1")
321+
322+    def test_exapps(self):
323+        args = ['app3_command1']
324+        out, err = self.run_manage(args)
325+        self.assertNoOutput(err)
326+        self.assertOutput(out, "EXECUTE:app3_command1")
327+
328+class PreloadedNamespacePackagedApps(AdminScriptTestCase):
329+    def setUp(self):
330+        self.write_settings('settings.py', apps=['nsapps.contrib.app1','nsapps.contrib.app2'])
331+        test_dir = os.path.dirname(os.path.dirname(__file__))
332+        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
333+        settings_file.write('import nsapps')
334+        settings_file.close()
335+
336+    def tearDown(self):
337+        self.remove_settings('settings.py')
338+
339+    def test_help(self):
340+        out, err = self.run_manage(['help'])
341+        self.assertNoOutput(err)
342+        self.assertOutput(out, "app1_command1")
343+        self.assertOutput(out, "app2_command1")
344+
345+    def test_nsapps(self):
346+        args = ['app1_command1']
347+        out, err = self.run_manage(args)
348+        self.assertNoOutput(err)
349+        self.assertOutput(out, "EXECUTE:app1_command1")
350+
351+        args = ['app2_command1']
352+        out, err = self.run_manage(args)
353+        self.assertNoOutput(err)
354+        self.assertOutput(out, "EXECUTE:app2_command1")
355+
356+
357+class NonPackageManagementApps(AdminScriptTestCase):
358+    def setUp(self):
359+        self.write_settings('settings.py', apps=['npapp'])
360+        settings_file = open(os.path.join(test_dir, 'settings.py'), 'a')
361+        settings_file.write('import npapp.management')
362+        settings_file.close()
363+
364+    def tearDown(self):
365+        self.remove_settings('settings.py')
366+
367+    def test_help(self):
368+        out, err = self.run_manage(['help'])
369+        self.assertNoOutput(err)
370+