Skip to content

Commit e6d2176

Browse files
committed
store init_state as persistent_term and add flame_env() to retrieve it
1 parent d78009c commit e6d2176

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

lib/pythonx.ex

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ defmodule Pythonx do
1717

1818
alias Pythonx.Object
1919

20-
@type python_config :: %__MODULE__{
20+
@install_env_name "PYTHONX_FLAME_INIT_STATE"
21+
22+
@type init_state :: %__MODULE__{
2123
python_dl_path: String.t(),
2224
python_home_path: String.t(),
2325
python_executable_path: String.t(),
@@ -71,7 +73,47 @@ defmodule Pythonx do
7173
opts = Keyword.validate!(opts, force: false, uv_version: Pythonx.Uv.default_uv_version())
7274

7375
Pythonx.Uv.fetch(pyproject_toml, false, opts)
74-
Pythonx.Uv.init(pyproject_toml, false, Keyword.take(opts, [:uv_version]))
76+
init_state = Pythonx.Uv.init(pyproject_toml, false, Keyword.take(opts, [:uv_version]))
77+
:persistent_term.put(:pythonx_init_state, init_state)
78+
end
79+
80+
@spec init_state() :: init_state()
81+
defp init_state() do
82+
:persistent_term.get(:pythonx_init_state)
83+
end
84+
85+
@doc ~s'''
86+
Fetches the pythonx init state from the system environment variable.
87+
'''
88+
@spec init_state_from_env() :: String.t() | nil
89+
def init_state_from_env(), do: System.get_env(@install_env_name)
90+
91+
@doc ~s'''
92+
Returns a map containing the environment variables required to initialize Pythonx.
93+
'''
94+
@spec install_env() :: map()
95+
def install_env() do
96+
init_state =
97+
init_state()
98+
|> :erlang.term_to_binary()
99+
|> Base.encode64()
100+
101+
%{name: @install_env_name, value: init_state}
102+
end
103+
104+
@doc ~s'''
105+
Returns a list of paths to copy to the flame runner.
106+
'''
107+
@spec install_paths() :: list(String.t())
108+
def install_paths() do
109+
init_state = init_state()
110+
111+
[
112+
init_state.python_dl_path,
113+
init_state.python_executable_path
114+
] ++
115+
init_state.sys_paths ++
116+
Path.wildcard(Path.join(init_state.python_home_path, "**"), match_dot: true)
75117
end
76118

77119
# Initializes the Python interpreter.
@@ -124,7 +166,7 @@ defmodule Pythonx do
124166
Pythonx.NIF.init(python_dl_path, python_home_path, python_executable_path, opts[:sys_paths])
125167
end
126168

127-
@spec init(python_config()) :: :ok
169+
@spec init(init_state()) :: :ok
128170
def init(%__MODULE__{
129171
python_dl_path: python_dl_path,
130172
python_home_path: python_home_path,

lib/pythonx/application.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ defmodule Pythonx.Application do
3131
Pythonx.Uv.fetch(pyproject_toml, true, opts)
3232
defp maybe_uv_init(), do: Pythonx.Uv.init(unquote(pyproject_toml), true, unquote(opts))
3333
else
34-
defp maybe_uv_init(), do: :noop
34+
defp maybe_uv_init() do
35+
case Pythonx.init_state_from_env() do
36+
nil ->
37+
:noop
38+
39+
init_state_env_value ->
40+
init_state_env_value
41+
|> Base.decode64!()
42+
|> :erlang.binary_to_term()
43+
|> Pythonx.init()
44+
end
45+
end
3546
end
3647

3748
defp enable_sigchld() do

lib/pythonx/uv.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule Pythonx.Uv do
6666
Initializes the interpreter using Python and dependencies previously
6767
fetched by `fetch/3`.
6868
"""
69-
@spec init(String.t(), boolean()) :: Pythonx.python_config()
69+
@spec init(String.t(), boolean()) :: Pythonx.init_state()
7070
def init(pyproject_toml, priv?, opts \\ []) do
7171
opts = Keyword.validate!(opts, uv_version: default_uv_version())
7272
project_dir = project_dir(pyproject_toml, priv?, opts[:uv_version])
@@ -95,7 +95,7 @@ defmodule Pythonx.Uv do
9595

9696
root_dir = Path.join(python_install_dir(priv?, opts[:uv_version]), versioned_dir_name)
9797

98-
python_config =
98+
init_state =
9999
case :os.type() do
100100
{:win32, _osname} ->
101101
# Note that we want the version-specific DLL, rather than the
@@ -156,8 +156,8 @@ defmodule Pythonx.Uv do
156156
}
157157
end
158158

159-
Pythonx.init(python_config)
160-
python_config
159+
Pythonx.init(init_state)
160+
init_state
161161
end
162162

163163
defp wildcard_one!(path) do

0 commit comments

Comments
 (0)