Source code for assembl.lib.antivirus

from tempfile import mkdtemp
from subprocess import call
from os import unlink, rmdir
from os.path import join
from random import random

from pyramid.path import DottedNameResolver

from . import config


resolver = DottedNameResolver(__package__)


class AntiVirus(object):
    def check(self, path):
        return NotImplementedError()


[docs]class MockPositiveAntiVirus(AntiVirus): "A Mock antivirus for testing. Reports no file infected." def check(self, path): return True
[docs]class MockRandomAntiVirus(AntiVirus): "A Mock antivirus for testing. Reports 10%% files infected." def check(self, path): return random() > 0.1
[docs]class MockRandomFileAntiVirus(AntiVirus): "A Mock antivirus for testing. Reports 10%% files infected." def check(self, path): return random() > 0.1
class SophosAntiVirus(AntiVirus): def check(self, path): try: return call(['savscan', '-f', path]) == 0 except Exception as e: return False class ClamScanAntiVirus(AntiVirus): command = 'clamscan' options = ['--no-summary', '-i'] def check(self, path): args = [self.command] args.extend(self.options) args.append(path) try: return call(args) == 0 except: return False class ClamDScanAntiVirus(ClamScanAntiVirus): command = 'clamdscan' options = ClamScanAntiVirus.options + ['--fdpass'] def get_antivirus(cls_name=None): cls_name = cls_name or config.get( "anti_virus", "assembl.lib.antivirus.ClamDScanVirus") av_class = resolver.resolve(cls_name) return av_class()