summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@oscaro.com>2015-02-22 21:18:12 +0100
committerAymeric Augustin <aymeric.augustin@oscaro.com>2015-02-23 19:55:10 +0100
commitb9c619abc101688fbbfa981525175f831d359483 (patch)
tree5107f1ee6b71afee6af9dba46a0fc4cb37575719 /django/utils
parent952ce778c66d2cc01969f0e6ee4edd16becf2e66 (diff)
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.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/module_loading.py18
1 files changed, 18 insertions, 0 deletions
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)