Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.03 KB | None | 0 0
  1. use strict;
  2. use Data::Dumper;
  3.  
  4. my $depends={};
  5. my $explicit={};
  6. my $installed={};
  7.  
  8. sub needed(){
  9.     my %res;
  10.    
  11.     my @items=keys %$explicit;
  12.     my $name;
  13.     while($name = shift @items){
  14.         for(@{ $depends->{$name} }){
  15.             push @items,$_ unless $res{$_};
  16.             $res{$_}=1;
  17.         }
  18.     }
  19.    
  20.     %res
  21. }
  22.  
  23. sub install($$){
  24.     my($name,$is_explicit)=@_;
  25.    
  26.     if($installed->{$name}){
  27.         print "    $name is already installed.\n" if $is_explicit;
  28.         return;
  29.     }
  30.    
  31.     for(@{ $depends->{$name} }){
  32.         install($_,0);
  33.     }
  34.    
  35.     print "    Installing $name\n";
  36.     $installed->{$name}=1;
  37.     $explicit->{$name}=1 if $is_explicit;
  38. }
  39. sub remove($$){
  40.     my($name,$is_explicit)=@_;
  41.    
  42.     if(! $installed->{$name}){
  43.         print "    $name is not installed.\n" if $is_explicit;
  44.         return;
  45.     }
  46.    
  47.     if($explicit->{$name} and not $is_explicit){
  48.         return;
  49.     }
  50.    
  51.     my %needed=needed;
  52.     if($needed{$name}){
  53.         print "    $name is still needed.\n" if $is_explicit;
  54.         return;
  55.     }
  56.    
  57.     print "    Removing $name\n";
  58.     delete $installed->{$name};
  59.     delete $explicit->{$name};
  60.    
  61.     for(@{ $depends->{$name} }){
  62.         remove($_,0);
  63.     }
  64. }
  65.  
  66. while(<DATA>){
  67.     print; chomp;
  68.  
  69.     /^DEPEND ([^ ]+) (.*)$/ and do{
  70.         my($n,$deps)=($1,$2);
  71.         $depends->{$n}||=[];
  72.         push @{$depends->{$n}},$_ foreach split / /,$deps;
  73.     };
  74.    
  75.     /^INSTALL ([^ ]+)$/ and do{
  76.         install($1,1);
  77.     };
  78.    
  79.     /^REMOVE ([^ ]+)$/ and do{
  80.         remove($1,1);
  81.     };
  82.    
  83.     /^LIST$/ and do{
  84.         for(sort keys %$installed){
  85.             print "    $_\n";
  86.         }
  87.     };
  88. }
  89.  
  90. #print Dumper $depends;
  91.  
  92. __DATA__
  93. DEPEND TCPIP NETCARD
  94. DEPEND TELNET TCPIP SOCKET
  95. DEPEND DNS TCPIP
  96. DEPEND HTML REGEX XML
  97. DEPEND REGEX PARSING
  98. DEPEND BROWSER DNS TCPIP HTML CSS
  99. INSTALL TCPIP
  100. REMOVE NETCARD
  101. REMOVE TCPIP
  102. REMOVE NETCARD
  103. INSTALL TCPIP
  104. LIST
  105. INSTALL TCPIP
  106. INSTALL foo
  107. REMOVE TCPIP
  108. INSTALL NETCARD
  109. INSTALL TCPIP
  110. REMOVE TCPIP
  111. LIST
  112. INSTALL TCPIP
  113. INSTALL NETCARD
  114. REMOVE TCPIP
  115. LIST
  116. REMOVE NETCARD
  117. INSTALL BROWSER
  118. LIST
  119. REMOVE BROWSER
  120. LIST
  121. INSTALL HTML
  122. INSTALL TELNET
  123. REMOVE SOCKET
  124. INSTALL DNS
  125. INSTALL BROWSER
  126. REMOVE NETCARD
  127. LIST
  128. REMOVE BROWSER
  129. LIST
  130. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement