Skip to content

Commit 307e425

Browse files
committed
minor code changes with some tests commented out
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 03d970f commit 307e425

File tree

7 files changed

+617
-42
lines changed

7 files changed

+617
-42
lines changed

src/attributecode/model.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,49 +1563,58 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
15631563
auth_error = Error(ERROR, u"Authorization denied. Invalid '--api_key'. License generation is skipped.")
15641564
if auth_error in errors:
15651565
break
1566-
if not about.license_file.value:
1566+
#if not about.license_file.value:
1567+
# FIXME
1568+
# Scancode returns license_expressions while ABcTK uses license_expression
1569+
lic_exp = ''
1570+
if about.license_expression or about.license_expressions:
15671571
if about.license_expression.value:
1568-
special_char_in_expression, lic_list = parse_license_expression(about.license_expression.value)
1569-
if special_char_in_expression:
1570-
msg = (about.about_file_path + u": The following character(s) cannot be in the license_expression: " +
1571-
str(special_char_in_expression))
1572-
errors.append(Error(ERROR, msg))
1573-
else:
1574-
for lic_key in lic_list:
1575-
if not lic_key in captured_license:
1576-
lic_url = ''
1577-
license_name = ''
1578-
license_filename = ''
1579-
license_text = ''
1580-
detail_list = []
1581-
if api_key:
1582-
license_name, _license_key, license_text, errs = api.get_license_details_from_api(url, api_key, lic_key)
1583-
for severity, message in errs:
1584-
msg = (about.about_file_path + ": " + message)
1585-
errors.append(Error(severity, msg))
1586-
license_filename = lic_key + '.LICENSE'
1587-
lic_url = lic_urn + lic_key
1588-
else:
1589-
license_url = url + lic_key + '.json'
1590-
license_text_url = url + lic_key + '.LICENSE'
1591-
try:
1592-
json_url = urlopen(license_url)
1593-
data = json.loads(json_url.read())
1594-
license_name = data['name']
1595-
license_text = urllib.request.urlopen(license_text_url).read().decode('utf-8')
1596-
license_filename = data['key'] + '.LICENSE'
1597-
lic_url = url + license_filename
1598-
except:
1599-
msg = about.about_file_path + u" : Invalid 'license': " + lic_key
1600-
errors.append(Error(ERROR, msg))
1601-
captured_license.append(lic_key)
1602-
detail_list.append(license_name)
1603-
detail_list.append(license_filename)
1604-
detail_list.append(license_text)
1605-
detail_list.append(lic_url)
1606-
key_text_dict[lic_key] = detail_list
1607-
if not about.license_key.value:
1608-
about.license_key.value = lic_list
1572+
lic_exp = about.license_expression.value
1573+
else:
1574+
lic_exp = about.license_expressions.value
1575+
1576+
if lic_exp:
1577+
special_char_in_expression, lic_list = parse_license_expression(lic_exp)
1578+
if special_char_in_expression:
1579+
msg = (about.about_file_path + u": The following character(s) cannot be in the license_expression: " +
1580+
str(special_char_in_expression))
1581+
errors.append(Error(ERROR, msg))
1582+
else:
1583+
for lic_key in lic_list:
1584+
if not lic_key in captured_license:
1585+
lic_url = ''
1586+
license_name = ''
1587+
license_filename = ''
1588+
license_text = ''
1589+
detail_list = []
1590+
if api_key:
1591+
license_name, _license_key, license_text, errs = api.get_license_details_from_api(url, api_key, lic_key)
1592+
for severity, message in errs:
1593+
msg = (about.about_file_path + ": " + message)
1594+
errors.append(Error(severity, msg))
1595+
license_filename = lic_key + '.LICENSE'
1596+
lic_url = lic_urn + lic_key
1597+
else:
1598+
license_url = url + lic_key + '.json'
1599+
license_text_url = url + lic_key + '.LICENSE'
1600+
try:
1601+
json_url = urlopen(license_url)
1602+
data = json.loads(json_url.read())
1603+
license_name = data['name']
1604+
license_text = urllib.request.urlopen(license_text_url).read().decode('utf-8')
1605+
license_filename = data['key'] + '.LICENSE'
1606+
lic_url = url + license_filename
1607+
except:
1608+
msg = about.about_file_path + u" : Invalid 'license': " + lic_key
1609+
errors.append(Error(ERROR, msg))
1610+
captured_license.append(lic_key)
1611+
detail_list.append(license_name)
1612+
detail_list.append(license_filename)
1613+
detail_list.append(license_text)
1614+
detail_list.append(lic_url)
1615+
key_text_dict[lic_key] = detail_list
1616+
if not about.license_key.value:
1617+
about.license_key.value = lic_list
16091618
return key_text_dict, errors
16101619

16111620

tests/test_attrib.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from testing_utils import get_test_loc
2222
from testing_utils import get_temp_file
2323

24+
from attributecode import INFO
2425
from attributecode import attrib
26+
from attributecode import gen
2527
from attributecode import model
2628

2729

@@ -135,6 +137,76 @@ def test_lic_key_name_sync(self):
135137

