Skip to content

Commit

Permalink
download projects from a giver user. almost done. rel #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Pinto committed May 19, 2013
1 parent 5002628 commit 7abcafb
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 76 deletions.
4 changes: 2 additions & 2 deletions groundhog.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"forge": "github",
"dest": "/opt/groundhog/dest",
"out": "/opt/groundhog/metrics",
"datetime": "2012-07-01 12:00",
"datetime": "2013-04-28 12:00",
"nprojects": 3,
"outputformat": "json",
"search": {
"projects": ["vraptor", "Turbine", "epona", "rails"],
"projects": ["hello-curiosity", "Turbine", "epona", "rails"],
"username":"gustavopinto"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/java/main/br/ufpe/cin/groundhog/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class Project {
private String name;
private String description;
private String owner;
private volatile String owner;
private String iconURL;
private SCM scm;
private String scmURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.io.File;
import java.util.Date;

import br.ufpe.cin.groundhog.scmclient.EmptyProjectAtDateException;

/**
* An interface that defines the code history functionality.
* This functionality allows temporal navigation through source code history,
Expand All @@ -25,7 +23,7 @@ public interface CodeHistory {
* @throws Exception when something nasty happens
*/
public File checkoutToDate(String project, String url, Date date)
throws CheckoutException, EmptyProjectAtDateException;
throws CheckoutException;

/**
* Checks out the given project according to the given date. Returns a new temporary folder
Expand All @@ -39,6 +37,6 @@ public File checkoutToDate(String project, String url, Date date)
* @throws Exception when something nasty happens
*/
public File checkoutToDate(String project, File repositoryFolder, Date date)
throws CheckoutException, EmptyProjectAtDateException;
throws CheckoutException;

}
23 changes: 11 additions & 12 deletions src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ public File downloadAndCheckoutProject(Project project,
logger.warn(format("Project %s was empty at specified date: %s", name, datetimeStr));
return null;
} catch (Exception e) {
logger.error(format("Error while downloading project %s", name));
e.printStackTrace();
logger.error(format("Error while downloading project %s: %s", name, e.getMessage()));
return null;
}
}
Expand Down Expand Up @@ -215,16 +216,14 @@ public void analyzeProject(Project project, File projectFolder,
JavaParser parser = new JavaParser(projectFolder);
String metrics = parser.format(metricsFormat);

if (metrics != null) {
// Save metrics to file
String metricsFilename = format("%s-%s.%s", name, datetimeStr, metricsFormat.simpleName());
logger.info(format("Project %s parsed, metrics extracted! Writing result to file %s...", name, metricsFilename));
// Save metrics to file
String metricsFilename = format("%s-%s.%s", name, datetimeStr, metricsFormat.simpleName());
logger.info(format("Project %s parsed, metrics extracted! Writing result to file %s...", name, metricsFilename));

File metricsFile = new File(metricsFolder, metricsFilename);
FileUtil.getInstance().writeStringToFile(metricsFile, metrics);
File metricsFile = new File(metricsFolder, metricsFilename);
FileUtil.getInstance().writeStringToFile(metricsFile, metrics);

logger.info(format("Metrics of project %s written to file %s", name, metricsFile.getAbsolutePath()));
}
logger.info(format("Metrics of project %s written to file %s", name, metricsFile.getAbsolutePath()));
} catch (NotAJavaProjectException e) {
logger.warn(format(e.getMessage(), name));
} catch (Exception e) {
Expand Down Expand Up @@ -255,10 +254,10 @@ public void run(JsonInput input) {
String term = input.getSearch().getProjects().get(0);

List<Project> allProjects = null;
if(username != null) {
allProjects = search.getProjects(term, 1);
if(username != null && !username.isEmpty()) {
allProjects = search.getProjects(term, username, 1);
} else {
allProjects = search.getProjects(term, username, 1);
allProjects = search.getProjects(term, 1);
}

List<Project> projects = new ArrayList<Project>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;

/**
* @author benitofe, jpso, filipeximenes, weslleyt, fjsj
*/
// see: http://docs.oracle.com/javase/6/docs/jdk/api/javac/tree/com/sun/source/tree/package-summary.html
public class CodeAnalyzerTreeVisitor extends TreePathScanner<Object, Trees> {
private HashMap<String, HashMap<String, MutableInt>> counters;
Expand Down
53 changes: 12 additions & 41 deletions src/java/main/br/ufpe/cin/groundhog/parser/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

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

import br.ufpe.cin.groundhog.parser.formater.Formater;

/**
Expand All @@ -26,10 +26,11 @@
*
*/
public class JavaParser {
private static String fileSeparator = File.separator;

private File folder; // this folder means the root folder of the downloaded project
private List<File> filesList;
private static Logger logger = LoggerFactory.getLogger(JavaParser.class);

private final File folder; // this folder means the root folder of the downloaded project
private final List<File> filesList;

/**
* Constructs a new JavaParser which will extract metrics of all Java source files inside
Expand All @@ -38,7 +39,7 @@ public class JavaParser {
*/
public JavaParser(File folder) {
this.folder = folder;
filesList = new LinkedList<File>();
this.filesList = new LinkedList<File>();
}

private void recursiveSearch(File start) {
Expand All @@ -50,7 +51,7 @@ private void recursiveSearch(File start) {
recursiveSearch(f);
} else if (f.isFile()) {
String path = f.getAbsolutePath();
if (!path.contains(fileSeparator + "__MACOSX") && f.getName().endsWith(".java")) {
if (!path.contains(File.separator + "__MACOSX") && f.getName().endsWith(".java")) {
filesList.add(f);
}
}
Expand Down Expand Up @@ -86,12 +87,12 @@ private HashMap<String, HashMap<String, MutableInt>> invokeProcessor() throws IO
* @throws IOException if something wrong happens when closing source file manager
*/
public HashMap<String, HashMap<String, MutableInt>> parse() throws IOException {
logger.info("Running parser..");
recursiveSearch(folder);
if (!filesList.isEmpty()) {
return invokeProcessor();
} else {
return null;
}
}
return null;
}

public String format(Formater metricsFormat) throws IOException{
Expand All @@ -101,34 +102,4 @@ public String format(Formater metricsFormat) throws IOException{
}
return metricsFormat.format(counters);
}

/**
* Pretty print metrics
* @param counters parse method result
*/
public static void printResult(HashMap<String, HashMap<String, MutableInt>> counters) {
for (String metric : counters.keySet()) {
System.out.println("Metric - " + metric);
HashMap<String, MutableInt> counter = counters.get(metric);
List<Entry<String, MutableInt>> entries = new ArrayList<Entry<String, MutableInt>>();
entries.addAll(counter.entrySet());
Collections.sort(entries, new Comparator<Entry<String, MutableInt>>() {
public int compare(Entry<String, MutableInt> e1, Entry<String, MutableInt> e2) {
return e2.getValue().get() - e1.getValue().get();
}
});

for (Entry<String, MutableInt> entry : entries) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("-----------------------");
System.out.println();
}
}

public static void main(String[] args) throws Exception {
HashMap<String, HashMap<String, MutableInt>> counters =
new JavaParser(new File("C:\\Users\\fjsj\\AppData\\Local\\Temp\\1341192512523-0\\javacv")).parse();
printResult(counters);
}
}
17 changes: 10 additions & 7 deletions src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public class GitClient {
* @throws TransportException
* @throws GitAPIException
*/
public void clone(String url, File destination)
throws InvalidRemoteException, TransportException, GitAPIException {
Repository rep = Git.cloneRepository().
setURI(url).
setDirectory(destination).
call().getRepository();
rep.close();
public void clone(String url, File destination) {
try {
Repository rep = Git.cloneRepository()
.setURI(url)
.setDirectory(destination)
.call().getRepository();
rep.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
Expand Down
49 changes: 40 additions & 9 deletions src/java/main/br/ufpe/cin/groundhog/search/SearchGitHub.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package br.ufpe.cin.groundhog.search;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import br.ufpe.cin.groundhog.GroundhogException;
import br.ufpe.cin.groundhog.Project;
import br.ufpe.cin.groundhog.SCM;
import br.ufpe.cin.groundhog.http.Requests;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -21,7 +27,7 @@
* @author fjsj, gustavopinto, rodrigoalvesvieira
*/
public class SearchGitHub implements ForgeSearch {
private static String root = "https://api.github.com";
private final static String root = "https://api.github.com";
private final Requests requests;
private final Gson gson;

Expand All @@ -31,16 +37,12 @@ public SearchGitHub(Requests requests) {
this.gson = new Gson();
}

public List<Project> getProjects(String term, int page)
throws SearchException {
private List<Project> gatherProjects(String url) {
try {
List<Project> projects = new ArrayList<Project>();
String searchUrl = root
+ String.format(
"/legacy/repos/search/%s?start_page=%s&language=java",
requests.encodeURL(term), page);

String json = requests.get(searchUrl);
String json = requests.get(url);

JsonObject jsonObject = gson.fromJson(json, JsonElement.class).getAsJsonObject();
JsonArray jsonArray = jsonObject.get("repositories").getAsJsonArray();

Expand All @@ -58,9 +60,38 @@ public List<Project> getProjects(String term, int page)
}
}

public List<Project> getProjects(String term, int page)
throws SearchException {
term = requests.encodeURL(term);
String searchUrl = String.format("%s/legacy/repos/search/%s?start_page=%s&language=java", root, term, page);
return gatherProjects(searchUrl);
}

@Override
public List<Project> getProjects(String term, String username, int page)
throws SearchException {
return getProjects(term, page);

try {
term = requests.encodeURL(term);
String url = String.format("%s/repos/%s/%s", root, username, term);

String json = requests.get(url);

Project p = new GsonBuilder().excludeFieldsWithModifiers(Modifier.VOLATILE).create().fromJson(json, Project.class);
p.setSCM(SCM.GIT);
p.setScmURL(String.format("git://github.com/%s/%s.git", username, p.getName()));

return Lists.newArrayList(p);

} catch (Throwable e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) throws FileNotFoundException {
String json = new Scanner(new File("/home/ghlp/Desktop/example.json")).nextLine();
Project p = new GsonBuilder().excludeFieldsWithModifiers(Modifier.VOLATILE).create().fromJson(json, Project.class);
System.out.println(p.getName());
}
}

0 comments on commit 7abcafb

Please sign in to comment.