1#!/usr/bin/python3
2
3import argparse
4import json
5import os
6
7
8class subcmd:
9    def __init__(self, parser: argparse._SubParsersAction) -> None:
10        p = parser.add_parser(
11            "report", help="Create final report"
12        )
13
14        p.set_defaults(cmd=self)
15
16
17    def run(self, args: argparse.Namespace) -> int:
18        commits_fp = os.path.join(args.dir, "commits.json")
19        reviews_fp = os.path.join(args.dir, "reviews.json")
20
21        results = {}
22
23        if not os.path.isfile(commits_fp):
24            print("Missing commits.json; run analyze-commits?")
25            return 1
26
27        if not os.path.isfile(reviews_fp):
28            print("Missing reviews.json; run analyze-reviews?")
29            return 1
30
31        with open(commits_fp, "r") as commits_file:
32            commits = json.load(commits_file)
33
34        with open(reviews_fp, "r") as reviews_file:
35            reviews = json.load(reviews_file)
36
37        for user in sorted(set(commits.keys()).union(reviews.keys())):
38            user_commits = len(commits.get(user, []))
39            user_reviews = len(reviews.get(user, []))
40
41            points = user_commits * 3 + user_reviews
42            print(user, points, user_commits, user_reviews)
43
44            qualified = points >= 15
45
46            results[user] = { "qualified": qualified, "points": points,
47                    "commits": user_commits, "reviews": user_reviews }
48
49        with open(os.path.join(args.dir, "report.json"), "w") as outfile:
50            outfile.write(json.dumps(results, indent = 4))
51
52        return 0
53