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

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