import csv 
import json 
import os
from git import Repo
import shutil

key = os.environ.get('KKEXKEY')


def main():
    wd = os.getcwd()
    with open("kk_final_expanded_data_final.csv", "r") as file:
        reader = csv.reader(file)
        for i, line in enumerate(reader):
            print("")
            repo_name = line[5].strip().split("/")[-1]
            print("on " + repo_name)
            repo_url = str(line[5].strip())
            temp_dir = "/data/users/mgaughan/kkex_readme_021824/tmp" + repo_name
            try:
                Repo.clone_from(repo_url, temp_dir)
            except:
                print("issue cloning")
                continue
            '''
            if os.path.exists(temp_dir + "/.all-contributorsrc"):
                shutil.copyfile(temp_dir + "/.all-contributorsrc", "/data/users/mgaughan/kkex_contrib_files_122023/contributing_lists/" + repo_name + "_contrib.json")
            if os.path.exists(temp_dir + "/CONTRIBUTING.md"):
                shutil.copyfile(temp_dir + "/CONTRIBUTING.md", "/data/users/mgaughan/kkex_contrib_files_122023/contribute_inst/" + repo_name + "_inst.md")
            '''
            #cannot run yet, with api chokepoint 
            if os.path.exists(temp_dir + "/README.md"):
                shutil.copyfile(temp_dir + "/README.md", "/data/users/mgaughan/kkex_files_022124/readme/" + repo_name + "_readme.md")
            if os.path.exists(temp_dir + "/GOVERNANCE.md"):
                shutil.copyfile(temp_dir + "/GOVERNANCE.md", "/data/users/mgaughan/kkex_files_022124/governance/" + repo_name + "_readme.md")
            shutil.rmtree(temp_dir, ignore_errors=True)
    print('done')



if __name__ == "__main__":
    main()