#! /usr/bin/env python3

import csv
from collections import namedtuple


# CVS Columns: "Repository", "License", "Stars", "Watchers", "Forks", "Created", "Updated", "Commits", "Issues", "PullRequests", "Committers", "Percent", "TopCommitterName", "TopCommiterAffiliation"

# Types of licenses
permissive = ['mit', 'apache-2.0', 'bsd-3-clause', 'unlicense', 'bsd-2-clause', 'cc0-1.0', 'isc', 'mit-0', 'wtfpl', 'artistic-2.0']
copyleft = ['gpl-3.0', 'gpl-2.0', 'agpl-3.0']
weak_copyleft = ['mpl-2.0', 'lgpl-3.0', 'epl-1.0', 'lgpl-2.1', 'epl-2.0']


def license_analysis(project):
    p, w, c, e, n = 0, 0, 0, 0, 0
    if project.License.lower() in permissive:
        p = 1
    elif project.License.lower() in weak_copyleft:
        w = 1
    elif project.License.lower() in copyleft:
        c = 1
    elif project.License == "N/A":
        n = 1
    else:
        e = 1
    return [p, w, c, e, n]


with open('projects.csv') as csvfile:
    csvreader = csv.reader(csvfile)
    Data = namedtuple("Data", next(csvreader))  # get names from column headers
    projects = [Data(*line) for line in csvreader]


###
###
# Analysis

###
# General Analysis
count, count2 = 0, 0
licenses_sum = []
for project in projects:
    licenses_sum.append(license_analysis(project))
    if project.TopCommiterAffiliation != "N/A":
        count2+=1
print("Total number of repos:", len(projects))
p, w, c, e, n = [sum(data) for data in zip(*licenses_sum)]
print("\tOf those, type of license (permissive, weak copyleft, copyleft, else, none):", p, w, c, e, n)
print()


###
# High activity repos: more than 1 committer, 9 commits
count, count2 = 0, 0
licenses_sum = []
for project in projects:
    if int(project.Committers) > 1  and int(project.Commits) > 9:
        count+=1
        licenses_sum.append(license_analysis(project))
        if project.TopCommiterAffiliation != "N/A":
            count2+=1
print("\tRepos with more than 1 committer and 9 commits:", count)
print("\t\tOf those, with affiliation:", count2)
p, w, c, e, n = [sum(data) for data in zip(*licenses_sum)]
print("\t\tOf those, type of license (permissive, weak copyleft, copyleft, else, none):", p, w, c, e, n)
print()


###
# High activity repos: more than 5 committers, 100 commits
count, count2 = 0, 0
licenses_sum = []
for project in projects:
    if int(project.Committers) > 4  and int(project.Commits) > 99:
        count+=1
        licenses_sum.append(license_analysis(project))
        if project.TopCommiterAffiliation != "N/A":
            count2+=1
print("\tRepos with more than 4 committers and 99 commits:", count)
print("\t\tOf those, with affiliation:", count2)
p, w, c, e, n = [sum(data) for data in zip(*licenses_sum)]
print("\t\tOf those, type of license (permissive, weak copyleft, copyleft, else, none):", p, w, c, e, n)
print()


###
# Repos with no stars, watchers nor forks
count, count2 = 0, 0
licenses_sum = []
for project in projects:
    if not(int(project.Stars)) and not(int(project.Watchers)) and not(int(project.Forks)):
        count+=1
        licenses_sum.append(license_analysis(project))
        if project.TopCommiterAffiliation != "N/A":
            count2+=1
print("\tRepos without stars, watchers and forks:", count)
print("\t\tOf those, with affiliation:", count2)
p, w, c, e, n = [sum(data) for data in zip(*licenses_sum)]
print("\t\tOf those, type of license (permissive, weak copyleft, copyleft, else, none):", p, w, c, e, n)
print()


###
# Repos with top committer having affiliation
count = 0
licenses_sum = []
for project in projects:
    if project.TopCommiterAffiliation != "N/A":
        count+=1
        licenses_sum.append(license_analysis(project))
print("\tRepos with top committer having affiliation:", count)
p, w, c, e, n = [sum(data) for data in zip(*licenses_sum)]
print("\t\tOf those, type of license (permissive, weak copyleft, copyleft, else, none):", p, w, c, e, n)
print()

