使用SBT管理包含Scala和Python的项目

Nic*_*son 8 python scala sbt

在当前的项目中,我构建了用于与特定数据源交互的Python代码; 现在,我正在构建一个Scala版本.

我重新安排了一些事情,以便所有Python代码都存在于src/main/python我的Scala代码的SBT项目中,但这让我想到:有没有什么好方法可以在两者之间集成项目管理?设置SBT以便我可以将Python distutils安装/ sdist生成或sphinx文档生成作为SBT任务运行?

或者,更一般地说:是否有通过SBT运行任意系统任务的标准方法?

Mik*_*lot 5

您可以创建一个压缩源文件的 python 任务。此示例取决于组装任务:

  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")
Run Code Online (Sandbox Code Playgroud)


Eug*_*kin 5

为了使用 SBT测试任务运行python 代码的Python单元测试,我在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
}
Run Code Online (Sandbox Code Playgroud)


myy*_*yyk 2

来自文档(http://www.scala-sbt.org/0.13/docs/Process.html):

sbt 包含一个进程库来简化与外部进程的工作。该库无需在构建定义中以及由 consoleProject 任务启动的解释器中导入即可使用。

要运行外部命令,请在其后添加感叹号!:

"find project -name *.jar" !
Run Code Online (Sandbox Code Playgroud)