fixed PY-12819 Unable to run GAE nosetests
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Wed, 30 Apr 2014 10:14:58 +0000 (14:14 +0400)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Wed, 30 Apr 2014 10:14:58 +0000 (14:14 +0400)
Regression produced in 67046c75

PyCharm actually has python specific libraries and such libraries should be included in Python Path in PyCharm.
Created special Python library type to mark this kind of libraries.

python/src/META-INF/python-core.xml
python/src/com/jetbrains/python/buildout/BuildoutFacet.java
python/src/com/jetbrains/python/facet/FacetLibraryConfigurator.java
python/src/com/jetbrains/python/library/PythonLibraryType.java [new file with mode: 0644]
python/src/com/jetbrains/python/run/PythonCommandLineState.java

index c15dcce907dde2577bfee89ca5c116c47debe88e..856bf074d36774db3d2815c32a1750c3360b4dec 100644 (file)
@@ -6,6 +6,7 @@
   <module value="com.intellij.modules.python"/>
 
   <extensions defaultExtensionNs="com.intellij">
+    <library.type implementation="com.jetbrains.python.library.PythonLibraryType"/>
     <targetElement implementation="com.jetbrains.python.magicLiteral.PyMagicTargetElementExtensionPoint"/>
     <nameSuggestionProvider implementation="com.jetbrains.python.refactoring.PyNameSuggestionProvider"/>
     <methodNavigationOffsetProvider implementation="com.jetbrains.python.codeInsight.PyMethodNavigationOffsetProvider"/>
index e5e607a7f4ccb20f03f3648872e56226dfd3833c..0ba5e7924f3f93af25dce57f20b7676dccf96ef1 100644 (file)
@@ -365,10 +365,10 @@ public class BuildoutFacet extends Facet<BuildoutFacetConfiguration> implements
       return;
     }
     final List<String> paths = facet.getConfiguration().getPaths();
-    FacetLibraryConfigurator.attachLibrary(module, null, BUILDOUT_LIB_NAME, paths);
+    FacetLibraryConfigurator.attachPythonLibrary(module, null, BUILDOUT_LIB_NAME, paths);
   }
 
   public static void detachLibrary(final Module module) {
-    FacetLibraryConfigurator.detachLibrary(module, BUILDOUT_LIB_NAME);
+    FacetLibraryConfigurator.detachPythonLibrary(module, BUILDOUT_LIB_NAME);
   }
 }
index 7f01830ef3edac418e2aa03ed1bc8098a1101485..a2a60e4384122874e8a1287b8713275c57bf1871 100644 (file)
@@ -21,11 +21,14 @@ import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.*;
 import com.intellij.openapi.roots.impl.OrderEntryUtil;
+import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
+import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
+import com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable;
 import com.intellij.openapi.roots.libraries.Library;
-import com.intellij.openapi.roots.libraries.LibraryTable;
 import com.intellij.openapi.vfs.JarFileSystem;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.jetbrains.python.library.PythonLibraryType;
 import org.jetbrains.annotations.Nullable;
 
 import java.util.Collections;
