-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
301 lines (262 loc) · 10.4 KB
/
analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import sys
import re
import subprocess
from pythonic import completeAnalysis
from pyapp.models import ProjectStatus, Repository, Result, EmailNotify
from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist
import urllib2
import json
from django.db import transaction
import idiomsInfo
import time
from django.core.mail import EmailMultiAlternatives
def log(msg, level=0):
"""
Print a message with differents levels of importance
0: trace, 1: info, 2: warn, 3: error
"""
color = ['\033[0m', '\033[94m', '\033[93m', '\033[91m']
if level < 0 or level > 3: level = 0
print color[level] + msg + '\033[0m'
sys.stdout.flush()
def isPythonFile(filepath):
"""
Is the path passed a Python file?
"""
isPython = False
(root, ext) = os.path.splitext(filepath)
if ext == '.py':
isPython = True
elif ext == '':
with open(filepath, 'r') as file:
isPython = bool(re.search('#!.*python', file.readline()))
return isPython
def getPyFilesIn(in_dir):
filesList = []
for root, dirs, files in os.walk(in_dir):
if re.search('\/.git\/', root):
continue
for name in files:
filepath = os.path.join(root, name)
if not os.path.islink(filepath) and isPythonFile(filepath):
filesList.append(filepath)
return filesList
def downloadRepo(url, dir_dwnld):
"""
Download the repository url to the dirRepo directory.
The url must be in format https://github.com/user/repository.git
If the repository is downloaded return True, otherwise, False
"""
if not os.path.exists(dir_dwnld):
try:
os.mkdir(dir_dwnld)
except OSError as e:
raise Exception("Cannot clone into {0}: {1}".format(dir_dwnld, e))
repoName = url.split('/')[-1][:-4]
userName = url.split('/')[-2]
clonedName = dir_dwnld + '/' + userName + "-" + repoName
urlInfoRepo = "https://api.github.com/repos/%s/%s" % (userName, repoName)
response = callGitHubAPI(urlInfoRepo)
if response.get("private", True):
return False
if os.path.exists(clonedName):
log("---WARNING---: {0}: repository already exists".format(clonedName), 2)
else:
ret = os.system('git clone --depth 1 {0} {1}'.format(url, clonedName))
if ret == 0:
log("{0}: Downloaded repository".format(clonedName), 1)
else:
log("Problems downloading the repository", 2)
return False
return True
def analyze(repo_url, onlymodified=False):
"""
- Guiven a url for a GitHub repository analyze it and returns an object.
- url must be in format https://github.com/user/repository.git
- DELETe all the previuos results
"""
clone_location = '/tmp/repositories'
ProjectStatus.objects.filter(projectUrl=repo_url).delete()
repoEntryDB = ProjectStatus(projectUrl=repo_url, status="Downloading repository...")
repoEntryDB.save()
if not downloadRepo(repo_url, clone_location):
repoEntryDB.status = "Repository unavailable"
repoEntryDB.save()
return
repoEntryDB.status = "Preparing..."
repoEntryDB.save()
repo_name = repo_url.split('/')[-1][:-4]
user_name = repo_url.split('/')[-2]
cloned_name = user_name + "-" + repo_name
python_files = getPyFilesIn(clone_location + '/' + cloned_name)
if onlymodified == 1:
python_files = filter_files(python_files)
total_files = len(python_files)
files_done = 0
ret_data = {
"repository": repo_url,
"idioms": {},
"antiidioms": {}
}
repoEntryDB.status = "Analyzing..."
repoEntryDB.totalFiles = total_files
repoEntryDB.save()
for file_name in python_files:
print "ANALYZING %s" % file_name
try:
(results, resultsAnti) = completeAnalysis(file_name)
except Exception as e:
log("******" + str(e) + "******", 3)
continue
abbrFileName = file_name.split(clone_location + '/' + cloned_name)[1]
for idiom in results:
if not idiom.name in ret_data['idioms']:
ret_data['idioms'][idiom.name] = []
actualIdiom = ret_data['idioms'][idiom.name]
for idiomItem in idiom.where:
idiomItem['file_name'] = abbrFileName
actualIdiom.append(idiomItem)
for idiomanti in resultsAnti:
if not idiomanti.name in ret_data['antiidioms']:
ret_data['antiidioms'][idiomanti.name] = []
actualIdiom = ret_data['antiidioms'][idiomanti.name]
for idiomItem in idiomanti.where:
idiomItem['file_name'] = abbrFileName
actualIdiom.append(idiomItem)
files_done += 1
porcentage = (float(files_done)/total_files)*100
repoEntryDB.analyzedFiles = files_done
repoEntryDB.save()
print "{0}% completed. {1}/{2} files.".format(porcentage, files_done, total_files)
#analysisResults = analyze_idioms(ret_data)
repoEntryDB.status = "Analyzed"
repoEntryDB.save()
saveAnalysis(ret_data)
return ret_data
@transaction.atomic
def saveAnalysis(data):
try:
repoEntry = Repository.objects.get(url=data['repository'])
except ObjectDoesNotExist:
repoEntry = Repository(url=data['repository'])
repoEntry.save() # update date and time
# delete previous analysis and store the new one
Result.objects.filter(repository=repoEntry).delete()
for idiom in data["idioms"]:
for item in data["idioms"][idiom]:
res = Result(repository=repoEntry, idiomName=idiom, line=item['line'],
file=item['file_name'], author=item['author'], method=item.get('method', ''))
res.save()
def analyze_data(data):
"""
Make an analysis about de idioms and return some marks
"""
beginner_list = idiomsInfo.new_beginner_list()
intermediate_list = idiomsInfo.new_itermediate_list()
advanced_list = idiomsInfo.new_advanced_list()
for idiom in beginner_list:
idiom["times"] = len(data['idioms'].get(idiom["name"], []))
for idiom in intermediate_list:
idiom["times"] = len(data['idioms'].get(idiom["name"], []))
for idiom in advanced_list:
idiom["times"] = len(data['idioms'].get(idiom["name"], []))
return {'beginner': beginner_list,
'intermediate': intermediate_list,
'advanced': advanced_list}
def analyze_data_b(repositories_data):
"""
Make an analysis about de idioms and return some marks
"""
beginner_list = idiomsInfo.new_beginner_list()
intermediate_list = idiomsInfo.new_itermediate_list()
advanced_list = idiomsInfo.new_advanced_list()
analysis_return = {
"beginner": {
"total": 0,
"found": 0,
"know": [],
"learn": []
}, "intermediate": {
"total": 0,
"found": 0,
"know": [],
"learn": []
}, "advanced": {
"total": 0,
"found": 0,
"know": [],
"learn": []
}}
# count each idioms
for repo_data in repositories_data:
for idiom in beginner_list:
idiom["times"] += len(repo_data['idioms'].get(idiom["name"], []))
for idiom in intermediate_list:
idiom["times"] += len(repo_data['idioms'].get(idiom["name"], []))
for idiom in advanced_list:
idiom["times"] += len(repo_data['idioms'].get(idiom["name"], []))
# split in know and learn idioms
analysis_return["beginner"]["know"] = [idiom for idiom in beginner_list if idiom["times"] > 0]
analysis_return["beginner"]["learn"] = [idiom for idiom in beginner_list if idiom["times"] == 0]
analysis_return["intermediate"]["know"] = [idiom for idiom in intermediate_list if idiom["times"] > 0]
analysis_return["intermediate"]["learn"] = [idiom for idiom in intermediate_list if idiom["times"] == 0]
analysis_return["advanced"]["know"] = [idiom for idiom in advanced_list if idiom["times"] > 0]
analysis_return["advanced"]["learn"] = [idiom for idiom in advanced_list if idiom["times"] == 0]
#count idioms of each category
analysis_return["beginner"]["total"] = len(beginner_list)
analysis_return["intermediate"]["total"] = len(intermediate_list)
analysis_return["advanced"]["total"] = len(advanced_list)
analysis_return["beginner"]["found"] = len(analysis_return["beginner"]["know"])
analysis_return["intermediate"]["found"] = len(analysis_return["intermediate"]["know"])
analysis_return["advanced"]["found"] = len(analysis_return["advanced"]["know"])
return analysis_return
def filter_files(files):
currentDir = os.getcwd()
new_list = []
for filepath in files:
fileName = filepath.split('/')[-1]
fileLocation = '/'.join(filepath.split('/')[:-1])
os.chdir(fileLocation)
command = 'git log --oneline'.split(' ')
command.append(fileName)
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE);
(data, error) = proc.communicate()
if len(data.splitlines()) > 1:
new_list.append(filepath)
os.chdir(currentDir)
return new_list
def callGitHubAPI(url):
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError as error:
print error
return {}
return json.load(response)
def control_threads(threads, url):
print url
while len(threads) > 0:
for th in threads:
if not th.isAlive():
print th.name, "STOPPED"
ps = ProjectStatus.objects.get(projectUrl=th.name)
if ps.status != "Analyzed" and ps.status != "Repository unavailable":
ps.status = "Error, try again"
ps.save()
threads.remove(th)
else:
print th.name, "RUNNING"
time.sleep(15) #TODO: Increment
notifs = EmailNotify.objects.filter(url=url)
for n in notifs:
print n.email
subject, from_email, to = 'Pythonic analysis', '[email protected]', n.email
text_content = 'You can see the results of the analysis following this link: ' + url
html_content = 'You can see the results of the analysis following this link: ' + url
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
notifs.delete()