Changeset 1985
- Timestamp:
- 04/20/17 18:30:00 (7 years ago)
- Location:
- trunk/LATMOS-Accounts
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LATMOS-Accounts/MANIFEST
r1965 r1985 53 53 bin/la-warn-expire 54 54 bin/la2xls 55 bin/la-load-csv 55 56 etc/cron.d/latmos-accounts 56 57 etc/init.d/la-sync-manager -
trunk/LATMOS-Accounts/Makefile.PL
r1963 r1985 83 83 bin/la-ban-passwd 84 84 bin/la-renewEmp 85 bin/la-load-csv 85 86 ) ], 86 87 macro => { -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/OCHelper.pm
r1023 r1985 112 112 } 113 113 114 =head2 $ochelper->Automate($info) 115 116 Try to create object from C<$info> w/o interacting with user. 117 If given infomation does not allow to create object, it failed. 118 119 Return 1 on success. 120 121 =cut 122 123 sub Automate { 124 my ($self, $info) = @_; 125 126 for (my $count = 0; $count < 3; $count++) { 127 my $status; 128 ($status, $info) = $self->step($info); 129 if ($status eq 'CREATED') { 130 return 1; 131 } elsif ($status eq 'ERROR') { 132 return; 133 } 134 } 135 } 136 114 137 1; 115 138 -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm
r1984 r1985 9 9 use File::Temp qw(tempfile); 10 10 use Crypt::Cracklib; 11 use Text::CSV; 11 12 12 13 our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; … … 21 22 22 23 @ISA = qw(Exporter); 23 @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno);24 @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno);24 @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno); 25 @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno); 25 26 26 27 =head2 to_ascii($text) … … 175 176 } 176 177 178 =head2 loadCSV($fh, $callback, $initcallback) 179 180 Parse CVS files and return an array for each parsed line. 181 182 If defined call C<$callback> for parsed line. 183 184 C<$initcallback>, if defined, is called just after the first line. 185 186 =cut 187 188 sub loadCSV { 189 my ($fh, $cb, $initcb) = @_; 190 191 my $csv = Text::CSV->new({ 192 blank_is_undef => 1, 193 binary => 1, 194 }); 195 196 binmode($fh, ":encoding(UTF-8)"); 197 198 # First line contains attribute 199 my $columns = $csv->getline( $fh ); 200 201 $csv->column_names($columns); 202 203 if ($initcb) { 204 $initcb->($csv); 205 } 206 207 my $all = []; 208 my $linecount = 1; 209 while ( my $row = $csv->getline_hr( $fh ) ) { 210 $linecount++; 211 if ($cb) { 212 if (! $cb->($row, $linecount)) { 213 return; 214 } 215 } 216 push(@{ $all }, $row); 217 } 218 $csv->eof () or do { 219 return; 220 }; 221 222 return $all; 223 } 224 177 225 =head2 check_oid_validity($name) 178 226 -
trunk/LATMOS-Accounts/t/05_utils.t
r1984 r1985 1 1 use strict; 2 2 use warnings; 3 use Test::More tests => 3 0;3 use Test::More tests => 32; 4 4 use File::Temp qw(mkstemp); 5 5 … … 10 10 ok(la_log(LA_NOTICE, "a notice"), "can run la_log"); 11 11 12 { 12 13 my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); 13 14 print $fh <<EOF; … … 32 33 ok(LATMOS::Accounts::Utils::dump_read_temp_file( 33 34 sub { 1; }, sub { 1; }), "Can edit temp file"); 35 36 } 37 { 38 my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); 39 binmode($fh, ":encoding(UTF-8)"); 40 print $fh <<EOF; 41 sn,givenName 42 Nom,Prénom 43 EOF 44 45 seek($fh, 0, 0); 46 47 my $res = LATMOS::Accounts::Utils::loadCSV($fh); 48 $fh = undef; 49 unlink($file); 50 51 # now testing 52 is(scalar(@{ $res }), 1, "loadCSV return same number result"); 53 is($res->[0]{givenName}, "Prénom", "Attribute givenName is properly return"); 54 55 } 34 56 35 57 ok(!LATMOS::Accounts::Utils::check_oid_validity('toto'), '"toto" is valid oid');
Note: See TracChangeset
for help on using the changeset viewer.