--- bin/ldap2model.pl 2008/05/01 13:41:44 102 +++ bin/ldap2model.pl 2008/05/01 16:31:00 105 @@ -2,22 +2,56 @@ use warnings; use strict; -# schema2model.pl - convert LDAP schema file into jifty model -# -# 04/30/08 20:55:21 CEST Dobrica Pavlinusic -# -# ./bin/ldap2model.pl data/all.ldif hrEduOrg +=head1 NAME +schema2model.pl - convert LDAP schema file into jifty model + +=head1 DESCRIPTION + +Create model from ldif data + + ./bin/ldap2model.pl --model hrEduOrg --path data/all.ldif + +or directly from LDAP server + + ./bin/ldap2model.pl --model inetOrgPerson + ./bin/ldap2model.pl --model organization + +which must match C in C + +With C<--debug> switch all output will go to C +instead to files. + +=cut + +use lib 'lib'; + +use Jifty; +use A3C::LDAP; use Net::LDAP::Schema; use File::Slurp; use Data::Dump qw/dump/; +use Getopt::Long; -my ( $path, $objectClass ) = @ARGV; +my ( $path, $objectClass, $debug ); -die "usage: $0 path/to/schema.ldif inetOrgPerson\n" unless $path && $objectClass; - -my $schema = Net::LDAP::Schema->new; -$schema->parse ( $path ) or die $schema->error; +GetOptions( + 'model|objectClass=s', => \$objectClass, + 'path=s', => \$path, + 'debug+', => \$debug, +); + +die "usage: $0 --model netOrgPerson [--path path/to/schema.ldif]\n" unless $objectClass; + +my $schema; +if ( $path ) { + $schema = Net::LDAP::Schema->new; + $schema->parse ( $path ) or die $schema->error; + warn "# loaded schema from $path\n"; +} else { + my $l = A3C::LDAP->new; + $schema = $l->ldap->schema; +} die "$objectClass objectClass not found in $path\n" unless $schema->objectclass( $objectClass ); @@ -54,7 +88,7 @@ map { - warn "# $objectClass must: ",dump( $_ ); + warn "# $objectClass must: ",dump( $_ ) if $debug; $model .= entry( $_, 'is mandatory' ); $create->{$_->{name}} = $_->{name}; } $schema->must( $objectClass ); @@ -62,7 +96,7 @@ $model .= qq/\t# $objectClass may:\n\n/; map { - warn "# $objectClass may: ",dump( $_ ); + warn "# $objectClass may: ",dump( $_ ) if $debug; $model .= entry( $_ ); } $schema->may( $objectClass ); @@ -79,9 +113,13 @@ 1; /; -my $model_path = "lib/A3C/Model/$objectClass.pm"; -write_file( $model_path, $model ); -warn "Created $model_path\n"; +if ( $debug ) { + print "##### ----- created model test\n$model\n"; +} else { + my $model_path = "lib/A3C/Model/$objectClass.pm"; + write_file( $model_path, $model ); + print "Created $model_path\n"; +} my $test = <<'__END_OF_TEST__'; #!/usr/bin/env perl @@ -141,11 +179,15 @@ foreach my $round ( 1 .. 2 ) { my $data; $data .= qq/\t\t'$_' => '$_ $round',\n/ foreach keys %$create; - warn "data = $data\n"; + warn "$round data = $data\n" if $debug; $test =~ s/_create_${round}_/$data/gs; } -my $test_path = "t/00-model-$objectClass.t"; -write_file( $test_path, $test ); -warn "Created $test_path\n"; -chmod 0755, $test_path; +if ( $debug ) { + print "##### ----- template test\n$test\n"; +} else { + my $test_path = "t/00-model-$objectClass.t"; + write_file( $test_path, $test ); + print "Created $test_path\n"; + chmod 0755, $test_path; +}