Skip to content

Instantly share code, notes, and snippets.

@zengargoyle
Created May 2, 2011 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zengargoyle/951161 to your computer and use it in GitHub Desktop.
Save zengargoyle/951161 to your computer and use it in GitHub Desktop.
simple fs structure check: http://ask.metafilter.com/184773
#!/usr/bin/perl
use strict;
use warnings;
my $top = shift(@ARGV) || '.';
chdir $top or die "could not chdir to $top: $!\n";
open my $find, '-|', 'find .' or die "$!\n";
my %top_must = (
Apple => 0,
Orange => 0,
Plum => 0,
);
my @ext = qw( png psd );
my $allowed_ext = join '|', @ext;
while (my $path = <$find>) {
chomp $path;
my @part = split /\//, $path;
# handle initial '.' directory case
shift @part;
next unless @part;
# 0 -1
# @part = qw( Apple some path part maybe filename.ext );
my $top = shift @part;
my $file = pop @part;
my @middle = @part;
unless ( exists $top_must{ $top } ) {
warn "W: directory $path not allowed\n";
next;
}
$top_must{ $top }++; # mark as seen
# skip further checks on directories
next unless -f $path;
if ( $file =~ /\s/ ) {
warn "W: file $path has spaces\n";
next;
}
if ( $file =~ /[A-Z]/ ) {
warn "W: file $path has uppercase\n";
next;
}
if ( $file !~ /\.(?:$allowed_ext)$/ ) {
warn "W: file $path extension not allowed\n";
next;
}
}
# check mandatory
for my $must ( keys %top_must ) {
warn "W: required directory $must: not found\n" unless $top_must{$must};
}
$ ls -R foo
foo:
Apple
foo/Apple:
bad_ext.txt blah space.txt Caps.txt should_be_ok.png
$ ./184773.pl foo
W: file ./Apple/bad_ext.txt extension not allowed
W: file ./Apple/blah space.txt has spaces
W: file ./Apple/Caps.txt has uppercase
W: requred directory Orange: not found
W: requred directory Plum: not found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment