# Path: mwscan/scan.py
# def parse_args():
# def scanpath_to_runfile(path):
# def load_last_run_results(scanpath):
# def write_last_run_results(scanpath, malware):
# def find_targets(root_path, required_extensions=None, 
#                  exclude_patterns=None, follow_symlinks=False):
# def scan_files(files, rules, whitelist, find_cb=None):
# def log_find(path, matches, sha1sum, excluded=None, show_sha1=False):
# def load_exclude_patterns(path):
# def main():
# 
# Path: mwscan/settings.py
# CACHEDIR = os.path.expanduser('~/.cache/mwscan')
# LAST_RUN_FILE = os.path.join(CACHEDIR, 'last_run')
# DEFAULT_EXCLUDEFILE = os.path.expanduser('~/.config/mwscan/excludes')
# CODE_EXT = ('php', 'phtml', 'js', 'jsx', 'html', 'php3', 'php4', 'php5', 'php7', 'sh', 'ini')
# 
# Path: mwscan/ruleset.py
# class Files(RulesProvider):
# 
#     # initialize with Files(args)
# 
#     def get_rules(self):
#         path = self._args.rules
#         logging.info("Loading {0}".format(self._args.rules))
#         with open(path, 'rb') as fh:
#             return fh.read().decode('utf-8', 'ignore')


import os
import time
    import mock
from unittest import TestCase
from mwscan import scan, settings
from mwscan.ruleset import Files
from collections import namedtuple
    from unittest import mock
        settings.LAST_RUN_FILE = '/tmp/last_run'

        self.fixture_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fixtures')
        self.rules_path = os.path.join(self.fixture_path, 'rules.yar')
        self.target_path = os.path.join(self.fixture_path, 'files')
        self.state_file = scan.scanpath_to_runfile(self.target_path)
        self.new_file = os.path.join(self.target_path, 'newer_malware')

        assert self.state_file.startswith('/tmp')

        # might still exist from cancelled earlier test
        for i in self.new_file, self.state_file:
            try:
                os.unlink(i)
            except OSError:
                pass

        self.rules, self.whitelist = self._load_file_rules(self.rules_path)

    def test_normal_scan(self):
        files = scan.find_targets(self.target_path)
        malware, whitelisted = scan.scan_files(files, self.rules, self.whitelist)

        self.assertEqual(len(malware), 2)
        self.assertEqual(len(whitelisted), 1)

    def test_scan_callback(self):

        targets = scan.find_targets(self.target_path)
        testcb = mock.MagicMock()

# Path: mwscan/scan.py
# def parse_args():
# def scanpath_to_runfile(path):
# def load_last_run_results(scanpath):
# def write_last_run_results(scanpath, malware):
# def find_targets(root_path, required_extensions=None, 
#                  exclude_patterns=None, follow_symlinks=False):
# def scan_files(files, rules, whitelist, find_cb=None):
# def log_find(path, matches, sha1sum, excluded=None, show_sha1=False):
# def load_exclude_patterns(path):
# def main():
# 
# Path: mwscan/settings.py
# CACHEDIR = os.path.expanduser('~/.cache/mwscan')
# LAST_RUN_FILE = os.path.join(CACHEDIR, 'last_run')
# DEFAULT_EXCLUDEFILE = os.path.expanduser('~/.config/mwscan/excludes')
# CODE_EXT = ('php', 'phtml', 'js', 'jsx', 'html', 'php3', 'php4', 'php5', 'php7', 'sh', 'ini')
# 
# Path: mwscan/ruleset.py
# class Files(RulesProvider):
# 
#     # initialize with Files(args)
# 
#     def get_rules(self):
#         path = self._args.rules
#         logging.info("Loading {0}".format(self._args.rules))
#         with open(path, 'rb') as fh:
#             return fh.read().decode('utf-8', 'ignore')


# Path: mwscan/tests/test_mwscan.py
import os
import time
    import mock
from unittest import TestCase
from mwscan import scan, settings
from mwscan.ruleset import Files
from collections import namedtuple
    from unittest import mock

        settings.LAST_RUN_FILE = '/tmp/last_run'

        self.fixture_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fixtures')
        self.rules_path = os.path.join(self.fixture_path, 'rules.yar')
        self.target_path = os.path.join(self.fixture_path, 'files')
        self.state_file = scan.scanpath_to_runfile(self.target_path)
        self.new_file = os.path.join(self.target_path, 'newer_malware')

        assert self.state_file.startswith('/tmp')

        # might still exist from cancelled earlier test
        for i in self.new_file, self.state_file:
            try:
                os.unlink(i)
            except OSError:
                pass

        self.rules, self.whitelist = self._load_file_rules(self.rules_path)

    def test_normal_scan(self):
        files = scan.find_targets(self.target_path)
        malware, whitelisted = scan.scan_files(files, self.rules, self.whitelist)

        self.assertEqual(len(malware), 2)
        self.assertEqual(len(whitelisted), 1)

    def test_scan_callback(self):

        targets = scan.find_targets(self.target_path)
        testcb = mock.MagicMock()

        scan.scan_files(targets, self.rules, self.whitelist, testcb)