-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_repos.py
198 lines (151 loc) · 5.69 KB
/
import_repos.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
'''
Imports repos to depth of 1.
Updates sidebars and navbar.
'''
import os, subprocess, shutil
import glob
import config
def _transform_string(string):
'''
Make strings more readable
'''
return string.replace("-"," ").capitalize()
def _reverse_transform(string):
'''
Undo transform string
'''
return string.replace(" ","-").lower()
def _unindent(lines):
return "\n".join(["*"+line[2:] for line in lines.split("\n")[:-1]])
def _human_message(file_type):
if file_type=="project":
lines=["Here's the list of projects currently compiled by the documentation engine.",
"Contact us to add your project to the auto-render."]
return "\n".join(lines)
elif file_type=="utils":
lines=["Here's the list of utilities currently compiled by the documentation engine.",
"Contact us to add your project to the auto-render."]
return "\n".join(lines)
def create_main_sidebar(repos,sidebar_file,navbar_file):
'''
Recurse and add to the sidebar file
* repos: dictionary config
'''
sidebar='''* [Home](README.md) \n'''
md_dirs=repos.keys()
# Side bar
repo_side_bar={}
repo_side_bar["home"]="* [Home](README.md) \n"
# Nav bar
repo_nav_bar="* [Home](README.md) \n* [Projects](projects.md) \n* [Utilities](utils.md) \n"
# Project list
project_bar=''
# Utils list
utils_bar=''
for dir in md_dirs:
dir_name=_transform_string(dir)
for root, dirs, files in os.walk(dir, topdown=True):
for name in files:
# Skip non markdown
if name[-2:]!="md":
continue
if root==dir and name=="README.md":
sidebar+='''* [{}]({}) \n'''.format(dir_name,dir+"/README.md")
repo_side_bar[dir_name]="* [{}]({}) \n".format(dir_name,dir+"/README.md")
side_bar="* [Home](README.md) \n"
side_bar+="\n* [Projects](projects.md) \n"
# Add projects to main sidebar
for dir_name in sorted(repo_side_bar.keys()):
if dir_name=="home":
continue
md_dir=_reverse_transform(dir_name)
if repos[md_dir]['type']=='project':
project_bar+="\t* [{}]({})\n".format(dir_name,md_dir+"/README.md")
# Collect projects
side_bar+=project_bar
side_bar+="* [Utils](utils.md) \n"
# Add utils to main sidebar
for dir_name in sorted(repo_side_bar.keys()):
if dir_name=="home":
continue
md_dir=_reverse_transform(dir_name)
if repos[md_dir]['type']=='utils':
utils_bar+="\t* [{}]({})\n".format(dir_name,md_dir+"/README.md")
# Collect utils
side_bar+=utils_bar
# Write sidebar
with open(sidebar_file,"w") as f:
f.write(side_bar)
# Project write ups
with open("projects.md","w") as f:
f.write(_human_message("project"))
f.write("\n\nProjects : \n")
f.write(_unindent(project_bar))
# Utils write ups
with open("utils.md","w") as f:
f.write(_human_message("utils"))
f.write("\n\nUtils : \n")
f.write(_unindent(utils_bar))
# navbar
with open("_navbar.md","w") as f:
f.write(repo_nav_bar)
def create_folder_sidebar(dir,sidebar_file):
'''
Recurse and add to the folder sidebar file
'''
sidebar='''* [{}]({}/README.md) \n'''.format(dir,dir)
root_covered=[]
for root, dirs, files in os.walk(dir, topdown=True):
for name in sorted(files):
# Skip non markdown
if name[-2:]!="md" or name[0]=="_":
continue
elif root=="/".join([dir,"g3docs"]):
sidebar+='''\t* [{}]({}) \n'''.format(name[:-3],os.path.join(root,name))
elif "/".join([dir,"g3docs"]) in root:
if root not in root_covered:
sidebar+="\t* "+root.split("/")[-1]+"\n"
root_covered.append(root)
sidebar+='''\t\t* [{}]({}) \n'''.format(name[:-3],os.path.join(root,name))
with open("/".join([dir,sidebar_file]),"w") as f:
f.write(sidebar)
def remove_all_except(dir,dirs,files):
'''
Removes all files and dir's except dirs
Args:
* dir: root dir
* dirs: dirs to keep
* files : files to keep
Example: remove_all_except(attendance-system,)
'''
files_dirs_to_remove=os.listdir(dir)
# Find files and dirs to remove
files_to_remove=[f for f in files_dirs_to_remove if os.path.isfile(os.path.join(dir, f)) and f not in files]
dirs_to_remove=[f for f in files_dirs_to_remove if not os.path.isfile(os.path.join(dir, f)) and f not in dirs]
for file in files_to_remove:
os.remove(os.path.join(dir,file))
for dirs in dirs_to_remove:
shutil.rmtree(os.path.join(dir,dirs))
def main():
count=1
for name in config.REPOS.keys():
link=config.REPOS[name]['link']
refresh=config.REPOS[name]['refresh']
print ("Count {} \t | Repo {} \t | Refresh {} \t | Link {} ".format(count,name,refresh,link))
if refresh:
if os.path.exists(name):
shutil.rmtree(name)
cmd = " ".join(["git clone --depth 1",link,name])
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
# Remove Dirs
remove_all_except(name,"g3docs","README.md")
if os.path.exists(os.path.join(name,"g3docs")):
create_folder_sidebar(name,"_sidebar.md")
print(os.path.join(name,"g3docs"))
else:
print(os.path.join(name,"g3docs"))
count+=1
create_main_sidebar(config.REPOS,"_sidebar.md","navbar.md")
if __name__=='__main__':
main()