Skip to content

Commit fed9423

Browse files
committed
Properly detect headless
1 parent 6b75499 commit fed9423

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/main/PythonExtension.scala

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.json.JsonReadFeature
44
import com.fasterxml.jackson.databind.json.JsonMapper
55
import com.fasterxml.jackson.databind.ObjectMapper
66

7+
import java.awt.GraphicsEnvironment
78
import java.io.{ BufferedReader, Closeable, File, IOException, InputStreamReader }
89
import java.lang.ProcessBuilder.Redirect
910
import java.nio.file.Paths
@@ -31,6 +32,8 @@ object PythonExtension {
3132
var menu: Option[Menu] = None
3233
val config: Config = Config.createForPropertyFile(classOf[PythonExtension], PythonExtension.codeName)
3334

35+
var headless = GraphicsEnvironment.isHeadless || System.getProperty("org.nlogo.preferHeadless") == "true"
36+
3437
def pythonProcess: Subprocess = {
3538
_pythonProcess.getOrElse(throw new ExtensionException(
3639
"Python process has not been started. Please run PY:SETUP before any other python extension primitive."))
@@ -49,7 +52,6 @@ object PythonExtension {
4952
}
5053

5154
class PythonExtension extends api.DefaultClassManager {
52-
5355
override def load(manager: api.PrimitiveManager): Unit = {
5456
manager.addPrimitive("setup", SetupPython)
5557
manager.addPrimitive("run", Run)
@@ -70,9 +72,7 @@ class PythonExtension extends api.DefaultClassManager {
7072
override def runOnce(em: ExtensionManager): Unit = {
7173
super.runOnce(em)
7274

73-
em.asInstanceOf[WorkspaceExtensionManager].workspace.asInstanceOf[AbstractWorkspace].isHeadless
74-
75-
val headless: Boolean = em match {
75+
PythonExtension.headless = PythonExtension.headless | (em match {
7676
case wem: WorkspaceExtensionManager =>
7777
wem.workspace match {
7878
case aw: AbstractWorkspace if aw.isHeadless =>
@@ -84,26 +84,25 @@ class PythonExtension extends api.DefaultClassManager {
8484

8585
case _ =>
8686
false
87-
}
87+
})
8888

89-
val extraProperties: Seq[ConfigProperty] = {
90-
if (headless) {
91-
Seq()
92-
} else {
93-
val py2Message = s"It is recommended to use Python 3 if possible and enter its path above. If you must use Python 2, enter the path to its executable folder below."
94-
val py2Property = new FileProperty("python2", "python2", PythonExtension.config.get("python2").getOrElse(""), py2Message)
89+
if (!PythonExtension.headless) {
90+
println(GraphicsEnvironment.isHeadless)
91+
println(System.getProperty("org.nlogo.preferHeadless"))
9592

96-
Seq(py2Property)
97-
}
98-
}
93+
val py2Message = s"It is recommended to use Python 3 if possible and enter its path above. If you must use Python 2, enter the path to its executable folder below."
94+
val py2Property = new FileProperty("python2", "python2", PythonExtension.config.get("python2").getOrElse(""), py2Message)
9995

100-
PythonExtension.menu = Menu.create(em, PythonExtension.longName, PythonExtension.extLangBin, PythonExtension.config, extraProperties)
96+
PythonExtension.menu = Menu.create(em, PythonExtension.longName, PythonExtension.extLangBin, PythonExtension.config, Seq(py2Property))
97+
}
10198
}
10299

103100
override def unload(em: ExtensionManager): Unit = {
104101
super.unload(em)
105102
PythonExtension.killPython()
106-
PythonExtension.menu.foreach(_.unload())
103+
104+
if (!PythonExtension.headless)
105+
PythonExtension.menu.foreach(_.unload())
107106
}
108107

109108
}
@@ -162,7 +161,9 @@ object SetupPython extends api.Command {
162161
PythonExtension.pythonProcess = Subprocess.start(context.workspace, pythonCmd, Seq(pyScript),
163162
PythonExtension.codeName, PythonExtension.longName,
164163
customMapper = Option(mapper))
165-
PythonExtension.menu.foreach(_.setup(PythonExtension.pythonProcess.evalStringified))
164+
165+
if (!PythonExtension.headless)
166+
PythonExtension.menu.foreach(_.setup(PythonExtension.pythonProcess.evalStringified))
166167
} catch {
167168
case e: Exception =>
168169
// Different errors can manifest in different operating systems. Thus, rather than dispatching in the specific

0 commit comments

Comments
 (0)