Commit | Line | Data |
---|---|---|
3a9e38cc | 1 | import imp |
dc95c462 | 2 | import os |
3a9e38cc EMS |
3 | import sys |
4 | ||
5 | from django.core import management | |
6 | ||
7 | ||
8 | def main(settings_file): | |
9 | try: | |
10 | mod = __import__(settings_file) | |
11 | components = settings_file.split('.') | |
12 | for comp in components[1:]: | |
13 | mod = getattr(mod, comp) | |
14 | ||
15 | except ImportError, e: | |
16 | import sys | |
17 | sys.stderr.write("Error loading the settings module '%s': %s" | |
18 | % (settings_file, e)) | |
19 | return sys.exit(1) | |
20 | ||
21 | management.execute_manager(mod) | |
22 | ||
23 | # Fix pour le ticket Django #14087 (https://code.djangoproject.com/ticket/14087) | |
24 | # On applique le patch https://code.djangoproject.com/attachment/ticket/14087/namespace_package_pth.diff | |
25 | # par monkey patching. | |
26 | ||
27 | def find_modules(name, path=None): | |
28 | """Find all modules with name 'name' | |
29 | ||
30 | Unlike find_module in the imp package this returns a list of all | |
31 | matched modules. | |
32 | """ | |
33 | results = [] | |
34 | if path is None: path = sys.path | |
35 | for p in path: | |
36 | importer = sys.path_importer_cache.get(p, None) | |
37 | if importer is None: | |
38 | find_module = imp.find_module | |
39 | else: | |
40 | find_module = importer.find_module | |
41 | ||
42 | try: | |
43 | result = find_module(name, [p]) | |
44 | if result is not None: | |
45 | results.append(result) | |
46 | except ImportError: | |
47 | pass | |
48 | if not results: | |
49 | raise ImportError("No module named %.200s" % name) | |
50 | return results | |
51 | ||
52 | def find_management_module(app_name): | |
53 | """ | |
54 | Determines the path to the management module for the given app_name, | |
55 | without actually importing the application or the management module. | |
56 | ||
57 | Raises ImportError if the management module cannot be found for any reason. | |
58 | """ | |
59 | parts = app_name.split('.') | |
60 | ||
61 | for i in range(len(parts), 0, -1): | |
62 | try: | |
63 | paths = sys.modules['.'.join(parts[:i])].__path__ | |
64 | except KeyError: | |
65 | continue | |
66 | ||
67 | parts = parts[i:] + ['management'] | |
68 | parts.reverse() | |
69 | break | |
70 | else: | |
71 | parts.append('management') | |
72 | parts.reverse() | |
73 | part = parts.pop() | |
74 | paths = None | |
75 | ||
76 | # When using manage.py, the project module is added to the path, | |
77 | # loaded, then removed from the path. This means that | |
78 | # testproject.testapp.models can be loaded in future, even if | |
79 | # testproject isn't in the path. When looking for the management | |
80 | # module, we need look for the case where the project name is part | |
81 | # of the app_name but the project directory itself isn't on the path. | |
82 | try: | |
83 | modules = find_modules(part, paths) | |
8d784906 | 84 | paths = [m[1] for m in modules if isinstance(m, tuple)] |
3a9e38cc EMS |
85 | except ImportError,e: |
86 | if os.path.basename(os.getcwd()) != part: | |
87 | raise e | |
88 | ||
89 | while parts: | |
90 | part = parts.pop() | |
91 | modules = find_modules(part, paths) | |
92 | paths = [m[1] for m in modules] | |
93 | return paths[0] | |
94 | ||
95 | # Patch! | |
96 | management.find_management_module = find_management_module |