Skip to content

Commit

Permalink
Separate YAML formatting into its own plugin
Browse files Browse the repository at this point in the history
This commit separates the yaml reporting code currently under Tern's
core code and puts it in its own plugin that Stevedore can utilize.
Specifically, the following changes were made:

1) Add yaml entrypoint to setup.cfg.

2) Remove the -y and --yaml command line option in tern/__main__.py.
   Moving forward we will utilize Stevedore to manage the plugin
   selection at runtime. Similar to the spdxtagvalue plugin, users can
   invoke the json reporting format by using "-m yaml" on the
   command line.

3) Copy the body of generate_yaml() in tern/report/report.py and move
   it under generate() in tern/formats/json/generator.py. Also copy
   print_yaml_report() function to tern/formats/json/generator.py so
   all the yaml-related formatting code is contained within the plugin.

4) Remove code corresponding to the -y command line option in
   tern/report/report.py.

5) Remove yaml_file constant in tern/utils/constants.py as it is no
   longer used.

6) Update the circleci test to run the yaml reporting using
   "-m yaml" instead of the deprecated -y command line option.

Resolves #370

Signed-off-by: Rose Judge <[email protected]>
  • Loading branch information
rnjudge committed Aug 28, 2019
1 parent 1a69aef commit e596ff9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ci/test_files_touched.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
# tern/report
re.compile('tern/report'): [
'tern -l report -i golang:alpine',
'tern -l report -y -i photon:3.0',
'tern -l report -m yaml -i photon:3.0',
'tern -l report -s -i photon:3.0',
'tern -l report -m json -i photon:3.0',
'tern -l report -m spdxtagvalue -i photon:3.0',
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ include_package_data = True
tern.formats =
spdxtagvalue = tern.formats.spdx.spdxtagvalue.generator:SpdxTagValue
json = tern.formats.json.generator:JSON
yaml = tern.formats.yaml.generator:YAML
console_scripts =
tern = tern.__main__:main

Expand Down
4 changes: 1 addition & 3 deletions tern/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def main():
metavar='REPORT_MODULE',
help="Format the report using one of the "
"available formats: "
"spdxtagvalue, json")
parser_report.add_argument('-y', '--yaml', action='store_true',
help="Create a report in yaml format")
"spdxtagvalue, json, yaml")
parser_report.add_argument('-f', '--file', default=None,
help="Write the report to a file; "
"If no file is given the default file in "
Expand Down
4 changes: 4 additions & 0 deletions tern/formats/yaml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause
30 changes: 30 additions & 0 deletions tern/formats/yaml/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

"""
YAML document generator
"""

import yaml
from tern.report import formats
from tern.utils.general import get_git_rev_or_version
from tern.formats import generator


def print_yaml_report(image):
'''Given an image object, create a yaml report'''
image_dict = {}
image_dict.update({'image': image.to_dict()})
return yaml.dump(image_dict, default_flow_style=False)


class YAML(generator.Generate):
def generate(self, image_obj_list):
'''Generate a yaml report'''
report = formats.disclaimer_yaml.format(
version_info=get_git_rev_or_version())
for image in image_obj_list:
report = report + print_yaml_report(image)
return report
9 changes: 0 additions & 9 deletions tern/report/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
Functions to generate content for the report
"""

import yaml

from tern.command_lib import command_lib
from tern.report import formats
from tern.utils.general import get_git_rev_or_version
Expand Down Expand Up @@ -131,10 +129,3 @@ def print_summary_report(image):
notes = notes + print_package(package, '')
notes = notes + formats.package_demarkation
return notes


def print_yaml_report(image):
'''Given an image object, create a yaml report'''
image_dict = {}
image_dict.update({'image': image.to_dict()})
return yaml.dump(image_dict, default_flow_style=False)
14 changes: 0 additions & 14 deletions tern/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def write_report(report, args):
'''Write the report to a file'''
if args.file:
file_name = args.file
elif args.yaml:
file_name = constants.yaml_file
else:
file_name = constants.report_file
with open(file_name, 'w') as f:
Expand Down Expand Up @@ -303,8 +301,6 @@ def generate_report(args, *images):
'''Generate a report based on the command line options'''
if args.report_format:
return generate_format(images, args.report_format)
if args.yaml:
return generate_yaml(images)
if args.summary:
return generate_verbose(True, images)
return generate_verbose(False, images)
Expand All @@ -325,16 +321,6 @@ def generate_verbose(is_summary, images):
return report


def generate_yaml(images):
'''Generate a yaml report'''
logger.debug('Creating YAML report...')
report = formats.disclaimer_yaml.format(
version_info=general.get_git_rev_or_version())
for image in images:
report = report + content.print_yaml_report(image)
return report


def generate_format(images, format_string):
'''Generate a report in the format of format_string given one or more
image objects. Here we will load the required module and run the generate
Expand Down
1 change: 0 additions & 1 deletion tern/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@
mergedir = 'mergedir'
# report file
report_file = 'report.txt'
yaml_file = 'report.yml'

0 comments on commit e596ff9

Please sign in to comment.