Skip to content

Commit ae6720e

Browse files
committed
Fixed #74
It's now using file logger to log the errors. In addition, it has its own function, get_genattrib_errors, to get the error from the attribtution generation instead of getting ALL the errors/warnings that are not relevant for the attribution generation. Improved some test cases.
1 parent 93a2a5f commit ae6720e

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

about_code_tool/about.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def repr_problem(obj):
8484
ASCII = 'ASCII problem'
8585
SPDX = 'SPDX license problem'
8686
UNKNOWN = 'Unknown problem'
87+
GENATTRIB = 'Attribution generation problem'
8788

8889
MANDATORY_FIELDS = (
8990
'about_resource',
@@ -929,7 +930,7 @@ def notice_text(self):
929930

930931
return "" # Returns empty string if the notice file does not exist
931932

932-
def dje_license(self):
933+
def get_dje_license_name(self):
933934
"""
934935
Return the dje_license value if the dje_license field exists
935936
"""
@@ -938,7 +939,7 @@ def dje_license(self):
938939
except Exception as e:
939940
pass
940941

941-
return "" # Returns empty string if the dje_license key does not exist
942+
return ""
942943

943944
def get_license_text_file_name(self):
944945
"""
@@ -949,7 +950,7 @@ def get_license_text_file_name(self):
949950
except Exception as e:
950951
pass
951952

952-
return "" # Returns empty string if the license_text_file key does not exist
953+
return ""
953954

954955
class AboutCollector(object):
955956
"""
@@ -967,6 +968,8 @@ def __init__(self, input_path):
967968
self._errors = []
968969
self._warnings = []
969970

971+
self.genattrib_errors = []
972+
970973
self.abouts = [AboutFile(f)
971974
for f in self._collect_about_files(self.absolute_path)]
972975

@@ -1097,22 +1100,45 @@ def generate_attribution(self, template_path='templates/default.html',
10971100
if not limit_to or about_object.about_resource_path in limit_to:
10981101
validated_fields.append(about_object.validated_fields)
10991102
notice_text.append(about_object.notice_text())
1100-
if about_object.dje_license():
1101-
if not about_object.dje_license() in unique_license \
1102-
and not about_object.dje_license() == None:
1103-
unique_license.append(about_object.dje_license())
1104-
license_text.append(about_object.license_text())
1103+
dje_license_name = about_object.get_dje_license_name()
1104+
if dje_license_name:
1105+
if not dje_license_name in unique_license \
1106+
and not dje_license_name == None:
1107+
if about_object.license_text():
1108+
unique_license.append(about_object.get_dje_license_name())
1109+
license_text.append(about_object.license_text())
1110+
else:
1111+
msg = 'About resource: %s - license_text does not exist.'\
1112+
' Attribution generation is skipped.'\
1113+
% about_object.about_resource_path
1114+
self.genattrib_errors.append(Error(GENATTRIB,\
1115+
'dje_license',\
1116+
dje_license_name, msg))
11051117
elif about_object.get_license_text_file_name():
1106-
if not about_object.get_license_text_file_name() in unique_license \
1107-
and about_object.license_text():
1108-
unique_license.append(about_object.get_license_text_file_name())
1109-
license_text.append(about_object.license_text())
1118+
if not about_object.get_license_text_file_name() in unique_license:
1119+
if about_object.license_text():
1120+
unique_license.append(about_object.get_license_text_file_name())
1121+
license_text.append(about_object.license_text())
1122+
else:
1123+
msg = 'About resource: %s - license_text does not exist.'\
1124+
' Attribution generation is skipped.'\
1125+
% about_object.about_resource_path
1126+
self.genattrib_errors.append(Error(GENATTRIB,\
1127+
'license_text',\
1128+
about_object.get_license_text_file_name(), msg))
1129+
else:
1130+
msg = 'No dje_license or license_text is found. Attribution generation is skipped.'
1131+
self.genattrib_errors.append(Error(GENATTRIB, 'about_resource',\
1132+
about_object.about_resource_path,\
1133+
msg))
11101134

11111135
return template.render(about_objects=validated_fields,
11121136
license_texts=license_text,
11131137
notice_texts=notice_text,
11141138
unique_licenses=unique_license)
11151139

1140+
def get_genattrib_errors(self):
1141+
return self.genattrib_errors
11161142

11171143
USAGE_SYNTAX = """\
11181144
Input can be a file or directory.

about_code_tool/genattrib.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33

4-
# =============================================================================
5-
# Copyright (c) 2013 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
6-
# Licensed under the Apache License, Version 2.0 (the "License");
7-
# you may not use this file except in compliance with the License.
8-
# You may obtain a copy of the License at
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
# Unless required by applicable law or agreed to in writing, software
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
15-
# =============================================================================
16-
174
"""
185
This is a tool to generate component attribution based on a set of .ABOUT files.
196
Optionally, one could pass a subset list of specific components for set of
@@ -45,13 +32,14 @@
4532
from os.path import exists, dirname, join, abspath, isdir, basename, normpath
4633
from StringIO import StringIO
4734

35+
LOG_FILENAME = 'error.log'
4836

4937
logger = logging.getLogger(__name__)
5038
handler = logging.StreamHandler()
5139
handler.setLevel(logging.CRITICAL)
5240
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
5341
logger.addHandler(handler)
54-
42+
file_logger = logging.getLogger(__name__+'_file')
5543

5644
__version__ = '0.9.0'
5745

@@ -125,6 +113,13 @@ def main(parser, options, args):
125113
reload(sys)
126114
sys.setdefaultencoding('utf-8')
127115

116+
# Clear the log file
117+
with open(join(dirname(output_path), LOG_FILENAME), 'w'):
118+
pass
119+
120+
file_handler = logging.FileHandler(join(dirname(output_path), LOG_FILENAME))
121+
file_logger.addHandler(file_handler)
122+
128123
if not exists(input_path):
129124
print('Input path does not exist.')
130125
parser.print_help()
@@ -152,12 +147,14 @@ def main(parser, options, args):
152147
attrib_str = collector.generate_attribution( limit_to = sublist )
153148
with open(output_path, "w") as f:
154149
f.write(attrib_str)
155-
150+
errors = collector.get_genattrib_errors()
151+
for error_msg in errors:
152+
logger.error(error_msg)
153+
file_logger.error(error_msg)
156154
else:
157155
# we should never reach this
158156
assert False, "Unsupported option(s)."
159157

160-
161158
def get_parser():
162159
class MyFormatter(optparse.IndentedHelpFormatter):
163160
def _format_text(self, text):

about_code_tool/tests/test_about.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,39 @@ def test_notice_text_returns_empty_string_when_ref_file_doesnt_exist(self):
578578
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/missing_notice_license_files.ABOUT'))
579579
notice_text = about_file.notice_text()
580580
self.assertEqual(notice_text, expected)
581+
582+
def test_get_dje_license_name(self):
583+
expected = 'apache-2.0'
584+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_resource_field_present.ABOUT'))
585+
output = about_file.get_dje_license_name()
586+
self.assertTrue(output == expected)
587+
588+
def test_get_license_text_file_name(self):
589+
expected = 'httpd.LICENSE'
590+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_resource_field_present.ABOUT'))
591+
output = about_file.get_license_text_file_name()
592+
self.assertTrue(output == expected)
593+
594+
def test_get_dje_license_name_no_value(self):
595+
expected = ''
596+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_empty_value_for_dje_license_license_text_file.ABOUT'))
597+
output = about_file.get_dje_license_name()
598+
self.assertTrue(output == expected)
599+
600+
def test_get_license_text_file_name_no_value(self):
601+
expected = ''
602+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_empty_value_for_dje_license_license_text_file.ABOUT'))
603+
output = about_file.get_license_text_file_name()
604+
self.assertTrue(output == expected)
605+
606+
def test_get_dje_license_name_no_key(self):
607+
expected = None
608+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_no_dje_license_no_license_text_file_keys.ABOUT'))
609+
output = about_file.get_dje_license_name()
610+
self.assertTrue(output == expected)
611+
612+
def test_get_license_text_file_name_no_key(self):
613+
expected = None
614+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_no_dje_license_no_license_text_file_keys.ABOUT'))
615+
output = about_file.get_license_text_file_name()
616+
self.assertTrue(output == expected)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: Apache HTTP Server
2+
version: 2.4.3
3+
4+
dje_license:
5+
license_text_file:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: Apache HTTP Server
2+
version: 2.4.3

about_code_tool/tests/testdata/parser_tests/about_resource_field_present.ABOUT

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ license_spdx: Apache-2.0
55
license_text_file: httpd.LICENSE
66
copyright: Copyright 2012 The Apache Software Foundation.
77
notice_file: httpd.NOTICE
8-
about_resource: about_resource.c
8+
about_resource: about_resource.c
9+
dje_license: apache-2.0

0 commit comments

Comments
 (0)