# Path: pypasta/Util.py
# def load_pkl_and_update(filename, update_command, *params):
#     ret = None
#     if os.path.isfile(filename):
#         ret = pickle.load(open(filename, 'rb'))
# 
#     if len(params) == 0:
#         ret, changed = update_command(ret)
#     else:
#         ret, changed = update_command(ret, *params)
# 
#     if changed:
#         pickle.dump(ret, open(filename, 'wb'))
# 
#     return ret


import os
import pygit2
import re
from enum import Enum
from functools import partial
from logging import getLogger
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
from .Util import load_pkl_and_update

def load_maintainers(config, versions):
    def __load_maintainers(ret, config, versions):
        if ret is None:
            ret = dict()

        # Only load what's not yet cached
        versions -= ret.keys()

        if len(versions) == 0:
            return ret, False

        global _repo
        _repo = config.repo
        p = Pool(processes=cpu_count())
        function = partial(_load_maintainer,
                           d_cluster = config.d_maintainers_cluster,
                           project_name = config.project_name)

        for tag, maintainers in tqdm(p.imap_unordered(function, versions),
                                     total=len(versions), desc='MAINTAINERS'):
            ret[tag] = maintainers

        p.close()
        p.join()
        _repo = None

        return ret, True

    log.info('Loading/Updating MAINTAINERS...')

# Path: pypasta/Util.py
# def load_pkl_and_update(filename, update_command, *params):
#     ret = None
#     if os.path.isfile(filename):
#         ret = pickle.load(open(filename, 'rb'))
# 
#     if len(params) == 0:
#         ret, changed = update_command(ret)
#     else:
#         ret, changed = update_command(ret, *params)
# 
#     if changed:
#         pickle.dump(ret, open(filename, 'wb'))
# 
#     return ret


# Path: pypasta/MAINTAINERS.py
import os
import pygit2
import re
from enum import Enum
from functools import partial
from logging import getLogger
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
from .Util import load_pkl_and_update


def load_maintainers(config, versions):
    def __load_maintainers(ret, config, versions):
        if ret is None:
            ret = dict()

        # Only load what's not yet cached
        versions -= ret.keys()

        if len(versions) == 0:
            return ret, False

        global _repo
        _repo = config.repo
        p = Pool(processes=cpu_count())
        function = partial(_load_maintainer,
                           d_cluster = config.d_maintainers_cluster,
                           project_name = config.project_name)

        for tag, maintainers in tqdm(p.imap_unordered(function, versions),
                                     total=len(versions), desc='MAINTAINERS'):
            ret[tag] = maintainers

        p.close()
        p.join()
        _repo = None

        return ret, True

    log.info('Loading/Updating MAINTAINERS...')

    maintainers_version = load_pkl_and_update(config.f_maintainers_pkl,