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.

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