blib/lib/App/tmclean.pm

filter: Statementstmt 26.4% 28 / 106 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 42.8% 9 / 21 Podpod 0.0% 0 / 10 totaltotal 19.1% 37 / 193 CC 23 SCAR 26.8
CC
23
Cov
19%
run
42.0
__ANON__
20.0
__ANON__
20.0
CRAP 14.5
↑ Summary
1 package App::tmclean;
2 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use version 0.77; our $VERSION = version->declare("v0.0.4");
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Getopt::Long qw/GetOptions :config posix_default no_ignore_case bundling auto_help/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Pod::Usage qw/pod2usage/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 new => 1,
11 ro => [qw/before days dry_run/],
12 1 );
13 1 ▶use HTTP::Date qw/str2time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Time::Piece ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Time::Seconds ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub logf {
Subroutine logf: not called CC=2 SCAR=17.9
Pod undocumented
18 0 my $msg = shift;
19 0 $msg = sprintf($msg, @_);
20 0 my $prefix = '[tmclean]' . Time::Piece->localtime->strftime('[%Y-%m-%d %H:%M:%S] ');
21 0 ▶ $msg .= "\n" if $msg !~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 0 0 0
22 0 print STDERR $prefix . $msg;
23 }
24  
25 0 ▶sub new_with_options {
Subroutine new_with_options: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my ($class, @argv) = @_;
27  
28 0 my ($opt) = $class->parse_options(@argv);
29 0 $class->new($opt);
30 }
31  
32 0 ▶sub parse_options {
Subroutine parse_options: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my ($class, @argv) = @_;
34  
35 0 local @ARGV = @argv;
36 0 ▶ GetOptions(\my %opt, qw/
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\my %opt, ("days=i", "before=s", "dry-run")) or pod2usage(2) 0 0 0
37 days=i
38 before=s
39 dry-run
40 /) or pod2usage(2);
41  
42 0 $opt{dry_run} = delete $opt{'dry-run'};
43 0 return (\%opt, \@ARGV);
44 }
45  
46 0 ▶sub run {
Subroutine run: not called CC=6 SCAR=37.4
Pod undocumented
47 0 my $self = shift;
48  
49 0 ▶ if (!$self->dry_run && $ENV{USER} ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dry_run and $ENV{"USER"} ne "root") 0 0 0
Condition0%0/3
not $self->dry_run and $ENV{"USER"} ne "root"
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->dry_run and $ENV{"USER"} ne "root"
not $self->dry_run $ENV{"USER"} ne "root"
Truth table0%0/3
not(A) and B
A= not $self->dry_runB= $ENV{"USER"} ne "root"
ABresult
0X0
100
111
50 0 die "tmutil requires root privileges\n";
51 }
52 0 $self->cmd(qw/tmutil stopbackup/);
53 0 $self->cmd(qw/tmutil disable/); # need sudo
54  
55 0 my @targets = $self->backups2delete;
56 0 ▶ unless (@targets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@targets) 0 0 0
57 0 logf 'no deletion targets found';
58 0 return 0;
59 }
60 0 my $mount_point = $self->mount_point;
61  
62 0 logf "following backups to be deleted:\n %s", join("\n ", @targets);
63 0 for my $bak (@targets) {
64 0 $self->cmd(qw/tmutil delete/, $bak); # need sudo
65 }
66 0 my $dev_name = dev_name($targets[0]);
67 0 $self->cmd(qw/hdiutil detach/, $dev_name);
68  
69 0 my $backupbundle_path = sprintf '%s/%s.sparsebundle', $mount_point, $self->machine_name;
70 0 ▶ if (! -d $backupbundle_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $backupbundle_path) 0 0 0
71 # backupbundle path is changed after Catalina
72 0 $backupbundle_path =~ s/\.sparsebundle$/.backupbundle/;
73 }
74 0 $self->cmd(qw/hdiutil compact/, $backupbundle_path); # need sudo
75 0 $self->cmd(qw/tmutil enable/); # need sudo
76 }
77  
78 0 ▶sub backups2delete {
Subroutine backups2delete: not called CC=3 SCAR=24.8
Pod undocumented
79 0 my $self = shift;
80 0 my @backups = `tmutil listbackups`;
81 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
82 0 die "failed to execute `tmutil listbackups`: $?\n";
83 }
84 # e.g. /Volumes/Time Machine Backup/Backups.backupdb/$machine/2018-01-07-033608
85 return grep {
86 0 chomp;
87 0 my @paths = split m!/!, $_;
88 0 my $backup_date = eval { Time::Piece->strptime($paths[-1], '%Y-%m-%d-%H%M%S') };
89 0 ▶ $backup_date && $self->before_tp > $backup_date;
Condition0%0/3
$backup_date && $self->before_tp > $backup_date
!ll&&!rl&&r
000
MC/DC0%0/2
$backup_date && $self->before_tp > $backup_date
$backup_date $self->before_tp > $backup_date
Truth table0%0/3
A && B
A= $backup_dateB= $self->before_tp > $backup_date
ABresult
0X0
100
111
90 } @backups;
91 }
92  
93 0 ▶sub mount_point {
Subroutine mount_point: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $self = shift;
95  
96 0 ▶ $self->{mount_point} ||= sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
97 0 my @lines = `tmutil destinationinfo`;
98 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
99 0 die "failed to execute `tmutil destinationinfo`: $?\n";
100 }
101 0 for my $line (@lines) {
102 0 chomp $line;
103 0 my ($key, $val) = split /\s+:\s+/, $line, 2;
104 0 ▶ if ($key eq 'Mount Point') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "Mount Point") 0 0 0
105 0 return $val;
106 }
107 }
108 0 die "no mount points found\n";
109 0 ▶ }->();
Condition0%0/3
$self->{"mount_point"} ||= sub { my(@lines) = `tmutil destinationinfo`; if ($? != 0) { die "failed to execute `tmutil destinationinfo`: $?\n"; } ; foreach my $line (@lines) { chomp $line; my($key, $val) = split(/\s+:\s+/, $line, 2); if ($key eq "Mount Point") { return $val; } ; } ; die "no mount points found\n"; } ->()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"mount_point"} ||= sub { my(@lines) = `tmutil destinationinfo`; if ($? != 0) { die "failed to execute `tmutil destinationinfo`: $?\n"; } ; foreach my $line (@lines) { chomp $line; my($key, $val) = split(/\s+:\s+/, $line, 2); if ($key eq "Mount Point") { return $val; } ; } ; die "no mount points found\n"; } ->()
$self->{"mount_point"} sub { my(@lines) = `tmutil destinationinfo`; if ($? != 0) { die "failed to execute `tmutil destinationinfo`: $?\n"; } ; foreach my $line (@lines) { chomp $line; my($key, $val) = split(/\s+:\s+/, $line, 2); if ($key eq "Mount Point") { return $val; } ; } ; die "no mount points found\n"; } ->()
Truth table0%0/3
A ||= B
A= $self->{"mount_point"}B= sub { my(@lines) = `tmutil destinationinfo`; if ($? != 0) { die "failed to execute `tmutil destinationinfo`: $?\n"; } ; foreach my $line (@lines) { chomp $line; my($key, $val) = split(/\s+:\s+/, $line, 2); if ($key eq "Mount Point") { return $val; } ; } ; die "no mount points found\n"; } ->()
ABresult
1X1
011
000
110 }
111  
112 0 ▶sub dev_name {
Subroutine dev_name: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $path = shift;
114 0 my @paths = split m!/!, $path;
115 0 join '/', @paths[0..2];
116 }
117  
118 0 ▶sub machine_name {
Subroutine machine_name: not called CC=3 SCAR=24.8
Pod undocumented
119 0 my $self = shift;
120  
121 0 ▶ $self->{machine_name} ||= do {
Condition0%0/3
$self->{"machine_name"} ||= do { chomp(my $hostname = `hostname`); if ($? != 0) { die "failed to execute `hostname`: $?\n"; } ; $hostname =~ s/\.local$//; $hostname }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"machine_name"} ||= do { chomp(my $hostname = `hostname`); if ($? != 0) { die "failed to execute `hostname`: $?\n"; } ; $hostname =~ s/\.local$//; $hostname }
$self->{"machine_name"} do { chomp(my $hostname = `hostname`); if ($? != 0) { die "failed to execute `hostname`: $?\n"; } ; $hostname =~ s/\.local$//; $hostname }
Truth table0%0/3
A ||= B
A= $self->{"machine_name"}B= do { chomp(my $hostname = `hostname`); if ($? != 0) { die "failed to execute `hostname`: $?\n"; } ; $hostname =~ s/\.local$//; $hostname }
ABresult
1X1
011
000
122 0 chomp(my $hostname = `hostname`);
123 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
124 0 die "failed to execute `hostname`: $?\n";
125 }
126 0 $hostname =~ s/\.local$//;
127 0 $hostname;
128 };
129 }
130  
131 0 ▶sub before_tp {
Subroutine before_tp: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my $self = shift;
133  
134 0 ▶ $self->{before_tp} ||= sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
135 0 ▶ if ($self->before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->before) 0 0 0
136 0 my $time = str2time $self->before; # str2time parses the time as localtime
137 0 ▶ die "unrecognized date format @{[$self->before]}" unless $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $time 0 0 0
138 0 return Time::Piece->localtime($time);
139 }
140 0 ▶ my $days = $self->days || 366;
Condition0%0/2
$self->days || 366
l!l
00
MC/DC0%0/1
$self->days || 366
$self->days
Truth table0%0/2
A
A= $self->days
Aresult
11
00
141 0 return Time::Piece->localtime() - Time::Seconds::ONE_DAY() * $days;
142 0 ▶ }->();
Condition0%0/3
$self->{"before_tp"} ||= sub { if ($self->before) { my $time = &str2time(scalar $self->before); die "unrecognized date format @{[$self->before];}" unless $time; return "Time::Piece"->localtime($time); } ; my $days = $self->days || 366; return "Time::Piece"->localtime - 86400 * $days; } ->()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"before_tp"} ||= sub { if ($self->before) { my $time = &str2time(scalar $self->before); die "unrecognized date format @{[$self->before];}" unless $time; return "Time::Piece"->localtime($time); } ; my $days = $self->days || 366; return "Time::Piece"->localtime - 86400 * $days; } ->()
$self->{"before_tp"} sub { if ($self->before) { my $time = &str2time(scalar $self->before); die "unrecognized date format @{[$self->before];}" unless $time; return "Time::Piece"->localtime($time); } ; my $days = $self->days || 366; return "Time::Piece"->localtime - 86400 * $days; } ->()
Truth table0%0/3
A ||= B
A= $self->{"before_tp"}B= sub { if ($self->before) { my $time = &str2time(scalar $self->before); die "unrecognized date format @{[$self->before];}" unless $time; return "Time::Piece"->localtime($time); } ; my $days = $self->days || 366; return "Time::Piece"->localtime - 86400 * $days; } ->()
ABresult
1X1
011
000
143 }
144  
145 0 ▶sub cmd {
Subroutine cmd: not called CC=4 SCAR=30.0
Pod undocumented
146 0 my ($self, @command) = @_;
147  
148 0 my $cmd_str = join(' ', @command);
149 0 ▶ logf 'execute%s: `%s`', $self->dry_run ? '(dry-run)' : '', $cmd_str;
Branch: 1 branch
BranchTrueFalseTotal
$self->dry_run ? : 0 0 0
150 0 ▶ if (!$self->dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dry_run) 0 0 0
151 0 ▶ !system(@command) or die "failed to execute command: $cmd_str: $?\n";
Branch: 1 branch
BranchTrueFalseTotal
not system(@command) or die "failed to execute command: ${cmd_str}: $?\n" 0 0 0
152 }
153 }
154  
155 1 1;
156 __END__
↑ Summary