@@ -40,7 +43,10 @@ public class FacetLibraryConfigurator {
   private FacetLibraryConfigurator() {
   }
 
-  public static void attachLibrary(final Module module, @Nullable final ModifiableRootModel existingModel, final String libraryName, final List<String> paths) {
+  public static void attachPythonLibrary(final Module module,
+                                         @Nullable final ModifiableRootModel existingModel,
+                                         final String libraryName,
+                                         final List<String> paths) {
     final ModifiableModelsProvider modelsProvider = ModifiableModelsProvider.SERVICE.getInstance();
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       public void run() {
@@ -51,6 +57,12 @@ public class FacetLibraryConfigurator {
           // update existing
           Library lib = orderEntry.getLibrary();
           if (lib != null) {
+            if (lib instanceof LibraryImpl && ((LibraryImpl)lib).getKind() == null) {   // replace old python libraries with python specific ones
+              model.removeOrderEntry(orderEntry);
+              ProjectLibraryTable.getInstance(model.getProject()).removeLibrary(lib);
+              createNewLibrary(model);
+              return;
+            }
             fillLibrary(module.getProject(), lib, paths);
             if (existingModel == null) {
               modelsProvider.commitModuleModifiableModel(model);
@@ -59,8 +71,13 @@ public class FacetLibraryConfigurator {
           }
         }
         // create new
-        final LibraryTable.ModifiableModel projectLibrariesModel = modelsProvider.getLibraryTableModifiableModel(model.getProject());
-        Library lib = projectLibrariesModel.createLibrary(libraryName);
+        createNewLibrary(model);
+      }
+
+      private void createNewLibrary(ModifiableRootModel model) {
+        final LibraryTableBase.ModifiableModelEx projectLibrariesModel =
+          (LibraryTableBase.ModifiableModelEx)modelsProvider.getLibraryTableModifiableModel(model.getProject());
+        Library lib = projectLibrariesModel.createLibrary(libraryName, PythonLibraryType.getInstance().getKind());
         fillLibrary(module.getProject(), lib, paths);
         projectLibrariesModel.commit();
         model.addLibraryEntry(lib);
@@ -101,7 +118,7 @@ public class FacetLibraryConfigurator {
     modifiableModel.commit();
   }
 
-  public static void detachLibrary(final Module module, final String libraryName) {
+  public static void detachPythonLibrary(final Module module, final String libraryName) {
     final ModifiableModelsProvider modelsProvider = ModifiableModelsProvider.SERVICE.getInstance();
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       public void run() {
diff --git a/python/src/com/jetbrains/python/library/PythonLibraryType.java b/python/src/com/jetbrains/python/library/PythonLibraryType.java
new file mode 100644 (file)
index 0000000..70925e9
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2000-2012 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.library;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.libraries.DummyLibraryProperties;
+import com.intellij.openapi.roots.libraries.LibraryType;
+import com.intellij.openapi.roots.libraries.NewLibraryConfiguration;
+import com.intellij.openapi.roots.libraries.PersistentLibraryKind;
+import com.intellij.openapi.roots.libraries.ui.LibraryEditorComponent;
+import com.intellij.openapi.roots.libraries.ui.LibraryPropertiesEditor;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import javax.swing.*;
+
+/**
+ * python library type should be used if library paths to be included in Python Path
+ */
+public class PythonLibraryType extends LibraryType<DummyLibraryProperties> {
+  private static final PersistentLibraryKind<DummyLibraryProperties> LIBRARY_KIND = new PersistentLibraryKind<DummyLibraryProperties>("python") {
+    @NotNull
+    @Override
+    public DummyLibraryProperties createDefaultProperties() {
+      return DummyLibraryProperties.INSTANCE;
+    }
+  };
+
+  protected PythonLibraryType() {
+    super(LIBRARY_KIND);
+  }
+
+  public static PythonLibraryType getInstance() {
+    return EP_NAME.findExtension(PythonLibraryType.class);
+  }
+
+  @Override
+  public String getCreateActionName() {
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public NewLibraryConfiguration createNewLibrary(@NotNull JComponent parentComponent,
+                                                  @Nullable VirtualFile contextDirectory,
+                                                  @NotNull Project project) {
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public LibraryPropertiesEditor createPropertiesEditor(@NotNull LibraryEditorComponent<DummyLibraryProperties> editorComponent) {
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public Icon getIcon() {
+    return null;
+  }
+
+}
index 5d4bb47a7296fb7b9a2484233498b6963e52f601..216e68c58e1d9d0a884fdc83178f47f9579e983d 100644 (file)
@@ -40,6 +40,9 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.projectRoots.Sdk;
 import com.intellij.openapi.projectRoots.SdkAdditionalData;
 import com.intellij.openapi.roots.*;
+import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.PersistentLibraryKind;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.vfs.JarFileSystem;
@@ -52,6 +55,7 @@ import com.jetbrains.python.debugger.PyDebugRunner;
 import com.jetbrains.python.debugger.PyDebuggerOptionsProvider;
 import com.jetbrains.python.facet.LibraryContributingFacet;
 import com.jetbrains.python.facet.PythonPathContributingFacet;
+import com.jetbrains.python.library.PythonLibraryType;
 import com.jetbrains.python.sdk.PySdkUtil;
 import com.jetbrains.python.sdk.PythonEnvUtil;
 import com.jetbrains.python.sdk.PythonSdkAdditionalData;
@@ -378,9 +382,6 @@ public abstract class PythonCommandLineState extends CommandLineState {
   }
 
   private static void addLibrariesFromModule(Module module, Collection<String> list) {
-    if (PlatformUtils.isPyCharm()) {
-      return;
-    }
     final OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
     for (OrderEntry entry : entries) {
       if (entry instanceof LibraryOrderEntry) {
@@ -390,7 +391,16 @@ public abstract class PythonCommandLineState extends CommandLineState {
           continue;
         }
         for (VirtualFile root : ((LibraryOrderEntry)entry).getRootFiles(OrderRootType.CLASSES)) {
-          addToPythonPath(root, list);
+          final Library library = ((LibraryOrderEntry)entry).getLibrary();
+          if (!PlatformUtils.isPyCharm()) {
+            addToPythonPath(root, list);
+          }
+          else if (library instanceof LibraryImpl) {
+            final PersistentLibraryKind<?> kind = ((LibraryImpl)library).getKind();
+            if (kind == PythonLibraryType.getInstance().getKind()) {
+              addToPythonPath(root, list);
+            }
+          }
         }
       }
     }