@@ -8,23 +8,10 @@ defmodule Pythonx do
88
99 @ moduledoc readme_docs
1010
11- defstruct [
12- :python_dl_path ,
13- :python_home_path ,
14- :python_executable_path ,
15- :sys_paths
16- ]
17-
1811 alias Pythonx.Object
1912
20- @ install_env_name "PYTHONX_FLAME_INIT_STATE "
13+ @ install_env_name "PYTHONX_INIT_STATE "
2114
22- @ type init_state :: % __MODULE__ {
23- python_dl_path: String . t ( ) ,
24- python_home_path: String . t ( ) ,
25- python_executable_path: String . t ( ) ,
26- sys_paths: [ String . t ( ) ]
27- }
2815 @ type encoder :: ( term ( ) , encoder ( ) -> Object . t ( ) )
2916
3017 @ doc ~s'''
@@ -73,47 +60,84 @@ defmodule Pythonx do
7360 opts = Keyword . validate! ( opts , force: false , uv_version: Pythonx.Uv . default_uv_version ( ) )
7461
7562 Pythonx.Uv . fetch ( pyproject_toml , false , opts )
76- init_state = Pythonx.Uv . init ( pyproject_toml , false , Keyword . take ( opts , [ :uv_version ] ) )
63+ install_paths = Pythonx.Uv . init ( pyproject_toml , false , Keyword . take ( opts , [ :uv_version ] ) )
64+
65+ init_state = % {
66+ type: :uv_init ,
67+ pyproject_toml: pyproject_toml ,
68+ opts: Keyword . drop ( opts , [ :force ] ) ,
69+ install_paths: install_paths
70+ }
71+
7772 :persistent_term . put ( :pythonx_init_state , init_state )
7873 end
7974
80- @ spec init_state ( ) :: init_state ( )
75+ @ spec init_state ( ) :: map ( )
8176 defp init_state ( ) do
8277 :persistent_term . get ( :pythonx_init_state )
8378 end
8479
85- @ doc ~s'''
86- Fetches the pythonx init state from the system environment variable.
87- '''
8880 @ spec init_state_from_env ( ) :: String . t ( ) | nil
89- def init_state_from_env ( ) , do: System . get_env ( @ install_env_name )
81+ defp init_state_from_env ( ) , do: System . get_env ( @ install_env_name )
9082
9183 @ doc ~s'''
92- Returns a map containing the environment variables required to initialize Pythonx.
84+ Returns a map with opaque environment variables to initialize Pythonx in
85+ the same way as the current initialization.
86+
87+ When those environment variables are set, Pythonx is initialized on boot.
88+
89+ In particular, this can be used to make Pythonx initialize on `FLAME` nodes.
9390 '''
9491 @ spec install_env ( ) :: map ( )
9592 def install_env ( ) do
93+ init_state = init_state ( )
94+
95+ if init_state == nil do
96+ raise "before calling Pythonx.install_env/0, you must initialize Pythonx"
97+ end
98+
9699 init_state =
97- init_state ( )
100+ init_state
98101 |> :erlang . term_to_binary ( )
99102 |> Base . encode64 ( )
100103
101- % { name: @ install_env_name , value: init_state }
104+ % { @ install_env_name => init_state }
102105 end
103106
104107 @ doc ~s'''
105- Returns a list of paths to copy to the flame runner.
108+ Returns a list of paths that `install_env/0` initialization depends on.
109+
110+ In particular, this can be used to make Pythonx initialize on `FLAME` nodes.
106111 '''
107112 @ spec install_paths ( ) :: list ( String . t ( ) )
108113 def install_paths ( ) do
109114 init_state = init_state ( )
110115
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 )
116+ if init_state == nil do
117+ raise "before calling Pythonx.install_paths/0, you must initialize Pythonx"
118+ end
119+
120+ init_state . install_paths
121+ end
122+
123+ @ doc false
124+ def maybe_init_from_env ( ) do
125+ case init_state_from_env ( ) do
126+ nil ->
127+ :noop
128+
129+ init_state_env_value ->
130+ % {
131+ type: :uv_init ,
132+ pyproject_toml: pyproject_toml ,
133+ opts: opts
134+ } =
135+ init_state_env_value
136+ |> Base . decode64! ( )
137+ |> :erlang . binary_to_term ( )
138+
139+ uv_init ( pyproject_toml , opts )
140+ end
117141 end
118142
119143 # Initializes the Python interpreter.
@@ -166,16 +190,6 @@ defmodule Pythonx do
166190 Pythonx.NIF . init ( python_dl_path , python_home_path , python_executable_path , opts [ :sys_paths ] )
167191 end
168192
169- @ spec init ( init_state ( ) ) :: :ok
170- def init ( % __MODULE__ {
171- python_dl_path: python_dl_path ,
172- python_home_path: python_home_path ,
173- python_executable_path: python_executable_path ,
174- sys_paths: sys_paths
175- } ) do
176- init ( python_dl_path , python_home_path , python_executable_path , sys_paths: sys_paths )
177- end
178-
179193 @ doc ~S'''
180194 Evaluates the Python `code`.
181195
0 commit comments