Skip to content

Commit 54820eb

Browse files
committed
#396 - Fixed some tests and code enhancement. Need add tests.
1 parent 03748ae commit 54820eb

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

src/attributecode/gen.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
217217
base_dir. Return errors and about objects.
218218
"""
219219
not_exist_errors = []
220+
notice_dict = {}
220221
api_url = ''
221222
api_key = ''
222223
gen_license = False
@@ -311,7 +312,22 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
311312
if about.license_name.value:
312313
about.license_name.present = True
313314

314-
about.dump(dump_loc, android)
315+
about.dump(dump_loc)
316+
317+
if android:
318+
"""
319+
Create MODULE_LICENSE_XXX and get context to create NOTICE file
320+
follow the standard from Android Open Source Project
321+
"""
322+
import os
323+
parent_path = os.path.dirname(util.to_posix(dump_loc))
324+
325+
about.android_module_license(parent_path)
326+
notice_path, notice_context = about.android_notice(parent_path)
327+
if notice_path in notice_dict.keys():
328+
notice_dict[notice_path] += '\n\n' + notice_context
329+
else:
330+
notice_dict[notice_path] = notice_context
315331

316332
for e in not_exist_errors:
317333
errors.append(Error(INFO, e))
@@ -324,4 +340,14 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
324340
u'%(dump_loc)s '
325341
u'with error: %(emsg)s' % locals())
326342
errors.append(Error(ERROR, msg))
343+
344+
if android:
345+
# Check if there is already a NOTICE file present
346+
for path in notice_dict.keys():
347+
if os.path.exists(path):
348+
msg = (u'NOTICE file already exist at: %s' % path)
349+
errors.append(Error(ERROR, msg))
350+
else:
351+
about.dump_android_notice(path, notice_dict[path])
352+
327353
return unique(errors), abouts

src/attributecode/model.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ def dumps(self):
10661066

10671067
return saneyaml.dump(data)
10681068

1069-
def dump(self, location, android):
1069+
def dump(self, location):
10701070
"""
10711071
Write formatted ABOUT representation of self to location.
10721072
"""
@@ -1087,37 +1087,54 @@ def dump(self, location, android):
10871087
if on_windows:
10881088
about_file_path = add_unc(about_file_path)
10891089

1090-
if android:
1091-
for lic_key in self.license_key.value:
1090+
with io.open(about_file_path, mode='w', encoding='utf-8') as dumped:
1091+
dumped.write(genereated_tk_version)
1092+
dumped.write(self.dumps())
1093+
1094+
def dump_android_notice(self, path, context):
1095+
"""
1096+
Write the NOITCE file consist of copyright, notice and license
1097+
"""
1098+
if on_windows:
1099+
path = add_unc(path)
1100+
1101+
with io.open(path, mode='w', encoding='utf-8') as dumped:
1102+
dumped.write(context)
1103+
1104+
def android_module_license(self, about_parent_path):
1105+
"""
1106+
Create MODULE_LICENSE_XXX which the XXX is the value of license key.
1107+
"""
1108+
for lic_key in self.license_key.value:
10921109
# Make uppercase and with dash and spaces and dots replaced by underscore
10931110
# just to look similar and consistent.
10941111
name = 'MODULE_LICENSE_' + lic_key.replace('.', '_').replace('-', '_').replace(' ', '_').upper()
1095-
module_lic_path = os.path.join(os.path.dirname(about_file_path), name)
1112+
module_lic_path = os.path.join(about_parent_path, name)
10961113
# Create an empty MODULE_LICESE_XXX file
10971114
open(module_lic_path, 'a').close()
10981115

1099-
# Create NOTICE file with the combination context of copyright,
1100-
# notice_file and license_file
1101-
notice_path = os.path.join(os.path.dirname(about_file_path), 'NOTICE')
1102-
notice_context = ''
1103-
if self.copyright.value:
1104-
notice_context += self.copyright.value
1105-
if self.notice_file.value:
1106-
notice_file_dict = self.notice_file.value
1107-
notice_file_key = notice_file_dict.keys()
1108-
for key in notice_file_key:
1109-
notice_context += '\n\n' + notice_file_dict[key]
1110-
if self.license_file.value:
1111-
lic_file_dict = self.license_file.value
1112-
lic_file_key = lic_file_dict.keys()
1113-
for key in lic_file_key:
1114-
notice_context += '\n\n' + lic_file_dict[key]
1115-
with io.open(notice_path, mode='w', encoding='utf-8') as dumped:
1116-
dumped.write(notice_context)
1117-
1118-
with io.open(about_file_path, mode='w', encoding='utf-8') as dumped:
1119-
dumped.write(genereated_tk_version)
1120-
dumped.write(self.dumps())
1116+
def android_notice(self, about_parent_path):
1117+
"""
1118+
Return a notice dictionary which the path of the notice file going
1119+
to create will be the key and its context will be the value of the dict.
1120+
"""
1121+
# Create NOTICE file with the combination context of copyright,
1122+
# notice_file and license_file
1123+
notice_path = posixpath.join(about_parent_path, 'NOTICE')
1124+
notice_context = ''
1125+
if self.copyright.value:
1126+
notice_context += self.copyright.value
1127+
if self.notice_file.value:
1128+
notice_file_dict = self.notice_file.value
1129+
notice_file_key = notice_file_dict.keys()
1130+
for key in notice_file_key:
1131+
notice_context += '\n' + notice_file_dict[key] + '\n'
1132+
if self.license_file.value:
1133+
lic_file_dict = self.license_file.value
1134+
lic_file_key = lic_file_dict.keys()
1135+
for key in lic_file_key:
1136+
notice_context += '\n\n' + lic_file_dict[key] + '\n\n'
1137+
return notice_path, notice_context
11211138

11221139
def dump_lic(self, location, license_dict):
11231140
"""

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def test_About_rejects_non_ascii_names_and_accepts_unicode_values(self):
582582
test_file = get_test_loc('test_model/parse/non_ascii_field_name_value.about')
583583
a = model.About(test_file)
584584
expected = [
585-
Error(CRITICAL, "Field name: 'mat\xedas' contains illegal name characters: 0 to 9, a to z, A to Z and _.")
585+
Error(CRITICAL, "Field name: 'mat\xedas' contains illegal name characters: 0 to 9, a to z, A to Z and _. (or empty spaces)")
586586
]
587587
assert expected == a.errors
588588

tests/testdata/test_cmd/help/about_gen_help.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Usage: about gen [OPTIONS] LOCATION OUTPUT
88
OUTPUT: Path to a directory where ABOUT files are generated.
99

1010
Options:
11+
--android Generate MODULE_LICENSE_XXX (XXX will be replaced by
12+
license key) and NOTICE as the same design as from
13+
Android.
1114
--fetch-license URL KEY Fetch license data and text files from a DejaCode
1215
License Library API URL using the API KEY.
1316
--reference DIR Path to a directory with reference license data and

0 commit comments

Comments
 (0)