Skip to content

Commit

Permalink
Fixed #69
Browse files Browse the repository at this point in the history
The function extract_dje_license is no longer writing to the output directly. Instead, it's returning the context.

New function "write licenses" is created to write the license.
  • Loading branch information
chinyeungli committed Jan 30, 2014
1 parent 9848cf5 commit e7728e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
28 changes: 20 additions & 8 deletions about_code_tool/genabout.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ def extract_dje_license(self, project_path, license_list, url, username, key):
"""
Extract license text from DJE
"""
license_context_list = []
for items in license_list:
gen_path = items[0]
license_key = items[1]
if '/' in gen_path:
if gen_path.startswith('/'):
gen_path = gen_path.partition('/')[2]
gen_license_path = join(project_path, gen_path, license_key) + '.LICENSE'
if not _exists(gen_license_path):
Expand All @@ -262,12 +263,22 @@ def extract_dje_license(self, project_path, license_list, url, username, key):
self.errors.append(Error('dje_license_key', license_key,
"Invalid 'dje_license_key'"))
else:
try:
with open(gen_license_path, 'wb') as output:
output.write(context.encode('utf8'))
except Exception as e:
self.errors.append(Error('Unknown', gen_license_path,
"Something is wrong."))
gen_path_context = []
gen_path_context.append(gen_license_path)
gen_path_context.append(context.encode('utf8'))
license_context_list.append(gen_path_context)
return license_context_list

def write_licenses(self, license_context_list):
for license in license_context_list:
gen_license_path = license[0]
license_context = license[1]
try:
with open(gen_license_path, 'wb') as output:
output.write(license_context)
except Exception as e:
self.errors.append(Error('Unknown', gen_license_path,
"Something is wrong."))

@staticmethod
def get_license_text_from_api(url, username, api_key, license_key):
Expand Down Expand Up @@ -680,7 +691,8 @@ def main(args, opts):
gen.write_output(formatted_output)

if dje_license_list:
gen.extract_dje_license(gen_location, dje_license_list, api_url, api_username, api_key)
license_list_context = gen.extract_dje_license(gen_location, dje_license_list, api_url, api_username, api_key)
gen.write_licenses(license_list_context)

gen.warnings_errors_summary(gen_location, verb_arg_num)

Expand Down
12 changes: 12 additions & 0 deletions about_code_tool/tests/test_genabout.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,15 @@ def test_copy_license_files_test_path_endswith_slash(self):
# deleting the temporary directory and its contents when done with it.
shutil.rmtree(tmp_path)

def test_write_licenses(self):
gen = genabout.GenAbout()
tmp_license = tempfile.NamedTemporaryFile(suffix='.LICENSE', delete=True)
tmp_license_context = 'This is a test.'
input_list = [[tmp_license.name, tmp_license_context]]
gen.write_licenses(input_list)
self.assertTrue(os.path.exists(tmp_license.name))
with open(tmp_license.name, "rU") as file_in:
context = ''
for line in file_in.readlines():
context = line
self.assertTrue(context == tmp_license_context)

0 comments on commit e7728e5

Please sign in to comment.