|
| 1 | +import subprocess #para executar comandos no prompt |
| 2 | +import psycopg2 #para conexao com o postgres |
| 3 | +import sys #utilizar parametros de entrada |
| 4 | + |
| 5 | +#PARAMETROS NA LINHA DE COMANDO |
| 6 | +#Para executar esse script: python2.7 mining.py PARAM1 PARAM2 |
| 7 | +#PARAM1 = nome do projeto como sera criado no postgres |
| 8 | +#PARAM2 = 0 o nome da busca na base do GitCProc sera o mesmo do PARAM1 |
| 9 | +# NOME_DO_PROJETO no GitCProc, quando o nome do projeto for muito extenso |
| 10 | + |
| 11 | +#banco de dados onde sera gravada a analise |
| 12 | +database='FerramentaAutomatica' |
| 13 | +user='postgres' |
| 14 | +password='toor' |
| 15 | +schema='public' |
| 16 | + |
| 17 | +#banco de dados onde foram gravados os dados do gitCproc |
| 18 | +databaseBugs='covninfo' |
| 19 | +userBugs='postgres' |
| 20 | +passwordBugs='toor' |
| 21 | +schemaBugs='covninfo' |
| 22 | + |
| 23 | +#nome do projeto |
| 24 | +projeto = str(sys.argv[1]) |
| 25 | + |
| 26 | +class Conexao(object): |
| 27 | + _db=None |
| 28 | + |
| 29 | + def __init__(self, mhost, db, usr, pwd): |
| 30 | + self._db = psycopg2.connect(host=mhost, database=db, user=usr, password=pwd) |
| 31 | + |
| 32 | + def manipular(self, sql): |
| 33 | + try: |
| 34 | + cur=self._db.cursor() |
| 35 | + cur.execute(sql) |
| 36 | + cur.close() |
| 37 | + self._db.commit() |
| 38 | + except: |
| 39 | + return False |
| 40 | + return True |
| 41 | + |
| 42 | + def consultar(self, sql): |
| 43 | + rs=None |
| 44 | + try: |
| 45 | + cur=self._db.cursor() |
| 46 | + cur.execute(sql) |
| 47 | + rs=cur.fetchall() |
| 48 | + except: |
| 49 | + return None |
| 50 | + return rs |
| 51 | + |
| 52 | + def proximaPK(self, tabela, chave): |
| 53 | + try: |
| 54 | + sql='select max('+chave+') from '+tabela |
| 55 | + rs = self.consultar(sql) |
| 56 | + pk = rs[0][0] |
| 57 | + return pk+1 |
| 58 | + except: |
| 59 | + return 1 |
| 60 | + |
| 61 | + def fechar(self): |
| 62 | + self._db.close() |
| 63 | + |
| 64 | +def identificarData(tag):#atraves do comando git show |
| 65 | + cmd = "git show | egrep -m 1 'Date:'" #pegar somente a linha onde existe a data atraves do comando git show |
| 66 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 67 | + ps.wait() |
| 68 | + data = ps.communicate()[0] |
| 69 | + |
| 70 | + data = str(data).replace("Date: ","") |
| 71 | + data = str(data).replace("Sun ", "") |
| 72 | + data = str(data).replace("Mon ", "") |
| 73 | + data = str(data).replace("Tue ", "") |
| 74 | + data = str(data).replace("Wed ", "") |
| 75 | + data = str(data).replace("Thu ", "") |
| 76 | + data = str(data).replace("Fri ", "") |
| 77 | + data = str(data).replace("Sat ", "") |
| 78 | + |
| 79 | + data = str(data).replace("Jan", "01") |
| 80 | + data = str(data).replace("Feb", "02") |
| 81 | + data = str(data).replace("Mar", "03") |
| 82 | + data = str(data).replace("Apr", "04") |
| 83 | + data = str(data).replace("May", "05") |
| 84 | + data = str(data).replace("Jun", "06") |
| 85 | + data = str(data).replace("Jul", "07") |
| 86 | + data = str(data).replace("Aug", "08") |
| 87 | + data = str(data).replace("Sep", "09") |
| 88 | + data = str(data).replace("Oct", "10") |
| 89 | + data = str(data).replace("Nov", "11") |
| 90 | + data = str(data).replace("Dec", "12") |
| 91 | + |
| 92 | + #extrair a data que esta no formato abaixo |
| 93 | + #01 28 13:35:52 2020 -0500 |
| 94 | + #02 1 22:48:09 2014 +0000 |
| 95 | + diaMes = data[0:str(data).rfind(":")-5] |
| 96 | + diaMes = diaMes.split(" ") |
| 97 | + mes = diaMes[0] |
| 98 | + dia = diaMes[1] |
| 99 | + #ano = data[str(data).rfind("-")-5:str(data).rfind("-")-1] |
| 100 | + ano = data[str(data).rfind(":") + 4:str(data).rfind(":") + 8] |
| 101 | + |
| 102 | + data = ano+ "-" + mes + "-" + dia |
| 103 | + return data |
| 104 | + |
| 105 | +def identificarLOC():#atraves do da tool cloc |
| 106 | + cmd = "cloc ../" + projeto + " | egrep -m 1 'Java'" # contar o numero de testes na tag |
| 107 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 108 | + ps.wait() |
| 109 | + loc = ps.communicate()[0] |
| 110 | + |
| 111 | + if loc[0] != 'J': |
| 112 | + loc = str(loc)[str(loc).rfind("Java "):len(loc)] |
| 113 | + loc = str(loc)[str(loc).rfind(' '):len(str(loc)) - 1] # retornar somente o numero referente ao LOC de java |
| 114 | + |
| 115 | + return loc |
| 116 | + |
| 117 | +def criarSql(tag, testes, bugs, contrib, loc): |
| 118 | + data=identificarData(tag) |
| 119 | + sql = "insert into " + schema + "." + projeto + " values (default," |
| 120 | + sql = sql + "'" + projeto + "'," |
| 121 | + sql = sql + "'" + str(tag) + "'," |
| 122 | + sql = sql + "'" + str(data) + "'," |
| 123 | + sql = sql + str(testes) + "," |
| 124 | + sql = sql + str(bugs) + "," |
| 125 | + sql = sql + str(contrib) + "," |
| 126 | + sql = sql + str(loc) + ")" |
| 127 | + return sql |
| 128 | + |
| 129 | +#apaga a tabela do projeto no banco de dados postgres |
| 130 | +con=Conexao('localhost',database,user,password) |
| 131 | +sql = "drop table "+ schema + "." + projeto |
| 132 | +if con.manipular(sql): |
| 133 | + print('tabela '+ schema + "." + projeto + ' DESTRUIDA com sucesso!') |
| 134 | +con.fechar() |
| 135 | + |
| 136 | +#cria a tabela do projeto no banco de dados postgres |
| 137 | +con=Conexao('localhost',database,user,password) |
| 138 | +sql = 'create table ' + schema + "." + projeto + ' (id serial primary key, projeto varchar(100), tag varchar(100), data date, testes integer, bugs integer, contributors integer, loc integer)' |
| 139 | +if con.manipular(sql): |
| 140 | + print('tabela '+ schema + "." + projeto + ' CRIADA com sucesso!') |
| 141 | +con.fechar() |
| 142 | + |
| 143 | +#lista todas as tags de um projeto e armazena na variavel tags |
| 144 | +list_tags = subprocess.Popen(["git", "tag"], stdout=subprocess.PIPE) |
| 145 | +tags=set(list_tags.stdout) |
| 146 | +count=1 |
| 147 | +con=Conexao('localhost',database,user,password) |
| 148 | +for tag in tags: |
| 149 | + tag = str(tag).replace("\n", "") |
| 150 | + |
| 151 | + #git checkout - b [nomeQueQuiser] [tag] |
| 152 | + cmd = "git checkout -b t"+ tag + " " + tag # criar o branch da tag e fazer o checkout |
| 153 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 154 | + ps.wait() |
| 155 | + |
| 156 | + # git checkout - b [nomeQueQuiser] |
| 157 | + cmd = "git checkout -f t" + tag # mudar de tag |
| 158 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 159 | + ps.wait() |
| 160 | + |
| 161 | + #git shortlog -s -n | wc -l |
| 162 | + cmd = "git shortlog -s -n | wc -l" #contar o numero de contributors na tag |
| 163 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 164 | + ps.wait() |
| 165 | + contributors = ps.communicate()[0] |
| 166 | + contributors = str(contributors).replace("\n", "") |
| 167 | + |
| 168 | + # python3 script.py |
| 169 | + cmd = "python3 script.py "+ projeto # contar o numero de testes na tag |
| 170 | + ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 171 | + ps.wait() |
| 172 | + testes = ps.communicate()[0] |
| 173 | + testes = str(testes).replace("\n", "") |
| 174 | + |
| 175 | + # executar cloc tool para identificar linhas de codigo java |
| 176 | + loc = identificarLOC() #extrair somente o loc de java |
| 177 | + |
| 178 | + sql=criarSql(tag,testes,-1,contributors,loc) |
| 179 | + print(sql) |
| 180 | + if con.manipular(sql): |
| 181 | + print('%d de %d inserido com sucesso!'%(count,len(tags))) |
| 182 | + count=count+1 |
| 183 | +con.fechar() |
| 184 | + |
| 185 | +#acessar a base de mining, ler as datas das tags |
| 186 | +#criar uma sql para buscar os bugs no BD do gitCproc |
| 187 | +#fazer o update na base de mining com o numero de bugs para a tag |
| 188 | +con=Conexao('localhost',database,user,password) |
| 189 | + |
| 190 | +rs=con.consultar("SELECT * FROM " + schema + "." + projeto + " ORDER BY data, tag") |
| 191 | +vetorDatas = list() |
| 192 | +total=-1 |
| 193 | +for linha in rs: #preencher um vetor com as datas das releases |
| 194 | + vetorDatas.append(linha[3]) |
| 195 | + total=total+1 |
| 196 | + |
| 197 | +count = 0 |
| 198 | +cbugs=0 |
| 199 | +for linha in rs: |
| 200 | + tag=linha[2] |
| 201 | + dataInicial = linha[3] |
| 202 | + if count<total: |
| 203 | + dataFinal = vetorDatas[count+1] |
| 204 | + count=count+1 |
| 205 | + else: |
| 206 | + dataFinal = '2050-01-01' #data final para a tag mais recente |
| 207 | + |
| 208 | + #conectando ao BD do gitCproc |
| 209 | + projetoGitCProc=projeto |
| 210 | + if str(sys.argv[2]) != '0': |
| 211 | + projetoGitCProc = str(sys.argv[2]) |
| 212 | + conBugs = Conexao('localhost', databaseBugs, userBugs, passwordBugs) |
| 213 | + sql="SELECT count(project) FROM "+ schemaBugs + ".change_summary WHERE is_bug=true AND commit_date >= '" + str(dataInicial) + "' AND commit_date < '"+ str(dataFinal) + "' AND project = '"+ projetoGitCProc + "'" |
| 214 | + print(sql) |
| 215 | + rsBugs = conBugs.consultar(sql) |
| 216 | + bug=int(rsBugs[0][0]) |
| 217 | + conBugs.fechar() |
| 218 | + |
| 219 | + cbugs=cbugs+bug |
| 220 | + |
| 221 | + #atualizar a tabela mining com os bugs para cada tag |
| 222 | + conUpdate = Conexao('localhost', database, user, password) |
| 223 | + sql="UPDATE " + schema + "." + projeto + " set bugs = " + str(bug) + " where tag = '" + str(tag)+ "'" |
| 224 | + if conUpdate.manipular(sql): |
| 225 | + print(sql) |
| 226 | + print('Bugs atualizados com sucesso!') |
| 227 | + print('Total de bugs %d'%(cbugs)) |
| 228 | + conUpdate.fechar() |
| 229 | + |
| 230 | +con.fechar() |
0 commit comments