diff --git a/python/build/groovy/org/jetbrains/intellij/build/pycharm/PyCharmPropertiesBase.groovy b/python/build/groovy/org/jetbrains/intellij/build/pycharm/PyCharmPropertiesBase.groovy index 7a9028a36299..f5cbfe3fb293 100644 --- a/python/build/groovy/org/jetbrains/intellij/build/pycharm/PyCharmPropertiesBase.groovy +++ b/python/build/groovy/org/jetbrains/intellij/build/pycharm/PyCharmPropertiesBase.groovy @@ -15,10 +15,7 @@ */ package org.jetbrains.intellij.build.pycharm -import org.jetbrains.intellij.build.ApplicationInfoProperties -import org.jetbrains.intellij.build.BuildContext -import org.jetbrains.intellij.build.BuildTasks -import org.jetbrains.intellij.build.ProductProperties +import org.jetbrains.intellij.build.* /** * @author nik @@ -45,9 +42,12 @@ abstract class PyCharmPropertiesBase extends ProductProperties { include(name: "*.pdf") } } - context.ant.copy(todir: "$targetDirectory/index", failonerror: false) { - fileset(dir: "$context.paths.projectHome/index", erroronmissingdir:false) { - include(name: "sdk-stubs*") + + new PyPrebuiltStubsGenerator().generateResources(context) + + context.ant.copy(todir: "$targetDirectory/index", failonerror: true) { + fileset(dir: "$context.paths.temp/index", erroronmissingdir:true) { + include(name: "*") } } } @@ -56,4 +56,27 @@ abstract class PyCharmPropertiesBase extends ProductProperties { String getEnvironmentVariableBaseName(ApplicationInfoProperties applicationInfo) { "PYCHARM" } +} + +class PyPrebuiltStubsGenerator implements ResourcesGenerator { + @Override + File generateResources(BuildContext context) { + CompilationTasks.create(context).compileModules(["python-community-tools"]) + List buildClasspath = context.getModuleRuntimeClasspath(context.findModule("python-community-tools"), false) + + def outputPath = "$context.paths.temp/index" + + context.ant.java(classname: "com.jetbrains.python.tools.PyPrebuiltStubsGeneratorKt", fork: true) { + jvmarg(line: "-ea -Xmx1500m") + arg(value: "$context.paths.projectHome/python-distributions") + arg(value: outputPath) + classpath { + buildClasspath.each { + pathelement(location: it) + } + } + } + + return new File(outputPath) + } } \ No newline at end of file diff --git a/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java b/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java index 1483737d9d3b..daa6759ef4b0 100644 --- a/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java +++ b/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java @@ -24,6 +24,9 @@ import org.jetbrains.annotations.NotNull; * @author traff */ public class PyPrebuiltStubsProvider extends PrebuiltStubsProviderBase { + + public static final String NAME = PythonFileType.INSTANCE.getName(); + @Override protected int getStubVersion() { return PyFileElementType.INSTANCE.getStubVersion(); @@ -32,6 +35,6 @@ public class PyPrebuiltStubsProvider extends PrebuiltStubsProviderBase { @NotNull @Override protected String getName() { - return PythonFileType.INSTANCE.getName(); + return NAME; } } diff --git a/python/tools/src/com/jetbrains/python/tools/BuildStubsForSdk.kt b/python/tools/src/com/jetbrains/python/tools/BuildStubsForSdk.kt index bf6e7452b435..c51fb930535b 100644 --- a/python/tools/src/com/jetbrains/python/tools/BuildStubsForSdk.kt +++ b/python/tools/src/com/jetbrains/python/tools/BuildStubsForSdk.kt @@ -78,7 +78,8 @@ fun packStdlibFromPath(baseDir: String, root: String) { val output = cph.runProcess() println(output.stdout + output.stderr) } - } finally { + } + finally { System.exit(0) } } @@ -89,13 +90,13 @@ class PyProjectSdkStubsGenerator : ProjectSdkStubsGenerator() { override fun createSdkProducer(sdkPath: String) = createPythonSdkProducer(sdkPath) - override fun createStubsGenerator() = PyStubsGenerator(stubsVersion) + override fun createStubsGenerator() = PyStubsGenerator() override val root: String? get() = System.getenv(PYCHARM_PYTHONS) } -class PyStubsGenerator(stubsVersion: String) : LanguageLevelAwareStubsGenerator(stubsVersion) { +class PyStubsGenerator : LanguageLevelAwareStubsGenerator(PyFileElementType.INSTANCE.stubVersion.toString()) { override fun defaultLanguageLevel(): LanguageLevel = LanguageLevel.getDefault() override fun languageLevelIterator() = LanguageLevel.SUPPORTED_LEVELS.iterator() diff --git a/python/tools/src/com/jetbrains/python/tools/PyPrebuiltStubsGenerator.kt b/python/tools/src/com/jetbrains/python/tools/PyPrebuiltStubsGenerator.kt new file mode 100644 index 000000000000..71d15522e202 --- /dev/null +++ b/python/tools/src/com/jetbrains/python/tools/PyPrebuiltStubsGenerator.kt @@ -0,0 +1,65 @@ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.jetbrains.python.tools + +import com.intellij.idea.IdeaTestApplication +import com.intellij.openapi.application.PathManager +import com.intellij.openapi.application.WriteAction +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.psi.stubs.PrebuiltStubsProviderBase +import com.intellij.testFramework.PlatformTestCase +import com.intellij.util.ReflectionUtil +import com.intellij.util.io.ZipUtil +import com.intellij.util.ui.UIUtil +import com.jetbrains.python.psi.impl.stubs.PyPrebuiltStubsProvider +import java.io.File + +fun main(args: Array) { + buildStubs(args[0], "${args[1]}/${PyPrebuiltStubsProvider.NAME}") +} + +fun buildStubs(root: String, outputPath: String) { + val app = createApp() + try { + FileUtil.delete(File(outputPath)) + + val roots = unzipArchivesToRoots(root) + + PyStubsGenerator().buildStubsForRoots("$outputPath/${PrebuiltStubsProviderBase.SDK_STUBS_STORAGE_NAME}", + ArrayList(roots.map { it -> LocalFileSystem.getInstance().findFileByIoFile(it)!! })) + } + catch (e: Throwable) { + e.printStackTrace() + } + finally { + UIUtil.invokeAndWaitIfNeeded(Runnable { + WriteAction.run { + app.dispose() + } + }) + FileUtil.delete(File(System.getProperty(PathManager.PROPERTY_PLUGINS_PATH))) + FileUtil.delete(File(System.getProperty(PathManager.PROPERTY_SYSTEM_PATH))) + System.exit(0) + } +} + +fun unzipArchivesToRoots(root: String): List { + val dir = File(root) + if (!dir.exists() || !dir.isDirectory) { + throw IllegalStateException("$root doesn't exist or isn't a directory") + } + return dir.listFiles { _, name -> name.endsWith(".zip") }.map { zip -> + val unzipRoot = File(root, zip.nameWithoutExtension) + println("Extracting $zip") + ZipUtil.extract(zip, unzipRoot, null) + unzipRoot + } +} + +private fun createApp(): IdeaTestApplication { + val candidates = ReflectionUtil.getField(PlatformTestCase::class.java, null, Array::class.java, "PREFIX_CANDIDATES") + candidates[0] = "PyCharm" + System.setProperty(PathManager.PROPERTY_PLUGINS_PATH, FileUtil.createTempDirectory("pystubs", "plugins").absolutePath) + System.setProperty(PathManager.PROPERTY_SYSTEM_PATH, FileUtil.createTempDirectory("pystubs", "system").absolutePath) + return IdeaTestApplication.getInstance() +}