136138
assert f1 == f2
137139

140+
"""
141+
def test_custom_template(self):
142+
test_file = get_test_loc('test_attrib/scancode_custom_template/clean-text-0.3.0-mod-lceupi.json')
143+
custom_template = get_test_loc('test_attrib/scancode_custom_template/scancode.template')
144+
errors, abouts = gen.load_inventory(test_file, scancode=True)
145+
expected_errors = [(40, 'Field about_resource: Unable to verify path: isc_lic.py: No base directory provided'), (30, "Field license_key: ignored duplicated list value: 'isc'"), (30, "Field license_name: ignored duplicated list value: 'ISC License'")]
146+
result = [(level, e) for level, e in errors if level > INFO]
147+
assert expected_errors == result
148+
#assert not errors
149+
150+
lic_dict = {'isc': {'key': 'isc', 'short_name': 'ISC License', 'name': 'ISC License',
151+
'category': 'Permissive', 'owner': 'ISC - Internet Systems Consortium',
152+
'homepage_url': 'https://www.isc.org/software/license', 'notes': 'Per SPDX.org, this license is OSI certified.',
153+
'spdx_license_key': 'ISC', 'text_urls': ['http://fedoraproject.org/wiki/Licensing:MIT#Old_Style_with_legal_disclaimer_2', 'http://openbsd.wikia.com/wiki/OpenBSD%27s_BSD_license', 'http://opensource.org/licenses/isc-license.txt', 'https://www.isc.org/software/license'],
154+
'osi_url': 'http://opensource.org/licenses/isc-license.txt', 'other_urls': ['http://openbsd.wikia.com/wiki/OpenBSD%27s_BSD_license', 'http://www.isc.org/software/license', 'http://www.opensource.org/licenses/ISC', 'https://opensource.org/licenses/ISC', 'https://www.isc.org/downloads/software-support-policy/isc-license/', 'https://www.isc.org/isc-license-1.0.html'],
155+
'license_text': 'Permission to use, copy, modify, and/or distribute this software for any purpose\nwith or without fee is hereby granted, provided that the above copyright notice\nand this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS\nOF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\nTORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\nTHIS SOFTWARE.\n'}}
156+
is_about_input = False
157+
errors, result = attrib.generate_from_file(abouts, is_about_input, lic_dict, min_license_score=0, template_loc=custom_template)
158+
expected_errors = []
159+
#result = [(level, e) for level, e in errors if level > INFO]
160+
#assert expected_errors == result
161+
assert not errors
162+
163+
expected_file = get_test_loc(
164+
'test_attrib/scancode_custom_template/expect.html')
165+
with open(expected_file) as exp:
166+
expected = exp.read()
167+
168+
# strip the timestamp: the timestamp is wrapped in italic block
169+
result = remove_timestamp(result)
170+
expected = remove_timestamp(expected)
171+
assert expected == result
172+
173+
def test_generate_with_default_template(self):
174+
test_file = get_test_loc('test_attrib/default_template/simple_sample.csv')
175+
errors, abouts = util.load_inventory(test_file)
176+
assert not errors
177+
178+
lic_dict = {'bsd-new':
179+
{'key': 'bsd-new', 'short_name': 'BSD-3-Clause', 'name': 'BSD-3-Clause',
180+
'category': 'Permissive', 'owner': 'Regents of the University of California',
181+
'homepage_url': 'http://www.opensource.org/licenses/BSD-3-Clause',
182+
'notes': 'Per SPDX.org, this license is OSI certified.',
183+
'spdx_license_key': 'BSD-3-Clause', 'osi_license_key': 'BSD-3',
184+
'text_urls': ['http://www.opensource.org/licenses/BSD-3-Clause'],
185+
'osi_url': 'http://www.opensource.org/licenses/BSD-3-Clause',
186+
'other_urls': ['http://framework.zend.com/license/new-bsd', 'https://opensource.org/licenses/BSD-3-Clause'],
187+
'license_text': 'Redistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\nRedistributions of source code must retain the above copyright notice, this list\nof conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright notice, this\nlist of conditions and the following disclaimer in the documentation and/or\nother materials provided with the distribution.\n\nNeither the name of the ORGANIZATION nor the names of its contributors may be\nused to endorse or promote products derived from this software without specific\nprior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\nARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS\nBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\nGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\nHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\nLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF\nTHE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'},
188+
'mit': {'key': 'mit', 'short_name': 'MIT License', 'name': 'MIT License',
189+
'category': 'Permissive', 'owner': 'MIT',
190+
'homepage_url': 'http://opensource.org/licenses/mit-license.php',
191+
'notes': 'Per SPDX.org, this license is OSI certified.', 'spdx_license_key': 'MIT',
192+
'text_urls': ['http://opensource.org/licenses/mit-license.php'],
193+
'osi_url': 'http://www.opensource.org/licenses/MIT',
194+
'other_urls': ['https://opensource.com/article/18/3/patent-grant-mit-license', 'https://opensource.com/article/19/4/history-mit-license', 'https://opensource.org/licenses/MIT'],
195+
'license_text': 'Permission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n"Software"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'}
196+
}
197+
error, result = attrib.generate_from_file(abouts, lic_dict, min_license_score=0)
198+
assert not error
199+
200+
expected_file = get_test_loc(
201+
'test_attrib/default_template/expect.html')
202+
with open(expected_file) as exp:
203+
expected = exp.read()
204+
205+
# strip the timestamp: the timestamp is wrapped in italic block
206+
result = remove_timestamp(result)
207+
expected = remove_timestamp(expected)
208+
assert expected == result
209+
"""
138210

