FOSSology  4.4.0
Open Source License Compliance by Open Source Software
createTestFiles.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
21 global $commentChar;
22 global $endChar;
23 
24  /*
25  * General flow:
26  * - use random suffix if none passed in.
27  * - create the default name, if none passed in
28  * - Determine comment style
29  * - pick a license randomly
30  * - construct all needed file parts (choosing comment style before)
31  * - write the file.
32  *
33  */
34 
35 /*
36  * usage: createTestFiles($name, $suffix, $lic?)
37  * if no name, output is to stdout
38  * if no suffix, one is picked
39  * if name, used as a prefix so multiple names can be generated
40  */
41 
42 function createPHP($FD, $license)
43 {
44  global $commentChar;
45  global $endChar;
46 
47  $newLine = "\n";
48 
49  $startTag = "#!/usr/bin/php\n<?php\n";
50  $endTag = "\n?>\n";
51 
52  $sayHey = "echo \"Hello World!$newLine\";\n";
53 
54  if(empty($license))
55  {
56  $license = _("Copyright Randy Rando, licensed under the BSD license\n");
57  }
58  $cLic = $commentChar . "\n" . $license . "\n" . $endChar . "\n";
59  $body = $startTag . $cLic . $sayHey . $endTag;
60  //echo "Body is:\n$body\n";
61  $howMany = fwrite($FD,$body);
62  fclose($FD);
63 }
64 
65 function createPerl($FD, $license)
66 {
67  global $commentChar;
68  $startTag = "#!/usr/bin/perl\n";
69 
70  $sayHey = "print \"Hello World!$newLine\";\n";
71 
72  if(empty($license))
73  {
74  $license = _("Copyright Randy Rando, licensed under the BSD license\n");
75  }
76  $cLic = $commentChar . "\n" . $license . "\n" . $endChar . "\n";
77  $body = $startTag . $cLic . $sayHey . $endTag;
78  //echo "Body is:\n$body\n";
79  $howMany = fwrite($FD,$body);
80  fclose($FD);
81  echo "Perl files not implimented\n";
82 }
83 
84 function createCprog($FD, $license)
85 {
86  global $commentChar;
87  echo "C files not implimented\n";
88 }
89 
90 function createHeader($FD, $license)
91 {
92  global $commentChar;
93  echo "header files not implimented\n";
94 }
95 
96 function createSh($FD, $license)
97 {
98  global $commentChar;
99  echo "shell files not implimented\n";
100 }
101 function createTxt($FD, $license)
102 {
103  global $commentChar;
104  echo "Text files not implimented\n";
105 }
106 function createJs($FD, $license)
107 {
108  global $commentChar;
109  echo "Javascript files not implimented\n";
110 }
111 
112 function createFile($suffix=NULL, $name=NULL, $license=NULL)
113 {
114  require_once 'licenseText.php';
115  global $commentChar;
116  global $endChar;
117 
118  echo "after require\n";
119 
120  $licenses = array(
121  $gpl2Text,
122  $gpl3Text,
123  $bsd_d,
124  $apache2,
125  );
126 
127  $suffix = array(
128  '.c',
129  '.h',
130  '.php',
131  '.pl',
132  '.sh',
133  '.txt',
134  '.js',
135  );
136 
137  $defaultName = 'TestFile';
138 
139  $sufixNum = rand(0,count($suffix)-1);
140  $licensePick = rand(0,count($licenses)-1);
141 
142  /*
143  * General flow:
144  * - use random suffix if none passed in.
145  * - create the default name, if none passed in
146  * - Determine comment style
147  * - pick a license randomly
148  * - construct all needed file parts (choosing comment style before)
149  * - write the file.
150  *
151  */
152 
153  // first imp: just use defaults for now.
154 
155  $name = $defaultName . $suffix[$sufixNum];
156  echo "***** Getting license *****\n";
157  $license = $licenses[$licensePick];
158 
159  // create the file
160  echo "name is:$name\n";
161  $FD = fopen($name,'w');
162 
163  $commentChar = NULL;
164  $endChar = NULL;
165  $newLine = "\n";
166 
167  switch ($suffix[$sufixNum])
168  {
169  case '.c':
170  $commentChar = '/*';
171  $endChar = '*/';
172  $rtn = createCprog($FD, $license);
173  break;
174  case '.h':
175  $commentChar = '/*';
176  $endChar = '*/';
177  $rtn = createHeader($FD, $license);
178  break;
179  case '.php':
180  $commentChar = '/*';
181  $endChar = '*/';
182  $rtn = createPHP($FD, $license);
183  break;
184  case '.js':
185  $commentChar = '/*';
186  $endChar = '*/';
187  break;
188  case '.pl':
189  $commentChar = '#';
190  $rtn = createPerl($FD, $license);
191  break;
192  case '.js':
193  $commentChar = '/*';
194  $endChar = '*/';
195  $rtn = createJs($FD, $license);
196  break;
197  case '.sh':
198  $commentChar = '#';
199  $rtn = createSh($FD, $license);
200  break;
201  case '.txt':
202  $commentChar = '#';
203  $rtn = createTxt($FD, $license);
204  break;
205 
206  default:
207  $commentChar = NULL; // should never happen
208  break;
209  }
210 }
211 
212 echo "*********** starting.... *************\n";
213 createFile();