| 70 | @click.option("--no-async", "fork_mode", flag_value=False) |
| 71 | @click.option("--download-only", "download_only", flag_value=True) |
| 72 | def scan( |
| 73 | uri, |
| 74 | verbose=None, |
| 75 | analyzer=None, |
| 76 | out_type=("text",), |
| 77 | min_score=None, |
| 78 | benchmark=False, |
| 79 | benchmark_sort="cumtime", |
| 80 | filter_tags=None, |
| 81 | fork_mode=False, |
| 82 | download_only=False, |
| 83 | ): |
| 84 | output_opts = { |
| 85 | "tags": filter_tags |
| 86 | } |
| 87 | if min_score is not None: |
| 88 | output_opts["min_score"] = min_score |
| 89 | |
| 90 | if verbose is not None: |
| 91 | output_opts["verbosity"] = verbose + 1 |
| 92 | |
| 93 | if not out_type: |
| 94 | out_type = ("text",) |
| 95 | |
| 96 | meta = { |
| 97 | "format": out_type, |
| 98 | "analyzers": analyzer, |
| 99 | "source": "cli", |
| 100 | "fork": fork_mode, |
| 101 | "output_opts": output_opts |
| 102 | } |
| 103 | if benchmark: |
| 104 | import cProfile, pstats, io |
| 105 | |
| 106 | pr = cProfile.Profile() |
| 107 | pr.enable() |
| 108 | meta["fork"] = False |
| 109 | else: |
| 110 | cProfile, pstats, pr, io = None, None, None, None |
| 111 | |
| 112 | try: |
| 113 | commands.scan_uri(uri, metadata=meta, download_only=download_only) |
| 114 | except exceptions.FeatureDisabled as e: |
| 115 | LOGGER.exception(e.args[0], exc_info=e) |
| 116 | click.secho(e.args[0], err=True, fg="red") |
| 117 | return sys.exit(2) |
| 118 | except exceptions.AuraException as e: |
| 119 | click.secho(e.args[0], err=True, fg='red') |
| 120 | return sys.exit(1) |
| 121 | |
| 122 | if pr: |
| 123 | pr.disable() |
| 124 | s = io.StringIO() |
| 125 | ps = pstats.Stats(pr, stream=s).sort_stats(benchmark_sort) |
| 126 | ps.print_stats(.1) |
| 127 | print(s.getvalue()) |
| 128 | |
| 129 | |