Skip to content

Commit

Permalink
#413 support spdx_license_expression
Browse files Browse the repository at this point in the history
 * some code skeleton is added, but the code is not working
  • Loading branch information
chinyeungli committed Aug 19, 2020
1 parent 04a9ca7 commit b7d73ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
26 changes: 21 additions & 5 deletions src/attributecode/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ def transform(location, output, configuration, quiet, verbose): # NOQA

@click.option('--convert_to',
required=True,
type=click.Choice(['djc', 'spdx']),
help='Convert to \'djc\' (aka \'license_expression\') from \'spdx_license_expression\','
type=click.Choice(['scancode', 'spdx']),
help='Convert to \'scancode\' (aka \'license_expression\') from \'spdx_license_expression\','
' or convert to \'spdx\' (aka \'spdx_licesne_expression\') from \'license_expression\'.'
)

Expand Down Expand Up @@ -533,16 +533,32 @@ def map_lic(location, output, convert_to, quiet, verbose):
output_filename, output_file_extension = os.path.splitext(output)
if not input_file_extension == output_file_extension:
raise click.UsageError('ERROR: The file extension of the input and output are not the same.')
mapping(location, output, convert_to)

"""
from attributecode.gen import load_inventory

errors, abouts = load_inventory(
location=location,
base_dir=output
)

halt = False

if errors:
halt_erorrs = ['CRITICAL', 'ERROR']
for severity, message in errors:
if severities.get(severity) in halt_erorrs:
halt = True

if not halt:
map_errors, abouts = mapping(abouts, convert_to)

errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
if not quiet:
abouts_count = len(abouts)
msg = '{abouts_count} .ABOUT files generated in {output}.'.format(**locals())
click.echo(msg)
sys.exit(errors_count)
"""


######################################################################
# Error management
Expand Down
31 changes: 26 additions & 5 deletions src/attributecode/license_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from collections import Counter
from collections import OrderedDict
import io
import os
import json

import attr
Expand All @@ -35,8 +36,28 @@
from attributecode.util import replace_tab_with_spaces


def mapping(location, output, convert_to):
errors, abouts = load_inventory(
location=location,
base_dir=output
)
def mapping(abouts, convert_to):
errors = []
map_file_location = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'spdx_mapping.json')
with open(map_file_location, 'r') as f:
map_file = json.loads(f.read())

# The map_file is a dictionary list with spdx license as the key and djc as the value.
# If 'convert_to == spdx', we need to create a new mapping file which the djc as the key and spdx as the value.
if convert_to == 'djc':
mapping_file = map_file.copy()
else:
mapping_file = {}
for k, v in map_file.items():
mapping_file[v] = k

errors, abouts = process_mapping(abouts, mapping_file)


return errors, abouts


def process_mapping(abouts, mapping_file):
for about in abouts:
print(about)

0 comments on commit b7d73ad

Please sign in to comment.