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