summaryrefslogtreecommitdiffstats
path: root/suricata/update/util.py
blob: 50788d8d9ceb7ad31ff2b96db9a9221779b7654d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2017 Open Information Security Foundation
# Copyright (c) 2013 Jason Ish
#
# You can copy, redistribute or modify this Program under the terms of
# the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# version 2 along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

""" Module for utility functions that don't really fit anywhere else. """

import hashlib
import tempfile
import atexit
import shutil
import zipfile

def md5_hexdigest(filename):
    """ Compute the MD5 checksum for the contents of the provided filename.

    :param filename: Filename to computer MD5 checksum of.

    :returns: A string representing the hex value of the computed MD5.
    """
    return hashlib.md5(open(filename).read().encode()).hexdigest()

def mktempdir(delete_on_exit=True):
    """ Create a temporary directory that is removed on exit. """
    tmpdir = tempfile.mkdtemp("suricata-update")
    if delete_on_exit:
        atexit.register(shutil.rmtree, tmpdir, ignore_errors=True)
    return tmpdir

class ZipArchiveReader:

    def __init__(self, zipfile):
        self.zipfile = zipfile
        self.names = self.zipfile.namelist()

    def __iter__(self):
        return self

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.zipfile.close()

    def next(self):
        if self.names:
            name = self.names.pop(0)
            if name.endswith("/"):
                # Is a directory, ignore
                return self.next()
            return name
        raise StopIteration

    def open(self, name):
        return self.zipfile.open(name)

    def read(self, name):
        return self.zipfile.read(name)

    @classmethod
    def from_fileobj(cls, fileobj):
        zf = zipfile.ZipFile(fileobj)
        return cls(zf)

GREEN = "\x1b[32m"
BLUE = "\x1b[34m"
REDB = "\x1b[1;31m"
YELLOW = "\x1b[33m"
RED = "\x1b[31m"
YELLOWB = "\x1b[1;33m"
ORANGE = "\x1b[38;5;208m"
BRIGHT_MAGENTA = "\x1b[1;35m"
BRIGHT_CYAN = "\x1b[1;36m"
RESET = "\x1b[0m"

def blue(msg):
    return "%s%s%s" % (BLUE, msg, RESET)

def bright_magenta(msg):
    return "%s%s%s" % (BRIGHT_MAGENTA, msg, RESET)

def bright_cyan(msg):
    return "%s%s%s" % (BRIGHT_CYAN, msg, RESET)

def orange(msg):
    return "%s%s%s" % (ORANGE, msg, RESET)