From b9c619abc101688fbbfa981525175f831d359483 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 22 Feb 2015 21:18:12 +0100 Subject: Prevented makemigrations from writing in sys.path[0]. There's no reason to assume that sys.path[0] is an appropriate location for generating code. Specifically that doesn't work with extend_sys_path which puts the additional directories at the end of sys.path. In order to create a new migrations module, instead of using an arbitrary entry from sys.path, import as much as possible from the path to the module, then create missing submodules from there. Without this change, the tests introduced in the following commit fail, which seems sufficient to prevent regressions for such a refactoring. --- django/utils/module_loading.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'django/utils/module_loading.py') diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 55d89caa46..5dc3aefe75 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -148,3 +148,21 @@ else: else: # Exhausted the search, so the module cannot be found. return False + + +def module_dir(module): + """ + Find the name of the directory that contains a module, if possible. + + Raise ValueError otherwise, e.g. for namespace packages that are split + over several directories. + """ + # Convert to list because _NamespacePath does not support indexing on 3.3. + paths = list(getattr(module, '__path__', [])) + if len(paths) == 1: + return paths[0] + else: + filename = getattr(module, '__file__', None) + if filename is not None: + return os.path.dirname(filename) + raise ValueError("Cannot determine directory containing %s" % module) -- cgit v1.3