-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
313 lines (242 loc) · 9.94 KB
/
Copy pathapplication.py
File metadata and controls
313 lines (242 loc) · 9.94 KB
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
import fnmatch
import os
import subprocess
import urllib.request
class Committer(object):
@staticmethod
def git_init():
subprocess.run(args="git init")
@staticmethod
def git_add_remote_origin(url):
subprocess.run(args="git remote add origin " + url)
@staticmethod
def git_add_all():
subprocess.run(args="git add .")
@staticmethod
def git_commit(message):
subprocess.run(args="git commit -m \"" + message + "\"")
@staticmethod
def git_push():
subprocess.run(args="git push origin master")
@staticmethod
def git_fetch():
subprocess.run(args="git fetch origin master")
@staticmethod
def git_pull():
Committer.git_fetch()
subprocess.run(args="git pull --allow-unrelated-histories origin master")
class Cloner(object):
@staticmethod
def clone_repo(clone_url, folder_name, already_cloned=False, path=None):
if already_cloned:
subprocess.run(args="git submodule add " + clone_url + "./" + folder_name)
elif path is not None and path is not "":
application_dir_path = os.getcwd()
os.chdir(path)
if os.path.exists("./" + folder_name):
Cloner.if_exists(folder_name)
else:
subprocess.run(args="git clone " + clone_url)
os.chdir(application_dir_path)
elif os.path.exists("./" + folder_name):
Cloner.if_exists(folder_name)
else:
subprocess.run(args="git clone " + clone_url)
@staticmethod
def if_exists(folder_name):
os.chdir(folder_name)
subprocess.run(args="git pull origin master")
os.chdir("../")
def clone_all_repos(self, clone_urls, data_dict, already_cloned=False, path=None):
count = 0
for clone_url in clone_urls:
self.clone_repo(clone_url=clone_url, folder_name=data_dict['name'][count], already_cloned=already_cloned, path=path)
count += 1
@staticmethod
def path_input():
path = input(
"\nWhere do you want to clone the repos?\n"
"**To clone to current folder just press enter**.\nEnter the whole path: ")
folder_ok = 1 # Sentinel for the while loop
# Continue if path is not chosen
if path == "":
folder_ok = 0
# Until a path that exists is being submitted as an input
while folder_ok != 0:
if os.path.exists(path):
folder_ok = 0
else:
path = input("Folder doesn't exist... Try again: ")
return path
class DataFetcher(object):
def __init__(self, url):
self.url = url
# def get_json_data(self):
# try:
# with urllib.request.urlopen(self.url) as response:
# data_json = json.load(response)
# except ConnectionError as con_err:
# print(con_err)
# raise
# return data_json
def get_data_to_text_file(self, filename):
try:
with urllib.request.urlopen(self.url) as response:
data_file = open(filename, "w")
data_file.write(str(response.read()))
except ConnectionError as con_err:
print(con_err)
@staticmethod
def data_from_text_to_dict(filename):
try:
data_dict = {}
names = []
clone_urls = []
html_urls = []
data_from_file = open(filename, "r")
data_string = data_from_file.read().split("\"")
counter = 0
for item in data_string:
data_item = item.replace(":", "").replace(",", "").replace("=", "")
if counter != 0:
if data_item is not "" and counter < len(data_string) - 2:
value = data_string[counter + 2]
if data_item.lower() == "name":
names.append(value)
data_dict[data_item] = names
if data_item.lower() == "clone_url":
clone_urls.append(value)
data_dict[data_item] = clone_urls
if data_item.lower() == "html_url":
html_urls.append(value)
data_dict[data_item] = html_urls
counter += 1
except EOFError as eof_err:
print(eof_err)
raise
return data_dict
@staticmethod
def get_clone_url_list(data_dict):
clone_urls = data_dict['clone_url']
# Used for json
# clone_urls = [item['clone_url'] for item in data]
return clone_urls
@staticmethod
def get_names_and_html_urls(data_dict):
names = []
urls = []
for key, value in data_dict.items():
if key != "clone_url":
for item in value:
if key == "name":
names.append(item)
else:
urls.append(item)
names_and_html_urls = dict(zip(names, urls))
# For json
# for item in data:
# names_and_html_urls[item['name'].replace("-", " ")] = item['html_url']
return names_and_html_urls
class FileWriter(object):
@staticmethod
def get_paragraph(path):
global data, start, end
matches = FileWriter.get_matches(path=path)
if len(matches) != 0:
for match in matches:
data = open(match, "r").read()
start = data.find("## Required reading")
end = data.find("## Required reading") + len("## Required reading")
return data[start:end]
return "## Couldn't find the title"
@staticmethod
def get_matches(path):
matches = []
for root, dir_names, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, 'README.md'):
matches.append(os.path.join(root, filename))
return matches
@classmethod
def get_names_and_links(cls, path=os.getcwd()):
matches = FileWriter.get_matches(path=path)
names_and_urls = {}
counter = 0
for match in matches:
if counter != 0: # to avoid README.md from own folder
data_file = open(match, "r").read()
if data_file.__contains__("# Mandatory Assignment: Required readings List"):
continue
start_index = data_file.find("## Required reading")
end_index = start_index + data_file[start_index:].find("### Supplementary reading")
string = data_file[start_index:end_index].split("\n")
for split in string:
if split is not "" and split != "## Required reading":
string_low_and_replace_symbols = split.lower().replace("* [", "").replace(")", "").replace(
"—", "-")
split_to_dict = string_low_and_replace_symbols.split("](")
name, url = split_to_dict[0], split_to_dict[1]
names_and_urls[name] = url
counter += 1
return names_and_urls
@staticmethod
def write_to_file(filename):
file = open(filename, "w+")
paragraph = FileWriter.get_paragraph(path=os.getcwd())
names_and_html_urls = FileWriter.get_names_and_links(path=os.getcwd())
# Start of the file:
file.write(paragraph + "\n > Python Elective II Spring 2019\n\n")
# Put all the necessary data in the file in a sorted order
for name, html_url in sorted(names_and_html_urls.items()):
file.write("* [" + name[0].upper() + name[1:].replace("-", " ") + "](" + html_url + ")\n")
file.close()
def main():
###
# Fetches all the data
###
url = "https://api.github.com/orgs/python-elective-2-spring-2019/repos?per_page=100"
print("Fetching all the data from {}".format(url))
data_fetcher = DataFetcher(url=url)
print("\nSaving all the data to a text file ...")
data_fetcher.get_data_to_text_file("data.txt")
print("\nGetting the necessary data from the text file to a dictionary ...")
data_dict = data_fetcher.data_from_text_to_dict("data.txt")
print("\nGet all the clone urls from the dictionary ...")
clone_urls = data_fetcher.get_clone_url_list(data_dict)
# data = data_fetcher.get_json_data()
###
# Clone all repos to directory
###
cloner = Cloner()
path = cloner.path_input() if cloner.path_input() is not "" else os.getcwd()
print("\nCloning all the repos in " + path + " ...")
cloner.clone_all_repos(clone_urls, data_dict=data_dict, path=path)
###
# Get names and html urls
###
print("\nGetting the names and urls from the dictionary ..")
names_and_html_urls = data_fetcher.get_names_and_html_urls(data_dict)
###
# Create required_reading.md file
###
print("\nCreating required_reading.md file ...")
FileWriter.write_to_file("required_reading.md")
###
# Commit to GitHub
###
directory = os.getcwd()
print("\nInitializing a .git in your directory: " + directory + " ...")
Committer.git_init()
print("\nAdding submodules for the clone repos ...")
cloner.clone_all_repos(clone_urls, data_dict, already_cloned=True)
print("\nAdded the remote origin to the repo url. It will continue if remote origin already exists... ")
Committer.git_add_remote_origin("https://github.com/martinloesethjensen/python-mandatory.git")
print("\nAdded all files ...")
Committer.git_add_all()
message = input("\nCommit changes ...\nPlease write your message for the commit:\t")
Committer.git_commit(message=message)
print("\nFetches and pulls changes if there's any changes on the master that you don't have in your directory ...")
Committer.git_pull()
print("\nPushing changes ...")
Committer.git_push()
if __name__ == '__main__':
main()