Skip to content

Commit d78009c

Browse files
committed
make uv_init return a struct with required paths
1 parent 57b5398 commit d78009c

File tree

2 files changed

+88
-55
lines changed

2 files changed

+88
-55
lines changed

lib/pythonx.ex

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ 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+
1118
alias Pythonx.Object
1219

20+
@type python_config :: %__MODULE__{
21+
python_dl_path: String.t(),
22+
python_home_path: String.t(),
23+
python_executable_path: String.t(),
24+
sys_paths: [String.t()]
25+
}
1326
@type encoder :: (term(), encoder() -> Object.t())
1427

1528
@doc ~s'''
@@ -90,7 +103,7 @@ defmodule Pythonx do
90103
# (`sys.path`). Defaults to `[]`.
91104
#
92105
@doc false
93-
@spec init(String.t(), String.t(), keyword()) :: :ok
106+
@spec init(String.t(), String.t(), String.t(), keyword()) :: :ok
94107
def init(python_dl_path, python_home_path, python_executable_path, opts \\ [])
95108
when is_binary(python_dl_path) and is_binary(python_home_path)
96109
when is_binary(python_executable_path) and is_list(opts) do
@@ -111,6 +124,16 @@ defmodule Pythonx do
111124
Pythonx.NIF.init(python_dl_path, python_home_path, python_executable_path, opts[:sys_paths])
112125
end
113126

127+
@spec init(python_config()) :: :ok
128+
def init(%__MODULE__{
129+
python_dl_path: python_dl_path,
130+
python_home_path: python_home_path,
131+
python_executable_path: python_executable_path,
132+
sys_paths: sys_paths
133+
}) do
134+
init(python_dl_path, python_home_path, python_executable_path, sys_paths: sys_paths)
135+
end
136+
114137
@doc ~S'''
115138
Evaluates the Python `code`.
116139

lib/pythonx/uv.ex

Lines changed: 64 additions & 54 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()) :: :ok
69+
@spec init(String.t(), boolean()) :: Pythonx.python_config()
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,59 +95,69 @@ defmodule Pythonx.Uv do
9595

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

98-
case :os.type() do
99-
{:win32, _osname} ->
100-
# Note that we want the version-specific DLL, rather than the
101-
# "forwarder DLL" python3.dll, otherwise symbols cannot be
102-
# found directly.
103-
python_dl_path =
104-
root_dir
105-
|> Path.join("python3?*.dll")
106-
|> wildcard_one!()
107-
|> make_windows_slashes()
108-
109-
python_home_path = make_windows_slashes(root_dir)
110-
111-
python_executable_path =
112-
project_dir
113-
|> Path.join(".venv/Scripts/python.exe")
114-
|> make_windows_slashes()
115-
116-
venv_packages_path =
117-
project_dir
118-
|> Path.join(".venv/Lib/site-packages")
119-
|> make_windows_slashes()
120-
121-
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
122-
sys_paths: [venv_packages_path]
123-
)
124-
125-
{:unix, osname} ->
126-
dl_extension =
127-
case osname do
128-
:darwin -> ".dylib"
129-
:linux -> ".so"
130-
end
131-
132-
python_dl_path =
133-
root_dir
134-
|> Path.join("lib/libpython3.*" <> dl_extension)
135-
|> wildcard_one!()
136-
|> Path.expand()
137-
138-
python_home_path = root_dir
139-
140-
python_executable_path = Path.join(project_dir, ".venv/bin/python")
141-
142-
venv_packages_path =
143-
project_dir
144-
|> Path.join(".venv/lib/python3*/site-packages")
145-
|> wildcard_one!()
146-
147-
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
148-
sys_paths: [venv_packages_path]
149-
)
150-
end
98+
python_config =
99+
case :os.type() do
100+
{:win32, _osname} ->
101+
# Note that we want the version-specific DLL, rather than the
102+
# "forwarder DLL" python3.dll, otherwise symbols cannot be
103+
# found directly.
104+
python_dl_path =
105+
root_dir
106+
|> Path.join("python3?*.dll")
107+
|> wildcard_one!()
108+
|> make_windows_slashes()
109+
110+
python_home_path = make_windows_slashes(root_dir)
111+
112+
python_executable_path =
113+
project_dir
114+
|> Path.join(".venv/Scripts/python.exe")
115+
|> make_windows_slashes()
116+
117+
venv_packages_path =
118+
project_dir
119+
|> Path.join(".venv/Lib/site-packages")
120+
|> make_windows_slashes()
121+
122+
%Pythonx{
123+
python_dl_path: python_dl_path,
124+
python_home_path: python_home_path,
125+
python_executable_path: python_executable_path,
126+
sys_paths: [venv_packages_path]
127+
}
128+
129+
{:unix, osname} ->
130+
dl_extension =
131+
case osname do
132+
:darwin -> ".dylib"
133+
:linux -> ".so"
134+
end
135+
136+
python_dl_path =
137+
root_dir
138+
|> Path.join("lib/libpython3.*" <> dl_extension)
139+
|> wildcard_one!()
140+
|> Path.expand()
141+
142+
python_home_path = root_dir
143+
144+
python_executable_path = Path.join(project_dir, ".venv/bin/python")
145+
146+
venv_packages_path =
147+
project_dir
148+
|> Path.join(".venv/lib/python3*/site-packages")
149+
|> wildcard_one!()
150+
151+
%Pythonx{
152+
python_dl_path: python_dl_path,
153+
python_home_path: python_home_path,
154+
python_executable_path: python_executable_path,
155+
sys_paths: [venv_packages_path]
156+
}
157+
end
158+
159+
Pythonx.init(python_config)
160+
python_config
151161
end
152162

153163
defp wildcard_one!(path) do

0 commit comments

Comments
 (0)