@@ -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
2013 @ install_env_name "PYTHONX_FLAME_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,20 +60,25 @@ 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'''
9284 Returns a map containing the environment variables required to initialize Pythonx.
@@ -98,7 +90,7 @@ defmodule Pythonx do
9890 |> :erlang . term_to_binary ( )
9991 |> Base . encode64 ( )
10092
101- % { name: @ install_env_name , value: init_state }
93+ % { @ install_env_name => init_state }
10294 end
10395
10496 @ doc ~s'''
@@ -108,12 +100,29 @@ defmodule Pythonx do
108100 def install_paths ( ) do
109101 init_state = init_state ( )
110102
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 )
103+ init_state . install_paths
104+ |> Enum . map ( fn path -> Path . wildcard ( "#{ path } /**" , match_dot: true ) end )
105+ |> List . flatten ( )
106+ end
107+
108+ @ doc false
109+ def maybe_uv_init_from_env ( ) do
110+ case init_state_from_env ( ) do
111+ nil ->
112+ :noop
113+
114+ init_state_env_value ->
115+ % {
116+ type: :uv_init ,
117+ pyproject_toml: pyproject_toml ,
118+ opts: opts
119+ } =
120+ init_state_env_value
121+ |> Base . decode64! ( )
122+ |> :erlang . binary_to_term ( )
123+
124+ uv_init ( pyproject_toml , opts )
125+ end
117126 end
118127
119128 # Initializes the Python interpreter.
@@ -166,16 +175,6 @@ defmodule Pythonx do
166175 Pythonx.NIF . init ( python_dl_path , python_home_path , python_executable_path , opts [ :sys_paths ] )
167176 end
168177
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-
179178 @ doc ~S'''
180179 Evaluates the Python `code`.
181180
0 commit comments