Skip to content

Commit

Permalink
plugins/semgrep: fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyw committed Mar 21, 2024
1 parent 6ee99fd commit 370463a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions py/plugins/semgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# You should have received a copy of the GNU General Public License
# along with csmock. If not, see <http://www.gnu.org/licenses/>.

"""
Semgrep client plugin
"""
import os

# disable metrics to be sent to semgrep cloud
Expand All @@ -34,7 +37,10 @@
SEMGREP_SCAN_LOG = "/builddir/semgrep-scan.log"


class PluginProps:
class PluginProps: # pylint: disable=too-few-public-methods
"""
Props of the plugin
"""
def __init__(self):
self.description = (
"A fast, open-source, static analysis engine for finding bugs, "
Expand All @@ -45,13 +51,16 @@ def __init__(self):


class Plugin:
"""
Semgrep static analysis engine plugin
"""
def __init__(self):
self.enabled = False

def get_props(self):
def get_props(self): # pylint: disable=missing-function-docstring
return PluginProps()

def enable(self):
def enable(self): # pylint: disable=missing-function-docstring
self.enabled = True

def init_parser(self, parser):
Expand Down Expand Up @@ -80,13 +89,12 @@ def init_parser(self, parser):
help="space-separated list of additional options passed to the 'semgrep scan' command",
)

def handle_args(self, parser, args, props):
def handle_args(self, parser, args, props): # pylint: disable=too-many-statements,missing-function-docstring
if not self.enabled:
return

if not args.semgrep_rules_repo:
parser.error("'--semgrep-rules-repo' is required to run semgrep scan")
return 1

# install semgrep cli and download semgrep rules
def prepare_semgrep_runtime_hook(results, props):
Expand Down Expand Up @@ -138,7 +146,7 @@ def prepare_semgrep_runtime_hook(results, props):

props.pre_mock_hooks += [prepare_semgrep_runtime_hook]

def scan_hook(results, mock, props):
def scan_hook(results, mock, props): # pylint: disable=unused-argument
semgrep_lib_dir = os.path.join(results.tmpdir, "semgrep_lib")
semgrep_prefix = f"env PATH={semgrep_lib_dir}/bin:$PATH PYTHONPATH={semgrep_lib_dir}"
# assuming semgrep rules are located under the 'rules' directory
Expand Down Expand Up @@ -204,8 +212,8 @@ def filter_hook(results):
# in its rules path. The following sed command strips suspicious path prefixes by removing
# any sequence of non left-square-bracket characters preceding '{tmp_path}'
cmd = (
f"csgrep {src} --mode=json --strip-path-prefix {chroot_root_path}{SEMGREP_SCAN_DIR}/"
f" | sed 's|[^\[]*{tmp_path}||' > {dst}"
fr"csgrep {src} --mode=json --strip-path-prefix {chroot_root_path}{SEMGREP_SCAN_DIR}/ "
fr"| sed 's|[^\[]*{tmp_path}||' > {dst}" # pylint: disable=W1401
)

return results.exec_cmd(cmd, shell=True)
Expand Down

0 comments on commit 370463a

Please sign in to comment.