forked from ivbeg/opensourcegovernment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetstats.py
executable file
·167 lines (153 loc) · 4.7 KB
/
getstats.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import csv
import yaml
import time
from StringIO import StringIO
from pygithub3 import Github
# Write valid login and password
GIT_NAME = 'ivbeg'
GIT_PASSWORD = file('../git_pass.txt', 'r').read()
FILEPATH = 'data/'
REPPATH = 'data/repos/'
def calc_list(l):
n = 0
try:
for k in l:
print k.login, k.name, k.email, k.id
n += 1
except KeyboardInterrupt:
sys.exit(0)
except:
return 0
return n
def get_list(l):
alist = []
print len(l)
try:
for k in l:
print k.login
# print k.login, k.name, k.email, k.id
alist.append({'login': k.login, 'id' : k.id})
except KeyboardInterrupt:
sys.exit(0)
return alist
def get_all():
g= yaml.load(open('governments.yml', 'r').read())
return g
def process_org(g, orgname):
if os.path.exists(FILEPATH + orgname+'.csv'): return
f = StringIO()
s = ('\t'.join(['orgname','name'])).encode('utf8')
f.write(s)
f.write(u'\n')
# try:
# org = g.orgs.get(orgname)
# except KeyboardInterrupt:
# sys.exit(0)
# except:
# print orgname, 'not found'
# pass
# return
# print orgname
repos = g.repos.list_by_org(orgname).all()
# print repos
for r in repos:
if r.fork: continue
repfilename = '_'.join([orgname, r.name]) + '.yml'
if os.path.exists(REPPATH + repfilename):
print ' - repository %s already processed' % (r.name)
arr = [orgname, r.name]
s = ('\t'.join(map(str, arr))).encode('utf8')
f.write(s)
f.write(u'\n')# print arr
continue
print ' - repository %s is being processed' % (r.name)
repfile = file(REPPATH + repfilename, 'w')
forks = g.repos.forks.list(orgname, r.name).all()
issues = g.issues.list_by_repo(orgname, r.name).all()
for n in range(0, 5):
try:
contributors = g.repos.list_contributors(orgname, r.name).all()
break
except:
time.sleep(2)
continue
data = {'orgname' : orgname, 'name': r.name, 'stargazers_count': r.stargazers_count, 'size' : r.size,
'forks': len(forks),
'issues' : len(issues),
'watchers_count': r.watchers_count,
'contributors': len(contributors)}
# print data
yaml.dump(data, repfile)
arr = [orgname, r.name]
s = ('\t'.join(map(str, arr))).encode('utf8')
f.write(s)
f.write(u'\n')# print arr
fo = open(FILEPATH + orgname+'.csv', 'w')
f.seek(0)
fo.write(f.read())
fo.close()
def process_all():
thelist = get_all()
g = Github(login=GIT_NAME, password=GIT_PASSWORD)
for country, orglist in thelist.items():
for r in orglist:
print 'Processing %s from %s' % (r, country)
process_org(g, r)
try:
pass
# process_org(g, r)
except:
print "Error occured, but we don't give up. Skip and follow to next one"
def calc_stats():
thelist = get_all()
frep = open('repositories.csv', 'w')
wrrep = csv.DictWriter(frep, ['country', 'orgname', 'repname', 'watchers_count', 'forks', 'issues', 'contributors', 'size'], delimiter=',')
wrrep.writeheader()
forgs = open('organizations.csv', 'w')
wrorgs = csv.DictWriter(forgs, ['country', 'orgname', 'repos', 'watchers_count', 'forks', 'issues', 'contributors', 'size'], delimiter=',')
wrorgs.writeheader()
f = open('countries.csv', 'w')
wr = csv.DictWriter(f, ['country', 'orgs', 'repos', 'watchers_count', 'forks', 'issues', 'contributors', 'size'], delimiter=',')
wr.writeheader()
for country, orglist in thelist.items():
c = {'country' : country, 'orgs' : 0, 'repos' : 0, 'contributors' : 0, 'issues' : 0, 'forks' : 0, "size" : 0, 'watchers_count' : 0}
for orgname in orglist:
c['orgs'] += 1
org = {'country' : country, 'orgname' : orgname, 'repos' : 0, 'contributors' : 0, 'issues' : 0, 'forks' : 0, "size" : 0, 'watchers_count' : 0}
try:
f = open(FILEPATH+orgname+'.csv', 'r')
except:
continue
r = csv.DictReader(f, delimiter="\t")
for row in r:
c['repos'] += 1
org['repos'] += 1
rep = {'country' : country, 'orgname' : orgname, 'repname' : row['name'], 'contributors' : 0, 'issues' : 0, 'forks' : 0, "size" : 0, 'watchers_count' : 0}
# for k in ['name', 'orgname']:
# c[k] = row[k]
repfilename = '_'.join([orgname, row['name']]) + '.yml'
repf = open(REPPATH + repfilename, 'r')
repdata = yaml.load(repf)
if not repdata:
print orgname, row['name'], 'no data'
continue
for k in ['contributors', 'forks', 'issues', 'size', 'watchers_count']:
c[k] += repdata[k]
for k in ['contributors', 'forks', 'issues', 'size', 'watchers_count']:
org[k] += repdata[k]
for k in ['contributors', 'forks', 'issues', 'size', 'watchers_count']:
rep[k] += repdata[k]
wrrep.writerow(rep)
wrorgs.writerow(org)
print c
wr.writerow(c)
# process_org(r)
# print dir(r.get_forks()
if __name__ == "__main__":
# process_all()
calc_stats()
# process_org(orgname='gcba')