Add empty-dirs verb to find movie directories with no movies.

This commit is contained in:
Bill Thiede 2020-03-29 15:46:33 -07:00
parent 6a52f3c5b1
commit 334d2a5e53
2 changed files with 35 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet;
use std::env; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fmt; use std::fmt;
@ -678,6 +679,28 @@ impl MovieLibrary {
Ok(serde_json::from_reader(r) Ok(serde_json::from_reader(r)
.context(format!("serde_json::from_reader {}", path.display()))?) .context(format!("serde_json::from_reader {}", path.display()))?)
} }
pub fn empty_dirs(&self) -> Result<Vec<String>, Error> {
let mut all_dirs = HashSet::new();
let root_len = self.root.len() + 1; // +1 for trailing slash
for de in Path::new(&self.root).read_dir()? {
let de = de?;
if de.metadata()?.is_dir() {
let path = de.path().to_string_lossy().to_string();
all_dirs.insert(path[root_len..].to_string());
}
}
for path in self.videos()?.keys() {
let dir = match path.find("/") {
Some(idx) => path[..idx].to_string(),
None => path.to_string(),
};
all_dirs.remove(&dir);
}
let mut empty_dirs: Vec<_> = all_dirs.into_iter().collect();
empty_dirs.sort();
Ok(empty_dirs)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -171,6 +171,8 @@ enum Command {
about = "Write full metadata files and update compact file on changes" about = "Write full metadata files and update compact file on changes"
)] )]
UpdateAndCompactMetadata, UpdateAndCompactMetadata,
#[structopt(about = "Print directories with no movie files in them")]
EmptyDirs,
} }
#[derive(StructOpt)] #[derive(StructOpt)]
@ -248,6 +250,16 @@ fn main() -> Result<(), Box<dyn Error>> {
lib.update_metadata()?; lib.update_metadata()?;
lib.compact_metadata()?; lib.compact_metadata()?;
} }
Command::EmptyDirs => {
let lib = MovieLibrary::new(MOVIE_DIR);
let dirs = lib.empty_dirs()?;
if !dirs.is_empty() {
println!("Empty directories:");
for d in dirs {
println!(" {}", d);
}
}
}
} }
Ok(()) Ok(())
} }