"""
Create a file
=============
"""

from pathlib import Path
from remote_run import (
    Executor,
    GuixEnvironment,
    remote,
    SshMachine,
)
import pickle

# %%
# Define an executor
executor = Executor(
    machine=SshMachine(
        host="shpc0003.ost.ch",
        working_directory=Path("/cluster/raid/home/reza.housseini"),
    ),
    environments=[
        GuixEnvironment(
            channels=Path("channels.scm").read_text(),
            manifest=Path("manifest.scm").read_text(),
        )
    ],
)


# %%
# decorate your function you want to run with an
# executor with the remote decorator
@remote
def create_file(text):
    a_file = Path("a_file")
    a_file.write_text(text)
    return a_file


# %%
# this call will run on the machine specified in the executor.
# When returning argument of type Path, the file will be downloaded
# from the machine and the path will be patched to point
# to this new location.
job = create_file("hello", executor=executor)

# %%
# I can also pickle the job and retrieve for later use
job_file = Path("job_file")
job_file.write_bytes(pickle.dumps(job))

# %%
# after getting the result, the file will be available locally
job = pickle.loads(job_file.read_bytes())
result = job.result()
assert result.read_text() == "hello"
