So I have created an Elixir application which uses erlport to connect with Python. Everything works well there. Now, some stuff of the Python code must be ran on an ARM architecture to even compile while my machine on which I develop is x86_64.
What I’ve done is created a mock file which overrides them to not produce an error on a non-ARM architecture which is great.
But I want those files to not ship with my application while in production.
Initially I got an idea that I could just use this at the beggining of my application before the erlport is called:
defp delete_rpi() do
if Mix.env == :prod do
[:code.priv_dir(:pheed_the_pi), "python", "RPi"]
|> Path.join()
|> IO.inspect(label: "This is my path")
|> File.rm_rf()
|> IO.inspect(label: "Deletion status")
end
end
And the response is:
This is my path: "/home/zastrix/Documents/Personal/Phoenix/pheed_the_pi/_build/prod/lib/pheed_the_pi/priv/python/RPi"
Deletion status: {:ok,
["/home/zastrix/Documents/Personal/Phoenix/pheed_the_pi/_build/prod/lib/pheed_the_pi/priv/python/RPi",
"/home/zastrix/Documents/Personal/Phoenix/pheed_the_pi/_build/prod/lib/pheed_the_pi/priv/python/RPi/pwm.py",
"/home/zastrix/Documents/Personal/Phoenix/pheed_the_pi/_build/prod/lib/pheed_the_pi/priv/python/RPi/__init__.py",
"/home/zastrix/Documents/Personal/Phoenix/pheed_the_pi/_build/prod/lib/pheed_the_pi/priv/python/RPi/GPIO.py"]}
And it seems that it only deleted those files under my _build
folder… but that’s not what happens. It also deletes the files in the source code in the priv
folder.
Meaning if I delete: _build/prod/lib/pheed_the_pi/priv/python/RPi
via File.rm_rf/1
I as well delete: priv/python/RPi
.
Is there any way for me to exclude the deletion of priv/python/RPi
or even exclude the folder during compilation?
Edit:
For now I have made a workaround by adding priv/python/RPi
to .gitignore
, moved it a folder up and using this Elixir code:
defp delete_rpi() do
[:code.priv_dir(:pheed_the_pi), "python", "RPi"]
|> Path.join()
|> File.rm_rf()
if Mix.env != :prod do
destination =
[:code.priv_dir(:pheed_the_pi), "python", "RPi"]
|> Path.join()
[:code.priv_dir(:pheed_the_pi), "RPi"]
|> Path.join()
|> File.cp_r(destination)
end
end
But this is not a good solution as then on every update of the code I’d have to manually copy from the ignored folder to the folder up.
Solution explanation:
I’ve kept the code in priv/python
which I am using for debugging and development but made the application copy it to .python
where it will delete the RPi
module data if necessary:
Tweaked the previous function:
defp setup_python!() do
destination =
[File.cwd!(), ".python"]
|> Path.join()
[:code.priv_dir(:pheed_the_pi), "python"]
|> Path.join()
|> File.cp_r(destination)
if Mix.env == :prod do
[destination, "RPi"]
|> Path.join()
|> File.rm_rf()
end
end
And calling the module like so:
def start() do
path = [
File.cwd!(), ".python"
] |> Path.join() |> IO.inspect(label: "Python priv path")
{:ok, pid} = :python.start_link([{:python_path, to_charlist(path)}])
pid
end