evobench_tools/run/
global_app_state_dir.rs

1use std::{fs::create_dir_all, path::PathBuf};
2
3use anyhow::Result;
4use cj_path_util::path_util::AppendToPath;
5
6use crate::{ctx, utillib::home::home_dir};
7
8/// Relative path to directory from $HOME in which to keep state files
9/// for the application.
10const GLOBAL_APP_STATE_DIR_NAME: &str = ".evobench";
11
12/// Representation of a directory below $HOME in which to keep state
13/// for the installation. The full folder structure of that folder
14/// should be represented via this type. Method calls to particular
15/// subfolders create subfolder(s) as necessary.
16pub struct GlobalAppStateDir {
17    base_dir: PathBuf,
18}
19
20impl GlobalAppStateDir {
21    /// Retrieves the $HOME value and creates the main subdir if
22    /// necessary.
23    pub fn new() -> Result<Self, anyhow::Error> {
24        let home = home_dir()?;
25        let base_dir = home.append(GLOBAL_APP_STATE_DIR_NAME);
26        create_dir_all(&base_dir).map_err(ctx!("creating dir {base_dir:?}"))?;
27        Ok(Self { base_dir })
28    }
29
30    pub fn subdir(&self, dir_name: &str) -> Result<PathBuf> {
31        let dir = (&self.base_dir).append(dir_name);
32        create_dir_all(&dir).map_err(ctx!("creating dir {dir:?}"))?;
33        Ok(dir)
34    }
35
36    pub fn run_queues_basedir(&self) -> Result<PathBuf> {
37        self.subdir("queues")
38    }
39
40    /// Signals written to this file indicate the need to (e.g.,
41    /// currently) regenerate the top-level HTML files for the output
42    /// directory
43    pub fn run_queue_signal_change_path(&self) -> PathBuf {
44        self.base_dir.join(".queues_change.signals")
45    }
46    /// Record of reactions to signals on ".queues_change.signals"
47    pub fn run_queue_change_done_path(&self) -> PathBuf {
48        self.base_dir.join(".queues_change.done")
49    }
50
51    /// The pool of project clones which are used for building and benchmarking
52    pub fn working_directory_pool_base(&self) -> Result<PathBuf> {
53        self.subdir("working_directory_pool")
54    }
55
56    /// The pool of project clones (only 1, but the pool
57    /// infrastructure is used to handle errors) for polling and for
58    /// verifying commit ids on insertion
59    pub fn working_directory_for_polling_pool_base(&self) -> Result<PathBuf> {
60        self.subdir("polling_pool")
61    }
62
63    /// A KeyVal database of (run_parameters -> insertion time), for
64    /// jobs already requested.
65    pub fn already_inserted_base(&self) -> Result<PathBuf> {
66        self.subdir("already_inserted")
67    }
68}