script/run2.pl untested

filter: Statementstmt 0.0% 0 / 341 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 511 CC 45 SCAR 46.0
CC
45
Cov
0%
display
342.0
initCache
90.0
usage
72.0
CRAP 99.1
1 #!/usr/local/bin/perl
2  
3 use Data::Freq;
4  
5 use Data::Dumper;
6 use Data::Printer;
7 use Term::ShellUI;
8 use File::stat qw(:FIELDS);
9 use AI::MicroStructure;
10 use Storable qw(lock_store lock_retrieve);
11 use Search::ContextGraph;
12 use Try::Tiny;
13 use Getopt::Long;
14 use JSON;
15 use JSON::XS;
16  
17 our @cmds = qw/index search new cat files load/;
18 our %name = (action=>[@cmds],#search get set change plot cloudy
19 type=>[qw(match dir indices)],# know growing fit term
20 location=>[@cmds]);
21 use File::Find::Rule;
22 use File::Temp qw(tempdir);
23 use Thread;
24  
25  
26 my$UNDERLINE=`tput smul`;
27 my $CYAN=`tput setaf 6`;
28 my $REVERSE=`tput smso`;
29 my $BRIGHT=`tput bold`;
30 my $NORMAL=`tput sgr0`;
31 our $rows = `tput lines`;
32 our $column = `tput cols`;
33 $column = $column;
34 my $quaters = (int $column/9);
35 my $curSysDate = `date +"%Y%m%d"`;
36 $curSysDate=~ s/\n//g;
37  
38 my @name = ();
39 my @vals =();
40 our $context = {};
41 our $cache = {};
42 our %opts = (
43 max_cache_age => 18000,
44 cache_file => sprintf("./realtime.cache"),
45 match =>"",
46 dir => " archive/multi/www.nicap.org/",
47 cache => 1,
48 );
49 GetOptions (\%opts, "query=s",
50 "max_cache_age=i",
51 "cache_file=s",
52 "match=s",
53 "dir=s",
54 "cache!",
55 );
56  
57  
58  
59 sub SHOW_LINE{
60 my $leng = shift;
61 $leng = $column unless($leng);
62 return sprintf("%s","-"x$leng);
63  
64 }
65  
66 sub getBaseCache { return $cache; }
67 sub trim
68 {
69 my $string = shift;
70 $string = "" unless $string;
71 $string =~ s/^\s+//;
72 $string =~ s/\s+$//;
73 $string =~ s/\t//;
74 $string =~ s/^\s//;
75 return $string;
76 }
77 sub initCache {
78 eval {
79 local $^W = 0; # because otherwhise doesn't pass errors
80  
81  
82 if(! -f $opts{cache_file}){
83 $cache = {};
84 lock_store($cache,$opts{cache_file});
85 }else{
86 $cache = lock_retrieve($opts{cache_file});
87 }
88  
89  
90 if(!defined($cache->{tempdir}) ||
91 ! -d $cache->{tempdir}){
92 $cache->{"tempdir"} = tempdir();
93  
94  
95  
96  
97 }
98  
99  
100  
101 $cache->{cache_version} = (defined($cache->{cache_version}) ? $cache->{cache_version} +1 : 1);
102 $cache->{time} = time();
103 $cache->{files} = {} unless(defined($cache->{files}));
104 $cache->{files}->{cng} = [] unless(defined($cache->{files}->{cng}));
105 $cache->{structures} = [] unless($cache->{structures});
106 $opts{"match"} = [split(" ",$opts{"match"})];# unless($opts{"match"} eq "");
107 $opts{"preg"} = join("|",@{$opts{"match"}});
108 $cache->{structures} = [grep{/$opts{"preg"}/} AI::MicroStructure->new()->structures()];
109  
110  
111  
112 $cache->{structures_from} = AI::MicroStructure->new()->{state}->{path}->{"cwd/structures"};
113 $cache->{preg} = $opts{preg};
114 $cache->{search_in} = $opts{dir};
115  
116 $opts{"preg"} = join("|",@{$cache->{structures}});
117 $cache->{require} = [grep{!/$opts{"preg"}/}@{$opts{"match"}}];
118 $cache->{opts}=\%opts;
119  
120  
121 if(!$cache->{files} ->{cng} || !$cache->{files} ->{cng}->[0]){
122 $cache->{files} ->{$_}= [find( file =>
123 name =>["*.".$_ ],
124 size => '>1k',
125 in => [grep { -d $_ } @INC])] for qw(cng);
126 }
127 warn "New cache!\n" unless defined $cache;
128 };
129  
130  
131  
132 }
133 initCache();
134  
135 our $cmd = {
136 "h" => { alias => "help", exclude_from_completion => 1 },
137 "?" => { alias => "help", exclude_from_completion =>0},
138 "quit" => { alias => "exit", exclude_from_completion =>1},
139 "help" => { desc => "Print helpful information", args => sub { shift->help_args(undef, @_); }, method => sub { shift->help_call(undef, @_); } },
140 "exit" => { desc => "Exits the program.", maxargs => 0, method => sub { shift->exit_requested(1); }, },
141 "is" => { desc => "Shows whether files exist", args => sub { shift->complete_files(@_); }, exclude_from_completion =>0 , proc => sub { print "exists: " . join(", ", map {-e($_) ? "<$_>":$_} @_) . "\n"; },
142 "quit" => { desc => "Quit using Fileman", maxargs => 0, method => sub { shift->exit_requested(1); }, },
143 '' => { proc => "No command here by that name!\n", desc => "No help for unknown commands.", doc => "Well, here's a little help: don't type them.\n", },
144 "show" => { desc => "An example of using subcommands", cmds => { "warranty" => { proc => "You have no warranty!\n" }, exclude_from_completion =>1 , "args" => {
145 args => [ sub {['create', 'delete']}, \&Term::ShellUI::complete_files ], desc => "Print the passed arguments", method => sub { my $self = shift; my $parms = shift; print $self->get_cname($parms->{cname}) . ": " . join(" ",@_), "\n"; }, }, }, },
146 "string" => { desc => "String operations", cmds => { "subs" => { args => ["(string)", "(pos)", "(len)"], desc => "Take STRING,POS,LEN and return a substring.", minargs => 1, maxargs => 3,proc => sub { print "Substring=".substr(shift,shift||0,shift||0)."\n" }, },
147 "len" => { args => "(any number of strings)", desc => "Calculate length of arguments", proc => sub { print "Length=" . join(", ", map { length } @_) . "\n" }, }, }, },
148 doc =>
149 <<EOL
150 Pass any number of filename.
151 If a file exists, it is
152 printed in <angle brackets>.
153 EOL
154 },
155 "history" => { desc => "Prints the command history", doc => "Specify a number to list the last N lines of history\nPass -c to clear the command history,-d NUM to delete a single item\n",
156 args => "[-c] [-d] [number]",
157 method => sub { shift->history_call(@_) },
158 exclude_from_history => 1,},
159  
160 };
161  
162  
163 END{
164  
165  
166 my $encoder = JSON->new->allow_blessed->pretty(1);
167 my $json = $encoder->encode($cache);
168  
169 lock_store($cache, sprintf("%s/%s",$cache->{ "tempdir"},$opts{cache_file}));
170 open my $fh, ">", sprintf("%s/cache.json",$cache->{ "tempdir"});
171 print $fh $json;
172 close $fh;
173  
174 #initCache();
175 p $cache;
176  
177 }
178  
179 sub status {
180 my $cont = shift ;
181 my $b = shift ;
182 $b = "" unless($b);
183 print "\n\n";
184 SHOW_LINE(19);
185 my $txt = "";
186 my @keys = keys %$cont;
187  
188 my @structures = @{$cont->{structures}} if($cont->{structures});
189 @structures = () unless(@structures);
190 my @files = @{$cont->{files}->{cng}} if($cont->{files}->{cng});
191 @files = () unless(@files);
192  
193 my $qq = sprintf("\t${REVERSE} cache_version %".($quaters)."s | micro-dir:%-".($quaters)."s | indices: %-".($quaters*2)."s | structures:%-".($quaters*2)."s $NORMAL"
194 ,$cont->{cache_version},$cont->{structures_from},scalar(@files),scalar(@structures));
195 print "\n\n";
196  
197 }
198  
199  
200  
201 sub actionBar {
202  
203 my $cont = shift ;
204 my $b = shift ;
205 $b .=sprintf("$BRIGHT $REVERSE%".($quaters-1)."s$NORMAL",`for i in {16..21} {21..16} ; do echo -en "\e[48;5;\${i}m \e[0m" ; done ;`) for 0..8 ;
206 # system q/perl -le 'print "line $_" for 1..20; <>'/;
207 # my @many = glob "{apple,tomato,cherry}={green,yellow,red}";
208 # p @many;
209 # printf($b);
210  
211 }
212  
213  
214 our $ms = AI::MicroStructure->new();
215  
216 our $descriptions = {action=>{},type=>{},location=>{}};
217 $descriptions->{action} = {
218  
219 # 'new' =>"get something !",
220 # 'knowen' =>"set something !",
221 # 'find' =>"get something !",
222  
223  
224 # 'desc' =>"show a description about something",
225 # 'delete' =>"remove something",
226 # 'search' =>"search something",
227 # 'change' =>"modify something",
228 # 'plot' =>"create vissualisation of something",
229 # 'cloudy' =>"create optimal query surface for something",
230 };
231  
232 $descriptions->{type} =
233 {
234  
235 #'new' =>"things you dont have yet",
236 #'my' =>"things you have already",
237 #'related' =>"things which are related to you",
238 #'known' =>"trending things",#
239 #'growing' =>"expanding things",
240 #'fit' =>"usefull things",
241 };
242  
243 $descriptions->{location} =
244 {
245 };
246  
247  
248 sub usage {
249  
250  
251 my @ASCII;
252 push @ASCII,"
253 $BRIGHT
254  
255 . . ) . .
256 . * . .
257 . .
258 . . .' .
259 '. * .
260 . ' .' . .
261 _.---._ . . *
262 * .' '.
263 _.-~===========~-._
264 (___________________) . *
265 __ \_______/ ______ __
266 | | | | |
267 | | | | |
268 | | | ___| |_
269 __|_______________________|__..--~~~~ micro ~--.
270  
271  
272  
273  
274 ";
275 push @ASCII,"
276 $BRIGHT. . . . . . . . . + .
277 . . : . .. :. .___---------___.
278 . . . . :.:. _''.^ .^ ^. '.. :''-_. .
279 . : . . .:../: . .^ :.:\.
280 . . :: +. :.:/: . . . . . .:\
281 . : . . _ :::/: . ^ . . .:\
282 .. . . . - : :.:./. . .:\
283 . . . :..|: . . ^. .:|
284 . . : : ..|| . . . !:|
285 . . . . ::. ::\( . :)/
286 . . : . : .:.|. ###### .#######::|
287 :.. . :- : .: ::|.####### ..########:|
288 . . . .. . .. :\ ######## :######## :/
289 . .+ :: : -.:\ ######## . ########.:/
290 . .+ . . . . :.:\. ####### #######..:/
291 :: . . . . ::.:..:.\ . . ..:/
292 . . . .. : -::::.\. | | . .:/
293 . : . . .-:.'':.::.\ ..:/
294 . -. . . . .: .:::.:.\. .:/
295 . . . : : ....::_:..:\ ___. :/
296 . . . .:. .. . .: :.:.:\ :/
297 . . : . ::. :.:. .:.|\ .:/|
298 ";
299  
300 push @ASCII, "
301 $BRIGHT
302 i!~!!))!!!!!!!!!!!!!!!!!!!!!!!!
303 i!!!{!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!i
304 i!!)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
305 '!h!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
306 '!!`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!i
307 /!!!~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
308 ' ':)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
309 ~:!!!!!!!!!!!!!!!!!!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!(~!!!!!!!!!!!!!~
333 `!!!!!!!!!!!{\``!!``(!!!!!!!!!~~ .
334 `!!!!!!!!!!!!!!!!!!!!!!!!(!:
335 .!!!!!!!!!!!!!!!!!!!!!\~
336 .`!!!!!!!/`.;;~;;`~!! '
337 -~!!!!!!!!!!!!!(!!/ .
338 `!!!!!!!!!!!!!!'
339 `\!!!!!!!!!~
340 ";
341  
342 my @rand = split( "\n",$ASCII[rand(3)]);
343 my $rand = "";
344  
345 @{$cache->{files}->{cng}} = sort {length($a)<=>length($b)} @{$cache->{files}->{cng}};
346  
347 `clear`;
348 actionBar();
349  
350 my @cat = @{$cache->{"categorys"}};
351 my @files = @{$cache->{files}->{cng}};
352  
353 if(@files){
354 printf("\033[0m\n\n\t<indices>\n");
355 printf("\n\t|-%s",$_,$rand[$_]) for @files;
356 }
357  
358 if(@cat) {
359 printf("\n\n<categorys>\n\t");
360 printf("\033[0;31m\n\t|-%s\033[255;34m\n%s",$_,$rand[$_]) for @cat;
361 }
362 printf(" \n\n\t\t$CYAN|<structures>\t
363 \t%20s
364 \033[0m",
365 sprintf("\033[0;31m\t%s\033[255;34m",join( "\n\t\t",grep {!/HASH/} map{$_ = defined($cache->{$_}) ? sprintf("%20s%4s$CYAN%s\033[0;31m" , $_,sprintf("%-2s","="),ref $cache->{$_} eq "ARRAY" ? scalar(@{$cache->{$_}}): ref $cache->{$_} =~/HASH/? join " ",keys %{$cache->{$_}}:$cache->{$_}) : $_; } reverse sort { length($a) <=> length($b)} keys %$cache))
366 );
367  
368 foreach(0..$#rand){
369  
370  
371 $led = defined($cache->{"categorys"}->[$_]) ? $cache->{"categorys"}->[$_] : "" unless($led);
372  
373 $rand .= sprintf("\n%s%".(($quaters*6)+length($rand[$_])-length($led))."s",$led,);
374 }
375  
376  
377  
378  
379 #<index>
380 #\t%20s
381 #sprintf("\033[0;31m|->=$CYAN%s\033[0;31m\033[255;34m",join( "\n\t\033[0;31m|->=$CYAN", @{$cache->{files}->{cng}})),
382 #$rand,
383  
384  
385 status($cache);
386  
387 }
388  
389  
390 sub setUp {
391 my (@files) = @_;
392 my $g = "";
393  
394 foreach(@files){
395 if(-f $_) {
396 try{
397 $g = Search::ContextGraph->retrieve($_);
398 $context->{pool}->{$_} = $g;
399  
400 }catch{
401 printf("error during load %s",$_);
402 }
403 }
404 }
405 }
406 sub index{
407 my $response = shift;
408  
409 if(-d $response){
410 $cache->{files} ->{$_}= [find( file =>
411 name =>["*.".$_ ],
412 size => '>1k',
413 in => [$response])] for qw(cng);
414 }
415  
416 }
417 sub promptUser {
418 my $default = shift;
419 my $defaultValue = sprintf("\n[%s]",$default);
420 print $defaultValue;
421  
422 chomp(my $input = <STDIN>);
423 return $input ? $input : $default;
424 }
425 sub Purge {
426 my $dir = shift;
427 open my $dh or die "$!, for $dir";
428 for my $file ( grep !/^\./, readdir $dh ) {
429 my $path = "$dir/$file";
430 if ( -d $path ) {
431 Purge( $path );
432 }
433 else {
434 ( -z $path ) and unlink $path;
435 }
436 }
437 close $dh or die "$!, for $dir";
438 }
439  
440 sub display {
441 foreach(@_){
442 if($_ =~ m/files/i){
443 my $show = join("|",@{$cache->{opts}->{match}});
444 my $response = promptUser($show);
445 my $match = $response;
446  
447 $response = promptUser("specify directory");
448 if(!-d $response){
449 printf("%s is not a directory",$response);
450 }else{
451 #my $dir = $cache->{search_in};
452 #my $cmd = `atw.pl --dir $dir --match \"$match\" --verbos 1`;
453 #print $cmd;
454  
455  
456 $cache->{search} ->{$_}= [find( file =>
457 name =>["*$_*.*" ],
458 size => '>0k',
459 in => [$response])] for @{$cache->{opts}->{match}};
460  
461  
462 return $cache;
463 }
464  
465 }elsif($_=~m/load (.*)/) {
466  
467 if(defined($1) && -d $1){
468  
469 push @INC,$1;
470 $cache->{files} ->{$_}= [find( file =>
471 name =>["*.".$_ ],
472 size => '>1k',
473 in => [grep { -d $_ } @INC])] for qw(cng);
474 }
475 my @files =@{$cache->{files}->{cng}};
476 @files = () unless(@files);
477 printf("\nno index available index\n") unless @files;
478 setUp(@files);
479 `clear`;
480 printf("\n"x100);
481 usage($cache);
482  
483 }elsif($_=~m/search (.*)/) {
484  
485 my @search = ();
486 push @search , $1 unless(!$1);
487  
488 `micro new $_ verbose` for @{$cache->{opts}->{match}};
489 push @search ,split "\n",trim(`micro all $_`) for @{$cache->{opts}->{match}};
490 p @search;
491  
492 $context->{pool}->{$_} = Search::ContextGraph->retrieve($_) for @{$cache->{files}->{cng}};
493  
494 foreach my $store (keys %{$context->{pool}}){
495  
496 $cache->{match} ->{$store}->{$_}= [$context->{pool}->{$store}->search($_)] for @search;
497 foreach(@search){
498 delete($cache->{match} ->{$store}->{$_}) unless($cache->{match} ->{$store}->{$_}->[0] || $cache->{match} ->{$store}->{$_}->[1]);
499 }
500 #$cache->{match} ->{$store}->{all}= [$context->{pool}->{$store}->search([@{$cache->{opts}->{match}}])]
501  
502 }
503 }elsif($_=~m/new (.*)/) {
504  
505 my $response = $1 ? $1 : promptUser("specify directory");
506 if(!-d $response){
507 printf("%s is not a directory",$response);
508 }else{
509 }
510 }elsif($_=~m/index (.*)/) {
511 my $response = $1 ? $1 : promptUser("specify directory");
512 if(!-d $response){
513 printf("%s is not a directory",$response);
514 }else{
515  
516  
517  
518 }
519  
520  
521  
522 }elsif($_=~m/cat (.*)/) {
523 my $search = $1;
524 my $cats={};
525 my @cats = ();
526 my @sp = split("\n" ,`getcat $1| sed -s "s/Category://"`);
527 foreach my $s (@sp){
528 my @qq =split(":",$s);
529 $cats->{$qq[1]}=$qq[0];
530 }
531  
532  
533 $cache->{categorys} = [reverse map{$_ = sprintf(( $_ =~/CATEGORYS/ ? "CATEGORYS:%": "%s%").(length($_)*2 -length($_))."s",$cats->{$_},$_ )}sort {length($a)<=>length($b)} keys %$cats];
534  
535  
536 }
537  
538 }
539 }
540  
541 our $location = {
542 "local" => {
543 args => "(path)",
544 desc => $descriptions->{location}->{local},
545 proc => sub {
546 my $self = shift;
547 my $parms = shift;
548 print get_cname($self,$parms) .
549 ": " . join(" ",@_), "\n";
550 },
551 },
552 "remote" => {
553 args => "(none)",
554 desc => $descriptions->{location}->{remote},
555 proc => sub {
556 my $self = shift;
557 my $parms = shift;
558 print get_cname($self,$parms) .
559 ": " . join(" ",@_), "\n";
560 },
561 }
562 };
563  
564  
565  
566 sub println{ return sprintf("%s %s \n",shift,localtime); }
567 sub show { my $prod=""; for(@_) { $prod.=$_; }; return $prod; }
568 sub set { my $sum=0; for(@_) { $sum+=$_; }; return $sum; }
569 sub mult { my $prod=1; for(@_) { $prod*=$_; }; return $prod; }
570 sub ashex { my $sub = shift; return sprintf("%x", &$sub(map { hex } @_)); }
571 sub danger { print "Not performing task -- too dangerous!\n"; return 0; }
572 sub getSubCommands { return {}; }
573  
574  
575 sub getCommands {
576  
577  
578 foreach (keys %name){
579 @vals = @{$name{$_}};
580  
581 #$ms->add_structure(
582 # $_ => $name{$_}
583 # );
584  
585  
586 my $ams = AI::MicroStructure->new("any");
587 push @name , $ams->name(0);
588  
589 try {
590 foreach my $ele(@vals){
591 if($_ =~ /action/) {
592 $cmd->{$ele} = {
593 args => sprintf("\033[0;31m%s\033[0m",join( " |", @{$name{type}})),
594 desc => $descriptions->{action}->{$ele},
595  
596 proc =>
597 sub { #my $thr = Thread->new( \&display,$_);
598 #my $foo = eval {$thr->join};
599  
600 my $foo = display($_);
601  
602  
603  
604  
605 }};
606  
607 }
608  
609 }
610 }catch{
611 printf("\nerror");
612 }
613  
614 }
615  
616  
617 return $cmd;
618 }
619  
620  
621  
622 our $type = {};
623 our $action = {};
624 my $term = new Term::ShellUI(commands =>getCommands (),history_file => '~/.active-memory');
625  
626 `clear`;
627 printf("\n"x30);
628 usage($cache);
629 $term->prompt('.'x$column."\n");
630 $term->run();
631  
632 1;
633 __DATA__