Switch to side-by-side view

--- a/OSSEval/OpenSourceProject/doap.py
+++ b/OSSEval/OpenSourceProject/doap.py
@@ -11,10 +11,11 @@
 # Author: Davide Galletti 
 
 
-
-from utils import Configuration, Forges
+import urllib2, json
+from utils import Configuration, Forges, StringList, StringHelper
 
 from django.db import connections
+
 
 class Apache():
 
@@ -26,67 +27,60 @@
         cursor.execute("SELECT idDoap, Name, Shortdesc, Description, Homepage FROM apache_doap WHERE LOWER(name) LIKE '%%" + name.lower() + "%%' LIMIT %(limit)s", parameters)
         results = cursor.fetchall()
         for record in results:
-            fp = FlossmoleProject()
-            fp.id = record[0]
-            fp.id_forge = Forges.FSF
-            fp.datasource_id = record[1]
-            fp.name = record[2]
-            fp.description = record[3]
-            project_list.append(fp)
+            dap = DoapApacheProject()
+            dap.id = record[0]
+            dap.id_forge = Forges.APACHE
+            dap.datasource_id = 0
+            dap.name = record[1]
+            dap.shortdesc = record[2]
+            dap.description = record[3]
+            dap.homepage = record[4]
+            project_list.append(dap)
         return project_list
 
+
     @staticmethod
     def getProjectInfo(project_identifier):
-        info = {}
+        dep = DoapApacheProject()
+        dep.load_from_db(project_identifier)
+        return { "doap" : dep.getDict() } 
+
+class Eclipse():
+
+    @staticmethod
+    def search(name):
+        project_list = []
         cursor = connections['flossmole'].cursor()
-        cursor.execute("SELECT max(datasource_id) FROM fsf_projects")
-        row = cursor.fetchone()
-        datasource_id = row[0]
-        parameters = { 'datasource_id': datasource_id, 'project_identifier': project_identifier } 
-        cursor.execute("SELECT proj_long_name, desc_long, url, real_url, released_on, proj_num FROM fsf_projects WHERE proj_unixname=%(project_identifier)s and datasource_id=%(datasource_id)s", parameters)
-        result = cursor.fetchone()
-        info['name'] = result[0]
-        info['desc_long'] = result[1]
-        info['url'] = result[2]
-        info['real_url'] = result[3]
-        info['released_on'] = result[4]
-        proj_num = result[5]
-        
-        info['licenses'] = []
-        parameters = { 'datasource_id': datasource_id, 'proj_num': proj_num }
-        cursor.execute("SELECT license FROM fsf_project_licenses WHERE proj_num=%(proj_num)s  and datasource_id=%(datasource_id)s", parameters)
+        parameters = { 'limit': Configuration.max_number_of_records } 
+        cursor.execute("SELECT idDoap, Name, Shortdesc, Description, Homepage FROM eclipse_doap WHERE LOWER(name) LIKE '%%" + name.lower() + "%%' LIMIT %(limit)s", parameters)
         results = cursor.fetchall()
         for record in results:
-            info['licenses'].append(record[0])
-
-        info['developers'] = []
-        cursor.execute("SELECT person_name, role, email FROM fsf_developer_projects WHERE proj_num=%(proj_num)s  and datasource_id=%(datasource_id)s", parameters)
-        results = cursor.fetchall()
-        for record in results:
-            developer = {}
-            developer['person_name'] = record[0]
-            developer['role'] = record[1]
-            developer['email'] = record[2]
-            info['developers'].append(developer)
-
-        info['categories'] = []
-        parameters = { 'datasource_id': datasource_id, 'proj_num': proj_num }
-        cursor.execute("SELECT project_category_title FROM fsf_project_categories WHERE proj_num=%(proj_num)s  and datasource_id=%(datasource_id)s", parameters)
-        results = cursor.fetchall()
-        for record in results:
-            info['categories'].append(record[0])
+            dep = DoapEclipseProject()
+            dep.id = record[0]
+            dep.id_forge = Forges.ECLIPSE
+            dep.datasource_id = 0
+            dep.name = record[1]
+            dep.shortdesc = record[2]
+            dep.description = record[3]
+            dep.homepage = record[4]
+            project_list.append(dep)
+        return project_list
+
+    @staticmethod
+    def getProjectInfo(project_identifier):
+        dep = DoapEclipseProject()
+        dep.load_from_db(project_identifier)
         
-        info['requirements'] = []
-        cursor.execute("SELECT requirement, requirement_type FROM fsf_project_requirements WHERE proj_num=%(proj_num)s  and datasource_id=%(datasource_id)s", parameters)
-        results = cursor.fetchall()
-        for record in results:
-            requirement = {}
-            requirement['requirement'] = record[0]
-            requirement['requirement_type'] = record[1]
-            info['requirements'].append(requirement)
-
-        return info
-
+        # I try to get metrics from Bitergia's Grimoire
+        try:
+            # http://dashboard.castalia.camp/projects/modeling.emf.compare_metrics.json
+            response = urllib2.urlopen("http://dashboard.castalia.camp/projects/" + dep.name + "_metrics.json")
+            metrics_json = response.read()
+            metrics_decoded = json.loads(metrics_json)
+            
+            return { "doap" : dep.getDict(), "metrics_decoded": metrics_decoded } 
+        except:
+            return { "doap" : dep.getDict() } 
 
 class FoafPerson:
     def __init__(self, firstName, lastName, login = ""):
@@ -139,18 +133,13 @@
         if hasattr(self, "git_repository") and (not (self.git_repository is None)):
             self.git_repository.parent_id_doap = self._idDoap
         
