Skip to content

Commit

Permalink
importing json file from command line. related to #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Pinto committed May 12, 2013
1 parent 24cdeb1 commit d629494
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 25 deletions.
14 changes: 14 additions & 0 deletions grounghog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"forge": "github",
"dest": "C:/groundhog/dest",
"out": "C:/groundhog/metrics",
"datetime": "2012-07-01_12_00",
"nprojects": 30,
"nthreads": 4,
"outputformat": "csv",
"search": {
"projects": ["rails", "bootstrap"],
"username":"gustavopinto"
}
}

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
Expand Down
72 changes: 72 additions & 0 deletions src/java/main/br/ufpe/cin/groundhog/main/JsonInputArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package br.ufpe.cin.groundhog.main;

import java.util.List;

public final class JsonInputArgs {
private final String forge;
private final String dest;
private final String out;
private final String datetime;
private final String nprojects;
private final String outputformat;
private final Search search;

public JsonInputArgs(String forge, String dest, String out,
String datetime, String nprojects, String outputformat,
Search search) {
this.forge = forge;
this.dest = dest;
this.out = out;
this.datetime = datetime;
this.nprojects = nprojects;
this.outputformat = outputformat;
this.search = search;
}

public String getForge() {
return forge;
}

public String getDest() {
return dest;
}

public String getOut() {
return out;
}

public String getDatetime() {
return datetime;
}

public String getNprojects() {
return nprojects;
}

public String getOutputformat() {
return outputformat;
}

public Search getSearch() {
return search;
}
}

class Search {
private final List<String> projects;
private final String username;

public Search(List<String> projects, String username) {
super();
this.projects = projects;
this.username = username;
}

public String getUsername() {
return username;
}

public List<String> getProjects() {
return projects;
}
}
89 changes: 64 additions & 25 deletions src/java/main/br/ufpe/cin/groundhog/main/Options.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package br.ufpe.cin.groundhog.main;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -10,47 +12,58 @@
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

import com.google.common.base.Joiner;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

enum SupportedForge {
GITHUB, SOURCEFORGE, GOOGLECODE
}

enum MetricsOutputFormat{
enum MetricsOutputFormat {
JSON, CSV
}

/**
* The command-line options parsing class
*
* @author fjsj, gustavopinto, rodrigoalvesvieira
*/
public class Options {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm");

@Option(name="-forge", usage="forge to be used in search and crawling process")
private static SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd_HH_mm");

@Option(name = "-forge", usage = "forge to be used in search and crawling process")
private SupportedForge forge = SupportedForge.GITHUB;
@Option(name="-dest", usage="destination folder into which projects will be downloaded")

@Option(name = "-dest", usage = "destination folder into which projects will be downloaded")
private File destinationFolder = null;
@Option(name="-out", usage="output folder to metrics files")

@Option(name = "-out", usage = "output folder to metrics files")
private File metricsFolder = null;
@Option(name="-datetime", usage="datetime of projects source code to be processed")

@Option(name = "-datetime", usage = "datetime of projects source code to be processed")
private String datetime = dateFormat.format(new Date());
@Option(name="-nprojects", usage="maximum number of projects to be downloaded and processed")

@Option(name = "-nprojects", usage = "maximum number of projects to be downloaded and processed")
private int nProjects = 4;
@Option(name="-nthreads", usage="maximum number of concurrent threads")

@Option(name = "-nthreads", usage = "maximum number of concurrent threads")
private int nThreads = 4;
@Option(name="-o", usage="determine the output format of the metrics")

@Option(name = "-o", usage = "determine the output format of the metrics")
private MetricsOutputFormat metricsFormat = MetricsOutputFormat.JSON;

@Option(name = "-in", usage = "all inputs in one json file")
private JsonInputArgs inputFile = null;

@Argument
private List<String> arguments = new ArrayList<String>();
private List<String> arguments = new ArrayList<String>();

/**
* Informs the code forge where the project search will be performed
*
* @return
*/
public SupportedForge getForge() {
Expand All @@ -59,14 +72,16 @@ public SupportedForge getForge() {

/**
* Returns the destination folder into which projects will be downloaded
*
* @return
*/
public File getDestinationFolder() {
return this.destinationFolder;
}

/**
* Sets the destination folder into which projects will be downloaded
*
* @param destinationFolder
*/
public void setDestinationFolder(File destinationFolder) {
Expand All @@ -75,6 +90,7 @@ public void setDestinationFolder(File destinationFolder) {

/**
* Informs the location of the folder where the metrics will be stored
*
* @return A {@link File} object correspondent to the metrics folder
*/
public File getMetricsFolder() {
Expand All @@ -83,6 +99,7 @@ public File getMetricsFolder() {

/**
* Sets the location of the folder where the metrics will be stored
*
* @param metricsFolder
*/
public void setMetricsFolder(File metricsFolder) {
Expand All @@ -99,6 +116,7 @@ public void setDatetime(String datetime) {

/**
* Informs the maximum number of projects to be downloaded and processed
*
* @return
*/
public int getnProjects() {
Expand All @@ -107,6 +125,7 @@ public int getnProjects() {

/**
* Sets the maximum number of projects to be downloaded and processed
*
* @param nProjects
*/
public void setnProjects(int nProjects) {
Expand All @@ -115,6 +134,7 @@ public void setnProjects(int nProjects) {

/**
* Informs the maximum number of concurrent threads to be ran
*
* @return
*/
public int getnThreads() {
Expand All @@ -123,6 +143,7 @@ public int getnThreads() {

/**
* Sets the maximum number of concurrent threads to be ran
*
* @param nThreads
*/
public void setnThreads(int nThreads) {
Expand All @@ -138,25 +159,43 @@ public void setArguments(List<String> arguments) {
}

/**
* Sets the forge where the search for projects will be performed
* Valid options are those specified in the {@link SupportedForge} enumerator
* Sets the forge where the search for projects will be performed Valid
* options are those specified in the {@link SupportedForge} enumerator
*
* @param forge
*/
public void setForge(SupportedForge forge) {
this.forge = forge;
}

public static SimpleDateFormat getDateFormat() {
return dateFormat;
}



public MetricsOutputFormat getMetricsFormat() {
return metricsFormat;
}

public void setMetricsFormat(MetricsOutputFormat metricsFormat) {
this.metricsFormat = metricsFormat;
}

}

public JsonInputArgs getInputFile() {
return inputFile;
}

public void setInputFile(File inputFile) {
try {
List<String> lines = Files.readLines(inputFile, Charset.defaultCharset());
String json = Joiner.on(" ").join(lines);
JsonInputArgs args = new Gson().fromJson(json, JsonInputArgs.class);
System.out.println(args);
this.inputFile = args;
} catch (JsonSyntaxException e) {
throw new RuntimeException("O formato do arquivo json parece estranho. De uma olhada nos nossos exemplos!");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Não consegui achar o arquivo " + inputFile.getName() + ". Ele não está em outro diretório?");
}
}
}
17 changes: 17 additions & 0 deletions src/java/test/br/ufpe/cin/groundhog/main/OptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.ufpe.cin.groundhog.main;

import java.io.File;

import org.junit.Assert;
import org.junit.Test;

public class OptionTest {

@Test
public void testJsonInputFile() {
Options op = new Options();
op.setInputFile(new File(
"/home/ghlp/workspace/java/groundhog/grounghog.json"));
Assert.assertNotNull(op.getInputFile());
}
}

0 comments on commit d629494

Please sign in to comment.