/usr/bin/click-review is in click-reviewers-tools 0.42.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #!/usr/bin/python3
from clickreviews import modules
import argparse
import json
import os
import sys
import textwrap
def print_findings(results, description):
'''
Print a summary of the issues found.
'''
if not description or not results:
return ''
print(description)
print(''.center(len(description), '-'))
for key in sorted(results.keys()):
print(' - %s' % key)
print('\t%s' % results[key]['text'])
if 'link' in results[key]:
print('\t%s' % results[key]['link'])
class Results(object):
results = {}
errors = {}
warnings = {}
info = {}
rc = 0
def __init__(self, args):
self.args = args
self.pkg_fn = self.args.filename
self.modules = modules.get_modules()
def _sumarise_results(self):
for module in self.results:
for key in self.results[module]['error']:
self.errors[key] = self.results[module]['error'][key]
for key in self.results[module]['warn']:
self.warnings[key] = self.results[module]['warn'][key]
if self.args.verbose:
for key in self.results[module]['info']:
self.info[key] = self.results[module]['info'][key]
def _complete_report(self):
self._sumarise_results()
if self.args.json:
print(json.dumps(self.results, sort_keys=True, indent=2,
separators=(',', ': ')))
else:
print_findings(self.errors, 'Errors')
print_findings(self.warnings, 'Warnings')
if self.args.verbose:
print_findings(self.info, 'Info')
if self.warnings or self.errors:
print('%s: FAIL' % self.args.filename)
else:
print('%s: pass' % self.args.filename)
if self.errors:
self.rc = 2
elif self.warnings:
self.rc = 3
def _report_module(self, section):
'''
This is currently only used in the --sdk option.
It will print the output for each section when it's
available. This will prevent the SDK from having to wait
until all checks have been run.
'''
output = self.results[section]
print('= %s =' % section)
print(json.dumps(output, sort_keys=True, indent=2,
separators=(',', ': ')))
if output['error'] or output['warn']:
self.rc = 1
def _run_module_checks(self, module, overrides):
# What we are doing here is basically what all the
# ./bin/click-check-* scripts do as well, so for
# example something like:
#
# review = cr_push_helper.ClickReviewPushHelper(sys.argv[1])
# review.do_checks()
# rc = review.do_report()
#
section = module.replace('cr_', 'click,snap.v1_')
section = section.replace('sr_', 'snap.v2_')
review = modules.init_main_class(module, self.pkg_fn,
overrides=overrides)
if review:
review.do_checks()
self.results[section] = review.click_report
return section
return None
def run_all_checks(self, overrides):
if self.args.sdk:
for module in self.modules:
section = self._run_module_checks(module, overrides)
if section:
self._report_module(section)
else:
for module in self.modules:
self._run_module_checks(module, overrides)
self._complete_report()
def main():
parser = argparse.ArgumentParser(
prog='click-review',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Check a click or snap package for errors',
epilog=textwrap.dedent('''\
RETURN CODES
0 found no errors or warnings
1 checks not run due to fatal error
2 found errors and/or warnings
3 found warnings
'''))
parser.add_argument('filename', type=str,
help='file to be inspected')
parser.add_argument('overrides', type=str,
nargs='?',
help='overrides to apply (eg, framework, security '
'policies, etc)',
default=None)
parser.add_argument('-v', '--verbose',
help='increase output verbosity',
action='store_true')
parser.add_argument('--json', help='print json output',
action='store_true')
parser.add_argument('--sdk',
help='use output format suitable for the Ubuntu SDK',
action='store_true')
args = parser.parse_args()
if not os.path.exists(args.filename):
print(".click file '%s' does not exist." % args.filename)
sys.exit(1)
results = Results(args)
if not results.modules:
print("No 'clickreviews' modules found.")
sys.exit(1)
overrides = None
if args.overrides:
overrides = json.loads(args.overrides)
results.run_all_checks(overrides)
sys.exit(results.rc)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Aborted.")
sys.exit(1)
|