Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcellustavares/687b3413957d6bcd52a6705c04942ab6 to your computer and use it in GitHub Desktop.
Save marcellustavares/687b3413957d6bcd52a6705c04942ab6 to your computer and use it in GitHub Desktop.
import com.jetbrains.python.envs.PythonEnvsPlugin
import com.liferay.gradle.util.FileUtil
import org.apache.tools.ant.taskdefs.condition.Os
buildscript {
dependencies {
classpath group: "com.liferay", name: "com.liferay.gradle.plugins.defaults", version: "latest.release"
classpath group: "gradle.plugin.com.jetbrains.python", name: "gradle-python-envs", version: "0.0.30"
}
repositories {
maven {
url "https://repository-cdn.liferay.com/nexus/content/groups/public"
}
}
}
configure(subprojects.findAll { FileUtil.exists(it, "src/main/python")}) {
apply plugin: PythonEnvsPlugin
task formatSourcePython()
task installBootstrapEnvDependencies()
task installProjectEnvDependencies()
task packagePython()
def bootstrapEnvName = "Miniconda3"
def pythonSource = "src/main/python"
envs {
bootstrapDirectory = new File(projectDir, '.bootstrap')
envsDirectory = new File(buildDir, '.envs')
conda bootstrapEnvName, "Miniconda3-py38_4.8.2", "64"
condaenv project.name, "3.8", "Miniconda3"
}
formatSourcePython {
def bootstrapEnvDir = "$project.envs.bootstrapDirectory/$bootstrapEnvName"
doLast {
exec {
commandLine _executablePath(bootstrapEnvDir, "python"), "-m", "yapf", "-i", "--recursive", pythonSource
}
}
}
installBootstrapEnvDependencies {
def bootstrapEnvDir = "$project.envs.bootstrapDirectory/$bootstrapEnvName"
doLast {
println "installBootstrapEnvDependencies"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
copy {
from file("$bootstrapEnvDir/Library/bin/libcrypto-1_1-x64.dll"), file("$bootstrapEnvDir/Library/bin/libssl-1_1-x64.dll")
into file("$bootstrapEnvDir/DLLs")
}
}
exec {
commandLine _executablePath(bootstrapEnvDir, "pip"), "install", "yapf"
}
}
}
installProjectEnvDependencies {
def projectEnvDir = "$project.envs.envsDirectory/$project.name"
doLast {
println "installProjectEnvDependencies"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
copy {
from file("$projectEnvDir/Library/bin/libcrypto-1_1-x64.dll"), file("$projectEnvDir/Library/bin/libssl-1_1-x64.dll")
into file("$projectEnvDir/DLLs")
}
}
print projectEnvDir
exec {
commandLine _executablePath(projectEnvDir, "pip"), "install", "-r", "$projectDir/$pythonSource/requirements.txt"
}
}
}
packagePython {
def buildLibs = new File("$buildDir/libs")
def pythonEnvDir = "$buildDir/.envs/$project.name"
def pythonTempDir = "$buildDir/python-temp"
doLast {
copy {
from pythonSource
into pythonTempDir
exclude "requirements-package.txt"
}
if (!buildLibs.exists()) {
buildLibs.mkdirs()
}
exec {
commandLine _executablePath(pythonEnvDir, "pip"), "install", "-r", "$projectDir/$pythonSource/requirements-package.txt", "--target", pythonTempDir
}
exec {
commandLine _executablePath(pythonEnvDir, "python"), "-m", "zipapp", pythonTempDir, "-o", "$buildDir/libs/${project.name}.py"
}
delete pythonTempDir
}
}
project.afterEvaluate {
assemble {
dependsOn packagePython
}
formatSourcePython {
dependsOn installProjectEnvDependencies
}
installBootstrapEnvDependencies {
dependsOn build_condas
}
installProjectEnvDependencies {
dependsOn build_conda_envs
}
packagePython {
dependsOn installBootstrapEnvDependencies
dependsOn installProjectEnvDependencies
installProjectEnvDependencies.mustRunAfter(installBootstrapEnvDependencies)
}
}
}
private String _executablePath(String pythonEnvDir, String executable) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
if (Objects.equals(executable, "python")) {
return "$pythonEnvDir/$executable"
}
else {
return "$pythonEnvDir/Scripts/$executable"
}
}
else {
return "$pythonEnvDir/bin/$executable"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment