In a current project, I've built Python code for interacting with a specific datasource; now, I'm working on building a Scala version.
I've rearranged things so that all of the Python code lives in src/main/python
within the SBT project for my Scala code, but this got me thinking: Is there any nice way to integrate the project management between the two? To set up SBT so that I can run my Python distutils
installation/sdist generation or sphinx document generation as SBT tasks?
Or, more generally: Is there a standard method for running arbitrary system tasks by way of SBT?
You can create a python task which zips the source files. This example depends on the assembly task:
lazy val pythonAssembly = TaskKey[Unit]("pythonAssembly", "Zips all files in src/main/python")
lazy val pythonAssemblyTask = pythonAssembly := {
val baseDir = sourceDirectory.value
val targetDir = assembly.value.getParentFile.getParent
val target = new File(targetDir + s"/python/rfu-api-client-python-${Commons.appVersion}.zip")
val pythonBaseDir = new File(baseDir + "/main/python")
val pythonFiles = Path.allSubpaths(pythonBaseDir)
println("Zipping files in " + pythonBaseDir)
pythonFiles foreach { case (_, s) => println(s) }
IO.zip(pythonFiles, target)
println(s"Created $target")
To run Python unit tests of python code with SBT test tasks I did this in build.sbt:
//define task that should be run with tests.
val testPythonTask = TaskKey[Unit]("testPython", "Run python tests.")
val command = "python3 -m unittest app_test.py"
val workingDirectory = new File("python/working/directory")
testPythonTask := {
val s: TaskStreams = streams.value
s.log.info("Executing task testPython")
Process(command,
// optional
workingDirectory,
// optional system variables
"CLASSPATH" -> "path/to.jar",
"OTHER_SYS_VAR" -> "other_value") ! s.log
}
//attach custom test task to default test tasks
test in Test := {
testPythonTask.value
(test in Test).value
}
testOnly in Test := {
testPythonTask.value
(testOnly in Test).value
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With