diff --git a/about_code_tool/gen.py b/about_code_tool/gen.py index c60b3509..25fbceed 100644 --- a/about_code_tool/gen.py +++ b/about_code_tool/gen.py @@ -185,6 +185,7 @@ def generate(mapping, extract_license, location, base_dir, policy=None, conf_loc """ api_url = '' api_key = '' + gen_license = False # Check if the extract_license contains valid argument if extract_license: # Strip the ' and " for api_url, and api_key from input @@ -245,24 +246,27 @@ def generate(mapping, extract_license, location, base_dir, policy=None, conf_loc about.about_resource.original_value = basename(about.about_file_path) about.about_resource.present = True + if gen_license: + # Write generated LICENSE file + lic_name, lic_context, lic_url = about.dump_lic(dump_loc, dje_license_dict) + if lic_name: + if not about.license_name.present: + about.license_name.value = lic_name + about.license_name.present = True + if not about.license_file.present: + about.license_file.value = [about.dje_license_key.value + u'.LICENSE'] + about.license_file.present = True + if not about.license_url.present: + about.license_url.value = [lic_url] + about.license_url.present = True + # Write the ABOUT file and check does the referenced file exist not_exist_errors = about.dump(dump_loc, with_empty=with_empty, with_absent=with_absent) for e in not_exist_errors: errors.append(Error(ERROR, e)) - if gen_license: - # Write generated LICENSE file - lic_name, lic_context, lic_url = about.dump_lic(dump_loc, dje_license_dict) - if not about.license_name.present: - about.license_name.value = lic_name - about.license_name.present = True - if not about.license_file.present: - about.license_file.value = about.dje_license_key.value + u'.LICENSE' - about.license_file.present = True - if not about.license_url.present: - about.license_url.value = lic_url - about.license_url.present = True + except Exception, e: # only keep the first 100 char of the exception emsg = repr(e)[:100] diff --git a/about_code_tool/model.py b/about_code_tool/model.py index c3ebc319..8d250ce2 100644 --- a/about_code_tool/model.py +++ b/about_code_tool/model.py @@ -140,7 +140,7 @@ def serialize(self): """ name = self.name value = self.serialized_value() or u'' - if self.has_content: + if self.has_content or self.value: value = value.splitlines(True) # multi-line if len(value) > 1: @@ -204,7 +204,7 @@ def _validate(self, *args, **kwargs): return errors def _serialized_value(self): - return self.value if self.has_content else u'' + return self.value if self.value else u'' def __eq__(self, other): """ @@ -287,7 +287,7 @@ def _validate(self, *args, **kwargs): return errors def _serialized_value(self): - return u'\n'.join(self.value) if self.has_content else u'' + return u'\n'.join(self.value) if self.value else u'' def __eq__(self, other): """ @@ -731,12 +731,12 @@ def all_fields(self, with_absent=True, with_empty=True, with_capture=True): elif field.present and with_capture: if with_empty: all_fields.append(field) - elif field.present and field.has_content: + elif field.present and field.value: all_fields.append(field) else: if field.present: - if not field.has_content: + if not field.value: if with_empty: all_fields.append(field) else: @@ -997,12 +997,15 @@ def dump_lic(self, location, license_dict): if self.dje_license_key.present: lic_key = self.dje_license_key.value - if license_dict[lic_key]: - license_path = posixpath.join(parent, lic_key) - license_path += u'.LICENSE' - license_name, license_context, license_url = license_dict[lic_key] - with codecs.open(license_path, mode='wb', encoding='utf-8') as lic: - lic.write(license_context) + try: + if license_dict[lic_key]: + license_path = posixpath.join(parent, lic_key) + license_path += u'.LICENSE' + license_name, license_context, license_url = license_dict[lic_key] + with codecs.open(license_path, mode='wb', encoding='utf-8') as lic: + lic.write(license_context) + except: + pass return license_name, license_context, license_url # valid field name diff --git a/about_code_tool/tests/test_gen.py b/about_code_tool/tests/test_gen.py index 0b0e1978..f207fd5c 100644 --- a/about_code_tool/tests/test_gen.py +++ b/about_code_tool/tests/test_gen.py @@ -70,25 +70,25 @@ def test_load_inventory(self): def test_generation_dir_endswith_space(self): mapping = None + extract_license = False location = get_test_loc('inventory/complex/about_file_path_dir_endswith_space.csv') gen_dir = get_temp_dir() - errors, abouts = gen.generate(mapping, location, + errors, abouts = gen.generate(mapping, extract_license, location, base_dir=gen_dir, with_empty=False, with_absent=False) expected_errors_msg = 'contains directory name ends with spaces which is not allowed. Generation skipped.' assert (len(errors) == 1, 'Should return 1 error.') - print("!!!!!!!!!!!!!!!!!!!") - print(errors[0].message) assert expected_errors_msg in errors[0].message def test_generation_with_no_about_resource(self): mapping = None + extract_license = False location = get_test_loc('gen/inv2.csv') gen_dir = get_temp_dir() - errors, abouts = gen.generate(mapping, location, + errors, abouts = gen.generate(mapping, extract_license, location, base_dir=gen_dir, with_empty=False, with_absent=False) expected_dict = OrderedDict() @@ -99,10 +99,11 @@ def test_generation_with_no_about_resource(self): def test_generation_with_no_about_resource_reference(self): mapping = None + extract_license = False location = get_test_loc('gen/inv3.csv') gen_dir = get_temp_dir() - errors, abouts = gen.generate(mapping, location, + errors, abouts = gen.generate(mapping, extract_license, location, base_dir=gen_dir, with_empty=False, with_absent=False) expected_dict = OrderedDict() @@ -115,10 +116,12 @@ def test_generation_with_no_about_resource_reference(self): @expectedFailure def test_generate(self): + mapping = '' + extract_license = False location = get_test_loc('gen/inv.csv') gen_dir = get_temp_dir() - errors, abouts = gen.generate(location, base_dir=gen_dir, + errors, abouts = gen.generate(mapping, extract_license, location, base_dir=gen_dir, with_empty=False, with_absent=False) expected_errors = [Error(INFO, u'Field custom1 is a custom field')] @@ -140,10 +143,12 @@ def test_generate(self): @expectedFailure def test_generate_complex_inventory(self): + mapping = '' + extract_license = False location = get_test_loc('inventory/complex/about/expected.csv') gen_dir = get_temp_dir() - errors, abouts = gen.generate(location, + errors, abouts = gen.generate(mapping, extract_license, location, base_dir=gen_dir, with_empty=False, with_absent=False) diff --git a/about_code_tool/tests/test_model.py b/about_code_tool/tests/test_model.py index 9ca64ce0..47bbc4cb 100644 --- a/about_code_tool/tests/test_model.py +++ b/about_code_tool/tests/test_model.py @@ -594,6 +594,7 @@ def test_About_dumps_does_not_transform_strings_in_lists(self): vcs_revision: checksum: spec_version: +dje_license_key: ''' # print() # print('a') @@ -670,6 +671,7 @@ def test_field_names(self): 'vcs_revision', 'checksum', 'spec_version', + 'dje_license_key', 'f', 'g'] result = model.field_names(abouts) @@ -768,6 +770,7 @@ def test_About_dumps_all_fields_if_not_present_with_absent_True(self): vcs_revision: checksum: spec_version: +dje_license_key: ''' result = a.dumps(with_absent=True) assert set(expected) == set(result) @@ -874,7 +877,8 @@ def test_About_as_dict_with_present(self): 'vcs_revision': u'', 'vcs_tag': u'', 'vcs_tool': u'', - 'version': u''} + 'version': u'', + 'dje_license_key': u''} result = a.as_dict(with_paths=False, with_empty=False, with_absent=True) diff --git a/about_code_tool/tests/testdata/inventory/basic/expected.csv b/about_code_tool/tests/testdata/inventory/basic/expected.csv index 8e5a61c1..6f7839d2 100644 --- a/about_code_tool/tests/testdata/inventory/basic/expected.csv +++ b/about_code_tool/tests/testdata/inventory/basic/expected.csv @@ -1,2 +1,2 @@ -about_file_path,about_resource_path,about_resource,name,version,download_url,description,home_url,notes,license,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_change,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum,spec_version -/about/about.ABOUT,/about/,.,AboutCode,0.11.0,,AboutCode is a tool to process ABOUT files. An ABOUT file is a file.,http://dejacode.org,,apache-2.0,,apache-2.0.LICENSE,,Copyright (c) 2013-2014 nexB Inc.,NOTICE,,,,,,,nexB Inc.,,,"Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez",git,https://github.com/dejacode/about-code-tool.git,,,,,, +about_file_path,about_resource_path,about_resource,name,version,download_url,description,home_url,notes,license,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_change,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum,spec_version,dje_license_key +/about/about.ABOUT,/about/,.,AboutCode,0.11.0,,AboutCode is a tool to process ABOUT files. An ABOUT file is a file.,http://dejacode.org,,apache-2.0,,apache-2.0.LICENSE,,Copyright (c) 2013-2014 nexB Inc.,NOTICE,,,,,,,nexB Inc.,,,"Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez",git,https://github.com/dejacode/about-code-tool.git,,,,,,, diff --git a/about_code_tool/tests/testdata/load/expected.csv b/about_code_tool/tests/testdata/load/expected.csv index 3e53cb41..8bdec379 100644 --- a/about_code_tool/tests/testdata/load/expected.csv +++ b/about_code_tool/tests/testdata/load/expected.csv @@ -1,2 +1,2 @@ -about_file_path,about_resource_path,about_resource,name,version,download_url,description,home_url,notes,license,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_change,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum,spec_version -/load/this.ABOUT,/load/,.,AboutCode,0.11.0,,,,,,,,,,,,,,,,,,,,,,,,,,,, +about_file_path,about_resource_path,about_resource,name,version,download_url,description,home_url,notes,license,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_change,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum,spec_version,dje_license_key +/load/this.ABOUT,/load/,.,AboutCode,0.11.0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,