<commit_before>#!/usr/bin/env python

import sys
import os
import re
import anchore.anchore_utils

# main routine

try:
    config = anchore.anchore_utils.init_query_cmdline(sys.argv, "params: <package> <package> ...\nhelp: search input image(s) for specified <package> installations")
except Exception as err:
    print str(err)
    sys.exit(1)

if not config:
    sys.exit(0)

if len(config['params']) <= 0:
    print "Query requires input: <packageA> <packageB> ..."

warns = list()
outlist = list()
outlist.append(["ImageID", "Repo/Tag", "QueryParam", "Package", "Version"])

try:
    pkgfile = '/'.join([config['dirs']['analyzerdir'], 'package_list', 'pkgs.all'])
    FH=open(pkgfile, 'r')

    pkgs = anchore.anchore_utils.read_kvfile_todict(pkgfile)
    
    if len(pkgs) <= 0 or (len(pkgs) == 1 and 'Unknown' in pkgs):
        warns.append(config['meta']['shortId'] + "(" + config['meta']['humanname'] + ") Image has been analyzed but package data is empty - nothing to search")

    for p in pkgs.keys():
        pkgs[p + "-" + pkgs[p]] = pkgs[p]

    for pkg in config['params']:
        
        if pkg in pkgs:
            version = pkgs[pkg]
            outlist.append([config['meta']['shortId'], config['meta']['humanname'], pkg, pkg, version])
        else:
            #outlist.append([pkg, "NOMATCH", "NOMATCH"])
            pass

except:
    #outlist.append(["NOMATCH", "NOMATCH", "NOMATCH"])
    pass

if len(outlist) < 2:
    #outlist.append(["NOMATCH", "NOMATCH", "NOMATCH"])
    pass

anchore.anchore_utils.write_kvfile_fromlist(config['output'], outlist)

if len(warns) > 0:
    anchore.anchore_utils.write_plainfile_fromlist(config['output_warns'], warns)

sys.exit(0)<commit_msg>add ability for queries to throw warnings which will be output to the user, and will cause anchore to exit 2<commit_after>#!/usr/bin/env python

import sys
import os
import re
import anchore.anchore_utils

# main routine

try:
    config = anchore.anchore_utils.init_query_cmdline(sys.argv, "params: <package> <package> ...\nhelp: search input image(s) for specified <package> installations")
except Exception as err:
    print str(err)
    sys.exit(1)

if not config:
    sys.exit(0)

if len(config['params']) <= 0:
    print "Query requires input: <packageA> <packageB> ..."

warns = list()
outlist = list()
outlist.append(["ImageID", "Repo/Tag", "QueryParam", "Package", "Version"])

try:
    pkgfile = '/'.join([config['dirs']['analyzerdir'], 'package_list', 'pkgs.all'])
    FH=open(pkgfile, 'r')

    pkgs = anchore.anchore_utils.read_kvfile_todict(pkgfile)
    
    if len(pkgs) <= 0 or (len(pkgs) == 1 and 'Unknown' in pkgs):
        warns.append(config['meta']['shortId'] + "(" + config['meta']['humanname'] + ") Image has been analyzed but package data is empty - nothing to search")

    for p in pkgs.keys():
        pkgs[p + "-" + pkgs[p]] = pkgs[p]

    for pkg in config['params']:
        
        if pkg in pkgs:
            version = pkgs[pkg]
            outlist.append([config['meta']['shortId'], config['meta']['humanname'], pkg, pkg, version])
        else:
            #outlist.append([pkg, "NOMATCH", "NOMATCH"])
            pass

except:
    #outlist.append(["NOMATCH", "NOMATCH", "NOMATCH"])
    pass

if len(outlist) < 2:
    #outlist.append(["NOMATCH", "NOMATCH", "NOMATCH"])
    pass

anchore.anchore_utils.write_kvfile_fromlist(config['output'], outlist)

# disable for now
#if len(warns) > 0:
#    anchore.anchore_utils.write_plainfile_fromlist(config['output_warns'], warns)

sys.exit(0)
{'fraction_non_alphanumeric': 0.16463241555492478, 'fraction_numerical': 0.004825432869713313, 'mean_word_length': 3.655217965653897, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003630', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1210, 'n_words': 292}