-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdyte.py
314 lines (287 loc) · 12.8 KB
/
dyte.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
import csv
from github import Github
import json
from progress.bar import Bar
from progress.bar import FillingSquaresBar
from progress.spinner import PieSpinner
import click
from pyfiglet import Figlet
# Function to check dependency version of nodeJS packages
# return : List
def nodejs_dependancy_version_checker(repo, dependency_name, repos, repo_link, version):
'''
return : List[]
'''
contents = repo.get_contents("package.json") # get content of package.json
decoded_content = contents.decoded_content # decode content into readable form
json_content = json.loads(decoded_content) # convert decoded content into json format
dependencies = json_content['dependencies']
# check if required dependencies present in File
if dependency_name not in dependencies:
ans = [repos, repo_link, "No Dependacy found", "No version found", "No version found"]
else:
if dependencies[dependency_name][1:] >= version:
ans = [repos, repo_link, dependency_name ,dependencies[dependency_name][1:], True]
else:
ans = [repos, repo_link, dependency_name,dependencies[dependency_name][1:], False]
return ans
# Function to check dependency version of nodeJS packages
# return : List
def python_dependancy_version_checker(repo, dependency_name, repos, repo_link, version):
'''
return : List[]
'''
contents = repo.get_contents("requirements.txt")
decoded_content = contents.decoded_content
str_content = decoded_content.decode('utf-8').split() # convert decoded content into st format
for library in str_content:
split_index = library.index("=")
python_library = library[:split_index]
python_library_version = library[split_index+2:]
if python_library==dependency_name:
if python_library_version >= version:
ans = [repos, repo_link, python_library , python_library_version, True]
else:
ans = [repos, repo_link, python_library , python_library_version, False]
return ans
return [repos, repo_link, "No Dependacy found", "No version found", "No version found"]
@click.group()
def messages():
pass
# Set commands and argument for
@click.command('check')
@click.option('--input_file', '-i')
@click.option('--dependancy_names', '-d', multiple=True)
def check_dependancy_versions(input_file, dependancy_names):
f = Figlet(font='slant')
print(f.renderText('Dyte SDK tool'))
bar = FillingSquaresBar('Processing', max=23)
# using an access token
print("How to generate Github Token? Visit link: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token")
print()
github_token = input("Enter your Github token : ")
g = Github(github_token)
output_file = open("output.csv", "w")
try:
input_file = open(input_file, "r")
except:
print("Error : No file found in directory")
exit()
try:
writer = csv.writer(output_file)
reader = csv.reader(input_file)
except:
print("Error : only CSV File allowed")
exit()
line_count = 0
data = {}
for row in reader:
bar.next()
print(" " + row[0] + "File Processing")
if line_count==0:
pass
else:
data.update({row[0]:row[1]})
line_count += 1
# print(data)
print("Total rows processed ", line_count)
header = ["repo", "repo link", "dependacy_name", "version", "version_satisfied"]
writer.writerow(header)
# dependency_args = ["axios@0.23.0", "dyte-client@0.36.1", "typescript@4.1.2", "Django@4.0.1"]
dependency_args = dependancy_names
for each_dependecy in dependency_args:
dependency = each_dependecy
bar.next()
print(" " + each_dependecy)
# spinner.next()
split_index = dependency.index("@")
dependency_name = dependency[:split_index]
version = dependency[split_index+1:]
for repos in data:
bar.next()
print(" " + "Repo clone in process")
repo_link = data[repos]
if "/"==repo_link[-1]:
repo_link = repo_link[19:len(repo_link)-1]
else:
repo_link = repo_link[19:]
try:
repo = g.get_repo(repo_link)
except:
print()
print("Error : Invalid Github Token")
exit()
try:
bar.next()
print(" checking nodejs package.json")
result = nodejs_dependancy_version_checker(repo, dependency_name, repos, repo_link, version)
print(result)
writer.writerow(result)
except:
bar.next()
print(' checking python requirements.txt')
try:
result = python_dependancy_version_checker(repo, dependency_name, repos, repo_link, version)
writer.writerow(result)
except:
print("File Not Found")
print()
print("Process completed.... 100%")
print("output.csv file generated")
bar.finish()
@click.command('update')
@click.option('--input_file', '-i')
@click.option('--dependancy_names', '-d', multiple=True)
def update_dependacy_version(input_file, dependancy_names):
f = Figlet(font='slant')
print(f.renderText('Dyte SDK tool'))
spinner = PieSpinner('Loading ')
bar = FillingSquaresBar('Processing', max=23)
# using an access token
print("How to generate Github? Visit link: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token")
print()
github_token = input("Enter your Github token : ")
g = Github(github_token)
output_file = open("updated_output.csv", "w")
try:
input_file = open(input_file, "r")
except:
print("Error : No file found in directory")
exit()
try:
writer = csv.writer(output_file)
reader = csv.reader(input_file)
except:
print("Error : only CSV File allowed")
exit()
line_count = 0
data = {}
for row in reader:
bar.next()
print(" " + row[0] + "File Processing")
if line_count==0:
pass
else:
data.update({row[0]:row[1]})
line_count += 1
# print(data)
print("Total rows processed ", line_count)
header = ["repo", "repo link", "dependacy_name", "version", "version_satisfied", "update_pr"]
writer.writerow(header)
# dependency_args = ["axios@0.23.0", "dyte-client@0.36.1", "typescript@4.1.2", "Django@4.0.1"]
dependency_args = dependancy_names
for each_dependecy in dependency_args:
dependency = each_dependecy
bar.next()
print(" " + each_dependecy)
# spinner.next()
split_index = dependency.index("@")
dependency_name = dependency[:split_index]
version = dependency[split_index+1:]
# print(dependency_name)
for repos in data:
bar.next()
print(" Repo clone in process")
repo_link = data[repos]
if "/"==repo_link[-1]:
repo_link = repo_link[19:len(repo_link)-1]
else:
repo_link = repo_link[19:]
try:
repo = g.get_repo(repo_link)
except:
print()
print("Error: Invalid Github Token")
exit()
try:
bar.next()
print(" checking nodejs package.json")
result = nodejs_dependancy_version_checker(repo, dependency_name, repos, repo_link, version)
if result[4]==False:
bar.next()
print(" creating PR request for " + repo_link)
github_user = g.get_user()
# print(github_user.login)
myfork = github_user.create_fork(repo)
sb = repo.get_branch('main')
try:
myfork.create_git_ref(ref='refs/heads/dependancy-update', sha=sb.commit.sha)
except:
pass
contents = myfork.get_contents("package.json", ref='dependancy-update')
decoded_content = contents.decoded_content
json_content = json.loads(decoded_content) # convert decoded content into json format
json_content['dependencies'][dependency_name] = "^"+version
new_content = json.dumps(json_content, indent=3)
pr = myfork.update_file(contents.path, "update dependacy", new_content, contents.sha, branch="dependancy-update")
# updating dependency version in package-lock.json
contents = myfork.get_contents("package-lock.json", ref='dependancy-update')
decoded_content = contents.decoded_content
json_content = json.loads(decoded_content) # convert decoded content into json format
json_content['dependencies'][dependency_name] = "^"+version
new_content = json.dumps(json_content, indent=3)
pr = myfork.update_file(contents.path, "update dependacy", new_content, contents.sha, branch="dependancy-update")
try:
pr = repo.create_pull(title="chore: updates " + dependency_name + " to " + version,
body="Updates the version of " + dependency_name + " from " + result[3] + " to " + version,
head=str(github_user.login)+":dependancy-update",
base="main",
)
pr_request = pr.html_url
result.append(pr_request)
except:
result.append('Your PR already exists')
else:
result.append('')
print(result)
writer.writerow(result)
except:
try:
bar.next()
print(" checking python requirements.txt")
result = python_dependancy_version_checker(repo, dependency_name, repos, repo_link, version)
# print(ans)
if result[4]==False:
bar.next()
print(" creating PR request for " + repo_link)
github_user = g.get_user()
myfork = github_user.create_fork(repo)
try:
sb = repo.get_branch('main')
except:
sb = repo.get_branch('master')
try:
myfork.create_git_ref(ref='refs/heads/dependancy-update', sha=sb.commit.sha)
except:
pass
contents = myfork.get_contents("requirements.txt", ref='dependancy-update')
decoded_content = contents.decoded_content
str_content = decoded_content.decode('utf-8').split()
for libs in range(len(str_content)):
if str_content[libs]==dependency_name:
str_content[libs] = dependency_name+"=="+version
break
new_content = "\n".join(str_content)
pr = myfork.update_file(contents.path, "update dependacy", new_content, contents.sha, branch="dependancy-update")
try:
pr = repo.create_pull(title="chore: updates " + dependency_name + " to " + version,
body="Updates the version of " + dependency_name + " from " + result[3] + " to " + version,
head=str(github_user.login)+":dependancy-update",
base="master",
)
pr_request = pr.html_url
result.append(pr_request)
except:
result.append('Your PR already exists')
else:
result.append('')
writer.writerow(result)
except:
print("File Not Found/Pull request already exist")
print()
print("Process completed.... 100%")
print("updated_output.csv file generated")
bar.finish()
messages.add_command(check_dependancy_versions)
messages.add_command(update_dependacy_version)
messages()