# Path: pypasta/Util.py
# def format_date_ymd(dt):
#     return dt.strftime('%Y-%m-%d')
# 
# def get_first_upstream(repo, cluster, commit):
#     upstream = cluster.get_upstream(commit)
#     if not upstream:
#         return None
#     return min(upstream, key=lambda x: repo[x].committer.date)


from multiprocessing import Pool, cpu_count
from .Util import format_date_ymd, get_first_upstream
                    f.write('%s,%s\n' % (version_group, stack.stack_version))

    def patch_groups(self, f_upstream, f_patches, f_occurrence,
                     cluster, date_selector):
        psd = self.psd
        upstream = open(f_upstream, 'w')
        patches = open(f_patches, 'w')
        occurrence = open(f_occurrence, 'w')

        # Write CSV Headers
        upstream.write('PatchGroup, UpstreamHash, UpstreamCommitDate, FirstStackOccurence\n')
        patches.write('PatchGroup, CommitHash, StackVersion, BaseVersion\n')
        occurrence.write('PatchGroup, OldestVersion, LatestVersion, FirstReleasedIn, LastReleasedIn\n')

        cntr = 0
        for d, _ in cluster.iter_split():
            if len(d) == 0:
                continue
            d = list(d)
            cntr += 1

            # write stack patches
            for patch in d:
                stack_of_patch = psd.get_stack_of_commit(patch)
                stack_version = stack_of_patch.stack_version
                base_version = stack_of_patch.base_version
                patches.write('%d,%s,%s,%s\n' %
                              (cntr, patch, stack_version, base_version))

            # optional: write upstream information

# Path: pypasta/Util.py
# def format_date_ymd(dt):
#     return dt.strftime('%Y-%m-%d')
# 
# def get_first_upstream(repo, cluster, commit):
#     upstream = cluster.get_upstream(commit)
#     if not upstream:
#         return None
#     return min(upstream, key=lambda x: repo[x].committer.date)


# Path: pypasta/Export.py
from multiprocessing import Pool, cpu_count
from .Util import format_date_ymd, get_first_upstream

                    f.write('%s,%s\n' % (version_group, stack.stack_version))

    def patch_groups(self, f_upstream, f_patches, f_occurrence,
                     cluster, date_selector):
        psd = self.psd
        upstream = open(f_upstream, 'w')
        patches = open(f_patches, 'w')
        occurrence = open(f_occurrence, 'w')

        # Write CSV Headers
        upstream.write('PatchGroup, UpstreamHash, UpstreamCommitDate, FirstStackOccurence\n')
        patches.write('PatchGroup, CommitHash, StackVersion, BaseVersion\n')
        occurrence.write('PatchGroup, OldestVersion, LatestVersion, FirstReleasedIn, LastReleasedIn\n')

        cntr = 0
        for d, _ in cluster.iter_split():
            if len(d) == 0:
                continue
            d = list(d)
            cntr += 1

            # write stack patches
            for patch in d:
                stack_of_patch = psd.get_stack_of_commit(patch)
                stack_version = stack_of_patch.stack_version
                base_version = stack_of_patch.base_version
                patches.write('%d,%s,%s,%s\n' %
                              (cntr, patch, stack_version, base_version))

            # optional: write upstream information

            commit = get_first_upstream(self.repo, cluster, d[0])