Skip to content

Commit

Permalink
Fixed #116
Browse files Browse the repository at this point in the history
It's now able to copy all the *_file field files such as license_text_file, notice_file etc...
  • Loading branch information
chinyeungli committed Jul 11, 2014
1 parent a8234e3 commit 7ade9bc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 70 deletions.
8 changes: 4 additions & 4 deletions README.rst
Expand Up @@ -128,11 +128,11 @@ information will be saved to the CSV file named "thirdparty_about.csv".
--all_in_one Generate all the ABOUT files in the [output_path] without
any project structure
--copy_license=COPY_LICENSE
Copy the 'license_text_file' from the project to the generated location

--copy_files=COPY_FILES
Copy the '*_file' from the project to the generated location
Project path - Project path

--license_text_location=LICENSE_TEXT_LOCATION
Copy the 'license_text_file' from the directory to the generated location
License path - License text files path
Expand Down
15 changes: 8 additions & 7 deletions USAGE
Expand Up @@ -70,8 +70,8 @@ Syntax
--all_in_one Generate all the ABOUT files in the [output_path] without
any project structure

--copy_license=COPY_LICENSE
Copy the 'license_text_file' from the project to the generated location
--copy_files=COPY_FILES
Copy the '*_file' from the project to the generated location
Project path - Project path

--license_text_location=LICENSE_TEXT_LOCATION
Expand Down Expand Up @@ -114,19 +114,20 @@ Options

$ python genabout.py --all_in_one <input path> <output path>

--copy_license
--copy_files

Copy the license files to the generated location based on the
'license_text_file' value in the input from the project
Copy the files to the generated location based on the
*_file value in the input from the project

Purpose of this option is for users who want to generate ABOUT files
in a different location other than the project side by side with the code.

For instance, the project is located at /home/project/, and users want to
generate ABOUT files to /home/about/ and also want to copy the licenses from
generate ABOUT files to /home/about/ and also want to copy the
'license_text_file' and 'notice_text_file' from
/home/project/ to /home/about/

$ python genabout.py --copy_license=/home/project/ <input path> /home/about/
$ python genabout.py --copy_files=/home/project/ <input path> /home/about/

--license_text_location

Expand Down
106 changes: 61 additions & 45 deletions about_code_tool/genabout.py
Expand Up @@ -196,34 +196,50 @@ def get_non_supported_fields(input_list):
first_line = input_list[0]
return [field for field in first_line.keys() if not field in SUPPORTED_FIELDS]

def verify_license_files(self, input_list, project_dir, license_in_project):
def verify_files_existance(self, input_list, project_dir, file_in_project):
"""
Verify the existence of the 'license text file'
"""
license_files_list = []
files_list = []
# Get all the dictionary keys
column_keys = input_list[0].keys()

# Get all the keys that ending with _file except for the 'about_file
file_keys = []
for key in column_keys:
if '_file' in key and key != 'about_file':
file_keys.append(key)

for line in input_list:
try:
if line['license_text_file']:
license_file = line['license_text_file']
for file_key in file_keys:
if line[file_key]:
file_path_list = []
file_value = []
file_location = line['about_file']
if file_location.startswith('/'):
file_location = file_location.partition('/')[2]
#if file_location.endswith('/'):
# file_location = file_location.rpartition('/')[0]
about_parent_dir = dirname(file_location)
if license_in_project:
license_file_path = join(project_dir, about_parent_dir, license_file)
else:
license_file_path = join(project_dir, license_file)
if _exists(license_file_path):
license_files_list.append((license_file_path, about_parent_dir))
if file_in_project:
if '\n' in line[file_key]:
file_value = line[file_key].split('\n')
else:
file_value.append(line[file_key])
for value in file_value:
file_path_list.append(join(project_dir, about_parent_dir, value))
else:
self.warnings.append(Warn('license_text_file', license_file_path, "License does not exist."))
except Exception as e:
print(repr(e))
print("The input does not have the 'license_text_file' key which is required.")
raise Exception(repr(e))
return license_files_list
if '\n' in line[file_key]:
file_value = line[file_key].split('\n')
else:
file_value.append(line[file_key])
for value in file_value:
file_path_list.append(join(project_dir, value))

for path in file_path_list:
if _exists(path):
files_list.append((path, about_parent_dir))
else:
self.warnings.append(Warn(file_key, path, "File does not exist."))
return files_list

