Skip to content

Commit

Permalink
using args4j as default. working on #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Pinto committed May 17, 2013
1 parent d629494 commit 8b51012
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
3 changes: 1 addition & 2 deletions grounghog.json → groundhog.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"out": "C:/groundhog/metrics",
"datetime": "2012-07-01_12_00",
"nprojects": 30,
"nthreads": 4,
"outputformat": "csv",
"search": {
"projects": ["rails", "bootstrap"],
"projects": ["vraptor", "Turbine", "epona"],
"username":"gustavopinto"
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/java/main/br/ufpe/cin/groundhog/main/ArgsMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package br.ufpe.cin.groundhog.main;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

public class ArgsMain {

public static void main(String[] args) {
Options opt = new Options();
CmdLineParser parser = new CmdLineParser(opt);
try {
parser.parseArgument(args);
System.out.println(opt.getInputFile());
} catch (CmdLineException e) {
e.printStackTrace();
System.err.println("java -jar myprogram.jar [options...] arguments...");
parser.printUsage(System.err);
return;
}
}
}
19 changes: 2 additions & 17 deletions src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -55,7 +53,7 @@
import com.google.inject.Injector;

public class CmdMain {
private static Logger logger = LoggerFactory.getLogger(CrawlGoogleCode.class);
private static Logger logger = LoggerFactory.getLogger(CmdMain.class);

/**
* Defines the code forge where the search will be performed.
Expand Down Expand Up @@ -228,17 +226,7 @@ public static void freeResources(ForgeCrawler crawler, OutputStream errorStream)
}
}

public static void main(String[] args) {
Options opt = new Options();
String date = new SimpleDateFormat("yyyy-MM-dd_HH_mm").format(new Date()); // Current datetime String

opt.setDatetime(date);
//opt.setDestinationFolder(new File("download"));
opt.setForge(SupportedForge.GITHUB);
opt.setMetricsFolder(new File("metrics"));
opt.setnProjects(5);
opt.setArguments(Arrays.asList("facebook"));
opt.setMetricsFormat(MetricsOutputFormat.JSON);
public static void main(Options opt) {

List<String> terms = opt.getArguments();
String term = Joiner.on(" ").join(terms);
Expand Down Expand Up @@ -273,9 +261,6 @@ public static void main(String[] args) {
e.printStackTrace();
}

// Set ThreadsConfig.nThreads
Config.MAX_NUMBER_OF_THREADS = opt.getnThreads();

// Search for projects
logger.info("Searching for projects...");
ForgeSearch search = defineForgeSearch(opt.getForge());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

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) {
import com.google.common.base.Objects;

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

public JsonInput(String forge, String dest, String out, String datetime,
String nprojects, String outputformat, Search search) {
super();
this.forge = forge;
this.dest = dest;
this.out = out;
Expand Down Expand Up @@ -50,9 +52,21 @@ public String getOutputformat() {
public Search getSearch() {
return search;
}

public String toString() {
return Objects.toStringHelper(this)
.add("forge", forge)
.add("dest", dest)
.add("out", out)
.add("datetime", datetime)
.add("nproject", nprojects)
.add("outputformat", outputformat)
.add("search", search)
.toString();
}
}

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

Expand All @@ -69,4 +83,11 @@ public String getUsername() {
public List<String> getProjects() {
return projects;
}

public String toString() {
return Objects.toStringHelper(this)
.add("projects", projects)
.add("username", username)
.toString();
}
}
50 changes: 16 additions & 34 deletions src/java/main/br/ufpe/cin/groundhog/main/Options.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.ufpe.cin.groundhog.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
Expand Down Expand Up @@ -49,14 +50,10 @@ public class Options {
@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")
private int nThreads = 4;

@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;
private JsonInput input = null;

@Argument
private List<String> arguments = new ArrayList<String>();
Expand Down Expand Up @@ -132,24 +129,6 @@ public void setnProjects(int nProjects) {
this.nProjects = nProjects;
}

/**
* Informs the maximum number of concurrent threads to be ran
*
* @return
*/
public int getnThreads() {
return this.nThreads;
}

/**
* Sets the maximum number of concurrent threads to be ran
*
* @param nThreads
*/
public void setnThreads(int nThreads) {
this.nThreads = nThreads;
}

public List<String> getArguments() {
return this.arguments;
}
Expand Down Expand Up @@ -180,22 +159,25 @@ public void setMetricsFormat(MetricsOutputFormat metricsFormat) {
this.metricsFormat = metricsFormat;
}

public JsonInputArgs getInputFile() {
return inputFile;
public JsonInput getInputFile() {
return input;
}

public void setInputFile(File inputFile) {
@Option(name = "-in", usage = "all above inputs in one json file")
public void setInputFile(File inputFile) throws FileNotFoundException {
try {
List<String> lines = Files.readLines(inputFile, Charset.defaultCharset());
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;
JsonInput args = new Gson().fromJson(json, JsonInput.class);
this.input = args;
} catch (JsonSyntaxException e) {
throw new RuntimeException("O formato do arquivo json parece estranho. De uma olhada nos nossos exemplos!");
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?");
throw new RuntimeException("Não consegui achar o arquivo "
+ inputFile.getName()
+ ". Ele não está em outro diretório?");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.Assert;
import org.junit.Test;

public class OptionTest {
public class OptionsTest {

@Test
public void testJsonInputFile() {
Expand Down

0 comments on commit 8b51012

Please sign in to comment.