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.7 kB

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