#!/usr/bin/python
import json
import os
import copy
from common.utils import sync
from common.gerrit import Gerrit, get_gerrit_event
from common.git import clone_gitproject

#================================================================================
# class DomainUserMap
#================================================================================
class DomainUserMap:
    def __init__(self, filename):
        self.f = open(filename, "r")
        self.domain_user_map = {}
        self.parse_domain(self.f)

    def __del__(self):
        self.f.close()

    def parse_email(self, line):
        lpos = line.find("<");
        rpos = line.rfind(">");
        return line[lpos+1 : rpos].strip()

    def append_person(self, level, email):
        if level not in self.domain_user_map[self.cur_domain]:
            self.domain_user_map[self.cur_domain][level] = []

        self.domain_user_map[self.cur_domain][level].append(email)

    def parse_parent_domain(self, parent_domain):
        # propagate parent domain's info
        self.domain_user_map[self.cur_domain] = copy.deepcopy(self.domain_user_map[parent_domain])


    def parse_domain_line(self, line):
        line = line.strip()
        if len(line) == 0:
            pass
        elif line[0] == 'D':
            # domain
            self.cur_domain = line[3:]
            if self.cur_domain in self.domain_user_map:
                raise Exception("Already declared domain name: "+self.cur_domain)
            self.domain_user_map[self.cur_domain] = {}
        elif line[0] == 'N':
            # parent domain
            self.parse_parent_domain(line[3:])
        elif line[0] == 'A':
            # architect
            self.append_person("A", self.parse_email(line))
        elif line[0] == 'M':
            self.append_person("M", self.parse_email(line))
            # maintainer
        elif line[0] == 'I':
            # integrator
            self.append_person("I", self.parse_email(line))
        elif line[0] == 'R':
            # reviewer
            self.append_person("R", self.parse_email(line))
        else:
            print "undefined : ", line,

    def parse_domain(self, f):
        while True:
            line = f.readline()
            if not line: break;
            self.parse_domain_line(line)

    def dump(self):
        print self.domain_user_map

    def print_to_file(self, filename):
        with open(filename, "w") as f:
            f.write(json.dumps(self.domain_user_map))

#================================================================================
# class GitDomainMap
#================================================================================
class GitDomainMap:
    def __init__(self, filename):
        self.f = open(filename, "r")
        self.git_domain_map = {}
        self.parse_git_tree(self.f)

    def __del__(self):
        self.f.close()

    def parse_line(self, line):
        line = line.strip()
        if len(line) == 0:
            pass
        elif line[0] == 'G':
            # git path
            self.cur_project = line[3:]
        elif line[0] == 'D':
            # domain
            domain = line[3:]
            if self.cur_project in self.git_domain_map:
                raise Exception("A project " + self.cur_project + "is matched to more than two domains:(" + self.cur_project + "," + self.git_domain_map[self.cur_project] + ")!!")
            self.git_domain_map[self.cur_project] = domain
        elif line[0] == 'S':
            # submit type
            pass
        elif line[0] == 'T':
            # package type
            pass
        elif line[0] == 'O':
            # owner
            pass
        elif line[0] == 'B':
            # branch
            pass
        elif line[0] == 'L':
            # license
            pass
        elif line[0] == 'C':
            # comments
            pass
        else:
            print "undefined : ", line,

    def parse_git_tree(self, f):
        while True:
            line = f.readline()
            if not line: break;
            self.parse_line(line)

    def dump(self):
        print self.git_domain_map

    def print_to_file(self, filename):
        with open(filename, "w") as f:
            f.write(json.dumps(self.git_domain_map))

#================================================================================
# generate_mappings
#================================================================================
def generate_mappings(prjdir, target_dir):
    d = DomainUserMap(os.path.join(prjdir, "domains"))
    g = GitDomainMap(os.path.join(prjdir,"git-trees"))

    domain_user_map_filename = "domain_user_map.json"
    git_domain_filename = "git_domain_map.json"

    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    d.print_to_file(os.path.join(target_dir, domain_user_map_filename));
    g.print_to_file(os.path.join(target_dir, git_domain_filename));

#target_dir = ".dashboard"

#generate_mappings(".", target_dir)
#exit(0)
#================================================================================
# main
#================================================================================
GERRIT_PROJECT = os.getenv('GERRIT_PROJECT')
WORKSPACE = os.getenv('WORKSPACE')
GERRIT_HOSTNAME = os.getenv('GERRIT_HOSTNAME')
GERRIT_USERNAME = os.getenv('GERRIT_USERNAME')
GERRIT_SSHPORT = os.getenv('GERRIT_SSHPORT')
GERRIT_SILENT_MODE = int(os.getenv('GERRIT_SILENT_MODE'))

events = get_gerrit_event()
print(events)

prjdir = os.path.join(WORKSPACE, "meta-git")

clone_gitproject(GERRIT_PROJECT, prjdir)

target_dir = ".dashboard"

generate_mappings("meta-git", target_dir)

sync_dest = os.path.join(os.getenv("IMG_SYNC_DEST_BASE"), "snapshots", target_dir)
# sync to the download server.
sync(target_dir, sync_dest)
