def main(): clp = command_line.create_basic_clp() clp["output_options"].add_argument( "--worst-offenders", default=10, type=int, help=("Produce a table of the worst offenders for each metric." " By default this is 10; setting it to 0 disables this" " feature.")) clp["output_options"].add_argument( "--ci", default=False, action="store_true", help=("Do not print any metrics report, only notify about violations." "This is the intended way to run in a CI environment.")) clp["output_options"].add_argument( "--text", default=None, metavar="FILE", help=("Print plain-text metrics summary to the given file. By" " default we print the summary to standard output.")) clp["output_options"].add_argument( "--html", default=None, metavar="FILE", help=("Write HTML metrics report to the file.")) options = command_line.parse_args(clp) if options.text: if os.path.exists(options.text) and not os.path.isfile(options.text): clp["ap"].error("cannot write metrics to %s, it exists and is" " not a file" % options.text) if options.html: if os.path.exists(options.html) and not os.path.isfile(options.html): clp["ap"].error("cannot write metrics to %s, it exists and is" " not a file" % options.text) if options.text and options.html: clp["ap"].error("the text and html options are mutually exclusive") if options.ci and (options.text or options.html): clp["ap"].error("the CI mode and and text/html options are mutually " "exclusive") if options.worst_offenders < 0: clp["ap"].error("the worst-offender option cannot be negative") mh = Message_Handler("metric") mh.show_context = not options.brief mh.show_style = False mh.autofix = False metric_backend = MH_Metric(options) command_line.execute(mh, options, {}, metric_backend)
def check(options): # Ignore on OSX if platform.system() in ['Darwin']: logging.info('Disabling swap not available on %s.' % (platform.system())) return True # Check on the current swap values response = command_line.execute(check_command) if response.stderr: return response.report_failure() found_expected_output = False for line in response.stdout.split('\n'): # Look for line with swap information if line.split()[0] == 'Swap:': found_expected_output = True # Ensure that the total swap is 0 if len(line.split()) > 1 and line.split()[1].isdigit() and int( line.split()[1]) > 0: if options.fix: return fix() else: logging.error('Swap not disabled.') return False if not found_expected_output: logging.warn('Did not find expected output for `%s`.' % check_command) return False logging.info('Swap disabled.') return True
def fix(): response = command_line.execute(set_command) if response.stderr: return response.report_failure() logging.info('Disabled swap.') return True
def fix(current_count_value=None): # Ensure directory that houses sysctl.conf exists and is writable sysctl_directory = os.path.dirname(sysctl_file) if not os.access(sysctl_directory, os.F_OK) or not os.access( sysctl_directory, os.W_OK): logging.error( 'Permission to write file %s to %s denied. Please check permissions or run as sudo.' % (os.path.basename(sysctl_file), sysctl_directory)) return False # Append persistent setting try: f = open(sysctl_file, 'a') f.write('vm.max_map_count = %s\n' % map_count_value) finally: f.close() # Attempt to set vm.max_map_count response = command_line.execute(set_command) # If attempt failed if response.stderr: return response.report_failure() logging.info('Set vm.max_map_count to %s from %s.' % (map_count_value, current_count_value)) return True
def check(options): # Ignore on OSX if platform.system() in ['Darwin']: logging.info('`sysctl` not available on %s.' % (platform.system())) return True # Attempt to read vm.max_map_count response = command_line.execute(read_command) # If stderr, or vm.max_map_count is not ideal, fix() if response.stderr: return response.report_failure() else: current_count_value = int(response.stdout.split()[-1]) if current_count_value < map_count_value: if options.fix: return fix(current_count_value) else: logging.error('vm.max_map_count is set to %s.' % current_count_value) logging.error('Suggested value is %s.' % map_count_value) return False logging.info('vm.max_map_count set correctly.') return True
def main(): rule_set = get_rules() clp = command_line.create_basic_clp() clp["ap"].add_argument("--fix", action="store_true", default=False, help=("Automatically fix issues where the fix" " is obvious")) clp["ap"].add_argument("--process-slx", action="store_true", default=False, help=("Style-check (but not yet auto-fix) code" " inside SIMULINK models. This option is" " temporary, and will be removed in" " future once the feature is good enough" " to be enabled by default.")) # Extra output options clp["output_options"].add_argument( "--html", default=None, help="Write report to given file as HTML") clp["output_options"].add_argument( "--no-style", action="store_true", default=False, help="Don't show any style message, only show warnings and errors.") # Debug options clp["debug_options"].add_argument( "--debug-dump-tree", default=None, metavar="FILE", help="Dump text-based parse tree to given file") clp["debug_options"].add_argument("--debug-validate-links", action="store_true", default=False, help="Debug option to check AST links") clp["debug_options"].add_argument("--debug-cfg", action="store_true", default=False, help="Build CFG for every function") style_option = clp["ap"].add_argument_group("rule options") # Add any parameters from rules for rule_kind in rule_set: for rule in rule_set[rule_kind]: rule_params = getattr(rule, "parameters", None) if not rule_params: continue for p_name in rule_params: style_option.add_argument("--" + p_name, **rule_params[p_name]) style_option.add_argument("--copyright-entity", metavar="STR", default=[], nargs="+", help=("Add (company) name to check for in " "Copyright notices. Can be specified " "multiple times.")) options = command_line.parse_args(clp) if options.html: if os.path.exists(options.html) and not os.path.isfile(options.html): clp["ap"].error("Cannot write to %s: it is not a file" % options.html) mh = HTML_Message_Handler("style", options.html) else: mh = Message_Handler("style") mh.show_context = not options.brief mh.show_style = not options.no_style mh.autofix = options.fix extra_options = { "fd_tree": None, "rule_set": rule_set, } if options.debug_dump_tree: extra_options["fd_tree"] = open(options.debug_dump_tree, "w") style_backend = MH_Style() command_line.execute(mh, options, extra_options, style_backend, options.process_slx) if options.debug_dump_tree: extra_options["fd_tree"].close()