import base64
import os
import re
import random
import shutil


def find_files(path, pattern, regex=False):
    result = []
    for root, dirs, files in os.walk(path):
        for f_name in files:
            full_path = os.path.join(root, f_name)
            search_in = full_path.split(path)[-1]
            if regex:
                match = re.search(pattern, search_in)
            else:
                match = pattern in search_in
            if match:
                result.append(full_path)
    return result


def cleanup_folder(path):
    for root, dirs, files in os.walk(path):
        for f in files:
            os.remove(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))


def bytes_to_b64(b):
    return base64.b64encode(b).decode('utf-8')


def generate_mac():
    return ':'.join('%02x' % random.randint(0, 255) for _ in range(6)).lower()
