55
55
from attributecode .util import csv
56
56
from attributecode .util import file_fields
57
57
from attributecode .util import filter_errors
58
+ from attributecode .util import get_spdx_key_and_lic_key_from_licdb
58
59
from attributecode .util import is_valid_name
59
60
from attributecode .util import on_windows
60
61
from attributecode .util import norm
@@ -802,6 +803,7 @@ def set_standard_fields(self):
802
803
('license_name' , ListField ()),
803
804
('license_file' , FileTextField ()),
804
805
('license_url' , UrlListField ()),
806
+ ('spdx_license_expression' , StringField ()),
805
807
('spdx_license_key' , ListField ()),
806
808
('copyright' , StringField ()),
807
809
('notice_file' , FileTextField ()),
@@ -1764,6 +1766,7 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
1764
1766
if errors :
1765
1767
return key_text_dict , errors
1766
1768
1769
+ spdx_sclickey_dict = get_spdx_key_and_lic_key_from_licdb ()
1767
1770
for about in abouts :
1768
1771
# No need to go through all the about objects if '--api_key' is invalid
1769
1772
auth_error = Error (
@@ -1779,6 +1782,27 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
1779
1782
about .license_expression .value = lic_exp
1780
1783
about .license_expression .present = True
1781
1784
1785
+ if not about .license_expression .value and about .spdx_license_expression .value :
1786
+ lic_exp_value = ""
1787
+ special_char_in_expression , lic_list = parse_license_expression (
1788
+ about .spdx_license_expression .value )
1789
+ if special_char_in_expression :
1790
+ msg = (about .about_file_path + u": The following character(s) cannot be in the spdx_license_expression: " +
1791
+ str (special_char_in_expression ))
1792
+ errors .append (Error (ERROR , msg ))
1793
+ else :
1794
+ spdx_lic_exp_segment = about .spdx_license_expression .value .split ()
1795
+ for spdx_lic_key in spdx_lic_exp_segment :
1796
+ if lic_exp_value :
1797
+ lic_exp_value = lic_exp_value + " " + convert_spdx_expression_to_lic_expression (
1798
+ spdx_lic_key , spdx_sclickey_dict )
1799
+ else :
1800
+ lic_exp_value = convert_spdx_expression_to_lic_expression (
1801
+ spdx_lic_key , spdx_sclickey_dict )
1802
+ if lic_exp_value :
1803
+ about .license_expression .value = lic_exp_value
1804
+ about .license_expression .present = True
1805
+
1782
1806
if about .license_expression .value :
1783
1807
special_char_in_expression , lic_list = parse_license_expression (
1784
1808
about .license_expression .value )
@@ -1855,6 +1879,30 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
1855
1879
return key_text_dict , errors
1856
1880
1857
1881
1882
+ def convert_spdx_expression_to_lic_expression (spdx_key , spdx_lic_dict ):
1883
+ """
1884
+ Translate the spdx_license_expression to license_expression and return
1885
+ errors if spdx_license_key is not matched
1886
+ """
1887
+ value = ""
1888
+ if spdx_key in spdx_lic_dict :
1889
+ value = spdx_lic_dict [spdx_key ]
1890
+ else :
1891
+ if spdx_key .startswith ('(' ):
1892
+ mod_key = spdx_key .partition ('(' )[2 ]
1893
+ value = '(' + \
1894
+ convert_spdx_expression_to_lic_expression (
1895
+ mod_key , spdx_lic_dict )
1896
+ elif spdx_key .endswith (')' ):
1897
+ mod_key = spdx_key .rpartition (')' )[0 ]
1898
+ value = convert_spdx_expression_to_lic_expression (
1899
+ mod_key , spdx_lic_dict ) + ')'
1900
+ else :
1901
+ # This can be operator or key that don't have match
1902
+ value = spdx_key
1903
+ return value
1904
+
1905
+
1858
1906
def parse_license_expression (lic_expression ):
1859
1907
licensing = Licensing ()
1860
1908
lic_list = []
0 commit comments