-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
284 lines (246 loc) · 8.98 KB
/
common.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
from argparse import ArgumentParser
from functools import reduce
from json import dumps, loads
from logging import getLogger
from logging.config import dictConfig
from os import chdir
from pathlib import Path
from queue import Queue
from sys import maxsize, setrecursionlimit, version_info
from github import BadCredentialsException, Github, GithubObject, RateLimitExceededException
from pandas import Timestamp, read_csv
from sqlitedict import SqliteDict
from urllib3.util.retry import Retry
log = getLogger(__name__)
DATE = Timestamp(2020, 5, 30)
KEYWORDS = [
"abandon",
"stale",
"any update",
"lack of update",
"no update",
"inactive",
"inactivity",
"lack of activity",
"no activity",
"not active",
"lack of reply",
"no reply",
"lack of response",
"no response",
]
TOKENS = {}
tokens = Queue()
for token in TOKENS:
tokens.put(token)
@property
def raw_data(self):
return self._rawData
GithubObject.GithubObject.data = raw_data
def initialize(directory=None):
if not (version_info[0:2] == (3, 9) and maxsize > 2**32):
raise RuntimeError("Python 3.9 (64-bit) is required")
setrecursionlimit(1_000_000)
if directory is None:
directory = paths("data")
directory = Path(__file__).parent / directory
directory.mkdir(parents=True, exist_ok=True)
chdir(directory)
def logger(name, level="INFO", modules=None):
name = Path(name).stem
dictConfig(
{
"version": 1,
"formatters": {
"file": {
"format": "{asctime}\t{levelname}\t{name}\t{message}",
"datefmt": "%Y-%m-%d %H:%M:%S",
"style": "{",
},
"stream": {
"()": "colorlog.ColoredFormatter",
"format": "{blue}{asctime}\t{name}\t{message_log_color}{message}",
"datefmt": "%Y-%m-%d %H:%M:%S",
"style": "{",
"secondary_log_colors": {
"message": {
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
}
},
},
},
"handlers": {
"file": {
"class": "logging.FileHandler",
"formatter": "file",
"filename": f"{name}.log",
},
"stream": {
"class": "colorlog.StreamHandler",
"formatter": "stream",
},
},
"root": {
"level": level,
"handlers": ["file", "stream"],
},
"disable_existing_loggers": False,
}
)
if modules is not None:
for module, level in modules.items():
getLogger(module).setLevel(level)
return getLogger(name)
def github(token=None, done=False):
if token is not None:
tokens.put(token)
if not done:
while True:
try:
token = tokens.get()
client = Github(
token,
timeout=20,
per_page=100,
retry=Retry(total=None, status=10, backoff_factor=1, status_forcelist=[500, 502, 503, 504]),
)
remaining, limit = client.rate_limiting
if limit < 5000:
raise BadCredentialsException(401, f"Token {token} is blocked", headers=None)
except BadCredentialsException:
log.warning(f"Token {token} is not valid")
except Exception as exception:
if not isinstance(exception, RateLimitExceededException):
log.error(f"Token {token} is not working due to {exception}")
tokens.put(token)
else:
if remaining > TOKENS[token]:
break
else:
tokens.put(token)
return token, client
def persist(file):
def encode(data):
return dumps(data, ensure_ascii=False, separators=(",", ":"))
def decode(data):
return loads(data)
return SqliteDict(file, tablename="data", autocommit=True, encode=encode, decode=decode)
def lookup(attributes, json):
if not isinstance(attributes, list):
attributes = [attributes]
for attribute in attributes:
if (
value := reduce(
lambda dictionary, key: dictionary.get(key) if dictionary else None, attribute.split("."), json
)
) not in [None, ""]:
return value
def paths(file, project=None):
if project is not None:
project = project.replace("/", "_").lower()
directory = f"{project}/"
files = {
# Working directory
"data": "data/",
# Generated in fetch_projects.py
"projects": "projects.csv",
# Generated in collect_data.py
"directory": directory,
"checkpoint": directory + f"{project}_checkpoint.db",
"pulls_raw": directory + f"{project}_pulls.db",
"timelines_raw": directory + f"{project}_timelines.db",
"commits": directory + f"{project}_commits.db",
"files": directory + f"{project}_files.db",
"metadata": directory + f"{project}.db",
# Generated in preprocess_data.py
"timelines_fixed": directory + f"{project}_timelines_fixed.db",
"timelines_preprocessed": directory + f"{project}_timelines.csv",
"pulls_preprocessed": directory + f"{project}_pulls.csv",
# Generated in process_data.py
"dataframe": directory + f"{project}_dataframe.csv",
# Generated in postprocess_data.py
"statistics": "statistics.csv",
"dataset": directory + f"{project}_dataset.csv",
"sample": directory + f"{project}_sample.csv",
# Generated in analyze_inactivity.py
"inactivity": "inactivity.csv",
# Generated in prelabel_data.py
"prelabeling": "prelabeling.csv",
# Generated in label_data.py
"labeling": "labeling.csv",
# Generated after labeling
"labels": "labels.xlsx",
# Generated in calculate_agreement.py
"agreement": "agreement.csv",
# Generated in extract_developers.py
"developers": "developers.csv",
# Generated after survey
"responses": f"{project}.csv",
# Generated in analyze_survey.py
"survey": "survey.xlsx",
# Generated in measure_features.py
"features": directory + f"{project}_features.csv",
# Generated in build_deeplearning.py
"deeplearning": "deeplearning.csv",
}
return Path(files[file])
def refresh():
parser = ArgumentParser()
parser.add_argument("-y", action="store_true", help="force fresh start")
parser.add_argument("-n", action="store_true", help="do not force fresh start")
if (parser := parser.parse_args()).y:
return True
elif parser.n:
return False
def cleanup(files, fresh=None, project=None):
if not isinstance(files, list):
files = [files]
files = [paths(file, project) for file in files]
if (exists := any([file.exists() for file in files])) and fresh is None:
message = "Do you want to force fresh start? [y/n] "
if project is not None:
message = f"{project}: {message}"
while True:
if (fresh := input(message).lower()) in ["y", "n"]:
fresh = True if fresh == "y" else False
break
if fresh:
for file in files:
file.unlink(missing_ok=True)
return True if fresh or not exists else False
def exist(files, project, exclude=None):
if not isinstance(files, list):
files = [files]
if exclude is None:
exclude = []
elif not isinstance(exclude, list):
exclude = [exclude]
return all([paths(file, project).exists() for file in files]) and not any(
[paths(file, project).exists() for file in exclude]
)
def tocollect():
if not (projects := paths("projects")).exists():
raise RuntimeError("List of projects is missing")
return read_csv(projects, usecols=["project"]).squeeze("columns").dropna()
def collected():
return [
project
for project in tocollect()
if exist(["pulls_raw", "timelines_raw", "commits", "files", "metadata"], project, exclude="checkpoint")
]
def preprocessed():
return [
project
for project in collected()
if exist(["timelines_fixed", "timelines_preprocessed", "pulls_preprocessed"], project)
]
def processed():
return [project for project in preprocessed() if exist("dataframe", project)]
def postprocessed():
return [project for project in processed() if exist(["dataset", "sample"], project)]
def measured():
return [project for project in postprocessed() if exist("features", project)]