|
39 | 39 | from commoncode import filetype |
40 | 40 | from commoncode import fileutils |
41 | 41 | from packagedcode import models |
| 42 | +from packagedcode.utils import combine_expressions |
| 43 | + |
42 | 44 |
|
43 | 45 | """ |
44 | 46 | Parse PHP composer package manifests, see https://getcomposer.org/ and |
@@ -94,7 +96,39 @@ def compute_normalized_license(self): |
94 | 96 | """ |
95 | 97 | Per https://getcomposer.org/doc/04-schema.md#license this is an expression |
96 | 98 | """ |
97 | | - return models.Package.compute_normalized_license(self) |
| 99 | + return compute_normalized_license(self.declared_license) |
| 100 | + |
| 101 | + |
| 102 | +def compute_normalized_license(declared_license): |
| 103 | + """ |
| 104 | + Return a normalized license expression string detected from a list of |
| 105 | + declared license items or string type. |
| 106 | + """ |
| 107 | + if not declared_license: |
| 108 | + return |
| 109 | + |
| 110 | + detected_licenses = [] |
| 111 | + |
| 112 | + if isinstance(declared_license, string_types): |
| 113 | + if declared_license == 'proprietary': |
| 114 | + return declared_license |
| 115 | + if '(' in declared_license and ')' in declared_license and ' or ' in declared_license: |
| 116 | + declared_license = declared_license.strip().rstrip(')').lstrip('(') |
| 117 | + declared_license = declared_license.split(' or ') |
| 118 | + else: |
| 119 | + return models.compute_normalized_license(declared_license) |
| 120 | + |
| 121 | + if isinstance(declared_license, list): |
| 122 | + for declared in declared_license: |
| 123 | + detected_license = models.compute_normalized_license(declared) |
| 124 | + detected_licenses.append(detected_license) |
| 125 | + else: |
| 126 | + declared_license = repr(declared_license) |
| 127 | + detected_license = models.compute_normalized_license(declared_license) |
| 128 | + |
| 129 | + if detected_licenses: |
| 130 | + # build a proper license expression: the defaultfor composer is OR |
| 131 | + return combine_expressions(detected_licenses, 'OR') |
98 | 132 |
|
99 | 133 |
|
100 | 134 | def is_phpcomposer_json(location): |
@@ -200,25 +234,11 @@ def licensing_mapper(licenses, package, is_private=False): |
200 | 234 | license and the `is_private` Fkag is True, we return a "proprietary-license" |
201 | 235 | license. |
202 | 236 | """ |
203 | | - if not licenses: |
| 237 | + if not licenses and is_private: |
| 238 | + package.declared_license = 'proprietary-license' |
204 | 239 | return package |
205 | 240 |
|
206 | | - if isinstance(licenses, list): |
207 | | - # For a package, when there is a choice between licenses |
208 | | - # ("disjunctive license"), multiple can be specified as array. |
209 | | - # build a proper license expression: the defaultfor composer is OR |
210 | | - lics = [l.strip() for l in licenses if l and l.strip()] |
211 | | - lics = ' OR '.join(lics) |
212 | | - |
213 | | - elif not isinstance(licenses, string_types): |
214 | | - lics = repr(licenses) |
215 | | - else: |
216 | | - lics = licenses |
217 | | - |
218 | | - if not lics and is_private: |
219 | | - lics ='proprietary-license' |
220 | | - |
221 | | - package.declared_license = lics or None |
| 241 | + package.declared_license = licenses |
222 | 242 | return package |
223 | 243 |
|
224 | 244 |
|
|
0 commit comments