<commit_before>/* vim: set et ts=4 sts=4 sw=4 tw=72 : */
/* See the LICENSE file for the license of the project */
package uk.ac.cam.cl.git;

import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

import uk.ac.cam.cl.git.api.DuplicateRepoNameException;
import uk.ac.cam.cl.git.api.RepositoryNotFoundException;
import uk.ac.cam.cl.git.configuration.ConfigurationLoader;

import com.jcraft.jsch.*;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 * @version 0.1
 */
public class ConfigDatabase {
    /* For logging */
    private static final Logger log = LoggerFactory.getLogger(ConfigDatabase.class);
    
    private static final String[] environmentVariables = new String[]
            {"HOME="  + ConfigurationLoader.getConfig().getGitoliteHome()
            , "PATH=" + ConfigurationLoader.getConfig().getGitolitePath()
            , "GL_LIBDIR=" + ConfigurationLoader.getConfig().getGitoliteLibdir()};

    @Inject private RepositoryCollection reposCollection;
    
    private static final Injector injector = Guice.createInjector(new DatabaseModule());
    
    private static final ConfigDatabase instance = injector.getInstance(ConfigDatabase.class);
    
    public static ConfigDatabase instance() {
        return instance;
    }

    /**
     * For unit testing only, to allow a mock collection to be used.
     * Replaces the repository collection with the argument.
     * @param reposCollection The collection to be used.
     */
    //@Inject
    void setReposCollection(RepositoryCollection rCollection) {
        reposCollection = rCollection;
    }

    /**
     * Returns a list of all the repository objects in the collection
     *
     * @return List of repository objects in the collection
     */
    public List<Repository> getRepos()
    {   /* TODO: Test ordered-ness or repositories. */
        return reposCollection.listRepos();
        
    }
    
    
    /**
     * Returns the repository object with the given name in the
     * database.
     * 
     * @param name The name of the repository
     * @return The requested repository object
     * @throws RepositoryNotFoundException 
     */
    public Repository getRepoByName(String name) throws RepositoryNotFoundException {
        return reposCollection.getRepo(name);
    }

    /**
     * Removes the repository object with the given name from the
     * database if present in the database.
     * 
     * @param name The name of the repository to remove
     * @throws IOException
     * @throws RepositoryNotFoundException
     */
    public void delRepoByName(String repoName) throws IOException, RepositoryNotFoundException {
        log.info("Deleting repository \"" + repoName + "\"");
        if (!reposCollection.contains(repoName))
            throw new RepositoryNotFoundException();
        reposCollection.removeRepo(repoName);
        generateConfigFile();
        log.info("Deleted repository \"" + repoName + "\"");
    }
    
    /**
     * Removes all repositories from the collection.
     * For unit testing only.
     */
    void deleteAll() throws IOException {
        reposCollection.removeAll();
        generateConfigFile();
    }

    /**
     * Generates config file for gitolite and writes it to gitoliteGeneratedConfigFile (see ConfigurationLoader).
     * <p>
     * Accesses the database to find repositories and assumes the
     * Repository.toString() method returns the appropriate representation. The
     * main conf file should have an include statement so that
     * when the hook is called, the updates are made. The hook is
     * called at the end of this method.
     *
     * @throws IOException Typically an unrecoverable problem.
     */
    public void generateConfigFile() throws IOException {
        log.info("Generating config file \"" +
                ConfigurationLoader.getConfig()
                    .getGitoliteGeneratedConfigFile()
                + "\"");
        StringBuilder output = new StringBuilder();

        for (Repository r : getRepos())
            output.append(r.toString() + "\n");

        /* Write out file */
        File configFile = new File(ConfigurationLoader.getConfig()
                .getGitoliteGeneratedConfigFile());
        BufferedWriter buffWriter = new BufferedWriter(new FileWriter(configFile, false));
        buffWriter.write(output.toString());
        buffWriter.close();
        runGitoliteUpdate(new String[] {"compile",
                                        "trigger POST_COMPILE"});
        log.info("Generated config file \"" +
                ConfigurationLoader.getConfig()
                    .getGitoliteGeneratedConfigFile()
                + "\"");
    }

