Nakamura/World.pm

filter: Statementstmt 70.8% 119 / 168 Branchbran 70.0% 42 / 60 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.0% 18 / 20 Podpod 40.0% 4 / 10 totaltotal 70.7% 186 / 263 CC 34 SCAR 28.7
CC
34
Cov
71%
run
44.2
add_from_file
20.7
add
3.4
CRAP 17.7
« Nakamura/Authn.pm ↑ Summary Nakamura/User.pm »
1 #!/usr/bin/perl -w
2  
3 package Sakai::Nakamura::World;
4  
5 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sakai::Nakamura;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sakai::Nakamura::Authn;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Sakai::Nakamura::WorldUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16  
17 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our @EXPORT_OK = ();
20  
21 1 our $VERSION = '0.13';
22  
23 #{{{sub new
24  
25 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
26 2 my ( $class, $authn, $verbose, $log ) = @_;
27 2 ▶ if ( !defined $authn ) { croak 'no authn provided!'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authn) 1 1 2
28 1 my $response;
29 1 ▶ $verbose = ( defined $verbose ? $verbose : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $verbose ? : 1 0 1
30 my $world = {
31 1 BaseURL => ${$authn}->{'BaseURL'},
32 Authn => $authn,
33 Message => q{},
34 Response => \$response,
35 Verbose => $verbose,
36 Log => $log
37 };
38 1 bless $world, $class;
39 1 return $world;
40 }
41  
42 #}}}
43  
44 #{{{sub set_results
45 1 ▶sub set_results {
Subroutine set_results: called CC=1 SCAR=0.0
Pod documented
46 1 my ( $world, $message, $response ) = @_;
47 1 $world->{'Message'} = $message;
48 1 $world->{'Response'} = $response;
49 1 return 1;
50 }
51  
52 #}}}
53  
54 #{{{sub add
55 1 ▶sub add {
Subroutine add: called CC=2 SCAR=12.2
Pod documented
56 1 my ( $world, $id, $title, $description, $tags, $visibility, $joinability,
57 $world_template )
58 = @_;
59 my $res = Apache::Sling::Request::request(
60 \$world,
61 Sakai::Nakamura::WorldUtil::add_setup(
62 $world->{'BaseURL'}, $id,
63 1 ${ $world->{'Authn'} }->{'Username'}, $title,
64 $description, $tags,
65 $visibility, $joinability,
66 $world_template
67 )
68 );
69 0 my $success = Sakai::Nakamura::WorldUtil::add_eval($res);
70 0 my $message = "World: \"$id\" ";
71 0 ▶ $message .= ( $success ? 'added!' : 'was not added!' );
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
72 0 $world->set_results( "$message", $res );
73 0 return $success;
74 }
75  
76 #}}}
77  
78 #{{{sub add_from_file
79 5 ▶sub add_from_file {
Subroutine add_from_file: called CC=20 SCAR=30.3
Pod documented
80 5 my ( $world, $file, $fork_id, $number_of_forks ) = @_;
81 5 ▶ $fork_id = defined $fork_id ? $fork_id : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $fork_id ? : 1 4 5
82 5 ▶ $number_of_forks = defined $number_of_forks ? $number_of_forks : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $number_of_forks ? : 1 4 5
83 5 my $csv = Text::CSV->new();
84 5 my $count = 0;
85 5 my $number_of_columns = 0;
86 5 my @column_headings;
87 5 ▶ if ( defined $file && open my ($input), '<', $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and open my $input, '<', $file) { } 4 1 5
Condition66%2/3
defined $file and open my $input, "<", $file
!ll&&!rl&&r
104
MC/DC50%1/2
defined $file and open my $input, "<", $file
defined $file open my $input, "<", $file
Truth table66%2/3
A and B
A= defined $fileB= open my $input, "<", $file
ABresult
0X0
100
111
88  
89 4 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 6 1 7
90 6 ▶ if ( $count++ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count++ == 0) { } 4 2 6
elsif ($fork_id == $count++ % $number_of_forks) { } 2 0 2
91  
92 # Parse file column headings first to determine field names:
93 4 ▶ if ( $csv->parse($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($_)) { } 4 0 4
94 4 @column_headings = $csv->fields();
95  
96 # First field must be id:
97 4 ▶ if ( $column_headings[0] !~ /^[Ii][Dd]$/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column_headings[0] =~ /^[Ii][Dd]$/msx) 1 3 4
98 1 croak
99 'First CSV column must be the world ID, column heading must be "id". Found: "'
100 . $column_headings[0] . "\".\n";
101 }
102 3 $number_of_columns = @column_headings;
103 }
104 else {
105 0 croak 'CSV broken, failed to parse line: '
106 . $csv->error_input;
107 }
108 }
109 elsif ( $fork_id == ( $count++ % $number_of_forks ) ) {
110 2 ▶ if ( $csv->parse($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($_)) { } 2 0 2
111 2 my @columns = $csv->fields();
112 2 my $columns_size = @columns;
113  
114 # Check row has same number of columns as there were column headings:
115 2 ▶ if ( $columns_size != $number_of_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns_size != $number_of_columns) 1 1 2
116 1 croak
117 "Found \"$columns_size\" columns. There should have been \"$number_of_columns\".\nRow contents was: $_";
118 }
119 1 my $id = $columns[0];
120 1 my $title;
121 my $description;
122 1 my $tags;
123 1 my $visibility;
124 1 my $joinability;
125 1 my $world_template;
126  
127 1 ▶ for ( my $i = 1 ; $i < $number_of_columns ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $number_of_columns) 7 0 7
128 7 my $heading = $column_headings[$i];
129 7 ▶ if ( $heading =~ /^[Tt][Ii][Tt][Ll][Ee]$/msx ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($heading =~ /^[Tt][Ii][Tt][Ll][Ee]$/msx) { } 1 6 7
elsif ($heading =~ /^[Dd][Ee][Ss][Cc][Rr][Ii][Pp][Tt][Ii][Oo][Nn]$/msx) { } 1 5 6
elsif ($heading =~ /^[Tt][Aa][Gg][Ss]$/msx) { } 1 4 5
elsif ($heading =~ /^[Vv][Ii][Ss][Ii][Bb][Ii][Ll][Ii][Tt][Yy]$/msx) { } 1 3 4
elsif ($heading =~ /^[Jj][Oo][Ii][Nn][Aa][Bb][Ii][Ll][Ii][Tt][Yy]$/msx) { } 1 2 3
elsif ($heading =~ /^[Ww][Oo][Rr][Ll][Dd][Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]$/msx) { } 1 1 2
130 1 $title = $columns[$i];
131 }
132 elsif ( $heading =~
133 /^[Dd][Ee][Ss][Cc][Rr][Ii][Pp][Tt][Ii][Oo][Nn]$/msx
134 )
135 {
136 1 $description = $columns[$i];
137 }
138 elsif ( $heading =~ /^[Tt][Aa][Gg][Ss]$/msx ) {
139 1 $tags = $columns[$i];
140 }
141 elsif ( $heading =~
142 /^[Vv][Ii][Ss][Ii][Bb][Ii][Ll][Ii][Tt][Yy]$/msx )
143 {
144 1 $visibility = $columns[$i];
145 }
146 elsif ( $heading =~
147 /^[Jj][Oo][Ii][Nn][Aa][Bb][Ii][Ll][Ii][Tt][Yy]$/msx
148 )
149 {
150 1 $joinability = $columns[$i];
151 }
152 elsif ( $heading =~
153 /^[Ww][Oo][Rr][Ll][Dd][Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]$/msx
154 )
155 {
156 1 $world_template = $columns[$i];
157 }
158 else {
159 1 croak
160 "Unsupported column heading \"$heading\" - please use: \"id\", \"title\", \"description\", \"tags\", \"visibility\", \"joinability\", \"worldtemplate\"";
161 }
162 }
163 0 $world->add( $id, $title, $description, $tags, $visibility,
164 $joinability, $world_template );
165 0 Apache::Sling::Print::print_result($world);
166 }
167 else {
168 0 croak q{CSV broken, failed to parse line: }
169 . $csv->error_input;
170 }
171 }
172 }
173 1 ▶ close $input or croak q{Problem closing input};
Branch: 1 branch
BranchTrueFalseTotal
close $input or croak("Problem closing input") 1 0 1
174 }
175 else {
176 1 croak 'Problem adding from file!';
177 }
178 1 return 1;
179 }
180  
181 #}}}
182  
183 #{{{ sub command_line
184 0 ▶sub command_line {
Subroutine command_line: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my ( $class, @ARGV ) = @_;
186 0 my $nakamura = Sakai::Nakamura->new;
187 0 my $config = $class->config( $nakamura, @ARGV );
188 0 my $authn = new Sakai::Nakamura::Authn( \$nakamura );
189 0 return $class->run( $nakamura, $config );
190 }
191  
192 #}}}
193  
194 #{{{sub config
195  
196 1 ▶sub config {
Subroutine config: called CC=2 SCAR=7.0
Pod undocumented
197 1 my ( $class, $nakamura, @ARGV ) = @_;
198 1 my $world_config = $class->config_hash( $nakamura, @ARGV );
199  
200 1 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions($world_config, "auth=s", "help|?", "log|L=s", "man|M", "pass|p=s", "threads|t=s", "url|U=s", "user|u=s", "verbose|v+", "add|a", "additions|A=s", "copy|c", "delete|d", "exists|e", "filename|n=s", "local|l=s", "move|m", "property|P=s", "remote|r=s", "remote-source|S=s", "replace|R", "view|V", "view-copyright=s", "view-description=s", "view-tags=s", "view-title=s", "view-visibility=s") or $class->help 1 0 1
201 $world_config, 'auth=s',
202 'help|?', 'log|L=s',
203 'man|M', 'pass|p=s',
204 'threads|t=s', 'url|U=s',
205 'user|u=s', 'verbose|v+',
206 'add|a', 'additions|A=s',
207 'copy|c', 'delete|d',
208 'exists|e', 'filename|n=s',
209 'local|l=s', 'move|m',
210 'property|P=s', 'remote|r=s',
211 'remote-source|S=s', 'replace|R',
212 'view|V', 'view-copyright=s',
213 'view-description=s', 'view-tags=s',
214 'view-title=s', 'view-visibility=s'
215 ) or $class->help();
216  
217 1 return $world_config;
218 }
219  
220 #}}}
221  
222 #{{{sub config_hash
223  
224 1 ▶sub config_hash {
Subroutine config_hash: called CC=1 SCAR=0.0
Pod undocumented
225 1 my ( $class, $nakamura, @ARGV ) = @_;
226 1 my $add;
227 my $additions;
228 1 my $id;
229 1 my $title;
230 1 my $description;
231 1 my $tags;
232 1 my $visibility;
233 1 my $joinability;
234 1 my $world_template;
235 my %world_config = (
236 'auth' => \$nakamura->{'Auth'},
237 'help' => \$nakamura->{'Help'},
238 'log' => \$nakamura->{'Log'},
239 'man' => \$nakamura->{'Man'},
240 'pass' => \$nakamura->{'Pass'},
241 'threads' => \$nakamura->{'Threads'},
242 'url' => \$nakamura->{'URL'},
243 'user' => \$nakamura->{'User'},
244 1 'verbose' => \$nakamura->{'Verbose'},
245 'add' => \$add,
246 'additions' => \$additions,
247 'title' => \$title,
248 'description' => \$description,
249 'tags' => \$tags,
250 'visibility' => \$visibility,
251 'joinability' => \$joinability,
252 'world_template' => \$world_template
253 );
254  
255 1 return \%world_config;
256 }
257  
258 #}}}
259  
260 #{{{ sub help
261 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod undocumented
262  
263 1 print <<"EOF";
264 Usage: perl $0 [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
265 The following options are accepted:
266  
267 --add or -a (worldid) - add specified world.
268 --additions or -A (file) - file containing list of worlds to be added.
269 --auth (type) - Specify auth type. If ommitted, default is used.
270 --description or -d - description of world
271 --help or -? - view the script synopsis and options.
272 --joinability or -j (joinability) - Joinability of world.
273 --log or -L (log) - Log script output to specified log file.
274 --man or -M - view the full script documentation.
275 --pass or -p (password) - Password of user performing actions.
276 --threads or -t (threads) - Used with -A, defines number of parallel
277 processes to have running through file.
278 --tags or -T (tags) - tags for world
279 --title or -t (title) - title for world
280 --url or -U (URL) - URL for system being tested against.
281 --user or -u (username) - Name of user to perform any actions as.
282 --verbose or -v or -vv or -vvv - Increase verbosity of output.
283 --visibility or -V (visibility) - Visibility of world.
284 --worldTemplate or -w - World template to use.
285  
286 Options may be merged together. -- stops processing of options.
287 Space is not required between options and their arguments.
288 For full details run: perl $0 --man
289 EOF
290  
291 1 return 1;
292 }
293  
294 #}}}
295  
296 #{{{ sub man
297 0 ▶sub man {
Subroutine man: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my ($world) = @_;
299  
300 0 print <<'EOF';
301 world perl script. Provides a means of managing worlds in nakamura from the command
302 line. The script also acts as a reference implementation for the World perl
303 library.
304  
305 EOF
306  
307 0 $world->help();
308  
309 0 print <<"EOF";
310 Example Usage
311  
312 * TODO: add examples
313  
314 perl $0 -U http://localhost:8080 -u admin -p admin
315 EOF
316  
317 0 return 1;
318 }
319  
320 #}}}
321  
322 #{{{sub run
323 2 ▶sub run {
Subroutine run: called CC=11 SCAR=37.9
Pod undocumented
324 2 my ( $world, $nakamura, $config ) = @_;
325 2 ▶ if ( !defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 1 1 2
326 1 croak 'No world config supplied!';
327 }
328 1 $nakamura->check_forks;
329 my $authn =
330 defined $nakamura->{'Authn'}
331 1 ▶ ? ${ $nakamura->{'Authn'} }
Branch: 1 branch
BranchTrueFalseTotal
defined $nakamura->{'Authn'} ? : 1 0 1
332 : new Sakai::Nakamura::Authn( \$nakamura );
333  
334 1 my $success = 1;
335  
336 0 ▶ if ( $nakamura->{'Help'} ) { $world->help(); }
Branch: 3 branches
BranchTrueFalseTotal
if ($nakamura->{'Help'}) { } 0 1 1
elsif ($nakamura->{'Man'}) { } 0 1 1
elsif (defined ${$$config{'additions'};}) { } 0 1 1
337 0 elsif ( $nakamura->{'Man'} ) { $world->man(); }
338 1 elsif ( defined ${ $config->{'additions'} } ) {
339 my $message =
340 0 "Adding worlds from file \"" . ${ $config->{'additions'} } . "\":\n";
341 0 Apache::Sling::Print::print_with_lock( "$message", $nakamura->{'Log'} );
342 0 my @childs = ();
343 0 for my $i ( 0 .. $nakamura->{'Threads'} ) {
344 0 my $pid = fork;
345 0 ▶ if ($pid) { push @childs, $pid; } # parent
Branch: 2 branches
BranchTrueFalseTotal
if ($pid) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
346 elsif ( $pid == 0 ) { # child
347 # Create a new separate user agent per fork in order to
348 # ensure cookie stores are separate, then log the user in:
349 0 $authn->{'LWP'} = $authn->user_agent( $nakamura->{'Referer'} );
350 0 $authn->login_user();
351 my $world =
352 new Sakai::Nakamura::World( \$authn, $nakamura->{'Verbose'},
353 0 $nakamura->{'Log'} );
354 0 $world->add_from_file( ${ $config->{'additions'} },
355 0 $i, $nakamura->{'Threads'} );
356 0 exit 0;
357 }
358 else {
359 0 croak "Could not fork $i!";
360 }
361 }
362 0 foreach (@childs) { waitpid $_, 0; }
363 }
364 else {
365 1 $authn->login_user();
366 1 ▶ if ( defined ${ $config->{'add'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$$config{'add'};}) { } 0 1 1
367 $world =
368 new Sakai::Nakamura::World( \$authn, $nakamura->{'Verbose'},
369 0 $nakamura->{'Log'} );
370 $success = $world->add(
371 0 ${ $config->{'add'} },
372 0 ${ $config->{'title'} },
373 0 ${ $config->{'description'} },
374 0 ${ $config->{'tags'} },
375 0 ${ $config->{'visibility'} },
376 0 ${ $config->{'joinability'} },
377 0 ${ $config->{'world_template'} }
378 );
379 0 Apache::Sling::Print::print_result($world);
380 }
381 else {
382 1 $world->help();
383 1 return 1;
384 }
385 }
386 0 return $success;
387 }
388  
389 #}}}
390  
391 1 1;
392  
393 __END__
« Nakamura/Authn.pm ↑ Summary Nakamura/User.pm »