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/3696f95b8ca58f71f683a30ec956e731 to your computer and use it in GitHub Desktop.
Save marcellustavares/3696f95b8ca58f71f683a30ec956e731 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 configureVirtualEnv()
task formatSourcePython()
task packagePython()
def pythonSource = "src/main/python"
envs {
bootstrapDirectory = new File(projectDir, '.bootstrap')
envsDirectory = new File(buildDir, '.envs')
conda "Miniconda3", "Miniconda3-py38_4.8.2", "64"
}
configureVirtualEnv {
doFirst {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
exec {
commandLine "cmd.exe", "/c", "mklink", "$project.envs.bootstrapDirectory/Miniconda3/DLLs/libcrypto-1_1-x64.dll", "$project.envs.bootstrapDirectory/Miniconda3/Library/bin/libcrypto-1_1-x64.dll"
}
exec {
commandLine "cmd.exe", "/c", "mklink", "$project.envs.bootstrapDirectory/Miniconda3/DLLs/libssl-1_1-x64.dll", "$project.envs.bootstrapDirectory/Miniconda3/Library/bin/libssl-1_1-x64.dll"
}
}
}
URI requirementsURI = new File(projectDir.path + "/$pythonSource/requirements.txt").toURI()
File requirementsFile = FileUtil.get(project, requirementsURI.toString())
project.envs {
condaenv project.name, "3.8", "Miniconda3", requirementsFile.readLines()
}
}
formatSourcePython {
def pythonEnvDir = "$project.envs.bootstrapDirectory/Miniconda3"
doLast {
exec {
commandLine _executablePath(pythonEnvDir, "python"), "-m", "yapf", "-i", "--recursive", pythonSource
}
}
}
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
}
build_envs {
dependsOn configureVirtualEnv
}
formatSource {
dependsOn build_envs
}
packagePython {
dependsOn build_envs
}
}
}
private String _executablePath(String pythonEnvDir, String executable) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
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