From ea276fbe73fe3c7c16e2fad8d2a9eadb026657d9 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 8 Nov 2021 12:14:13 +0100 Subject: [PATCH] Detect if Python3 is installed before launching PythonPort Genserver Signed-off-by: Thomas Citharel --- lib/mobilizon.ex | 8 +++++++- lib/service/export/participants/ods.ex | 6 ++---- lib/service/export/participants/pdf.ex | 6 ++---- lib/service/python_port.ex | 22 +++++++++++++++++++--- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/mobilizon.ex b/lib/mobilizon.ex index 95fdc27cf..608bb4c2c 100644 --- a/lib/mobilizon.ex +++ b/lib/mobilizon.ex @@ -51,7 +51,6 @@ defmodule Mobilizon do # workers Guardian.DB.Token.SweeperServer, ActivityPub.Federator, - Mobilizon.PythonWorker, TzWorld.Backend.DetsWithIndexCache, cachex_spec(:feed, 2500, 60, 60, &Feed.create_cache/1), cachex_spec(:ics, 2500, 60, 60, &ICalendar.create_cache/1), @@ -73,6 +72,13 @@ defmodule Mobilizon do ] ++ task_children(@env) + children = + if Mobilizon.PythonPort.python_exists?() do + children ++ [Mobilizon.PythonWorker] + else + children + end + ErrorReporting.configure() # Only attach the telemetry logger when we aren't in an IEx shell diff --git a/lib/service/export/participants/ods.ex b/lib/service/export/participants/ods.ex index bf5299cae..e6eb6c4c9 100644 --- a/lib/service/export/participants/ods.ex +++ b/lib/service/export/participants/ods.ex @@ -3,10 +3,8 @@ defmodule Mobilizon.Service.Export.Participants.ODS do Export a list of participants to ODS """ - alias Mobilizon.Events + alias Mobilizon.{Events, Export, PythonPort, PythonWorker} alias Mobilizon.Events.Event - alias Mobilizon.Export - alias Mobilizon.PythonWorker alias Mobilizon.Storage.Repo alias Mobilizon.Web.Gettext, as: GettextBackend import Mobilizon.Web.Gettext, only: [gettext: 2] @@ -91,7 +89,7 @@ defmodule Mobilizon.Service.Export.Participants.ODS do @spec dependencies_ok? :: boolean def dependencies_ok? do - PythonWorker.has_module("pyexcel_ods3") + PythonPort.python_exists?() && PythonWorker.has_module("pyexcel_ods3") end @spec enabled? :: boolean diff --git a/lib/service/export/participants/pdf.ex b/lib/service/export/participants/pdf.ex index e1e8259dd..3d488e6c5 100644 --- a/lib/service/export/participants/pdf.ex +++ b/lib/service/export/participants/pdf.ex @@ -3,10 +3,8 @@ defmodule Mobilizon.Service.Export.Participants.PDF do Export a list of participants to PDF """ - alias Mobilizon.Events + alias Mobilizon.{Events, Export, PythonPort, PythonWorker} alias Mobilizon.Events.Event - alias Mobilizon.Export - alias Mobilizon.PythonWorker alias Mobilizon.Storage.Repo alias Mobilizon.Web.ExportView alias Mobilizon.Web.Gettext, as: GettextBackend @@ -105,7 +103,7 @@ defmodule Mobilizon.Service.Export.Participants.PDF do @spec dependencies_ok? :: boolean def dependencies_ok? do - PythonWorker.has_module("weasyprint") + PythonPort.python_exists?() && PythonWorker.has_module("weasyprint") end @spec enabled? :: boolean diff --git a/lib/service/python_port.ex b/lib/service/python_port.ex index 356c32c8a..af9aff69a 100644 --- a/lib/service/python_port.ex +++ b/lib/service/python_port.ex @@ -5,15 +5,23 @@ defmodule Mobilizon.PythonPort do use Export.Python + @python_path "/usr/bin/python3" + + @doc """ + Whether Python3 is installed + """ + @spec python_exists? :: boolean + def python_exists? do + File.exists?(python_path()) + end + @doc """ ## Parameters - path: directory to include in python path """ @spec python_instance(String.t()) :: pid def python_instance(path) do - python = "/usr/bin/python3" - - {:ok, pid} = Python.start(python: python, python_path: path) + {:ok, pid} = Python.start(python: python_path(), python_path: path) pid end @@ -25,4 +33,12 @@ defmodule Mobilizon.PythonPort do def call_python(pid, module, function, arguments \\ []) do Python.call(pid, module, function, arguments) end + + @spec python_path :: String.t() + defp python_path do + case get_in(Application.get_env(:mobilizon, __MODULE__), [:path]) do + path when is_binary(path) -> path + nil -> @python_path + end + end end