Skip to content

Commit

Permalink
Refactor: Added check for command library keys
Browse files Browse the repository at this point in the history
In order for the tool to be successful in retrieving package information
certain retrieval method listings need to be present in the command
library. Added a list of keys to command_lib.py that should be present.
If they are not present, the tool will not fail but will give a warning
message about adding the retrieval method.

- Added base image keys and package keys immutable sets
- Added a module to check for the keys
- Added error messages to the errors template

At this point the main executable does not work

Signed-off-by: Nisha K <[email protected]>
  • Loading branch information
Nisha K committed Feb 10, 2018
1 parent 5f0113d commit 4aab3ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 51 deletions.
54 changes: 18 additions & 36 deletions command_lib/command_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@
with open(os.path.abspath(snippet_file)) as f:
command_lib['snippets'] = yaml.safe_load(f)
# list of package information keys that the command library can accomodate
package_keys = ['names',
'versions',
'licenses',
'src_urls',
'srcs'
'name',
'version',
'src_url',
'license',
'deps',
'src']
base_keys = {'names', 'versions', 'licenses', 'src_urls', 'srcs'}
package_keys = {'name', 'version', 'src_url', 'license', 'src'}

# global logger
logger = logging.getLogger('ternlog')
Expand Down Expand Up @@ -115,6 +106,22 @@ def check_for_unique_package(package_list, package_name):
return pkg


def check_library_key(listing, key):
'''Given the command library listing, check to see if a key is present.
If the key is in the list of keys that should be in there then provide
a note'''
try:
return listing[key], ''
except KeyError as e:
if e in base_keys and e not in package_keys:
return {}, errors.no_listing_for_base_key.format(
listing_key=e)
if e in package_keys and e not in base_keys:
return {}, errors.no_listing_for_package_key.format(
listing_key=e)
return {}, errors.unsupported_listing_for_key.format(listing_key=e)


def set_command_attrs(command_obj):
'''Given the command object, move the install and remove listings to
subcommands and set the flags, then return True. If the command name
Expand All @@ -139,31 +146,6 @@ def set_command_attrs(command_obj):
break


def check_keys(listing):
'''Given a base listing or snippet package listing, check if the keys are
in the allowed list of keys. Return the list of keys that are present
and the error messages for the keys that are missing'''
report = ''
listing_key = ''
# assuming that the listing is either under base image and tag
# or under a package dictionary get the list of keys
cmd_lib_keys = listing.keys()
# find the intersection of the keys - this is what we will return
valid_keys = set(package_keys).intersection(set(cmd_lib_keys))
# find the keys that are not listed in the known list of keys
# these will have notes
note_keys = set(package_keys).difference(valid_keys)
for key in note_keys:
listing_key = key
report = report + errors.no_listing_for_key
# find the keys that are not valid keys and not in the allowed keys
warning_keys = set(cmd_lib_keys).difference(valid_keys)
for key in warning_keys:
listing_key = key
report = report + errors.unsupported_listing_key
return list(valid_keys), report


def get_packages_per_run(docker_run_command):
'''Given a Docker RUN instruction retrieve a dictionary of recognized
and unrecognized commands
Expand Down
36 changes: 21 additions & 15 deletions report/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
Error messages
'''

no_packages = '''Unable to recover packages for layer {layer}.
Consider either entering them manually or create a bash script to retrieve the
package in the command library.\n'''
no_version = '''No version for package {package}.
Consider either entering the version manually or creating a script to retrieve
it in the command library\n'''
no_license = '''No license for package {package}.
Consider either entering the license manually or creating a script to retrieve
it in the command library\n'''
no_src_url = '''No source url for package {package}.
Consider either entering the source url manually or creating a script to
retrieve it in the command library\n'''
env_dep_dockerfile = '''Docker build failed: {build_fail_msg} \n
Since the Docker image cannot be built, Tern will try to retrieve package
information from the Dockerfile itself.\n'''
no_packages = '''Unable to recover packages for layer {layer_id}. Consider ''' \
'''either entering them manually or create a bash script to retrieve ''' \
'''the package in the command library.\n'''
no_version = '''No version for package {package_name}. Consider either ''' \
'''entering the version manually or creating a script to retrieve ''' \
'''it in the command library\n'''
no_license = '''No license for package {package_name}. Consider either ''' \
'''entering the license manually or creating a script to retrieve it ''' \
'''in the command library\n'''
no_src_url = '''No source url for package {package_name}. Consider either ''' \
'''entering the source url manually or creating a script to retrieve ''' \
'''it in the command library\n'''
env_dep_dockerfile = '''Docker build failed: {build_fail_msg} \n Since ''' \
'''the Docker image cannot be built, Tern will try to retrieve ''' \
'''package information from the Dockerfile itself.\n'''
no_listing_for_base_key = '''No listing for key {listing_key}. ''' \
'''Consider adding this listing to command_lib/base.yml.\n'''
no_listing_for_package_key = '''No listing for key {listing_key}. ''' \
'''Consider adding this listing to command_lib/snippets.yml.\n'''
unsupported_listing_for_key = '''Unsupported listing for key ''' \
'''{listing_key}.\n'''

0 comments on commit 4aab3ad

Please sign in to comment.