Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
add dependency check
Browse files Browse the repository at this point in the history
  • Loading branch information
frennkie committed May 3, 2017
1 parent aa33c4b commit 43064f9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
63 changes: 63 additions & 0 deletions viper/common/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
# This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.

import sys
import argparse
from distutils.spawn import find_executable
import pkg_resources

import logging

import viper.common.out as out
from viper.core.config import console_output
from viper.common.exceptions import ArgumentErrorCallback

log = logging.getLogger('viper')


class ArgumentParser(argparse.ArgumentParser):
def print_usage(self):
Expand All @@ -31,9 +39,64 @@ class Module(object):
authors = []
output = []

min_python_version = (2, 7)
dependency_list_python = []
dependency_list_system = []

def __init__(self):
self.parser = ArgumentParser(prog=self.cmd, description=self.description)

def check(self):
min_python_version = self._check_min_python_version()
dep_python = self._check_dependencies_python()
dep_system = self._check_dependencies_system()
if min_python_version and dep_python and dep_system:
return True
else:
return False

def _check_min_python_version(self):
if sys.version_info >= self.min_python_version:
log.debug("{}: Python Version ok".format(self.__class__.__name__))
return True
else:
log.warning("{}: Python Version NOT ok".format(self.__class__.__name__))
return False

def _check_dependencies_python(self):
if not self.dependency_list_python:
return True

missing = []

for item in self.dependency_list_python:
try:
pkg_resources.require(item)
except pkg_resources.DistributionNotFound as err:
log.debug("{}: Missing Python dependency: {}".format(self.__class__.__name__, err))
missing.append(item)
except pkg_resources.VersionConflict as err:
log.debug("{}: Python dependency wrong version: {}".format(self.__class__.__name__, err))
missing.append(item)

if missing:
log.warning("{}: Missing/Failed Python dependencies: {}".format(self.__class__.__name__, missing))
return False

return True

def _check_dependencies_system(self):
if not self.dependency_list_system:
return True

missing = [item for item in self.dependency_list_system if not find_executable(item)]

if missing:
log.warning("{}: Missing System dependencies: {}".format(self.__class__.__name__, missing))
return False
else:
return True

def set_commandline(self, command):
self.command_line = command

Expand Down
16 changes: 13 additions & 3 deletions viper/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

import pkgutil
import inspect
import logging
import importlib

from viper.common.out import print_warning
from viper.common.abstracts import Module

log = logging.getLogger('viper')


def load_modules():
# Import modules package.
Expand All @@ -31,10 +34,17 @@ def load_modules():
# Walk through all members of currently imported modules.
for member_name, member_object in inspect.getmembers(module):
# Check if current member is a class.
if inspect.isclass(member_object):
# Yield the class if it's a subclass of Module.
if issubclass(member_object, Module) and member_object is not Module:
if not inspect.isclass(member_object):
continue
# Yield the class if it's a subclass of Module.
if issubclass(member_object, Module) and member_object is not Module:
# run dependency check on each module
if member_object().check():
log.debug("Module: {} - dependency check ok".format(member_name))
plugins[member_object.cmd] = dict(obj=member_object, description=member_object.description)
else:
log.warning("Module: {} - failed dependency check".format(member_name))
print_warning("Module: {} - failed dependency check".format(member_name))

return plugins

Expand Down
2 changes: 2 additions & 0 deletions viper/modules/misp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class MISP(Module):
description = 'Upload and query IOCs to/from a MISP instance'
authors = ['Raphaël Vinot']

min_python_version = (3, 7) # TODO(frennkie) remove .. for testing only!

def __init__(self):
super(MISP, self).__init__()
self.cur_path = __project__.get_path()
Expand Down

0 comments on commit 43064f9

Please sign in to comment.