Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 170 lines (122 sloc) 3.64 KB
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Deepcopy = 1;
$Data::Dumper::Sortkeys = 1;
use Getopt::Long;
use Pod::Usage;
use Path::Class qw(file dir);
use Digest::MD5::File qw(file_md5);
use UNIVERSAL::require;
my $Debug = 0;
sub dprint (@) {
return unless $Debug;
chomp(my @m = @_);
print STDERR 'DEBUG: ', join(" ", @m),"\n";
}
# you can add
my %Generator = (
yaml => \&as_yaml,
pair => \&as_pair,
);
MAIN: {
my $input_dir = "";
my $output_dir = "";
Getopt::Long::Configure("bundling");
GetOptions(
'input_dir|i=s' => \$input_dir,
'output_dir|o=s' => \$output_dir,
'debug|d+' => \$Debug,
'help|h|?' => sub{ pod2usage(-verbose=>1) }) or pod2usage();
-d $input_dir || pod2usage("no such input directory: $input_dir");
-d $output_dir || pod2usage("no such output directory: $output_dir");
$input_dir = dir($input_dir);
$output_dir = dir($output_dir);
my $cidr = read_cidrs($input_dir);
generate_cidr_files($cidr, $output_dir);
exit 0;
}
sub read_cidrs {
my($input_dir) = @_;
my $cidr = {};
for my $cidr_file (grep !$_->is_dir, $input_dir->children) {
$cidr->{ $cidr_file->basename } = [ grep !/^(#|$)/, $cidr_file->slurp( chomp => 1 ) ];
}
return $cidr;
}
sub generate_cidr_files {
my($cidr, $output_dir) = @_;
dprint "generate_cidr_files\n";
while (my($name, $generator) = each %Generator) {
dprint " $name\n";
$generator->($cidr, $output_dir->subdir($name));
}
}
sub as_yaml {
my($cidr, $output_dir) = @_;
"YAML"->require;
_write_one_file($cidr, $output_dir, sub {
my($fh, $cidr) = @_;
printf $fh YAML::Dump($cidr);
});
}
sub as_pair {
my($cidr, $output_dir) = @_;
_write_one_file($cidr, $output_dir, sub {
my($fh, $cidr) = @_;
while (my($bname, $cidrs) = each %$cidr) {
for my $c (@$cidrs) {
printf $fh "%-18s %s;\n", $c, $bname;
}
}
});
}
sub _write_one_file {
my($cidr, $output_dir, $write) = @_;
-d $output_dir or $output_dir->mkpath or croak $!;
my $file = $output_dir->file("cidr");
my $file_tmp = $output_dir->file("cidr".'.tmp');
my $fh = $file_tmp->open('>', 0664) or carp $!;
$write->($fh, $cidr);
$fh->close;
if ( updated_cidr($file, $file_tmp) ) {
rename $file_tmp->stringify, $file->stringify or carp $!;
} else {
$file_tmp->remove or carp $!;
}
}
sub updated_cidr {
my($old, $new) = (shift->stringify, shift->stringify);
return ! -e $old || file_md5($old) ne file_md5($new);
}
__END__
=head1 NAME
B<generate-cidr-files> - generate files in various format from CIDR data
=head1 SYNOPSIS
B<generate_cidr_files>
(B<-i>|B<--input_dir>) I<INPUT_DIR>
(B<-o>|B<--output_dir>) I<OUTPUT_DIR>
[ B<-d>|B<--debug> ]
=head1 OPTIONS
=over 4
=item B<-i> I<Str>
=item B<--input_dir> I<Str>
read original CIDR data form files under this directory.
INPUT_DIR/{cidr1,cidr2,cidr3,...}
=item B<-o> I<Str>
=item B<--output_dir> I<Str>
write files under this directory.
OUTPUT_DIR/GENERATER/FILE
=item B<-d>
=item B<--debug>
increase debug level.
-d -d more verbosely.
=back
=head1 AUTHOR
HIROSE, Masaaki E<lt>hirose31 _at_ gmail.comE<gt>
=head1 COPYRIGHT & LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.