-
     def load_from_db(self, idDoap):
         self.idDoap = idDoap
         try:
-            # expression for modified, modified_release is due to a bug in mysqldb for BIT data type
-            cursor = CrawlerDatabase.execute_cursor("SELECT name, shortdesc, description, homepage, created, mailing_list, download_page, bug_database, platform, service_endpoint, audience, blog,  IF(modified=1,1,0) as modified, old_homepage, category, license, download_mirror, wiki, programming_language, os, language, idDWBatch, idProject, IF(modified_release=1,1,0) as modified_release FROM Doap WHERE idDoap="+str(idDoap))
+            cursor = connections['flossmole'].cursor()
+            cursor.execute("SELECT name, shortdesc, description, homepage, created, mailing_list, download_page, bug_database, platform, service_endpoint, audience, blog,  old_homepage, category, license, download_mirror, wiki, programming_language, os, language FROM " + self.table_prefix + "_doap WHERE idDoap="+str(idDoap))
             result = cursor.fetchone()
-            #for each project in the batch
-            if (result is None):
-                #throw
-                pass
-            else:
+            if not (result is None):
                 self.name = result[0]
                 self.shortdesc = result[1]
                 self.description = result[2]
@@ -163,21 +152,18 @@
                 self.service_endpoint = result[9]
                 self.audience = result[10]
                 self.blog = result[11]
-                self.modified = True if result[12] == b'\x00' else False
-                self.modified_release = True if result[23] == b'\x00' else False
-                self.old_homepage = StringList().load_base64(result[13]).plain
-                self.category = StringList().load_base64(result[14]).plain
-                self.license = StringList().load_base64(result[15]).plain
-                self.download_mirror = StringList().load_base64(result[16]).plain
-                self.wiki = StringList().load_base64(result[17]).plain
-                self.programming_language = StringList().load_base64(result[18]).plain
-                self.os = StringList().load_base64(result[19]).plain
-                self.language = StringList().load_base64(result[20]).plain
-                self.idDWBatch = result[21]
-                self.idProject = result[22] 
+                self.old_homepage = StringList().load_base64(result[12]).plain
+                self.category = StringList().load_base64(result[13]).plain
+                self.license = StringList().load_base64(result[14]).plain
+                self.download_mirror = StringList().load_base64(result[15]).plain
+                self.wiki = StringList().load_base64(result[16]).plain
+                self.programming_language = StringList().load_base64(result[17]).plain
+                self.os = StringList().load_base64(result[18]).plain
+                self.language = StringList().load_base64(result[19]).plain
                 #DoapVersion Table
                 self.release = []
-                cur = CrawlerDatabase.execute_cursor("SELECT platform, revision, file_release, created, name FROM DoapVersion WHERE idDoap=" + str(self.idDoap))
+                cur = connections['flossmole'].cursor()
+                cur.execute("SELECT platform, revision, file_release, created, name FROM " + self.table_prefix + "_DoapVersion WHERE idDoap=" + str(self.idDoap))
                 results = cur.fetchall()
                 for record in results:
                     dv = DoapVersion()
@@ -188,7 +174,8 @@
                     dv.file_release = StringList().load_base64(record[2]).plain  
                     self.release.append(dv)
                 #DoapRepository Table 
-                cur = CrawlerDatabase.execute_cursor("SELECT browse, anon_root, location, type FROM DoapRepository WHERE idDoap=" + str(self.idDoap))
+                cur = connections['flossmole'].cursor()
+                cur.execute("SELECT browse, anon_root, location, type FROM " + self.table_prefix + "_DoapRepository WHERE idDoap=" + str(self.idDoap))
                 results = cur.fetchall()
                 for record in results:
                     dr = DoapRepository(self)
@@ -223,7 +210,8 @@
                 parameters = {
                               'idDoapProject': idDoap
                               }
-                cur = CrawlerDatabase.execute_cursor("SELECT fp.firstName, fp.lastName, fp.login, dpfp.idDoapRole FROM FoafPerson fp JOIN DoapProjectFoafPerson dpfp on fp.idFoafPerson=dpfp.idFoafPerson WHERE idDoapProject=%(idDoapProject)s", parameters)
+                cur = connections['flossmole'].cursor()
+                cur.execute("SELECT fp.firstName, fp.lastName, fp.login, dpfp.idDoapRole FROM " + self.table_prefix + "_FoafPerson fp JOIN " + self.table_prefix + "_DoapProjectFoafPerson dpfp on fp.idFoafPerson=dpfp.idFoafPerson WHERE idDoapProject=%(idDoapProject)s", parameters)
                 results = cur.fetchall()
                 for record in results:
                     fp = FoafPerson(record[0], record[1], record[2])
@@ -241,4 +229,28 @@
                     elif idDoapRole == 6:
                         self.helper.append(fp)
         except Exception, e:
-            Logger.error(str(e))
+            print(str(e))
+
+    def getDict(self):
+        dic_var = self.__dict__.copy()
+        dic_var['maintainer'] = []
+        for m in self.maintainer:
+            dic_var['maintainer'].append(m.__dict__)
+        dic_var['release'] = []
+        for r in self.release:
+            dic_var['release'].append(r.__dict__)
+        if hasattr(self, 'svn_repository'): 
+            dic_var['svn_repository'] = self.svn_repository.__dict__
+
+        return dic_var
+
+
+class DoapApacheProject(DoapProject):
+    def __init__(self):
+        super(DoapApacheProject, self).__init__()
+        self.table_prefix = "apache"
+
+class DoapEclipseProject(DoapProject):
+    def __init__(self):
+        super(DoapEclipseProject, self).__init__()
+        self.table_prefix = "eclipse"