choose what to arrange: -kinds, -exclude, am survey

-kinds picks any of image, raw, video (default all). -exclude skips
files and whole directories by name glob, and can be repeated. am
survey stats media per directory (counts by kind, videos over 1G,
sizes) so things like ripped DVDs stand out before arranging. It
never opens files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sm
2026-10-26 15:57:23 -07:00
co-authored by Claude Opus 5.5
parent 6d971118b7
commit f63b855179
6 changed files with 270 additions and 15 deletions
+2 -2
View File
@@ -10,14 +10,14 @@ import (
"mcquay.me/arrange"
)
func arr(indir, outdir string) error {
func arr(indir, outdir string, f arrange.Filter) error {
if !*dryRun {
if err := arrange.PrepOutput(outdir); err != nil {
return fmt.Errorf("problem creating directory structure: %v", err)
}
}
work := arrange.Source(indir)
work := arrange.SourceFiltered(indir, f)
streams := []<-chan arrange.Media{}
workers := runtime.NumCPU()
+61 -3
View File
@@ -7,12 +7,15 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"mcquay.me/arrange"
)
const usage = "am <arr|clean|verify|meta> [flags]"
const arrUsage = "am arr [-h|-n|-hash=sha256|md5|-cores=N] <in> <out>"
const usage = "am <arr|survey|clean|verify|meta> [flags]"
const arrUsage = "am arr [-h|-n|-kinds=image,raw,video|-exclude=PATTERN...|-hash=sha256|md5|-cores=N] <in> <out>"
const surveyUsage = "am survey [-h|-depth=N|-kinds=...|-exclude=PATTERN...] <directory>"
const cleanUsage = "am clean [-h|-n|-cores=N] <directory>"
const verifyUsage = "am verify [-h|-cores=N] <directory>"
const metaUsage = "am meta [-h|-cores=N] <file0> <file1> ... <fileN>"
@@ -20,6 +23,45 @@ const metaUsage = "am meta [-h|-cores=N] <file0> <file1> ... <fileN>"
var cores = flag.Int("cores", 0, "how many threads to use")
var dryRun = flag.Bool("n", false, "dry run: print the plan as tab-separated lines, change nothing")
var hashName = flag.String("hash", "sha256", "content-address hash: sha256, or md5 to add to a store built by older versions")
var kinds = flag.String("kinds", "image,raw,video", "comma-separated kinds of media to include: image, raw, video")
var depth = flag.Int("depth", 1, "survey: how many directory levels to group by")
var excludes patterns
func init() {
flag.Var(&excludes, "exclude", "skip files and directories whose name matches this glob (repeatable), e.g. VIDEO_TS or '*.vob'")
}
// patterns collects a repeated flag.
type patterns []string
func (p *patterns) String() string { return strings.Join(*p, ",") }
func (p *patterns) Set(v string) error {
if _, err := filepath.Match(v, ""); err != nil {
return fmt.Errorf("bad pattern %q: %v", v, err)
}
*p = append(*p, v)
return nil
}
// filter builds the arrange.Filter from the -kinds and -exclude flags.
func filter() (arrange.Filter, error) {
f := arrange.Filter{Classes: map[arrange.Class]bool{}, Exclude: excludes}
for _, k := range strings.Split(*kinds, ",") {
c := arrange.Class(strings.TrimSpace(k))
switch c {
case arrange.Image, arrange.Raw, arrange.Video:
f.Classes[c] = true
case "":
default:
return f, fmt.Errorf("unknown kind %q (known: image, raw, video)", k)
}
}
if len(f.Classes) == 0 {
return f, fmt.Errorf("-kinds selects nothing")
}
return f, nil
}
func main() {
if len(os.Args) < 2 {
@@ -42,6 +84,12 @@ func main() {
os.Exit(1)
}
f, err := filter()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
switch sub {
case "ar", "arr", "arrange":
args := flag.Args()
@@ -50,10 +98,20 @@ func main() {
os.Exit(1)
}
in, out := args[0], args[1]
if err := arr(in, out); err != nil {
if err := arr(in, out, f); err != nil {
fmt.Fprintf(os.Stderr, "problem arranging media: %v\n", err)
os.Exit(1)
}
case "s", "survey":
args := flag.Args()
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "%s\n", surveyUsage)
os.Exit(1)
}
if err := survey(args[0], f); err != nil {
fmt.Fprintf(os.Stderr, "problem surveying: %v\n", err)
os.Exit(1)
}
case "c", "cl", "clean":
args := flag.Args()
if len(args) != 1 {
+89
View File
@@ -0,0 +1,89 @@
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"mcquay.me/arrange"
)
// bigVideo is the size past which a video is more likely a ripped movie than
// a home video.
const bigVideo = 1 << 30
type group struct {
dir string
images, raws, videos int
big int
bytes, videoBytes int64
}
// survey tallies media under dir by directory, *depth levels down, so ripped
// movies and other things to keep out of a store stand out. It only stats
// files, and prints directory names, so its output is for the person running
// it.
func survey(dir string, f arrange.Filter) error {
if _, err := os.Stat(dir); err != nil {
return err
}
groups := map[string]*group{}
for path := range arrange.SourceFiltered(dir, f) {
fi, err := os.Stat(path)
if err != nil {
log.Printf("%v", err)
continue
}
key := groupKey(dir, path, *depth)
g := groups[key]
if g == nil {
g = &group{dir: key}
groups[key] = g
}
g.bytes += fi.Size()
c, _ := arrange.ClassOf(path)
switch c {
case arrange.Image:
g.images++
case arrange.Raw:
g.raws++
case arrange.Video:
g.videos++
g.videoBytes += fi.Size()
if fi.Size() >= bigVideo {
g.big++
}
}
}
gs := make([]*group, 0, len(groups))
for _, g := range groups {
gs = append(gs, g)
}
sort.Slice(gs, func(i, j int) bool { return gs[i].bytes > gs[j].bytes })
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.AlignRight)
fmt.Fprintln(tw, "size\timages\traw\tvideos\tvideos>1G\tvideo size\t\tdir")
for _, g := range gs {
fmt.Fprintf(tw, "%s\t%d\t%d\t%d\t%d\t%s\t\t%s\n",
human(g.bytes), g.images, g.raws, g.videos, g.big, human(g.videoBytes), g.dir)
}
return tw.Flush()
}
// groupKey is the directory of path relative to root, cut to depth levels.
func groupKey(root, path string, depth int) string {
rel, err := filepath.Rel(root, filepath.Dir(path))
if err != nil || rel == "." {
return "."
}
parts := strings.Split(rel, string(filepath.Separator))
if depth > 0 && len(parts) > depth {
parts = parts[:depth]
}
return filepath.Join(parts...)
}