-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer.py
181 lines (144 loc) · 6.35 KB
/
transformer.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
import os
import re
from glob import glob
from tqdm import tqdm
import javalang
import pandas as pd
def get_source_code(commitId, project):
import random
import requests
from robobrowser import RoboBrowser
HEADERS_LIST = [
'Mozilla/5.0 (Windows; U; Windows NT 6.1; x64; fr; rv:1.9.2.13) Gecko/20101203 Firebird/3.6.13',
'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201',
'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
'Mozilla/5.0 (Windows NT 5.2; RW; rv:7.0a1) Gecko/20091211 SeaMonkey/9.23a1pre'
]
link = []
session = requests.Session()
browser = RoboBrowser(session=session, user_agent=random.choice(HEADERS_LIST), parser="lxml")
url = "https://github.com/" + project.replace("-", "/") + "/commit/" + commitId
browser.open(url + "?diff=unified")
results = browser.find_all("a")
for item in results:
if ".java" in str(item):
second_url = "https://raw.githubusercontent.com/" + project.replace("-",
"/") + "/" + commitId + "/" + item.string
browser.open(second_url)
return browser.find().text
def removeComments(string):
string = re.sub(re.compile("/\*.*?\*/", re.DOTALL), "",
string) # remove all occurance streamed comments (/*COMMENT */) from string
string = re.sub(re.compile("//.*?\n"), "",
string) # remove all occurance singleline comments (//COMMENT\n ) from string
return string
temp_subfiles = []
pattern = "*.java"
for dir, _, _ in os.walk("/home/manny/PycharmProjects/GithubScraper/java_code_files"):
temp_subfiles.extend(glob(os.path.join(dir, pattern)))
# print(temp_subfiles)
if (False):
for file in tqdm(temp_subfiles):
with open(file, 'r') as myfile:
data = myfile.read() # .replace('\n', '')
# data = removeComments(data)
tree = javalang.parse.parse(data)
# for codeblock in tree.children:
# print(codeblock)
# print("===============")
# for proto in codeblock:
# print(proto)
#
# print("*************")
#
# tokens = list(javalang.tokenizer.tokenize(data))
# for token in tokens:
# # print(token.value)
# # print(token.position)
# print(type(token))
import os
import re
from glob import glob
from tqdm import tqdm
import javalang
import pandas as pd
def get_source_code(commitId, project):
import random
import requests
from robobrowser import RoboBrowser
HEADERS_LIST = [
'Mozilla/5.0 (Windows; U; Windows NT 6.1; x64; fr; rv:1.9.2.13) Gecko/20101203 Firebird/3.6.13',
'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201',
'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
'Mozilla/5.0 (Windows NT 5.2; RW; rv:7.0a1) Gecko/20091211 SeaMonkey/9.23a1pre'
]
link = []
codes = []
while (len(codes) < 1):
session = requests.Session()
browser = RoboBrowser(session=session, user_agent=random.choice(HEADERS_LIST), parser="lxml")
url = "https://github.com/" + project.replace("-", "/") + "/commit/" + commitId
browser.open(url + "?diff=unified")
results = browser.find_all("a")
for item in results:
if ".java" in str(item):
try:
# print(item.string)
second_url = "https://raw.githubusercontent.com/" + project.replace("-",
"/") + "/" + commitId + "/" + item.string
browser.open(second_url)
codes.append(browser.find().text)
except:
pass
return codes
def removeComments(string):
string = re.sub(re.compile("/\*.*?\*/", re.DOTALL), "",
string) # remove all occurance streamed comments (/*COMMENT */) from string
string = re.sub(re.compile("//.*?\n"), "",
string) # remove all occurance singleline comments (//COMMENT\n ) from string
return string
code = {"JavaSyntaxError": [], "code_col": [], "code_line": [], "code": [], "astcode": [], 'database_source': []}
temp_subfiles = []
pattern = "*.java"
for dir, _, _ in os.walk("/home/manny/PycharmProjects/GithubScraper/java_code_files"):
temp_subfiles.extend(glob(os.path.join(dir, pattern)))
# print(temp_subfiles)
if (True):
for file in tqdm(temp_subfiles):
with open(file, 'r') as myfile:
data = myfile.read() # .replace('\n', '')
# data = removeComments(data)
try:
tree = javalang.parse.parse(data)
for codeblock in tree.children:
try:
code['code'].append(codeblock._position._source)
except AttributeError:
code['code'].append(None)
try:
code['code_line'].append(codeblock._position.line)
except AttributeError:
code['code_line'].append(None)
try:
code['code_col'].append(codeblock._position.column)
except AttributeError:
code['code_col'].append(None)
code['astcode'].append(codeblock)
code['database_source'].append("githubrepos")
code['JavaSyntaxError'].append(False)
except (javalang.parser.JavaSyntaxError, KeyError):
code['code'].append(source_code)
code['astcode'].append(None)
code['code_line'].append(None)
code['code_col'].append(None)
code['JavaSyntaxError'].append(True)
code['database_source'].append("Promise")
pass
def dict_append(code, key, value):
if value == None:
code[key].append("nan")
else:
code[key].append("value")
return