From 02bdee42d4373ce7709f57a184735d0c3791eb1d Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 26 Apr 2012 22:26:46 +0400 Subject: [PATCH] fixed PY-3852 Since 1.5 I can not launch the django runserver via the PyCharm fixed PY-5076 Unable to run manage.py commands from PyCharm 2.0 beta --- python/helpers/pycharm/django_manage.py | 5 ++++- python/helpers/pycharm/django_test_manage.py | 16 +++++++------- .../python/run/PythonCommandLineState.java | 21 +++++++++++++++---- .../com/jetbrains/python/run/PythonTask.java | 9 ++++++-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/python/helpers/pycharm/django_manage.py b/python/helpers/pycharm/django_manage.py index 11df9fb900ae..245fd572dc44 100644 --- a/python/helpers/pycharm/django_manage.py +++ b/python/helpers/pycharm/django_manage.py @@ -1,4 +1,7 @@ #!/usr/bin/env python +import sys +base_path = sys.argv.pop() +sys.path.insert(0, base_path) from fix_getpass import fixGetpass import os from django.core.management import execute_manager @@ -14,5 +17,5 @@ if not manage_file: if __name__ == "__main__": fixGetpass() - run_module(manage_file, None, '__main__') + run_module(manage_file, None, '__main__', True) diff --git a/python/helpers/pycharm/django_test_manage.py b/python/helpers/pycharm/django_test_manage.py index 1c0c6b34cc03..1e1935ae8fb7 100644 --- a/python/helpers/pycharm/django_test_manage.py +++ b/python/helpers/pycharm/django_test_manage.py @@ -11,11 +11,13 @@ if not manage_file: settings_file = os.getenv('DJANGO_SETTINGS_MODULE') if not settings_file: - settings_file = 'settings' + settings_file = 'settings' -proj_name = sys.argv[-1] + +base_path = sys.argv.pop() +proj_name = base_path.split("/")[-1] +sys.path.insert(0, base_path) os.environ.setdefault('DJANGO_SETTINGS_MODULE',proj_name+".settings") - from django.core import management from django.core.management.commands.test import Command from django.conf import settings @@ -37,9 +39,9 @@ class PycharmTestCommand(Command): # handle south migration in tests management.get_commands() if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: - # point at the core syncdb command when creating tests - # tests should always be up to date with the most recent model structure - management._commands['syncdb'] = 'django.core' + # point at the core syncdb command when creating tests + # tests should always be up to date with the most recent model structure + management._commands['syncdb'] = 'django.core' else: try: from south.management.commands import MigrateAndSyncCommand @@ -97,7 +99,7 @@ if __name__ == "__main__": subcommand = 'help' # Display help if no arguments were given. if subcommand == 'test': - utility = PycharmTestManagementUtility(sys.argv[:-1]) + utility = PycharmTestManagementUtility(sys.argv) else: utility = ManagementUtility() utility.execute() \ No newline at end of file diff --git a/python/src/com/jetbrains/python/run/PythonCommandLineState.java b/python/src/com/jetbrains/python/run/PythonCommandLineState.java index 666afe4b57ec..9c046814e077 100644 --- a/python/src/com/jetbrains/python/run/PythonCommandLineState.java +++ b/python/src/com/jetbrains/python/run/PythonCommandLineState.java @@ -306,13 +306,21 @@ public abstract class PythonCommandLineState extends CommandLineState { private static void addToPythonPath(VirtualFile file, Collection pathList) { if (file.getFileSystem() instanceof JarFileSystem) { - VirtualFile realFile = JarFileSystem.getInstance().getVirtualFileForJar(file); + final VirtualFile realFile = JarFileSystem.getInstance().getVirtualFileForJar(file); if (realFile != null) { - pathList.add(FileUtil.toSystemDependentName(realFile.getPath())); + addIfNeeded(realFile, pathList); } } else { - pathList.add(FileUtil.toSystemDependentName(file.getPath())); + addIfNeeded(file, pathList); + } + } + + private static void addIfNeeded(@NotNull final VirtualFile file, @NotNull final Collection pathList) { + final Set vals = Sets.newHashSet(pathList); + final String filePath = FileUtil.toSystemDependentName(file.getPath()); + if (!vals.contains(filePath)) { + pathList.add(filePath); } } @@ -323,9 +331,14 @@ public abstract class PythonCommandLineState extends CommandLineState { @NotNull public static Collection collectPythonPath(@Nullable Module module) { + return collectPythonPath(module, true); + } + + @NotNull + public static Collection collectPythonPath(@Nullable Module module, final boolean addProjectRoots) { Collection pythonPathList = Sets.newLinkedHashSet(); pythonPathList.add(PythonHelpersLocator.getHelpersRoot().getPath()); - if (module != null) { + if (module != null && addProjectRoots) { addLibrariesFromModule(module, pythonPathList); Set dependencies = new HashSet(); ModuleUtil.getDependencies(module, dependencies); diff --git a/python/src/com/jetbrains/python/run/PythonTask.java b/python/src/com/jetbrains/python/run/PythonTask.java index 4da2829610c4..a354ff6491fc 100644 --- a/python/src/com/jetbrains/python/run/PythonTask.java +++ b/python/src/com/jetbrains/python/run/PythonTask.java @@ -142,8 +142,13 @@ public class PythonTask { } protected List setupPythonPath() { - List pythonPath = Lists.newArrayList(PythonCommandLineState.getAddedPaths(mySdk)); - pythonPath.addAll(PythonCommandLineState.collectPythonPath(myModule)); + return setupPythonPath(true); + } + + protected List setupPythonPath(final boolean addProjectRoot) { + final List pythonPath = Lists.newArrayList(PythonCommandLineState.getAddedPaths(mySdk)); + if (addProjectRoot) + pythonPath.addAll(PythonCommandLineState.collectPythonPath(myModule)); return pythonPath; }