139211
def remove_timestamp(html_text):
140212
"""
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
<!doctype html>
3+
<html>
4+
<head>
5+
<style type="text/css">
6+
div.additional-license-text-list {display:block}
7+
body {font-family: Helvetica, Arial, sans-serif;}
8+
pre {white-space: pre-wrap;}
9+
</style>
10+
<title>Open Source Software Information</title>
11+
</head>
12+
13+
<body>
14+
<h1>OPEN SOURCE SOFTWARE INFORMATION</h1>
15+
<h2> </h2>
16+
<div>
17+
<p>Licenses, acknowledgments and required copyright notices for
18+
open source components:</p>
19+
</div>
20+
21+
<div class="oss-table-of-contents">
22+
23+
<p><a href="#component_0">cryptohash-sha256 v 0.11.100.1</a></p>
24+
25+
<p><a href="#component_1">some_component v 0.0.1</a></p>
26+
27+
</div>
28+
29+
<hr/>
30+
31+
32+
33+
<div class="oss-component" id="component_0">
34+
<h3 class="component-name">cryptohash-sha256
35+
v 0.11.100.1
36+
</h3>
37+
38+
<p>This component is licensed under
39+
bsd-new and mit
40+
41+
42+
43+
44+
45+
46+
<p>Full text of
47+
<a class="bsd-new" href="#component-license-bsd-new">
48+
bsd-new
49+
</a>
50+
is available at the end of this document.</p>
51+
52+
53+
54+
<p>Full text of
55+
<a class="mit" href="#component-license-mit">
56+
mit
57+
</a>
58+
is available at the end of this document.</p>
59+
60+
61+
62+
63+
</div>
64+
65+
<div class="oss-component" id="component_1">
66+
<h3 class="component-name">some_component
67+
v 0.0.1
68+
</h3>
69+
70+
<p>This component is licensed under
71+
mit
72+
73+
74+
75+
76+
77+
78+
<p>Full text of
79+
<a class="mit" href="#component-license-mit">
80+
mit
81+
</a>
82+
is available at the end of this document.</p>
83+
84+
85+
86+
87+
</div>
88+
89+
90+
<hr/>
91+
92+
<h3>Licenses Used in This Product</h3>
93+
94+
95+
<h3 id="component-license-bsd-new">bsd-new</h3>
96+
<pre>Redistribution and use in source and binary forms, with or without modification,
97+
are permitted provided that the following conditions are met:
98+
99+
Redistributions of source code must retain the above copyright notice, this list
100+
of conditions and the following disclaimer.
101+
102+
Redistributions in binary form must reproduce the above copyright notice, this
103+
list of conditions and the following disclaimer in the documentation and/or
104+
other materials provided with the distribution.
105+
106+
Neither the name of the ORGANIZATION nor the names of its contributors may be
107+
used to endorse or promote products derived from this software without specific
108+
prior written permission.
109+
110+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
111+
&#34;AS IS&#34; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
112+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
113+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
114+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
115+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
116+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
117+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
118+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
119+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</pre>
120+
121+
<pre>(http://www.opensource.org/licenses/BSD-3-Clause)</pre>
122+
123+
124+
<h3 id="component-license-mit">mit</h3>
125+
<pre>Permission is hereby granted, free of charge, to any person obtaining
126+
a copy of this software and associated documentation files (the
127+
&#34;Software&#34;), to deal in the Software without restriction, including
128+
without limitation the rights to use, copy, modify, merge, publish,
129+
distribute, sublicense, and/or sell copies of the Software, and to
130+
permit persons to whom the Software is furnished to do so, subject to
131+
the following conditions:
132+
133+
The above copyright notice and this permission notice shall be
134+
included in all copies or substantial portions of the Software.
135+
136+
THE SOFTWARE IS PROVIDED &#34;AS IS&#34;, WITHOUT WARRANTY OF ANY KIND,
137+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
138+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
139+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
140+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
141+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
142+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</pre>
143+
144+
<pre>(http://opensource.org/licenses/mit-license.php)</pre>
145+
146+
147+
148+
<h3><a id="End">End</a></h3>
149+
<i>This file was generated with AttributeCode version: 0.0.1 on: 2020-12-09 09:43:02.595712 (UTC)</i>
150+
</body>
151+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name,version,license_expression
2+
cryptohash-sha256,v 0.11.100.1,bsd-new and mit
3+
some_component,v 0.0.1,mit

0 commit comments

Comments
 (0)