FOSSology  4.4.0
Open Source License Compliance by Open Source Software
clean-tars.php
1 #!/usr/bin/php
2 <?php
3 /*
4  get-fsrc.php
5  SPDX-FileCopyrightText: © 2007 Hewlett-Packard Development Company, L.P.
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
19 $usage = <<< USAGE
20 clean-tars.php [-h] -i <input-path>
21 
22 Clean cruft out of fedora packages. The following is cleaned:
23 make.out - the make prep output
24 .cvsignore - cvs file
25 compressed archives - all .tgz, .gz, bzip2, bz2, zip files
26 
27 Dead packages and packages with no .spec file are skipped.
28 
29 Where:
30 -h standard help, usage message.
31 -i input-path path where sources/packages will be cleaned
32 
33 USAGE;
34 
35 $options = getopt("hi:");
36 //print_r($options);
37 if (empty($options))
38 {
39  echo $usage;
40  exit(1);
41 }
42 if (array_key_exists("h",$options))
43 {
44  echo $usage;
45  exit(0);
46 }
47 if (array_key_exists("i",$options))
48 {
49  $in_path = $options['i'];
50  if (empty($in_path))
51  {
52  echo $usage;
53  exit(1);
54  }
55 }
56 
57 chdir($in_path) or die("Can't chdir to $in_path: $php_errormsg\n");
58 
59 $toss = exec('ls', $list, $rtn);
60 if($rtn != 0){
61  echo "ERROR, ls of $in_path did not return zero: $rtn\n";
62  exit(1);
63 }
64 
65 $date=`date`;
66 echo "Starting at: $date";
67 $whereami = `pwd`;
68 
69 foreach($list as $pkg)
70 {
71  echo "Package is:$pkg\n";
72  if(!(chdir($pkg))){
73  print "ERROR: Can't chdir to $pkg, skipping: $php_errormsg\n";
74  continue;
75  }
76  $plist = array();
77  $toss = exec('ls -a', $plist, $rtn);
78  if($rtn != 0)
79  {
80  echo "ERROR, ls of $pkg did not return zero: $rtn\n";
81  exit(1);
82  }
83  /*
84  * Dam, folks sure do make this part tricky.... need to have very
85  * robust patterns.
86  *
87  * Remove compressed archives, .cvsignore and make.out
88  * skip dead packages and packages with no .spec.
89  */
90 
91  foreach($plist as $file)
92  {
93  $match = array();
94  if (preg_match('/dead\.package/i', $file))
95  {
96  print "$pkg is a dead.package, skipping\n";
97  continue;
98  }
99  $alist = `ls`;
100  if (!(preg_match('/.*\.spec/i', $alist)))
101  {
102  echo "$pkg has no spec file, skipping\n";
103  continue;
104  }
105 
106  // the pattern for gz files is meant to match tgz and gz
107  if(preg_match('/.*?gz$/i', $file, $match))
108  {
109  echo "Executing set -o noclobber; rm -rf $match[0]\n";
110  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
111  if($rtn != 0){
112  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
113  }
114  }
115  if(preg_match('/.*?bz2$/i', $file, $match))
116  {
117  echo "Executing set -o noclobber; rm -rf $match[0]\n";
118  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
119  if($rtn != 0)
120  {
121  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
122  }
123  }
124  if(preg_match('/.*?Bzip2$/i', $file, $match))
125  {
126  echo "Executing set -o noclobber; rm -rf $match[0]\n";
127  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
128  if($rtn != 0){
129  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
130  }
131  }
132  if(preg_match('/.*?zip$/i', $file, $match))
133  {
134  echo "Executing set -o noclobber; rm -rf $match[0]\n";
135  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
136  if($rtn != 0){
137  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
138  }
139  }
140  if(preg_match('/\.cvsignore$/i', $file, $match))
141  {
142  echo "Executing set -o noclobber; rm -rf $match[0]\n";
143  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
144  if($rtn != 0){
145  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
146  }
147  }
148  if(preg_match('/make\.out$/i', $file, $match))
149  {
150  echo "Executing set -o noclobber; rm -rf $match[0]\n";
151  $toss = system("set -o noclobber; rm -rf $match[0]", $rtn);
152  if($rtn != 0){
153  echo "ERROR, remove of {$match[0]} did not return zero: $rtn\n";
154  }
155  }
156  } // foreach($plist...
157  chdir('..') or die("Can't chdir to ..: $php_errormsg\n");
158 } // foreach($list as ...
159 
160 $date=`date`;
161 print "Ending at: $date";
FUNCTION void usage(char *name)
Definition: usage.c:18