/
beet-cleanup-cover-only-directories
executable file
·64 lines (49 loc) · 1.92 KB
/
beet-cleanup-cover-only-directories
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
# abstract: clean up after beet imports that leave only a coverfile after a 'Remove old' action
# Also report directories where the content adds up to no more than 2M
# Write to the console and a logfile than can be examined before being executed
# as a shell script
# Paths and special shell characters are quoted properly
# The shell script will delete itself once executed
use strict;
use warnings;
use Cwd qw(abs_path);
use File::Find;
use File::LsColor qw(ls_color);
my $start_path = shift // '.';
my $max_size = 2 * 1024 * 1024; # 2 MB
my $tmpdir = './dirs_with_only_cover_in_them_or_less_than_2M';
system("mkdir -vp $tmpdir");
my $logfile = 'dirs_with_only_cover_in_them_or_less_than_2M.log';
open(my $fh, '>', $logfile) or die "Can't open $logfile: $!";
$|++;
sub check_dir {
my $dir = $_;
return unless -d $dir;
# Check for only one file, cover.jpg
opendir(my $dh, $dir) or die "Can't open directory $dir: $!";
my @files = grep { !/^\.\.?$/ } readdir($dh);
closedir($dh);
if (@files == 1 && $files[0] eq "cover.jpg") {
my $abspath = abs_path($dir);
print sprintf("only cover.jpg: %s\n", ls_color($abspath));
# print $fh sprintf("rm -r @{[ quotemeta($abspath) ]}\n");
print $fh sprintf("mv @{[ quotemeta($abspath) ]} -t $tmpdir\n");
return;
}
# Check for size less than 2M
my $size = 0;
find(sub { $size += -s if -f }, $dir);
# skip check of +tracks/ directory, AND make sure not to output the artist
# directory itself if it concists only of this +tracks/ directory
if ($size < $max_size and $dir ne '+tracks' and $dir =~ m/^[+]/) {
my $abspath = abs_path($dir);
print sprintf("< 2M %s\n", ls_color($abspath));
# print $fh sprintf("rm -r @{[ quotemeta($abspath) ]}\n");
print $fh sprintf("mv @{[ quotemeta($abspath) ]} -t $tmpdir\n");
}
}
finddepth(\&check_dir, $start_path);
# remove the generated bash script when it's executed
print $fh 'rm -v $0';
close $fh;