FOSSology  4.4.0
Open Source License Compliance by Open Source Software
diffm.php
1 #!/usr/bin/php
2 <?php
3 /*
4  diffm.php
5  SPDX-FileCopyrightText: © 2007 Hewlett-Packard Development Company, L.P.
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
42 /*
43 
44 /*
45 * This program works by extracting the project short name and the latest
46 * release version from an rdf and storing them in arrays. The arrays are
47 * sorted and then diff'ed. Any differences in the latest release version
48 * cause that project to be added to the list of projects that need updating.
49 * The associated xml entries with that project are also added to the xml
50 * file that will be used by get-projects to retrieve them from the net.
51 *
52 */
53 require_once("FIXMETOBERELATIVE/pathinclude.php");
54 require_once("$LIBDIR/lib_projxml.h.php");
55 
56 $usage = <<< USAGE
57 Usage: diffm [-h] -f <file1> <file2> [-o <dir-path>]
58  Where <file1> path to an uncompressed top1000 Freshmeat rdf XML file
59  <file2> path to an uncompressed top1000 Freshmeat rdf XML file
60 
61  For the differences to be found as expected file1 should be the newer
62  file. E.g. f1.2008-1-14 f2.2008-1-13.
63 
64  <dir-path> fully qualified path where output files will be placed.
65  If no -o option given, the cwd is used for the output files.
66 
67  Output files are: FM-projects2update and Update.fm.rdf
68 
69  See mktop1k to create a top1000 Freshmeat file from the master rdf file.
70 
71 USAGE;
72 
73 for ($i = 1; $i < $argc; $i++) {
74  switch ($argv[$i]) {
75  case '-f':
76  $i++;
77  $dash_f = true;
78  if ((isset($argv[$i])) and (isset($argv[$i+1]))){
79  $in_file1 = $argv[$i];
80  $i++;
81  $in_file2 = $argv[$i];
82  //pdbg("DIFFM: input files are 1->$in_file1\n2->$in_file2");
83  }
84  else {
85  echo("ERROR: Must specify 2 uncompressed filenames after -f");
86  echo $usage;
87  exit(1);
88  }
89  // is the second argument start with a dash? if so, wasn't given
90  // the correct args.
91  if(eregi('^-+', $in_file2)) {
92  echo("ERROR: Must specify 2 uncompressed filenames after -f");
93  echo $usage;
94  exit(1);
95  }
96  break;
97  case '-o':
98  $i++;
99  if (isset($argv[$i])) {
100  $dir_path = $argv[$i];
101  }
102  else {
103  die("ERROR: Must specify fully qualified directory path after -o");
104  }
105  break;
106  case '-h':
107  echo $usage;
108  exit(0);
109  break;
110  default:
111  die("ERROR: Unknown argument: $argv[$i]\n$usage");
112  break;
113  }
114 }
115 
116 // -f is a required parameter
117 if(!$dash_f){
118  echo "ERROR: -f is a required parameter\n";
119  echo $usage;
120  exit(-1);
121 }
122 // Test for existence then size....
123 if (false == file_exists($in_file1)) {
124  echo "Error: $in_file1 does not exist\n";
125  echo $usage;
126  exit(-1);
127 }
128 if (false == file_exists($in_file2)) {
129  echo "Error: $in_file2 does not exist\n";
130  echo $usage;
131  exit(-1);
132 }
133 
134 if (0 == $size = filesize($in_file1)){
135  echo "Error, file $in_file1 is empty\n";
136  exit(1);
137 }
138 if (0 == $size = filesize($in_file2)){
139  echo "Error, file $in_file2 is empty\n";
140  exit(-1);
141 }
142 // open the files and get a project_name and version from each.
143 
144 $F1 = fopen($in_file1, 'r') or die("Can't open: $in_file1 $php_errormsg\n");
145 $F2 = fopen($in_file2, 'r') or die("Can't open: $in_file2 $php_errormsg\n");
146 
147 echo "Comparing the following files:\n$in_file1\n$in_file2\n\n";
148 
149 // files are opened outside of the loop, wastful, if nothing to save, but
150 // this way, won't end up opening them multiple times in the loop.
151 $dstamp = date('Y-m-d');
152 $projs2update = $dir_path . 'FM-projects2update.' . $dstamp;
153 $xml_changes = $dir_path . 'Update.fm.rdf.' . $dstamp;
154 //pdbg("output file names are: $projs2update $xml_changes");
155 $P2up = fopen($projs2update, 'w') or die("Can't open: $php_errormsg\n");
156 $Cxml = fopen($xml_changes, 'w') or die("Can't open: $php_errormsg\n");
157 
158 $diffs_found = 0;
159 
160 /*
161  * diff the lists, size of array is number of diffs found.
162  * foreach item in the diff list (array), look for that project in the xml file
163  * and get the xml for that project, write it to a file.
164  */
165 
166 $list1 = mklists($in_file1);
167 $list2 = mklists($in_file2);
168 
169 ksort($list1);
170 ksort($list2);
171 
172 $adiffs = array_diff_assoc($list1, $list2);
173 //pdbg("diffs are:",$adiffs);
174 
175 // If no diffs, exit 0, nothing to do, remove files and stop
176 $diffs_found = count($adiffs);
177 if ($diffs_found == 0){
178  echo "NOTE: No differences were found. \n";
179  $junk = exec("rm -f $projs2update $xml_changes", $dummy, $rtn);
180  if($rtn != 0){
181  echo "cound not remove files $projs2update\nand\n$xml_changes\n";
182  echo "Please remove them manually";
183  }
184  exit($diffs_found); // should be 0
185 }
186 // differences found, save them up in the files. We only save from
187 // file 1 as that is the newest file.
188 write_hdr($Cxml);
189 while( false != ($f1_line = fgets($F1, 1024))) {
190 
191  $proj1 = array();
192  if (preg_match('/<project>/', $f1_line)) {
193  $m1 = ftell($F1);
194  $proj1 = get_entry($F1, $m1);
195  //pdbg("DIFFM: P1 Entries:",$proj1);
196  }
197  else {
198  continue;
199  }
200  // we now have a project, is it one of the ones that need updating?
201  // If so, save it, and record the project name and version.
202  $proj_name = xtract($proj1[4]);
203  foreach($adiffs as $name => $version){
204  if ($proj_name == $name){
205  //pdbg("Found $name, saving");
206  $Yupdate = "$name " . "has a new version:" . " $version\n";
207  save_Yupdated($P2up, $Yupdate);
208  //pdbg("\$proj1 is:", $proj1);
209  write_pxml($Cxml, $proj1);
210  break;
211  }
212  }
213 }
214 // must write the closing tag before closing the xml file
215 close_tag($Cxml);
216 fclose($P2up);
217 fclose($Cxml);
218 
219 if($diffs_found > 0){
220  echo
221  "$diffs_found differences were found. Please consult the following files:\n";
222  echo "$projs2update and $xml_changes\n";
223  exit($diffs_found);
224 }
225 
236  function mklists($xml_file){
237  // Make an array out of the passed in xml file.
238  // returns array.
239  $pdoc1= simplexml_load_file("$xml_file");
240 
241  $projects = $pdoc1->xpath('/project-listing/project');
242 
243  foreach($projects as $proj){
244  list($pname_short) = $proj->xpath('projectname_short');
245  // echo "\$pname_short:$pname_short\n";
246  list($lrv) = $proj->xpath('latest_release/latest_release_version');
247  $p["$pname_short"] = "$lrv";
248  }
249  return($p);
250  }
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
if(!preg_match("/\s$projectGroup\s/", $groups) &&(posix_getgid() !=$gInfo[ 'gid']))
get monk license list of one specified uploadtree_id
Definition: migratetest.php:33
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308