You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.rs 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use dora_core::{get_pip_path, get_python_path};
  2. use dora_download::download_file;
  3. use eyre::{ContextCompat, WrapErr};
  4. use std::path::{Path, PathBuf};
  5. use xshell::{cmd, Shell};
  6. #[tokio::main]
  7. async fn main() -> eyre::Result<()> {
  8. let python = get_python_path().context("Could not get python binary")?;
  9. // create a new shell in this folder
  10. let sh = prepare_shell()?;
  11. // prepare Python virtual environment
  12. prepare_venv(&sh, &python)?;
  13. // build the `dora` binary (you can skip this if you use `cargo install dora-cli`)
  14. let dora = prepare_dora(&sh)?;
  15. // install/upgrade pip, then install requirements
  16. cmd!(sh, "{python} -m pip install --upgrade pip").run()?;
  17. let pip = get_pip_path().context("Could not get pip binary")?;
  18. cmd!(sh, "{pip} install -r requirements.txt").run()?;
  19. // build the dora Python package (you can skip this if you installed the Python dora package)
  20. {
  21. let python_node_api_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
  22. .join("apis")
  23. .join("python")
  24. .join("node");
  25. let _dir = sh.push_dir(python_node_api_dir);
  26. cmd!(sh, "maturin develop").run()?;
  27. }
  28. download_file(
  29. "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt",
  30. Path::new("yolov8n.pt"),
  31. )
  32. .await
  33. .context("Could not download weights.")?;
  34. // start up the dora daemon and coordinator
  35. cmd!(sh, "{dora} up").run()?;
  36. // start running the dataflow.yml -> outputs the UUID assigned to the dataflow
  37. let output = cmd!(sh, "{dora} start dataflow.yml --attach").read_stderr()?;
  38. let uuid = output.lines().next().context("no output")?;
  39. // stop the dora daemon and coordinator again
  40. cmd!(sh, "{dora} destroy").run()?;
  41. // verify that the node output was written to `out`
  42. sh.change_dir("out");
  43. sh.change_dir(uuid);
  44. let sink_output = sh.read_file("log_object_detection.txt")?;
  45. if sink_output.lines().count() < 50 {
  46. eyre::bail!("object dectection node did not receive the expected number of messages")
  47. }
  48. Ok(())
  49. }
  50. /// Prepares a Python virtual environment.
  51. ///
  52. /// You can use the normal `python3 -m venv .venv` + `source .venv/bin/activate`
  53. /// if you're running bash.
  54. fn prepare_venv(sh: &Shell, python: &Path) -> eyre::Result<()> {
  55. cmd!(sh, "{python} -m venv ../.env").run()?;
  56. let venv = sh.current_dir().join("..").join(".env");
  57. sh.set_var(
  58. "VIRTUAL_ENV",
  59. venv.to_str().context("venv path not valid unicode")?,
  60. );
  61. // bin folder is named Scripts on windows.
  62. // 🤦‍♂️ See: https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
  63. let venv_bin = if cfg!(windows) {
  64. venv.join("Scripts")
  65. } else {
  66. venv.join("bin")
  67. };
  68. let path_separator = if cfg!(windows) { ';' } else { ':' };
  69. sh.set_var(
  70. "PATH",
  71. format!(
  72. "{}{path_separator}{}",
  73. venv_bin.to_str().context("venv path not valid unicode")?,
  74. std::env::var("PATH")?
  75. ),
  76. );
  77. Ok(())
  78. }
  79. /// Prepares a shell and set the working directory to the parent folder of this file.
  80. ///
  81. /// You can use your system shell instead (e.g. `bash`);
  82. fn prepare_shell() -> Result<Shell, eyre::Error> {
  83. let sh = Shell::new()?;
  84. let root = Path::new(env!("CARGO_MANIFEST_DIR"));
  85. sh.change_dir(root.join(file!()).parent().unwrap());
  86. Ok(sh)
  87. }
  88. /// Build the `dora` command-line executable from this repo.
  89. ///
  90. /// You can skip this step and run `cargo install dora-cli --locked` instead.
  91. fn prepare_dora(sh: &Shell) -> eyre::Result<PathBuf> {
  92. cmd!(sh, "cargo build --package dora-cli").run()?;
  93. let root = Path::new(env!("CARGO_MANIFEST_DIR"));
  94. let dora = root.join("target").join("debug").join("dora");
  95. Ok(dora)
  96. }