    /**
     * Adds a new repository to the mongo database for inclusion in the
     * conf file when generated.
     *
     * @param repo The repository to be added
     * @throws DuplicateRepoNameException A repository with this name already
     * exists.<commit_msg>Added a flush() call before recompiling<commit_after>/* vim: set et ts=4 sts=4 sw=4 tw=72 : */
/* See the LICENSE file for the license of the project */
package uk.ac.cam.cl.git;

import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

import uk.ac.cam.cl.git.api.DuplicateRepoNameException;
import uk.ac.cam.cl.git.api.RepositoryNotFoundException;
import uk.ac.cam.cl.git.configuration.ConfigurationLoader;

import com.jcraft.jsch.*;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 * @version 0.1
 */
public class ConfigDatabase {
    /* For logging */
    private static final Logger log = LoggerFactory.getLogger(ConfigDatabase.class);
    
    private static final String[] environmentVariables = new String[]
            {"HOME="  + ConfigurationLoader.getConfig().getGitoliteHome()
            , "PATH=" + ConfigurationLoader.getConfig().getGitolitePath()
            , "GL_LIBDIR=" + ConfigurationLoader.getConfig().getGitoliteLibdir()};

    @Inject private RepositoryCollection reposCollection;
    
    private static final Injector injector = Guice.createInjector(new DatabaseModule());
    
    private static final ConfigDatabase instance = injector.getInstance(ConfigDatabase.class);
    
    public static ConfigDatabase instance() {
        return instance;
    }

    /**
     * For unit testing only, to allow a mock collection to be used.
     * Replaces the repository collection with the argument.
     * @param reposCollection The collection to be used.
     */
    //@Inject
    void setReposCollection(RepositoryCollection rCollection) {
        reposCollection = rCollection;
    }

    /**
     * Returns a list of all the repository objects in the collection
     *
     * @return List of repository objects in the collection
     */
    public List<Repository> getRepos()
    {   /* TODO: Test ordered-ness or repositories. */
        return reposCollection.listRepos();
        
    }
    
    
    /**
     * Returns the repository object with the given name in the
     * database.
     * 
     * @param name The name of the repository
     * @return The requested repository object
     * @throws RepositoryNotFoundException 
     */
    public Repository getRepoByName(String name) throws RepositoryNotFoundException {
        return reposCollection.getRepo(name);
    }

    /**
     * Removes the repository object with the given name from the
     * database if present in the database.
     * 
     * @param name The name of the repository to remove
     * @throws IOException
     * @throws RepositoryNotFoundException
     */
    public void delRepoByName(String repoName) throws IOException, RepositoryNotFoundException {
        log.info("Deleting repository \"" + repoName + "\"");
        if (!reposCollection.contains(repoName))
            throw new RepositoryNotFoundException();
        reposCollection.removeRepo(repoName);
        generateConfigFile();
        log.info("Deleted repository \"" + repoName + "\"");
    }
    
    /**
     * Removes all repositories from the collection.
     * For unit testing only.
     */
    void deleteAll() throws IOException {
        reposCollection.removeAll();
        generateConfigFile();
    }

    /**
     * Generates config file for gitolite and writes it to gitoliteGeneratedConfigFile (see ConfigurationLoader).
     * <p>
     * Accesses the database to find repositories and assumes the
     * Repository.toString() method returns the appropriate representation. The
     * main conf file should have an include statement so that
     * when the hook is called, the updates are made. The hook is
     * called at the end of this method.
     *
     * @throws IOException Typically an unrecoverable problem.
     */
    public void generateConfigFile() throws IOException {
        log.info("Generating config file \"" +
                ConfigurationLoader.getConfig()
                    .getGitoliteGeneratedConfigFile()
                + "\"");
        StringBuilder output = new StringBuilder();

        for (Repository r : getRepos())
            output.append(r.toString() + "\n");

        /* Write out file */
        File configFile = new File(ConfigurationLoader.getConfig()
                .getGitoliteGeneratedConfigFile());
        BufferedWriter buffWriter = new BufferedWriter(new FileWriter(configFile, false));
        buffWriter.write(output.toString());
        buffWriter.flush();
        buffWriter.close();
        runGitoliteUpdate(new String[] {"compile",
                                        "trigger POST_COMPILE"});
        log.info("Generated config file \"" +
                ConfigurationLoader.getConfig()
                    .getGitoliteGeneratedConfigFile()
                + "\"");
    }

    /**
     * Adds a new repository to the mongo database for inclusion in the
     * conf file when generated.
     *
     * @param repo The repository to be added
     * @throws DuplicateRepoNameException A repository with this name already
     * exists.
{'fraction_non_alphanumeric': 0.0935868754660701, 'fraction_numerical': 0.0016778523489932886, 'mean_word_length': 2.699655172413793, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4937038', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2754, 'n_words': 833}