Skip to content

Commit

Permalink
Fix buildPython tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Nov 15, 2023
1 parent 5cc130c commit 2f872c5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 49 deletions.
75 changes: 39 additions & 36 deletions product/gradle-plugin/src/main/kotlin/PythonTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,45 +55,39 @@ internal class TaskBuilder(
}
}

fun createBuildPackagesTask(): Provider<BuildPackagesTask> {
val taskName = "extractPythonBuildPackages"
try {
return project.tasks.named<BuildPackagesTask>(taskName)
} catch (e: UnknownDomainObjectException) {
return project.tasks.register<BuildPackagesTask>(taskName) {
var bp: List<String>?
try {
bp = findBuildPython()
} catch (e: BuildPythonException) {
bp = null
exception = e
}
inputs.property("buildPython", bp).optional(true)
fun createBuildPackagesTask() =
registerTask("extract", "buildPackages", BuildPackagesTask::class) {
var bp: List<String>?
try {
bp = findBuildPython()
} catch (e: BuildPythonException) {
bp = null
exception = e
}
inputs.property("buildPython", bp).optional(true)

// Keep the path short to avoid the the Windows 260-character limit.
outputFiles = project.fileTree(plugin.buildSubdir("env")) {
exclude("**/__pycache__")
}
// Keep the path short to avoid the the Windows 260-character limit.
outputFiles = project.fileTree(plugin.buildSubdir("env", variant)) {
exclude("**/__pycache__")
}

if (bp != null) {
doLast {
project.exec {
commandLine(bp)
args("-m", "venv", "--without-pip", outputDir)
}
if (bp != null) {
doLast {
project.exec {
commandLine(bp)
args("-m", "venv", "--without-pip", outputDir)
}

val zipPath = plugin.extractResource(
"gradle/build-packages.zip", plugin.buildSubdir())
project.copy {
from(project.zipTree(zipPath))
into(sitePackages)
}
project.delete(zipPath)
val zipPath = plugin.extractResource(
"gradle/build-packages.zip", plugin.buildSubdir())
project.copy {
from(project.zipTree(zipPath))
into(sitePackages)
}
project.delete(zipPath)
}
}
}
}

open class BuildPackagesTask : OutputDirTask() {
@get:Internal
Expand All @@ -112,19 +106,20 @@ internal class TaskBuilder(

@get:Internal
val sitePackages by lazy {
if (osName() == "windows") {
outputDir.resolve("Lib/site-packages")
val libPythonDir = if (osName() == "windows") {
assertExists(outputDir.resolve("Lib"))
} else {
val libDir = outputDir.resolve("lib")
val libDir = assertExists(outputDir.resolve("lib"))
val pythonDirs = libDir.listFiles()!!.filter {
it.name.startsWith("python")
}
if (pythonDirs.size != 1) {
throw GradleException(
"found ${pythonDirs.size} python directories in $libDir")
}
pythonDirs[0].resolve("site-packages")
pythonDirs[0]
}
libPythonDir.resolve("site-packages")
}
}

Expand Down Expand Up @@ -769,3 +764,11 @@ fun makeZip(tree: FileTree, outFile: File) {
})
}
}


fun assertExists(f: File) : File {
if (!f.exists()) {
throw GradleException("$f does not exist")
}
return f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'

android {
namespace "com.chaquo.python.test"
compileSdk 23
defaultConfig {
applicationId "com.chaquo.python.test"
minSdk 21
targetSdk 23
versionCode 1
versionName "0.0.1"
python {
}
ndk {
abiFilters "x86"
}
}
}
36 changes: 23 additions & 13 deletions product/gradle-plugin/src/test/integration/test_gradle_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,26 +599,35 @@ def test_variant_merge(self):
def test_syntax_error(self):
self.RunGradle("base", "Pyc/syntax_error", app=["bad.py", "good.pyc"], pyc=["stdlib"])

def test_build_python_warning(self):
run = self.RunGradle("base", "Pyc/build_python_warning", pyc=["stdlib"])
def test_buildpython_warning(self):
run = self.RunGradle("base", "Pyc/buildpython_warning", pyc=["stdlib"])
self.assertInStdout(
WARNING + self.FAILED +
re.escape(BuildPython.INVALID.format("pythoninvalid")) + self.SEE,
run, re=True)

run.apply_layers("Pyc/build_python_warning_suppress")
run.apply_layers("Pyc/buildpython_warning_suppress")
run.rerun(pyc=["stdlib"])
self.assertNotInLong(self.FAILED, run.stdout)

def test_build_python_error(self):
run = self.RunGradle("base", "Pyc/build_python_error", succeed=False)
def test_buildpython_error(self):
run = self.RunGradle("base", "Pyc/buildpython_error", succeed=False)
self.assertInStderr(
BuildPython.INVALID.format("pythoninvalid") + BuildPython.SEE, run)

def test_buildpython_missing(self):
run = self.RunGradle("base", "Pyc/buildpython_missing", "BuildPython/missing",
add_path=["bin"], succeed=False)
self.assertInStderr(BuildPython.MISSING, run)
def test_buildpython_missing_warning(self):
run = self.RunGradle(
"base", "Pyc/buildpython_missing_warning", "BuildPython/missing",
add_path=["bin"])
self.assertInStdout(
WARNING + self.FAILED + BuildPython.MISSING + self.SEE,
run, re=True)

def test_buildpython_missing_error(self):
run = self.RunGradle(
"base", "Pyc/buildpython_missing_error", "BuildPython/missing",
add_path=["bin"], succeed=False)
self.assertInStderr(BuildPython.MISSING + BuildPython.SEE, run)

def test_magic_warning(self):
run = self.RunGradle("base", "Pyc/magic_warning",
Expand All @@ -638,7 +647,7 @@ def test_magic_error(self):
class BuildPython(GradleTestCase):
# Some of these messages are also used in other test classes.
SEE = "See https://chaquo.com/chaquopy/doc/current/android.html#buildpython"
MISSING = "Couldn't find Python. " + SEE
MISSING = "Couldn't find Python. "
INVALID = "[{}] does not appear to be a valid Python command. "
FAILED = (r"Process 'command '.+'' finished with non-zero exit value 1 \n\n"
r"To view full details in Android Studio:\n"
Expand Down Expand Up @@ -706,7 +715,8 @@ def test_missing_both(self):
def test_silent_failure(self):
run = self.RunGradle("base", "BuildPython/silent_failure", succeed=False)
self.assertInStderr(
"common was not created: please check your buildPython setting", run)
f"{join('python', 'env', 'debug', '[Ll]ib')} does not exist",
run, re=True)

def test_variant(self):
run = self.RunGradle("base", "BuildPython/variant", variants=["red-debug"],
Expand Down Expand Up @@ -784,7 +794,7 @@ def test_buildpython_missing(self):
run = self.RunGradle(
"base", "PythonReqs/buildpython_missing", "BuildPython/missing",
add_path=["bin"], succeed=False)
self.assertInLong(BuildPython.MISSING, run.stderr)
self.assertInLong(BuildPython.MISSING + BuildPython.SEE, run.stderr)

def test_download_wheel(self):
# Our current version of pip shows the full URL for custom indexes, but only
Expand Down Expand Up @@ -1452,7 +1462,7 @@ def test_buildpython_missing(self):
run = self.RunGradle(
"base", "StaticProxy/buildpython_missing", "BuildPython/missing",
add_path=["bin"], succeed=False)
self.assertInLong(BuildPython.MISSING, run.stderr)
self.assertInLong(BuildPython.MISSING + BuildPython.SEE, run.stderr)

def test_change(self):
run = self.RunGradle("base", "StaticProxy/reqs", requirements=self.reqs,
Expand Down

0 comments on commit 2f872c5

Please sign in to comment.