bin/archive-dir

Mon, 27 Mar 2017 17:13:20 -0400

author
Meredith Howard <mhoward@roomag.org>
date
Mon, 27 Mar 2017 17:13:20 -0400
changeset 536
4d01cb9a0bb1
parent 529
99f120bc1ad9
child 549
0bd45054802e
permissions
-rwxr-xr-x

add vinarise - hex edit helper

476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 #!/usr/bin/env perl
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
3 use warnings;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
4 use strict;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
5 use Time::Piece;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
6 use Path::Tiny;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
7 use Getopt::Long::Descriptive;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
8
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
9 sub main {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10 my ($opt, $usage) = describe_options(
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
11 'archive-dir %o <directory> ...',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
12 ['dest|d=s' => "destination path"],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13 ['subdir' => hidden => {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14 one_of => [
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 ['year|y' => 'file by year'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16 ['month|m' => 'file by month'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18 default => 'year',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 ['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
22 ['files|f' => "operate on plain files only"],
529
99f120bc1ad9 oops, overloaded -y
Meredith Howard <mhoward@roomag.org>
parents: 485
diff changeset
23 ['yes|Y' => "actually move stuff"],
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24 ['help|h' => "print usage message and exit", {shortcircuit => 1}],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 );
485
a58682115510 print usage on no args too
Meredith Howard <mhoward@roomag.org>
parents: 477
diff changeset
26 print($usage->text), exit if $opt->help || !@ARGV;
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28 archive_dir($opt, $_) for @ARGV;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 sub archive_dir {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32 my $opt = shift;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33 my $dir = path(shift);
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 my $destdir = path($opt->dest // '.');
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35 $destdir = $dir->child($destdir) if $destdir->is_relative;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 my $destfmt = ($opt->subdir eq 'month') ? '%Y-%m' : '%Y';
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 my $nowish = time;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38 my $age = $opt->age * 24 * 60 * 60;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40 for my $child ($dir->children) {
477
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
41 next if $child->is_dir && (
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
42 $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
43 );
eeafc178ddc4 add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents: 476
diff changeset
44 next if $opt->files && $child->basename =~ /^\./;
476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
45
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
46 my $mtime = $child->stat->mtime;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47 next unless ($nowish - $mtime) >= $age;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
48
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
49 my $dest = $destdir->child(localtime($mtime)->strftime($destfmt));
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
50
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
51 print "$dest \t $child\n";
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 next unless $opt->yes;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
54 $dest->mkpath;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
55 $child->move($dest . '/' . $child->relative($dir));
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
56 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
57 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
58
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
59 main();

mercurial