Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented the '--extract_license' option
 - Added a new supported key: 'dje_license_key'
 - Fill in the license_file, license_name, license_url fields if not
present
 - Update test code
  • Loading branch information
chinyeungli committed May 6, 2016
1 parent 9f85bf2 commit 87b5320
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
28 changes: 16 additions & 12 deletions about_code_tool/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
25 changes: 14 additions & 11 deletions about_code_tool/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions about_code_tool/tests/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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')]
Expand All @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion about_code_tool/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -670,6 +671,7 @@ def test_field_names(self):
'vcs_revision',
'checksum',
'spec_version',
'dje_license_key',
'f',
'g']
result = model.field_names(abouts)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions about_code_tool/tests/testdata/inventory/basic/expected.csv
Original file line number Diff line number Diff line change
@@ -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,,,,,,,
4 changes: 2 additions & 2 deletions about_code_tool/tests/testdata/load/expected.csv
Original file line number Diff line number Diff line change
@@ -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,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

0 comments on commit 87b5320

Please sign in to comment.