bin/archive-dir

Sat, 16 Mar 2019 04:30:15 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sat, 16 Mar 2019 04:30:15 -0500
changeset 747
a3c2fbf5a623
parent 689
840057d4b493
child 761
21d7274d64a4
permissions
-rwxr-xr-x

disable ansi perldoc until consistent

476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 #!/usr/bin/env perl
677
15d353917450 try using autofetch
Meredith Howard <mhoward@roomag.org>
parents: 549
diff changeset
2
549
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
3 use 5.014;
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
4 use warnings;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
5 use strict;
677
15d353917450 try using autofetch
Meredith Howard <mhoward@roomag.org>
parents: 549
diff changeset
6
689
840057d4b493 portability improvement
Meredith Howard <mhoward@roomag.org>
parents: 677
diff changeset
7 use lib glob('~/.lib/perl5');
677
15d353917450 try using autofetch
Meredith Howard <mhoward@roomag.org>
parents: 549
diff changeset
8 use Autofetch;
15d353917450 try using autofetch
Meredith Howard <mhoward@roomag.org>
parents: 549
diff changeset
9
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10 use Time::Piece;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
11 use Path::Tiny;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
12 use Getopt::Long::Descriptive;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14 sub main {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 my ($opt, $usage) = describe_options(
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16 'archive-dir %o <directory> ...',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 ['dest|d=s' => "destination path"],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18 ['subdir' => hidden => {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 one_of => [
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 ['year|y' => 'file by year'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 ['month|m' => 'file by month'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
23 default => 'year',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26 ['age|a=i' => "minimum age in days for archival", {default => 60}],
477
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
27 ['files|f' => "operate on plain files only"],
529
99f120bc1ad9 oops, overloaded -y
Meredith Howard <mhoward@roomag.org>
parents: 485
diff changeset
28 ['yes|Y' => "actually move stuff"],
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29 ['help|h' => "print usage message and exit", {shortcircuit => 1}],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30 );
485
a58682115510 print usage on no args too
Meredith Howard <mhoward@roomag.org>
parents: 477
diff changeset
31 print($usage->text), exit if $opt->help || !@ARGV;
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33 archive_dir($opt, $_) for @ARGV;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 sub archive_dir {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 my $opt = shift;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38 my $dir = path(shift);
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39 my $destdir = path($opt->dest // '.');
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40 $destdir = $dir->child($destdir) if $destdir->is_relative;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41 my $destfmt = ($opt->subdir eq 'month') ? '%Y-%m' : '%Y';
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42 my $nowish = time;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
43 my $age = $opt->age * 24 * 60 * 60;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44
549
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
45 my @work;
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
46
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47 for my $child ($dir->children) {
477
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
48 next if $child->is_dir && (
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
49 $opt->files || $child eq $destdir || $child =~ /^(?:\d{4}|\d\d)$/
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
50 );
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
51 next if $opt->files && $child->basename =~ /^\./;
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
52
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
53 my $mtime = $child->stat->mtime;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
54 next unless ($nowish - $mtime) >= $age;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
55
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
56 my $dest = $destdir->child(localtime($mtime)->strftime($destfmt));
549
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
57 push @work, [$dest, $child];
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
58 }
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
59
549
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
60 my $lastdest = '';
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
61 for my $work (sort {$a->[0] cmp $b->[0]} @work) {
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
62 my ($dest, $child) = @$work;
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
63
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
64 say $lastdest = $dest
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
65 if $dest ne $lastdest;
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
66
0bd45054802e sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents: 529
diff changeset
67 say "\t$child";
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
68
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
69 next unless $opt->yes;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
70 $dest->mkpath;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
71 $child->move($dest . '/' . $child->relative($dir));
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
72 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
73 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
74
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
75 main();

mercurial