WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4ba6784

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

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/pythonx.ex

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

1818
alias Pythonx.Object
1919

20-
@type python_config :: %__MODULE__{
20+
@type init_state :: %__MODULE__{
2121
python_dl_path: String.t(),
2222
python_home_path: String.t(),
2323
python_executable_path: String.t(),
@@ -71,7 +71,41 @@ defmodule Pythonx do
7171
opts = Keyword.validate!(opts, force: false, uv_version: Pythonx.Uv.default_uv_version())
7272

7373
Pythonx.Uv.fetch(pyproject_toml, false, opts)
74-
Pythonx.Uv.init(pyproject_toml, false, Keyword.take(opts, [:uv_version]))
74+
init_state = Pythonx.Uv.init(pyproject_toml, false, Keyword.take(opts, [:uv_version]))
75+
:persistent_term.put(:pythonx_init_state, init_state)
76+
end
77+
78+
@spec init_state() :: init_state()
79+
defp init_state() do
80+
:persistent_term.get(:pythonx_init_state)
81+
end
82+
83+
@doc ~s'''
84+
Returns a map containing the environment variables required to initialize Pythonx.
85+
'''
86+
@spec flame_env() :: map()
87+
def flame_env() do
88+
init_state =
89+
init_state()
90+
|> :erlang.term_to_binary()
91+
|> Base.encode64()
92+
93+
%{name: "PYTHONX_FLAME_INIT_STATE", value: init_state}
94+
end
95+
96+
@doc ~s'''
97+
Returns a list of paths to copy to the flame runner.
98+
'''
99+
@spec flame_paths_to_copy() :: list(String.t())
100+
def flame_paths_to_copy() do
101+
init_state = init_state()
102+
103+
[
104+
init_state.python_dl_path,
105+
init_state.python_home_path,
106+
init_state.python_executable_path,
107+
init_state.sys_paths
108+
]
75109
end
76110

77111
# Initializes the Python interpreter.
@@ -124,7 +158,7 @@ defmodule Pythonx do
124158
Pythonx.NIF.init(python_dl_path, python_home_path, python_executable_path, opts[:sys_paths])
125159
end
126160

127-
@spec init(python_config()) :: :ok
161+
@spec init(init_state()) :: :ok
128162
def init(%__MODULE__{
129163
python_dl_path: python_dl_path,
130164
python_home_path: python_home_path,

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)