33 'licenses' => array(
'licenseId',
'licenseText',
'name'),
34 'exceptions' => array(
'licenseExceptionId',
'licenseExceptionText',
'name')
38 function startProcessingLicenseData()
44 $newLicenseRefData = array();
45 $usage =
"Usage: " . basename($argv[0]) .
" [options] 47 Create new licenseref.json file. Options are: 48 -E Update all existing licenses and also add new licenses. 49 (NOTE: there may be failure of test cases) 51 -e Only update existing licenses. 52 (NOTE: there may be failure of test cases) 54 -n Only add new licenses. 56 --type Usually licenses/exceptions (optional) 57 (ex: --type 'licenses') 59 --url From where you want to download (optional) 60 (ex: --url 'https://spdx.org/licenses/licenses.json') 63 (if --type and --url is empty then the script will automatically download the from below) 64 For type 'licenses' URL is : $scanList[licenses] 66 For type 'exceptions' URL is : $scanList[exceptions]";
68 'licenses' =>
'https://spdx.org/licenses/licenses.json',
69 'exceptions' =>
'https://spdx.org/licenses/exceptions.json' 72 $options = getopt(
"hcEen", array(
"type:",
"url:"));
74 $type = array_key_exists(
"type", $options) ? $options[
"type"] :
'';
75 $URL = array_key_exists(
"url", $options) ? $options[
"url"] :
'';
76 foreach ($options as $option => $optVal) {
81 $updateWithNew = $option;
84 $updateExisting = $option;
87 $addNewLicense = $option;
100 if (!empty($updateWithNew) || !empty($updateExisting) || !empty($addNewLicense)) {
101 if (!empty($type) && !empty($URL)) {
102 $newLicenseRefData = $this->
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $newLicenseRefData);
103 }
else if (!empty($type) && empty($URL)) {
104 echo
"Notice: --url cannot be empty if --type is provided \n";
105 }
else if (empty($type) && !empty($URL)) {
106 echo
"Notice: --type cannot be empty if --url is provided \n";
108 foreach ($scanList as $type => $URL) {
109 $newLicenseRefData = $this->
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $newLicenseRefData);
112 $newFileName =
"licenseRefNew.json";
113 if (file_exists($newFileName)) {
114 unlink($newFileName);
116 $file = fopen($newFileName,
'w+');
117 file_put_contents($newFileName, json_encode($newLicenseRefData, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES));
119 echo
"\n\n INFO: new $newFileName file created \n\n";
121 echo
"\nINVALID OPTION PROVIDED\n\n";
135 if (strpos($RFShortName,
"-only") !==
false) {
136 return strstr($RFShortName,
"-only",
true);
137 }
else if (strpos($RFShortName,
"-or-later") !==
false) {
138 $licenseShortname = strstr($RFShortName,
"-or-later",
true);
139 return $licenseShortname.
"+";
155 function getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData)
159 if (!is_dir($LIBEXECDIR)) {
160 print
"FATAL: Directory '$LIBEXECDIR' does not exist.\n";
164 $dir = opendir($LIBEXECDIR);
166 print
"FATAL: Unable to access '$LIBEXECDIR'.\n";
170 $fileName =
"$LIBEXECDIR/licenseRef.json";
171 if (!file_exists($fileName)) {
172 print
"FATAL: File '$fileName' does not exist.\n";
176 if (empty($existingLicenseRefData)) {
177 echo
"INFO: get existing licenseRef.json from $LIBEXECDIR\n";
178 $getExistingLicenseRefData = file_get_contents(
"$fileName");
180 $existingLicenseRefData = (array) json_decode($getExistingLicenseRefData,
true);
183 $maxkey = array_search(
max($existingLicenseRefData), $existingLicenseRefData);
184 $newRfPk = $existingLicenseRefData[$maxkey][
'rf_pk'] + 1;
186 $getList = json_decode(file_get_contents($URL));
187 foreach ($getList->$type as $listValue) {
189 $getCurrentData = file_get_contents($listValue->detailsUrl);
190 $getCurrentData = (array) json_decode($getCurrentData,
true);
191 echo
"INFO: search for license ".$getCurrentData[$this->mapArrayData[$type][0]].
"\n";
193 $licenseIdCheck = array_search($getCurrentData[$this->mapArrayData[$type][0]], array_column($existingLicenseRefData,
'rf_shortname'));
194 $currentMD5 = md5($getCurrentData[$this->mapArrayData[$type][1]]);
195 $MD5Check = array_search($currentMD5, array_column($existingLicenseRefData,
'rf_md5'));
196 if (!is_numeric($licenseIdCheck)) {
198 $getCurrentData[$this->mapArrayData[$type][0]] = $this->
getLicenseNameWithOutSuffix($getCurrentData[$this->mapArrayData[$type][0]]);
199 $getCurrentData[$this->mapArrayData[$type][0]];
200 $licenseIdCheck = array_search($getCurrentData[$this->mapArrayData[$type][0]], array_column($existingLicenseRefData,
'rf_shortname'));
202 if (is_numeric($licenseIdCheck) &&
203 !is_numeric($MD5Check) &&
205 !empty($updateWithNew) ||
206 !empty($updateExisting)
209 $existingLicenseRefData[$licenseIdCheck][
'rf_fullname'] = $getCurrentData[$this->mapArrayData[$type][2]];
210 $existingLicenseRefData[$licenseIdCheck][
'rf_text'] = $getCurrentData[$this->mapArrayData[$type][1]];
211 $existingLicenseRefData[$licenseIdCheck][
'rf_url'] = $getCurrentData[
'seeAlso'][0];
212 $existingLicenseRefData[$licenseIdCheck][
'rf_md5'] = $currentMD5;
213 $existingLicenseRefData[$licenseIdCheck][
'rf_notes'] = (array_key_exists(
"licenseComments", $getCurrentData) ? $getCurrentData[
'licenseComments'] : $existingLicenseRefData[$licenseIdCheck][
'rf_notes']);
214 echo
"INFO: license ".$getCurrentData[$this->mapArrayData[$type][0]].
" updated\n\n";
216 if (!is_numeric($licenseIdCheck) &&
217 !is_numeric($MD5Check) &&
219 !empty($updateWithNew) ||
220 !empty($addNewLicense)
223 $existingLicenseRefData[] = array(
224 'rf_pk' =>
"$newRfPk",
225 'rf_shortname' => $getCurrentData[$this->mapArrayData[$type][0]],
226 'rf_text' => $getCurrentData[$this->mapArrayData[$type][1]],
227 'rf_url' => $getCurrentData[
'seeAlso'][0],
228 'rf_add_date' => null,
229 'rf_copyleft' => null,
230 'rf_OSIapproved' => null,
231 'rf_fullname' => $getCurrentData[$this->mapArrayData[$type][2]],
232 'rf_FSFfree' => null,
233 'rf_GPLv2compatible' => null,
234 'rf_GPLv3compatible' => null,
235 'rf_notes' => (array_key_exists(
"licenseComments", $getCurrentData) ? $getCurrentData[
'licenseComments'] : null),
239 'rf_text_updatable' =>
"f",
240 'rf_md5' => $currentMD5,
241 'rf_detector_type' => 1,
244 'rf_spdx_compatible' =>
"t",
247 echo
"INFO: new license ".$getCurrentData[$this->mapArrayData[$type][0]].
" added\n\n";
251 return $existingLicenseRefData;
255 echo $obj->startProcessingLicenseData();
getLicenseNameWithOutSuffix($RFShortName)
check if -only or -or-later exists.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData)
get SPDX license or exception list and update licenseref.json