-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·912 lines (787 loc) · 33.6 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import sys
import json
import time
import datetime
import requests
import unicodedata
import pathos.pools as mp
import pymongo as Pymongo
import multiprocessing as mtp
from itertools import chain
from collections import Counter
from pymongo import MongoClient
from joblib import Parallel, delayed
try:
from Octopus import crawler, search, repository
except ImportError as error:
raise ImportError(error)
try:
from CrawlerLibs import follow, cyclopslair
except ImportError as error:
raise ImportError(error)
authentication = []
"""add here authenticates keys"""
#authentication.append({"client_id":'',"client_secret":''})
projectUserAssociation = {"COMMITTER":"committers","MEMBER":"members","OWNER":"owner","CONTRIBUTOR":"contributors","COLLABORATOR":"collaborators"}
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
def getInfo(projetoName,authen):
client = MongoClient('localhost', 27017)
inicio = time.time()
arg = projetoName.split("/")
namerepo = ""
if "." in arg[1]:
namerepo = arg[1].replace(".","Dot")
else:
namerepo = arg[1]
db = client[namerepo]
stateClose = "closed"
stateOpen = "open"
crawlerP = crawler.Crawler(authen)
repo_object = repository.Repository(arg[0], arg[1], crawlerP)
commits = repo_object.commits()
for item in commits:
try:
db.commits.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
pullRequestsOpen = repo_object.pull_requests(stateOpen)
for item in pullRequestsOpen:
try:
db.pullrequests.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
pullRequestsClosed = repo_object.pull_requests(stateClose)
for item in pullRequestsClosed:
try:
db.pullrequests.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
contributors = repo_object.contributors()
for item in contributors:
try:
db.contributors.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
issuesOpen = repo_object.issues(stateOpen)
for item in issuesOpen:
try:
db.issues.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
issuesClose = repo_object.issues(stateClose)
for item in issuesClose:
try:
db.issues.insert(item)
except TypeError, e:
print 'I got a TypeError - reason "%s"' % str(e)
pass
def getLoginAssociation(userObj):
try:
login = userObj.get("user").get("user").get("login")
association = userObj.get("user").get("author_association")
if association == "NONE":
association = "COMMITTER"
elif login == owner:
association = "OWNER"
aux ={
"login":login,
"association":association
}
return aux
except KeyError, e:
print ('I got a KeyError - reason "{}"').format(str(e))
except IndexError, e:
print 'I got an IndexError - reason "%s"' % str(e)
def getAllContributors(owner,repo):
client = MongoClient('localhost', 27017)
print "Get all Users ......."
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
cursor = list(db.contributors.find({},{"_id":0,"login":1}))
userType = ["userAssociates","owner","members","contributors","collaborators","committers"]
index_name = "uniqueLogin"
for types in userType:
my_collection = db2[types]
my_collection.create_index([('login',1)], name=index_name, unique=True)
for contributor in cursor:
data = byteify(contributor)
try:
db2.committers.insert({"login":data.get("login"),"association":"COMMITTER"})
except Pymongo.errors.DuplicateKeyError:
continue
final = db.pullrequests.aggregate([
{"$sort": { "user.login": 1}},
{"$group":{
"_id": "$user.login",
"user": { "$last": "$$ROOT" }
}
}
],allowDiskUse=True)
final = list(final)
print "Extract Associations ....."
results = Parallel(n_jobs=mtp.cpu_count())(delayed(getLoginAssociation)(x) for x in final)
for user in results:
login = str(user.get("login"))
associ = user.get("association")
try:
association = user.get("association")
login = login.strip()
owner = owner.strip()
if(login == owner):
association = "OWNER"
userDB = projectUserAssociation.get(association)
db2[userDB].insert({"login":login,"association":associ})
db2.userAssociates.insert({"login":login,"association":associ})
except Pymongo.errors.DuplicateKeyError:
continue
userType = ["owner","members","contributors","collaborators"]
for types in userType:
aux = db2[types].find({})
for x in aux:
try:
db2.committers.delete_one({"login":x.get("login")})
except Pymongo.errors.OperationFailure,e:
print e
continue
def getAllComments(owner,repo):
client = MongoClient('localhost', 27017)
db = client[repo]
#get issues with range less than 30 elements
allIssuesComments = list(db.issues.find({"comments": {"$exists":True,"$lt":31,"$gt":0}}))
print len(allIssuesComments)
resultList = cyclopslair.getIssuesCommentsList(allIssuesComments,owner,repo)
for issue in allIssuesComments:
issue["number"]
try:
db.issuesComments.insert({"number":issue.get("number"),"qtComments":issue.get("comments"),"comments":resultList.get(issue.get("number"))})
except KeyError, e:
print 'I got a KeyError - reason "%s"' % str(e)
except IndexError, e:
db.issuesComments.insert({"number":issue.get("number"),"qtComments":0,"comments":0})
print 'I got an IndexError - reason "%s"' % str(e)
#get issues with range great than 30 elements
allIssuesComments = list(db.issues.find({"comments": {"$exists":True,"$gte":31}}))
print len(allIssuesComments)
for issue in allIssuesComments:
resultList = cyclopslair.getIssuesCommentsOne(issue.get("comments_url"),owner,repo)
db.issuesComments.insert({"number":issue.get("number"),"qtComments":issue.get("comments"),"comments":resultList})
#get issues with 0 comments
allIssuesComments = list(db.issues.find({"comments": {"$exists":True,"$eq":0}}))
print len(allIssuesComments)
for issue in allIssuesComments:
db.issuesComments.insert({"number":issue.get("number"),"qtComments":0,"comments":0})
def getMentions(issueComments):
pattern = re.compile(r"(\@\w*)")
mentions = []
participants = {}
arrayParticipants = []
comments = issueComments.get("comments")
if comments != None:
for comment in comments:
aux = pattern.findall(comment.get("body"))
if aux != []:
mentions.append(aux)
else:
mentions.append(aux)
try:
if comment.get("user").get("login") in arrayParticipants:
aux = comment.get("user").get("login")
participants[aux] = participants[aux]+1
else:
participants[comment["user"]["login"]] = 1
arrayParticipants.append(comment["user"]["login"])
except KeyError, e:
print 'I got a KeyError - reason "%s"' % str(e)
except IndexError, e:
print 'I got an IndexError - reason "%s"' % str(e)
itemsMentions = list(chain.from_iterable(mentions[i] for i in xrange(len(mentions))))
itemsAux = [i.replace('@', '') for i in itemsMentions]
items = []
for k in itemsAux:
if k != "":
items.append(k)
item = {
"number":issueComments.get("number"),
"participants":participants,
"mentions":items
}
return item
def getIssuesMentions(owner,repo):
print "get Issues Mentions ......"
client = MongoClient('localhost', 27017)
db = client[repo]
allIssueComments = list(db.issuesComments.find({"qtComments": {"$exists":True,"$gt":0}}))
itemsComments = Parallel(n_jobs=mtp.cpu_count())(delayed(getMentions)(x)for x in allIssueComments)
return itemsComments
def getAllCommentsTypes(owner,repo):
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
#All Commenters get here-------
getAllComments(owner,repo)
#------------------------------
commentF = getIssuesMentions(owner,repo)
for commentBox in commentF:
if commentBox != None:
try:
if(commentBox["mentions"] != []):
db.issuesComments.update({"number":commentBox.get("number")},{"$set": {"mentions":commentBox.get("mentions")}})
else:
db.issuesComments.update({"number":commentBox.get("number")},{"$set": {"mentions":0}})
db.issuesComments.update({"number":commentBox.get("number")},{"$set": {"participants":commentBox.get("participants")}})
except Pymongo.errors.OperationFailure,e:
print e
def getlistUsers(owner,repo):
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
allUsers = []
aux = []
allcommitters = list(db2.committers.find({}))
aux.append(allcommitters)
allowner = list(db2.owner.find({}))
aux.append(allowner)
allcontributors = list(db2.contributors.find({}))
aux.append(allcontributors)
allmembers = list(db2.members.find({}))
aux.append(allmembers)
allcollaborators = list(db2.collaborators.find({}))
aux.append(allcollaborators)
allUsers = list(chain.from_iterable(aux[i] for i in xrange(len(aux))))
return allUsers
def codeGetterInfo(owner,repo):
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
allCommits = list(db.commits.find({}))
print len(allCommits)
resultListCommit = cyclopslair.getCommitInfoList(allCommits,owner,repo)
print len(resultListCommit)
for commit in resultListCommit:
db.commitsInfo.insert({"sha":commit.get("sha"),"author":commit.get("author"),"committer":commit.get("committer"),\
"filequantity":commit.get("filequantity"),"deletions":commit.get("deletions"),"additions":commit.get("additions"),\
"totalModified":commit.get("totalModified"),"contentValue":commit.get("contentValue")})
allIssue = list(db.pullrequests.find({}))
print len(allIssue)
resultListIssues = cyclopslair.getPRInfoList(allIssue,owner,repo)
for pr in resultListIssues:
db.prInfo.insert({"number":pr.get("number"),"author":pr.get("author"),"merged":pr.get("merged"),\
"mergedby":pr.get("mergedby"),"filequantity":pr.get("filequantity"),"deletions":pr.get("deletions"),"additions":pr.get("additions"),\
"prState":pr.get("prState"),"mergedInCommit":pr.get("mergedInCommit"),"created_at":pr.get("created_at"),"merged_at":pr.get("merged_at"),\
"totalModified":pr.get("totalModified"),"contentValue":pr.get("contentValue")})
#nao funciona sem a validacao certa, somente se o usuario pesquisado tiver instalado um app que vc criou
#https://developer.github.com/v3/users/followers/#check-if-one-user-follows-another
def closenessOwner(owner,repo):
client = MongoClient('localhost', 27017)
print("closeness in project .....")
dbname = (repo+"_metrics")
db = client[dbname]
ownerList = db.owner.find_one({})
members = list(db.members.find({},{"_id":0}))
users = getlistUsers(owner,repo)
allUsers = list(users)
memberLen = len(members)
ownerLen = 0
if ownerList != None:
ownerLen = 1
if (ownerLen > 0) and (memberLen > 0):
followings = follow.followingsList(ownerList.get("login"))
if followings != 0:
for user in allUsers:
if user in followings:
db.owner.update({"login":ownerList.get("login")},{"$push":{"closeness":user}})
else:
db.owner.update({"login":ownerList.get("login")},{"$push":{"closeness":0}})
memberList = byteify(list(members))
for member in memberList:
results = follow.followingsList(member.get("login"))
if results != 0:
followings = list(results)
flag = False
for user in allUsers:
if user in followings:
print("{}:{}").format(member.get("login"),user.get("login"))
flag = True
db.members.update({"login":member.get("login")},{"$push":{"closeness":user}})
if(flag == False):
db.members.update({"login":member.get("login")},{"$push":{"closeness":0}})
else:
print("{}:{}").format(member.get("login"),0)
db.members.update({"login":member.get("login")},{"$push":{"closeness":0}})
elif(ownerLen > 0) and (memberLen == 0):
followings = follow.followingsList(ownerList.get("login"))
if followings != 0:
for user in allUsers:
if user in followings:
db.owner.update({"login":ownerList.get("login")},{"$push":{"closeness":user}})
else:
db.owner.update({"login":ownerList.get("login")},{"$push":{"closeness":0}})
elif(ownerLen == 0) and (memberLen > 0):
memberList = byteify(list(members))
for member in memberList:
results = follow.followingsList(member.get("login"))
if results != 0:
followings = list(results)
flag = False
for user in allUsers:
if user in followings:
print("{}:{}").format(member.get("login"),user.get("login"))
flag = True
db.members.update({"login":member.get("login")},{"$push":{"closeness":user}})
if(flag == False):
db.members.update({"login":member.get("login")},{"$push":{"closeness":0}})
else:
print("{}:{}").format(member.get("login"),0)
db.members.update({"login":member.get("login")},{"$push":{"closeness":0}})
def daysInProject(user,dbclient):
commitlist = list(dbclient.commits.find({'author.login':user}).sort('commit.author.date', 1).limit(1))
issuelist = list(dbclient.issues.find({'user.login':user}).sort('created_at', 1).limit(1))
firstcommit = 0
firstIssue = 0
datenow = str(datetime.datetime.date(datetime.datetime.now()))
d2 = datetime.datetime.strptime(datenow, "%Y-%m-%d")
if len(commitlist) > 0:
commit = commitlist[0]
date1 = commit.get("commit").get("author").get("date")
date1f = str(date1.split("T")[0])
d1 = datetime.datetime.strptime(date1f, "%Y-%m-%d")
firstcommit = abs((d2 - d1).days)
if len(issuelist) > 0:
issue = issuelist[0]
date1 = issue.get("created_at")
date1f = date1.split("T")[0]
d1 = datetime.datetime.strptime(date1f, "%Y-%m-%d")
firstIssue = abs((d2 - d1).days)
if(firstcommit >= firstIssue):
return firstcommit
else:
return firstIssue
def status(owner,repo):
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db2 = client[dbname]
userList = getlistUsers(owner,repo)
allUsers = list(userList)
followers = follow.followersList(allUsers)
for user in allUsers:
print user["login"]
followersqtd = followers[user["login"]]
try:
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"status": followersqtd}})
except Pymongo.errors.OperationFailure,e:
print e
def sourceOfLearning(owner,repo):
print("sourceOfLearning .....")
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
userMentions = {}
userMentionsArray = []
allUsers = getlistUsers(owner,repo)
for user in allUsers:
allMentionUser = list(db.issuesComments.find({"mentions":user.get("login")}))
if len(allMentionUser) > 0:
userlogin = user.get("login")
for usermention in allMentionUser:
mentionsN = usermention.get("mentions")
occurrence = Counter(mentionsN)
if userlogin in userMentionsArray:
value = occurrence.get(userlogin)
userMentions[userlogin] = userMentions[userlogin]+int(value)
else:
userMentions[userlogin] = int(occurrence.get(userlogin))
userMentionsArray.append(userlogin)
if userlogin in userMentionsArray:
try:
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"sourceOfLearning": userMentions.get(userlogin)}})
except Pymongo.errors.OperationFailure,e:
print e
else:
try:
print user.get("login")
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"sourceOfLearning": 0}})
except Pymongo.errors.OperationFailure,e:
print e
def ParticipationWcode(owner,repo):
print ("participation With Comments .......")
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
allUsers = getlistUsers(owner,repo)
for user in allUsers:
allprOpen = list(db.pullrequests.find({"author.login":user.get("login")}))
qtprOpen = len(allprOpen)
allprClosed = list(db.prInfo.find({"mergedby":user.get("login")}))
qtprClosed = len(allprClosed)
ParticipationWcode = int(qtprClosed+qtprOpen)
if ParticipationWcode > 0:
try:
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"participationWcode": ParticipationWcode}})
except Pymongo.errors.OperationFailure,e:
print e
else:
try:
print user.get("login")
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"participationWcode": 0}})
except Pymongo.errors.OperationFailure,e:
print e
def contentValueInProject(owner,repo):
print ("content Value In Project .......")
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
userCommits = {}
userCommitsArray = []
allUsers = getlistUsers(owner,repo)
for user in allUsers:
allcommitsInfo = list(db.commitsInfo.find({"author":user.get("login")}))
userlogin = user.get("login")
for commitsInfo in allcommitsInfo:
value = float(commitsInfo.get("contentValue"))
if userlogin in userCommitsArray:
userCommits[userlogin] = userCommits[userlogin]+value
else:
userCommits[userlogin] = float(value)
userCommitsArray.append(userlogin)
if userlogin in userCommitsArray:
contentValueInProject = userCommits.get(userlogin)
try:
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"contentValueInProject": contentValueInProject}})
except Pymongo.errors.OperationFailure,e:
print e
else:
try:
print user.get("login")
collect = user.get("association")
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"contentValueInProject": 0}})
except Pymongo.errors.OperationFailure,e:
print e
def longTimeInteractionWproject(owner,repo):
print ("long Time Interaction With Project .......")
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
allUsers = getlistUsers(owner,repo)
global projectUserAssociation
for user in allUsers:
association = user.get("association")
userDB = projectUserAssociation.get(association)
userLogin = user.get("login")
userAux = db2[userDB].find_one({"login":userLogin})
if userAux != None:
qtComments = userAux.get("participationWComments")
qtCommits = len(list(db.commits.find({"author.login":userLogin})))
actions = qtComments+qtCommits
timeInRepo = daysInProject(userLogin,db)
if(timeInRepo > 0):
longTimeInteraction = float(actions)/float(timeInRepo)
else:
longTimeInteraction = 0
db2[userDB].update({"login":userLogin},{"$set": {"longTimeInteraction": longTimeInteraction}})
def participationWComments(owner,repo):
print ("participation With Comments .......")
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[repo]
db2 = client[dbname]
global projectUserAssociation
userComments = {}
userCommentsArray = []
allUsers = getlistUsers(owner,repo)
allCommentsUser = list(db.issuesComments.find({}))
for usercomment in allCommentsUser:
occurrence = usercomment.get("participants")
if occurrence != None:
occurrenceKeys = occurrence.keys()
for userlogin in occurrenceKeys:
if userlogin in userCommentsArray:
value = occurrence.get(userlogin)
userComments[userlogin] = userComments[userlogin]+int(value)
else:
userComments[userlogin] = int(occurrence.get(userlogin))
userCommentsArray.append(userlogin)
for user in allUsers:
userlogin = user.get("login")
if userlogin in userCommentsArray:
try:
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"participationWComments": userComments.get(userlogin)}})
except Pymongo.errors.OperationFailure,e:
print e
else:
try:
print user.get("login")
collect = user.get("association")
association = user.get("association")
userDB = projectUserAssociation.get(association)
db2[userDB].update({"login":user.get("login")},{"$set": {"participationWComments": 0}})
except Pymongo.errors.OperationFailure,e:
print e
def createInit(owner, repo):
print("Create and Intializing ........")
db_names = ["global_metrics","global_status"]
conn = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = conn[repo]
db2 = conn[dbname]
db = conn[str(db_names[0])]
for db_name in db_names:
if bool(db_name in conn.database_names()):
print "Base {} found".format(db_name)
else:
if ("global_status" == db_name):
db = conn[db_name]
index_name = "uniqueLogin"
my_collection = db["global_status_names"]
my_collection.create_index([('login',1)], name=index_name, unique=True)
print "Base {} create".format(db_name)
elif ("global_metrics" == db_name):
db = conn[db_name]
index_name = "uniqueProjectName"
my_collection = db["projectMetrics"]
my_collection.create_index([('projectName',1)], name=index_name, unique=True)
print "Base {} create".format(db_name)
def statusGlobalSave(owner, repo):
client = MongoClient('localhost', 27017)
db4 = client["projects"]
allUsers = getlistUsers(owner,namerepo)
for user in allUsers:
userLogin = user.get("login")
db4 = client["global_status"]
foundUser = db4.global_status_names.find_one({"login":userLogin})
if foundUser != None:
qtProject = foundUser.get("qtProject")
qtProject = qtProject+1
db4.global_status_names.update({"login":userLogin},{"$push":{"project":project}})
db4.global_status_names.update({"login":userLogin},{"$set":{"qtProject":qtProject}})
else:
db4.global_status_names.insert_one({"login":user.get("login"),"qtProject":1})
db4.global_status_names.update({"login":userLogin},{"$push":{"project":project}})
def archiveProject(owner, repo,rpname):
client = MongoClient('localhost', 27017)
dbname = (rpname+"_metrics")
db = client[dbname]
archiveRepos = client["global_metrics"]
reponame = (owner+"/"+repo)
collaborators = list(db.collaborators.find({}))
committers = list(db.committers.find({}))
contributors = list(db.contributors.find({}))
members = list(db.members.find({}))
owner = db.owner.find_one({})
repository = {
"projectName":reponame,
"collaborators":collaborators,
"committers":committers,
"contributors":contributors,
"members":members,
"owner":owner
}
archiveRepos.projectMetrics.insert_one(repository)
client.drop_database(dbname)
client.drop_database(rpname)
def ownerSearch(ownerRepo,repo):
client = MongoClient('localhost', 27017)
dbname = (repo+"_metrics")
db = client[dbname]
global projectUserAssociation
members = list(db.members.find({}))
ownerList = list(db.owner.find({}))
alluser = getlistUsers(owner,repo)
if(len(members) == 0):
if(len(ownerList) == 0):
for user in alluser:
login = user.get("login")
association = user.get("association")
dbName = projectUserAssociation[association]
if login == ownerRepo:
auxUser = db[dbName].find_one({"login":login})
db[dbName].delete_one({"login":login})
auxUser["association"] = "OWNER"
db.owner.insert_one(auxUser)
def statusInProject():
client = MongoClient('localhost', 27017)
db = client["global_metrics"]
db2 = client["global_status"]
allMetrics = list(db.projectMetrics.find({}))
auxstate = list(db2.global_status_names.find({}))
auxStatusGlobal = []
for projectState in auxstate:
auxStatusGlobal.append(projectState.get("login"))
statusGlobal = {}
print len(allMetrics)
print len(auxStatusGlobal)
for status in auxstate:
loginStatus = status.get("login")
qtProject = status.get("qtProject")
statusGlobal[loginStatus] = qtProject
for repoMetrics in allMetrics:
projectName = repoMetrics.get("projectName")
committers = repoMetrics.get("committers")
auxCommitters = []
if committers != []:
for committer in committers:
committerlogin = committer.get("login")
if committerlogin in auxStatusGlobal:
value = statusGlobal[committerlogin]
committer["statusInProject"] = value
auxCommitters.append(committer)
members = repoMetrics.get("members")
auxMembers = []
if members != []:
for member in members:
memberlogin = member.get("login")
if memberlogin in auxStatusGlobal:
value = statusGlobal[memberlogin]
member["statusInProject"] = value
auxMembers.append(member)
collaborators = repoMetrics.get("collaborators")
auxCollaborators = []
if collaborators != []:
for collaborator in collaborators:
collaboratorlogin = collaborator.get("login")
if collaboratorlogin in auxStatusGlobal:
value = statusGlobal[collaboratorlogin]
collaborator["statusInProject"] = value
auxCollaborators.append(collaborator)
contributors = repoMetrics.get("contributors")
auxContributors = []
if contributors != []:
for contributor in contributors:
contributorlogin = contributor.get("login")
if contributorlogin in auxStatusGlobal:
value = statusGlobal[contributorlogin]
contributor["statusInProject"] = value
auxContributors.append(contributor)
owner = repoMetrics.get("owner")
auxOwner = {}
if owner != None:
ownerlogin = owner.get("login")
if ownerlogin in auxStatusGlobal:
value = statusGlobal[ownerlogin]
owner["statusInProject"] = value
auxOwner = owner
else:
auxOwner = None
newProject = {
"projectName":projectName,
"members":auxMembers,
"committers":auxCommitters,
"contributors":auxContributors,
"collaborators":auxCollaborators,
"owner":auxOwner
}
db.projectMetrics.delete_one({"projectName":projectName})
db.projectMetrics.insert_one(newProject)
if __name__ == '__main__':
#owner/projectname
client = MongoClient('localhost', 27017)
db = client["projects"]
createInit("davisking","dlib")
projectsName = list(db.projectsNames.find({"visited":{"$eq":False}}).limit(1))
# name = {"name":"davisking/dlib","visited":False}
# projectsName = [name]
for project in projectsName:
name = project.get("name")
arg = name.split("/")
owner = arg[0]
repo = arg[1]
namerepo = ""
flag404 = False
if "." in arg[1]:
namerepo = arg[1].replace(".","Dot")
else:
namerepo = arg[1]
print "{}:{}".format(owner,namerepo)
now = datetime.datetime.now()
print ("{}:{}:{}").format(now.hour,now.minute,now.second)
url = ("https://api.github.com/repos/{}/{}/commits?client_id={}&client_secret={}").format(owner,repo,authentication[0].get("client_id"),authentication[0].get("client_secret"))
rqt404 = requests.get(url)
statusCode = int(rqt404.status_code)
aux = rqt404.json()
try:
msg = aux.get("message")
if("is empty" in msg):
flag404 = True
except AttributeError, e:
print ("AttributeError list")
pass
if (statusCode == 404):
aux = rqt404.json()
msg = aux.get("message")
print msg
if("Not Found" in msg):
flag404 = True
if(flag404 != True):
""""Get All Commits,Issues,PRs and Contributors"""
getInfo(name,authentication)
"""Get all Contributors, Owner, Members and Committers"""
getAllContributors(owner,namerepo)
ownerSearch(owner,namerepo)
"""Get All Comments in Repository Issues and PRs """
getAllCommentsTypes(owner,namerepo)
"""Get Code Information """
codeGetterInfo(owner,namerepo)
print "Working in Metrics ....."
closenessOwner(owner,namerepo)
status(owner,namerepo)
ParticipationWcode(owner,namerepo)
participationWComments(owner,namerepo)
contentValueInProject(owner,namerepo)
sourceOfLearning(owner,namerepo)
longTimeInteractionWproject(owner,namerepo)
statusGlobalSave(owner,namerepo)
archiveProject(owner,repo,namerepo)
db.projectsNames.update({"name":name},{"$set":{"visited":True}})
else:
now = datetime.datetime.now()
date = "{}:{}:{}".format(now.hour,now.minute,now.second)
log = {
"name": name,
"date": date
}
with open("logNotFound.txt.json","a") as fp:
json.dump(log, fp)
db.projectsNames.update({"name":name},{"$set":{"visited":True}})
# statusInProject()
now = datetime.datetime.now()
print ("{}:{}:{}").format(now.hour,now.minute,now.second)