def request_license_data(self, url, username, api_key, license_key):
"""
Expand Down Expand Up @@ -278,15 +294,15 @@ def request_license_data(self, url, username, api_key, license_key):
return data

@staticmethod
def copy_license_files(gen_location, license_list):
def copy_files(gen_location, files_list):
"""
Copy the 'license_text_file' into the gen_location
Copy the files into the gen_location
"""
for license_path, component_path in license_list:
output_license_path = join(gen_location, component_path)
if not _exists(output_license_path):
makedirs(output_license_path)
shutil.copy2(license_path, output_license_path)
for file_path, component_path in files_list:
output_file_path = join(gen_location, component_path)
if not _exists(output_file_path):
makedirs(output_file_path)
shutil.copy2(file_path, output_file_path)

def write_licenses(self, license_context_list):
for gen_license_path, license_context in license_context_list:
Expand Down Expand Up @@ -513,8 +529,8 @@ def _exists(file_path):
any project structure
"""

COPY_LICENSE_HELP = """\
Copy the 'license_text_file' from the project to the generated location
COPY_FILES_HELP = """\
Copy the '*_file' from the project to the generated location
Project path - Project path
"""

Expand Down Expand Up @@ -547,7 +563,7 @@ def main(parser, options, args):
verbosity = options.verbosity
action = options.action
all_in_one = options.all_in_one
copy_license_path = options.copy_license
copy_files_path = options.copy_files
license_text_path = options.license_text_location
mapping_config = options.mapping
extract_license = options.extract_license
Expand Down Expand Up @@ -575,8 +591,8 @@ def main(parser, options, args):
else:
action_num = action

if copy_license_path:
if not _exists(copy_license_path):
if copy_files_path:
if not _exists(copy_files_path):
print("The project path does not exist.")
sys.exit(errno.EINVAL)

Expand Down Expand Up @@ -656,20 +672,20 @@ def main(parser, options, args):
if ignored_fields_list:
input_list = gen.get_only_supported_fields(input_list, ignored_fields_list)

if copy_license_path:
if not isdir(copy_license_path):
print("The '--copy_license' <project_path> must be a directory.")
print("'--copy_license' is skipped.")
if copy_files_path:
if not isdir(copy_files_path):
print("The '--copy_files' <project_path> must be a directory.")
print("'--copy_files' is skipped.")
else:
#if not copy_license_path.endswith('/'):
# copy_license_path += '/'
project_parent_dir = dirname(copy_license_path)
#if not copy_files_path.endswith('/'):
# copy_files_path += '/'
project_parent_dir = dirname(copy_files_path)
licenses_in_project = True
license_list = gen.verify_license_files(input_list, project_parent_dir, licenses_in_project)
license_list = gen.verify_files_existance(input_list, project_parent_dir, licenses_in_project)
if not license_list:
print("None of the 'license_text_file' is found. '--copy_license' is ignored.")
print("None of the file is found. '--copy_files' is ignored.")
else:
gen.copy_license_files(output_path, license_list)
gen.copy_files(output_path, license_list)

if license_text_path:
if not isdir(license_text_path):
Expand All @@ -680,11 +696,11 @@ def main(parser, options, args):
# license_text_path += '/'
license_dir = dirname(license_text_path)
licenses_in_project = False
license_list = gen.verify_license_files(input_list, license_dir, licenses_in_project)
license_list = gen.verify_files_existance(input_list, license_dir, licenses_in_project)
if not license_list:
print("None of the 'license_text_file' is found. '--copy_license' is ignored.")
print("None of the file is found. '--copy_files' is ignored.")
else:
gen.copy_license_files(output_path, license_list)
gen.copy_files(output_path, license_list)

if extract_license:
if not api_url or not api_username or not api_key:
Expand Down Expand Up @@ -768,7 +784,7 @@ def format_option(self, option):
parser.add_option('--verbosity', type=int, help=VERBOSITY_HELP)
parser.add_option('--action', type=int, help=ACTION_HELP)
parser.add_option('--all_in_one', action='store_true', help=ALL_IN_ONE_HELP)
parser.add_option('--copy_license', type='string', help=COPY_LICENSE_HELP)
parser.add_option('--copy_files', type='string', help=COPY_FILES_HELP)
parser.add_option('--license_text_location', type='string', help=LICENSE_TEXT_LOCATION_HELP)
parser.add_option('--mapping', action='store_true', help=MAPPING_HELP)
parser.add_option(
Expand Down
28 changes: 14 additions & 14 deletions about_code_tool/tests/test_genabout.py
Expand Up @@ -401,60 +401,60 @@ def test_format_output_with_continuation(self):
self.assertFalse(gen.warnings, "No warnings should be returned.")
self.assertFalse(gen.errors, "No errors should be returned.")

def test_verify_license_files_exist(self):
def test_verify_files_existance_exist(self):
gen = genabout.GenAbout()
input_list = [{'version': '0.8.1', 'about_file': '/TESTCASE/',
'license_text_file': 'apache2.LICENSE',
'name': 'ABOUT tool', 'about_resource': '.'}]
path = '.'
expected_list = [(join('.', 'apache2.LICENSE'), 'TESTCASE')]
output = gen.verify_license_files(input_list, path, False)
output = gen.verify_files_existance(input_list, path, False)
self.assertEqual(expected_list, output)
self.assertFalse(gen.warnings, "No warnings should be returned.")
self.assertFalse(gen.errors, "No errors should be returned.")

def test_verify_license_files_exist_license_in_project(self):
def test_verify_files_existance_exist_license_in_project(self):
gen = genabout.GenAbout()
input_list = [{'version': '0.8.1', 'about_file': '.',
'license_text_file': 'apache2.LICENSE',
'name': 'ABOUT tool', 'about_resource': '.'}]
path = '.'
expected_list = [(join('.', 'apache2.LICENSE'), '')]
output = gen.verify_license_files(input_list, path, True)
output = gen.verify_files_existance(input_list, path, True)
self.assertEqual(expected_list, output)
self.assertFalse(gen.warnings, "No warnings should be returned.")
self.assertFalse(gen.errors, "No errors should be returned.")

def test_verify_license_files_not_exist(self):
def test_verify_files_existance_not_exist(self):
gen = genabout.GenAbout()
input_list = [{'version': '0.8.1', 'about_file': '/about.py.ABOUT',
'license_text_file': 'not_exist.LICENSE.txt',
'name': 'ABOUT tool', 'about_resource': '.'}]
path = '.'
expected_list = []
output = gen.verify_license_files(input_list, path, False)
output = gen.verify_files_existance(input_list, path, False)
self.assertTrue(expected_list == output)
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warning.")
self.assertFalse(gen.errors, "No errors should be returned.")

def test_verify_license_files_not_exist_license_in_project(self):
def test_verify_files_existance_not_exist_license_in_project(self):
gen = genabout.GenAbout()
input_list = [{'version': '0.8.1', 'about_file': '/TESTCASE/',
'license_text_file': 'not_exist.LICENSE.txt',
'name': 'ABOUT tool', 'about_resource': '.'}]
path = '.'
expected_list = []
output = gen.verify_license_files(input_list, path, False)
output = gen.verify_files_existance(input_list, path, False)
self.assertTrue(expected_list == output)
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warning.")
self.assertFalse(gen.errors, "No errors should be returned.")

def test_verify_license_files_no_key(self):
def test_verify_files_existance_no_key(self):
gen = genabout.GenAbout()
input_list = [{'version': '0.8.1', 'about_file': '/about.py.ABOUT',
'name': 'ABOUT tool', 'about_resource': '.'}]
path = '.'
self.assertRaises(Exception, gen.verify_license_files, input_list, path)
self.assertRaises(Exception, gen.verify_files_existance, input_list, path)

def test_gen_license_list_license_text_file_no_value(self):
gen = genabout.GenAbout()
Expand All @@ -476,25 +476,25 @@ def test_gen_license_list_no_license_text_file_key(self):
output = gen.gen_license_list(input_list)
self.assertTrue(expected_list == output)

def test_copy_license_files_test_path_not_endswith_slash(self):
def test_copy_files_test_path_not_endswith_slash(self):
gen = genabout.GenAbout()
input_list = [('apache2.LICENSE', '.')]
expected_list = ['apache2.LICENSE']
project_path = os.path.abspath('.')
tmp_path = tempfile.mkdtemp()
gen.copy_license_files(tmp_path, input_list)
gen.copy_files(tmp_path, input_list)
self.assertTrue(expected_list == os.listdir(tmp_path))
# According to the doc, the user of mkdtemp() is responsible for
# deleting the temporary directory and its contents when done with it.
shutil.rmtree(tmp_path)

def test_copy_license_files_test_path_endswith_slash(self):
def test_copy_files_test_path_endswith_slash(self):
gen = genabout.GenAbout()
input_list = [('apache2.LICENSE', '.')]
expected_list = ['apache2.LICENSE']
project_path = os.path.abspath('.')
tmp_path = tempfile.mkdtemp() + '/'
gen.copy_license_files(tmp_path, input_list)
gen.copy_files(tmp_path, input_list)
self.assertTrue(expected_list == os.listdir(tmp_path))
# According to the doc, the user of mkdtemp() is responsible for
# deleting the temporary directory and its contents when done with it.
Expand Down

0 comments on commit 7ade9bc

Please sign in to comment.