csvwriter.writerow([projectName, 0])
        else:
            if percentage == 0:
                percentage = 0.5
            total_number_of_lines = math.ceil(matchedLines * 100 / percentage)
            csvwriter.writerow([projectName, total_number_of_lines])
    number_of_lines_file.close()


logFile = open('metrics-calculation-{0}.log'.format(datetime.datetime.now()),
               'w')
sys.stdout = logFile
for phase in range(1, 5):
    try:
        print("Processing phase {0}".format(phase))
        config.updatePhase(phase)
        print(config.URL)

        if os.path.exists(config.PHASE_DIRECTORY):
            print("Removing directory {0}".format(config.PHASE_DIRECTORY))
            shutil.rmtree(config.PHASE_DIRECTORY)
            print("Removed directory {0}".format(config.PHASE_DIRECTORY))
        #Download all projects of current phase to local workstation
        subprocess.call(["git", "clone", config.URL])

        #Change current directory to the directory of the current phase
        os.chdir(config.PHASE_DIRECTORY)

        #Cleanup and change directory structure as pre-processing for MOSS
        output = subprocess.check_output("perl ../moss.pl -d */*.js */*.html",
                                         shell=True)
    os.mkdir("cross-phase")
for phase in range(2, 5):
    os.chdir(os.path.join(homedir, "cross-phase"))
    curPhase = phase
    prevPhase = phase - 1
    prevCrossPhaseDirectory = None
    prevCrossPhaseDirectory = "phase-{1}-{0}".format(prevPhase - 1,
                                                     curPhase - 1)
    currentCrossPhaseDirectory = "phase-{1}-{0}".format(prevPhase, curPhase)
    if not os.path.exists(currentCrossPhaseDirectory):
        os.mkdir(currentCrossPhaseDirectory)
    try:
        print(
            "Calculating cross-phase similarity for phases {0} and {1}".format(
                prevPhase, curPhase))
        config.updatePhase(prevPhase)
        prevPhaseDirectory = os.path.join(homedir, config.PHASE_DIRECTORY)
        config.updatePhase(curPhase)
        curPhaseDirectory = os.path.join(homedir, config.PHASE_DIRECTORY)
        if not (len(sys.argv) > 1 and sys.argv[1] == 'skipMoss'):
            for curPhaseProject in os.listdir(curPhaseDirectory):
                if os.path.isdir(
                        os.path.join(homedir, curPhaseDirectory,
                                     curPhaseProject)
                ) and not curPhaseProject.startswith(r'.'):
                    os.chdir(
                        os.path.join(homedir, "cross-phase",
                                     currentCrossPhaseDirectory))
                    try:
                        print(
                            "Performing git diff for {0} across phase {1} and {2}"