@@ -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 ,
0 commit comments