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