Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle local archives without requiring network tooling #65

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/xla.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ defmodule XLA do
end

defp download_external!(url, archive_path) do
assert_network_tool!()
unless local_archive(url), do: assert_network_tool!()
Logger.info("Downloading XLA archive from #{url}")
download_archive!(url, archive_path)
end
Expand Down Expand Up @@ -242,6 +242,14 @@ defmodule XLA do
end

defp download(url, dest) do
if local_path = local_archive(url) do
File.cp!(local_path, dest)
else
download_remote(url, dest)
end
end

defp download_remote(url, dest) do
command =
case network_tool() do
:curl -> "curl --fail -L -o #{dest} #{curl_options()} #{url}"
Expand Down Expand Up @@ -296,4 +304,15 @@ defmodule XLA do
headers = http_headers()
Enum.map_join(headers, " ", &"--header='#{&1}'")
end

defp local_archive("file:" <> rest) do
case rest do
"//localhost/" <> path -> "/" <> path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we don't support reading from any other host either, I would remove this version.

"///" <> path -> "/" <> path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't support "file:///c:/foo/bar" on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to test on Windows; we have to rewrite that to use backslashes, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's not necessary, but you don't need to add a prefixing "/". Using a separate env var will be simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to add a prefixing "/"

Indeed. 🙈

"/" <> path when binary_part(path, 0, 1) != "/" -> rest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should support this version. I can't see it on the spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it seems to be used by KDE. I am 👎.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RFC 3986 mentions that:

the "file" URI scheme is defined so that no authority, an empty host, and "localhost" all mean the end-user's machine

_ -> nil
end
end

defp local_archive(_), do: nil
end