FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
trac.php
1 #!/usr/bin/php
2 <?php
3 
4 /***********************************************************
5  Copyright (C) 2008 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  ***********************************************************/
20 
45 require_once ('Classes/GetFreshmeatRdf.php');
46 require_once ('Classes/FreshmeatRdfs.php');
47 require_once ('Classes/ReadInputFile.php');
48 
49 /*
50  * format for input file is 1 project per line, # for comments, blank
51  * lines OK. (make a class to do this!)
52  */
53 // open/ & get a line from input file
54 // open fm rdf and get that data
55 // find each input in the rdf file (or not)
56 
57 /*
58  * record the results in a file (space seperated)
59  * 1. the package name
60  * 2. the url
61  * 3. the version
62  * 4. description
63  * 5. ??
64  */
65 
66 $usage = "trac [-h] -i input-file [-o path-to-output] -r path-to-rdf-file\n";
67 $options = getopt("hi:o:r:");
68 //print_r($options);
69 if (empty ($options))
70 {
71  echo $usage;
72  exit (1);
73 }
74 
75 if (array_key_exists("h", $options))
76 {
77  echo $usage;
78  exit (0);
79 }
80 
81 if (array_key_exists("i", $options))
82 {
83  $in_file = $options['i'];
84 } else
85 {
86  print "ERROR, -i is a required parameter\n";
87  exit (1);
88 }
89 
90 if (array_key_exists("o", $options))
91 {
92  $in_file = $options['o'];
93 } else
94 {
95  // default
96  $out_file = 'STDOUT';
97 }
98 
99 if (array_key_exists("r", $options))
100 {
101  $rdf_file = $options['r'];
102 } else
103 {
104  print "ERROR, -r is a required parameter\n";
105  exit (1);
106 }
107 
108 // Should still check $in_file and $rdf_file
109 
110 $INF = new ReadInputFile($in_file);
111 
112 $gRdf = new GetFreshMeatRdf('');
113 
114 $gRdf->get_rdf($gRdf->rdf_name);
115 if ($gRdf->error_code != 0)
116 {
117  print "ERROR getting the Freshmeat RDF file\n";
118  print "ERROR code was:$gRdf->error_code\n";
119  print "command output was:";
120  print_r($gRdf->error_out);
121 }
122 
123 $FRdf = new FreshMeatRdfs($gRdf->rdf_name);
124 
125 if (!$FRdf->Uncompress($gRdf->rdf_name))
126 {
127  print "Could not uncompress the file $gRdf->rdf_name\n";
128  print "return code from uncompress:$FRdf->error_code\n";
129  print "Output from uncompress:$FRdf->error_out\n";
130 }
131 
132 $FMprojects = $FRdf->XtractProjInfo($FRdf->uncompressed_file);
133 
134 //print "We got the following from the rdf\n";
135 //var_dump($FMprojects);
136 
137 print "starting read and search\n";
138 while ($line = $INF->GetLine($INF->file_resource))
139 {
140  // Convert to lower case, as FM does not capitalize....
141 
142  $lc_proj = strtolower($line);
143  //print "DB-TRAC: Looking for $lc_proj\n";
144  $found_it = $FRdf->FindInProjInfo($lc_proj, $FMprojects);
145  //print "DB: TRAC: found_it is:\n";
146  //var_dump($found_it);
147  if (!is_null($found_it))
148  {
149  //print "Found a match in Freshmeat: $found_it\n";
150  $found["$lc_proj"] = $found_it;
151  }
152 }
153 //print "DB: TRAC: After while: found in FM is:\n";
154 //var_dump($found);
155 
156 /*
157  * at this point, need to determine if there is something to get, then
158  * write the results to a file (even if there is nothing to get)
159  *
160  * Additionally, some of the url's may point to something that doesn't
161  * really download. Need to explore cURL.
162  */
163 print "DB: Looking for valid download urls\n";
164 $projects = get_proj_url($found);
165 
166 $PF = fopen('ol-projects-in-FM', 'w') or die("Can't open file, $php_errormsg\n");
167 foreach($projects as $line)
168 {
169 // print "line is:$line\n";
170  if(fputcsv($PF, $line) === false)
171  {
172  print "ERROR: can't write $line\n";
173  }
174 }
175 fclose($PF);
176 // for now just open the file and try to parse
177 
178 $PL = fopen('ol-projects-in-FM', 'r') or die("Can't open file, $php_errormsg\n");
179 while ($tokens = fgetcsv($PL, 1024))
180 {
181  print "tokens is:\n";
182  var_dump($tokens);
183 }
184 
185 
198 function get_proj_url($pdata)
199 {
200  $url = NULL;
201  foreach ($pdata as $proj_name=>$aindex)
202  {
203  foreach ($aindex as $value)
204  {
205 
206  if (empty ($value))
207  {
208  continue;
209  }
210  /* test in this order, our preference is bz2, tgz then zip */
211  else
212  {
213  //print "inner loop, else: proj_name is:$proj_name\n";
214  //print "inner loop, else: value is:\n$value\n";
215  if(preg_match('/[0-9.]/', $value))
216  {
217  //print "*****DB:GPU: setting version*****\n";
218  $version = $value;
219  }
220  /* have we already pick a url? if so, skip */
221  /*if(!is_null($url))
222  {
223  print "DB:GPU: URL is NULL, Skipping to next entry\n";
224  continue;
225  }
226  */
227  if (preg_match('/\/url_zip\/$/', $value))
228  {
229  //print "DB:GPU: matched zip, value is:$value\n";
230  $url = $value;
231  }
232  elseif (preg_match('/\/url_tgz\/$/', $value))
233  {
234  //print "DB:GPU: matched tgz, value is:$value\n";
235  $url = $value;
236  }
237  elseif (preg_match('/\/url_bz2\/$/', $value))
238  {
239  //print "DB:GPU: matched bz2, value is:$value\n";
240  $url = $value;
241  }
242  else
243  {
244  print "DB:GPU: Testing URL for NULL\n";
245  if(is_null($url))
246  {
247  print "DB:GPU: Setting URL is NULL\n";
248  $url = 'NO URL FOR THIS PROJECT';
249  }
250  }
251  }
252  }
253 
254  //print "DB: GPU: pname url, version are:\n$proj_name\n$url\n$version\n";
255  $proj_data[0] = $proj_name;
256  $proj_data[1] = $url;
257  $proj_data[2] = $version;
258  $projects[] = $proj_data;
259  $url = NULL;
260  }
261  //print "DB: projURL: the projects are:\n";
262  //var_dump($projects);
263  return ($projects);
264 }
265 ?>