Advertisement
Guest User

unpack2.pl

a guest
Apr 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.63 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use File::Basename;
  4. use Cwd 'abs_path';
  5. use lib dirname($0).'/oebin';
  6. use Getopt::Long;
  7. use Pod::Usage;
  8. use File::Basename;
  9.  
  10. my ($uninstall, $help) = ();
  11. GetOptions (
  12.     "uninstall"  => \$uninstall
  13. );
  14.  
  15. my $user=uc($ENV{'LOGNAME'}?$ENV{'LOGNAME'}:uc(`id -un`));
  16. chomp $user;
  17. my $BYTES_PER_CYLINDER=696000;
  18.  
  19. my $rplRoot="___REPLACE_ROOT___";
  20. my $accountNumber=($ENV{'COMPILER_INSTALL_ACCOUNT_NUMBER'}?$ENV{'COMPILER_INSTALL_ACCOUNT_NUMBER'}:'0');
  21. my $usrHLQ=($ENV{'COMPILER_INSTALL_HLQ'}?$ENV{'COMPILER_INSTALL_HLQ'}:$user);
  22. my $msgCLS=($ENV{'COMPILER_INSTALL_MSGCLASS'}?$ENV{'COMPILER_INSTALL_MSGCLASS'}:'S');
  23. my $jclVol=($ENV{'COMPILER_INSTALL_VOLSER'}?" VOLUME($ENV{'COMPILER_INSTALL_VOLSER'})":());
  24. my $rplHLQ="___REPLACE_HLQ___";
  25. my $sqfHLQ="USRHLQ";
  26. my $instroot=abs_path(dirname($0));
  27. my $ussRoot="$instroot/uss.images";
  28. my $tgtRoot="$instroot/bin";
  29.  
  30. my @files=();
  31. my @listOfDatasets=();
  32.  
  33. if (&isUninstall) {
  34.   die "<ERR> failed to uninstall because target path $tgtRoot does not exist, please make sure to run ".
  35.     "this script under the same directory where unpack.pl was previously run during installation"
  36.     if (!-d $tgtRoot);
  37. } else {
  38.   (system("mkdir -p $tgtRoot && cp -rmf $ussRoot/* $tgtRoot/") == 0)||die "copy failed";
  39. }
  40.  
  41. open (FLIST,"find $tgtRoot -type f 2>/dev/null|")||die "failed to find files under $srcRoot";
  42. while (<FLIST>) {
  43.   my $file=$_;
  44.   chomp $file;
  45.   if ($file =~ m/\/xmits\/$sqfHLQ.*.XMIT$/) {
  46.     push @listOfDatasets,$file;
  47.   } else {
  48.     push @files,$file;
  49.   }
  50. }
  51. close FLIST;
  52.  
  53. foreach my $ds (@listOfDatasets) {
  54.   if (&isUninstall) {
  55.     print "<DEB> deleting ".&mapDatasetName($ds)."\n";
  56.     &deleteDS(&mapDatasetName($ds)) || print "<DEB> cannot delete ".&mapDatasetName($ds)." -- has it already been deleted?\n";
  57.   } else {
  58.     print "<DEB> unloading $ds into ".&mapDatasetName($ds)."\n";
  59.     &receiveDSfromFile($ds, &mapDatasetName($ds)) || die "<ERR> cannot receive $ds to ". &mapDatasetName($ds);
  60.   }
  61. }
  62.  
  63. if (&isUninstall) {
  64.   opendir my $ussbindir, "$ussRoot/" or die "<ERR> cannot open uss.images dir $ussRoot:$!";
  65.   my @ussbinfiles = grep { /[^\.]/ } readdir ($ussbindir);
  66.   die "<ERR> $ussbindir is empty" if (scalar(@ussbinfiles)<1);
  67.   my $filesToDelete = join(' ',@ussbinfiles);
  68.   closedir $ussbindir;
  69.   (system("cd $tgtRoot/ && rm -rf $filesToDelete") == 0)||die "delete failed:$!";
  70.   print "<INFO> uninstallation complete!\n";
  71. } else {
  72.   foreach my $f (@files) {
  73.     print "<DEB> processing file $f\n";
  74.     &doReplaceDataset($f) || die "cannot parse $f to replace dataset";
  75.     &doReplaceRoot($f) || die "cannot parse $f to replace HFS root";
  76.   }
  77.  
  78.   &runIVP() || die "fail to verify installation";
  79.   print "<INFO> installation complete!\n";
  80. }
  81.  
  82. sub submit_jcl {
  83.     open (FJCL,">.tempjcl")||die "failed to open .tempjcl";
  84.     my (@list) = @_;
  85.     foreach my $line ( @list ) {
  86.         print FJCL "$line\n";
  87.     }
  88.     close FJCL;
  89.     my $cmd = "submit .tempjcl";
  90.     return (system ($cmd) == 0);
  91. }
  92.  
  93. sub runIVP {
  94.   my $temphello="._temp.hello.cpp";
  95.   open (FHELLO,">$temphello")||die "failed to oepn $temphello";
  96.   print FHELLO "#include <iostream>\n";
  97.   print FHELLO "using namespace std;\n";
  98.   print FHELLO "int main () {\n";
  99.   print FHELLO "  cout << \"Hello C++ World!\" << endl;\n";
  100.   print FHELLO "  return 0;\n";
  101.   print FHELLO "}\n";
  102.   close FHELLO;
  103.   my $cmd="$tgtRoot/njsc++ $temphello -o hello -qphaseid && $tgtRoot/run ./hello && rm -f $temphello hello";
  104.   print "<INFO> running : $cmd!\n";
  105.   return(system("$cmd")==0);
  106. }
  107.  
  108. sub doReplaceDataset {
  109.  my ($file) = @_;
  110.  my $ofile = "${file}._temp_out";
  111.  open (FIN,"<$file")||die "failed to open $file to read";
  112.  open (FOUT,">$ofile")||die "failed to open ofile to write";
  113.  while (<FIN>) {
  114.   my $line=$_;
  115.   $line=~s/$rplHLQ/$usrHLQ/ig;
  116.   print FOUT $line;
  117.  }
  118.  close FIN;
  119.  close FOUT;
  120.  (system("mv $ofile $file && chmod 755 $file")==0)||die " failed to rename $ofile to $file";
  121.  return 1;
  122. }
  123.      
  124. sub doReplaceRoot {
  125.   my ($file) = @_;
  126.   my $ofile = "${file}._temp_out";
  127.   open (FIN,"<$file")||die "failed to open $file to read";
  128.   open (FOUT,">$ofile")||die "failed to open ofile to write";
  129.   while (<FIN>) {
  130.     my $line=$_;
  131.     if ($line=~m/$rplRoot/) {
  132.       $line=~s/$rplRoot/$tgtRoot/g;
  133.     }
  134.     print FOUT $line;
  135.   }
  136.   close FIN;
  137.   close FOUT;
  138.   (system("mv $ofile $file && chmod 755 $file")==0)||die " failed to rename $ofile to $file";
  139.   return 1;
  140. }
  141.  
  142. sub mapDatasetName {
  143.   my ($dataset) = @_;
  144.   if ($dataset =~ m/.*\/$sqfHLQ\.([^\/]*)\.XMIT$/g) {
  145.     $dataset = "$usrHLQ.$1";
  146.   }
  147.   return $dataset;
  148. }
  149.  
  150. sub receiveDSfromFile {
  151.   my ($file, $dataset) = @_;
  152.  
  153.   my $tempDS = getTempDSName();
  154.  
  155.   if (! copyFileToSequentialDS($file, $tempDS)) {
  156.     warn "An error occurred while copying file $file to dataset $tempDS. -- $!";
  157.     return 0;
  158.   }
  159.   if (! receiveDSfromDS($tempDS, $dataset)) {
  160.     warn "An error occurred while receiving temporary dataset $tempDS to dataset $dataset.";
  161.     return 0;
  162.   }
  163.   if (! deleteDS($tempDS)) {
  164.     warn "An error occurred when deleting $tempDS.";
  165.     return 0;
  166.   }
  167.    
  168.   return 1;
  169. }
  170.  
  171. sub receiveDSfromDS {
  172.   my ($in_dataset, $target_dataset) = @_;
  173.   $in_dataset=uc($in_dataset);
  174.   $target_dataset=uc($target_dataset);
  175.   my @jcl;
  176.   my $hostname=uc(`hostname -s`);
  177.   chomp($hostname);
  178.    
  179.   return 0 if (!$in_dataset || !$target_dataset || !$user || !$hostname);
  180.    
  181.   my @receive_jcl = (
  182.     "//${user}A JOB ${accountNumber},'RECEIVE',MSGLEVEL=(1,1),MSGCLASS=$msgCLS,",
  183.     "//  CLASS=A,REGION=0M,USER=$user",
  184.     "//REC      EXEC PGM=IKJEFT01,DYNAMNBR=3000",
  185.     "//SYSTSPRT DD SYSOUT=*",
  186.     "//SYSPRINT DD SYSOUT=*",
  187.     "//SYSTSIN  DD DATA,DLM='!!'",
  188.     " RECEIVE +",
  189.     " INDATASET('$in_dataset')",
  190.     " DATASET('$target_dataset')".($jclVol?' +':''),
  191.         ($jclVol?$jclVol:()),
  192.   );
  193.   push (@receive_jcl, "!!");
  194.   push (@receive_jcl, "//");
  195.    
  196.   submit_jcl(@receive_jcl);
  197.   return 1;
  198. }
  199.  
  200. sub copyFileToSequentialDS {
  201.     my ($file, $dataset) = @_;
  202.     return 0 if (!$dataset) && (!$file);
  203.  
  204.     my $num_cyls = getNumCylinders($file);
  205.  
  206.     #The space parameter doesn't seem to work properly and the documentation is incorrect.
  207.     #It seems that we always get 1 cylinder for the primary extent, so I have specified the
  208.     #required size twice, for primary and secondary allocation units.
  209.     my $copy_cmd = "cp -B -P \"recfm=fb, lrecl=80, blksize=3120, space=(cyl,($num_cyls,$num_cyls))\" $file \"//'$dataset'\"";
  210.     return (system ($copy_cmd) == 0);
  211. }
  212.  
  213. sub getNumCylinders {
  214.     my ($file) = @_;
  215.  
  216.     return 0 if (!$file);
  217.     my $size = -s $file;
  218.     #Determine the number of cylinders that we need for the DS (always round up by 1)
  219.     my $num_cyls = int($size / $BYTES_PER_CYLINDER) + 1;
  220. }
  221.  
  222. sub deleteDS {
  223.   my ($dataset) = @_;
  224.   $dataset=uc($dataset);
  225.  
  226.   my @delete_jcl = (
  227.     "//${user}1 JOB ${accountNumber},",
  228.     "//  'DELETE',",
  229.     "//  MSGLEVEL=(1,1),",
  230.     "//  MSGCLASS=$msgCLS,",
  231.     "//  CLASS=A,",
  232.     "//  USER=$user",
  233.     "//DELETE  EXEC PGM=IEFBR14",
  234.     "//ACLBSR1 DD   DISP=(MOD,DELETE,DELETE),",
  235.     "//  DSN=$dataset,",
  236.     "//  SPACE=(TRK,(1,1))",
  237.     "//",
  238.   );
  239.   submit_jcl(@delete_jcl);
  240.   return 1;
  241. }
  242.  
  243. sub getTempDSName {
  244.   my $time = time();
  245.   my @random_chars = ('A'..'Z');
  246.   #<LAST 7 DIGITS OF CURRENT TIME IN MS>.T<PID>
  247.   $random_string = "T" . substr($time, -7) . ".T" . substr($$, -7);
  248.   $random_string .= "." . $random_chars[int(rand(scalar(@random_chars)))];
  249.   $random_string .= int(rand(scalar(9999999)));
  250.  
  251.   my $tempDS = "$user.TMP.$random_string";
  252.  
  253.   return $tempDS;
  254. }
  255.  
  256. sub isUninstall {
  257.   return ($uninstall?1:0);
  258. }
  259.  
  260. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement