-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_docs.py
305 lines (249 loc) · 9.02 KB
/
generate_docs.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
"""
Download GitHub README files and wiki documentation, and parse for
compomics.github.io pages.
- Removes ./pages/<project_name> directory and all of its contents
- Download README.md and wiki pages
- Parse markdownfiles
- Adapt wiki URLs to https://<user>.github.io/project/wiki/<file>
- Add required Jekyll front-end matter
"""
import re
import os
import shutil
import base64
import logging
import argparse
import urllib.parse
from glob import glob
from pathlib import Path
import yaml
import git
import rst_to_myst
from github import Github, Auth
# Globals
LOG_LEVEL = logging.INFO
PUSH_USER = "compomics"
PUSH_REPO_NAME = "compomics.github.io"
def argument_parser():
parser = argparse.ArgumentParser(description='Download GitHub README files\
and wiki documentation, and parse for compomics.github.io pages.\
Use YAML config file or CLI arguments.')
parser.add_argument('-c', dest='path_to_config', action='store',
help='YAML configuration file')
parser.add_argument('-t', dest='github_token', action='store',
help='GitHub API access token')
parser.add_argument('-p', dest='project', action='store',
help='Name of GitHub repository to parse')
parser.add_argument('-u', dest='user', action='store',
help='Name of GitHub profile to parse from.')
parser.add_argument('-a', action='store_true', default=False,
dest='update_all', help='Update all existing projects')
args = parser.parse_args()
return args
def load_config(args):
"""
Load YAML config file
"""
if args.path_to_config:
with open(args.path_to_config, 'r') as stream:
config = yaml.safe_load(stream)
else:
config = dict()
if args.github_token:
config['github_token'] = args.github_token
if args.project:
config['projects'] = [args.project]
elif args.update_all:
exising_projects = [p.split('/')[1] for p in glob('pages/*')]
config['projects'] = exising_projects
if args.user:
config['user'] = args.user
# Check if config options are passed
for k in ['github_token', 'projects', 'user']:
if k not in config:
logging.error("`%s` not specified. Use either YAML or CLI arguments\
to specifiy `%s`", k, k)
exit(1)
return config
def construct_project_home_header(project_name, repo_meta):
"""
Return project_home Jekyll front matter header for compomics.github.io
pages.
"""
args = {
'project_name': project_name,
'html_url': repo_meta['html_url'],
'description': repo_meta['description']
}
header_string = """---
title: "{project_name}"
project: "{project_name}"
github_project: "{html_url}"
description: "{description}"
layout: default
tags: project_home, {project_name}
permalink: /projects/{project_name}
---
""".format(**args)
return header_string
def parse_wiki_title(filename):
title = filename\
.replace('-', ' ')\
.replace('.md', '')\
.replace('.MD', '')
return title
def parse_wiki_permalink(filename, project_name):
parsed_name = filename\
.replace('.md', '')\
.replace('.MD', '')\
.replace('.', '')\
.replace(':', '')
parsed_name = urllib.parse.quote(parsed_name)
permalink = "/projects/{}/wiki/{}".format(project_name, parsed_name)
return permalink
def construct_wiki_header(wiki_meta):
"""
Return wiki page Jekyll front matter header for compomics.github.io pages.
"""
header = """---
title: "{title}"
layout: default
permalink: "{permalink}"
tags: {tags}
project: "{project}"
github_project: "{github_project}"
---
""".format(**wiki_meta)
return header
def url_sub(matches):
"""
Function for re.sub() that returns a parsed URL
"""
url = matches.group(1).rstrip("/")
if url.endswith('wiki'):
return '/projects' + url + ')'
elif url.endswith('issues') or url.endswith('releases') or url.endswith('releases/latest'):
return matches.group(0)
else:
return '/projects' + url + ')'
def file_parser(line):
"""
Parse Markdown page for compomics.github.io pages:
- Replace wiki URLs to pages URLs
"""
line = re.sub(
r'https:\/\/github\.com\/compomics([^.]*)\)',
url_sub,
line
)
return line
def get_readme_file(config, repo_meta, github_instance):
"""
Download default branch README.md file from GitHub and parse for github.io
pages.
"""
project_name = config['project_name']
project_dir = os.path.join("pages", project_name)
# Get README contents of default branch
repo = github_instance.get_repo("{}/{}".format(config['user'], config['project_name']))
readme = repo.get_readme()
readme_content = base64.b64decode(readme.content).decode()
readme_suffix = Path(readme.download_url).suffix.lower()
if readme_suffix == ".rst":
readme_md = rst_to_myst.rst_to_myst(readme_content, use_sphinx=False).text
elif readme_suffix == ".md":
readme_md = readme_content
else:
raise ValueError("README in unsupported filetype")
# Write to file
with open(os.path.join(project_dir, project_name + '.md'), 'wt') as readme_out:
readme_out.write(construct_project_home_header(project_name, repo_meta))
readme_out.write(file_parser(readme_md))
def get_wiki_files(config, repo_meta):
"""
Download and parse all wiki files to ./pages/<project_name>/wiki/...
"""
# Remove tmp files (if exists)
if os.path.isdir("tmp_wiki_clone"):
shutil.rmtree("tmp_wiki_clone")
# Remove existing wiki files
wiki_dir = os.path.join('pages', config['project_name'], 'wiki')
if os.path.isdir(wiki_dir):
shutil.rmtree(wiki_dir)
os.makedirs(wiki_dir)
# Clone into tmp_wiki_clone
wiki_url = "https://github.com/{}/{}.wiki.git".format(
config['user'],
config['project_name']
)
try:
repo = git.Repo.clone_from(wiki_url, "tmp_wiki_clone")
except:
logging.warning("Failed to clone wiki repository. Is it empty? \
Disable wiki in GitHub repository options to prevent warning.")
return None
wiki_files = [f for f in os.listdir('tmp_wiki_clone') if f[-3:] == '.md']
# Parse all metadata
wiki_files = [{
'filename': f,
'title': parse_wiki_title(f),
'project': config['project_name'],
'permalink': parse_wiki_permalink(f, config['project_name']),
'tags': "wiki, {}".format(config['project_name']),
'github_project': repo_meta['html_url']
} for f in wiki_files]
for wiki_meta in wiki_files:
f_in_name = os.path.join('tmp_wiki_clone', wiki_meta['filename'])
f_out_name = os.path.join(wiki_dir, wiki_meta['filename'])
with open(f_in_name, 'rt') as f_in:
with open(f_out_name, 'wt') as f_out:
f_out.write(construct_wiki_header(wiki_meta))
for i, line in enumerate(f_in):
if i == 0:
if not line.startswith('# '):
f_out.write("# {}\n".format(wiki_meta['title']))
f_out.write(file_parser(line))
# Remove tmp files
shutil.rmtree("tmp_wiki_clone")
def main():
# Set up logging
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=LOG_LEVEL
)
# Get config
args = argument_parser()
config = load_config(args)
# Login into GitHub
auth = Auth.Token(config['github_token'])
g = Github(auth=auth)
for project in config['projects']:
config['project_name'] = project
# Get project meta data
repo = g.get_repo("{}/{}".format(config['user'], config['project_name']))
repo_meta = {
'html_url': repo.html_url,
'has_wiki': repo.has_wiki,
'description': repo.description,
}
logging.info("Parsing %s/%s", config['user'], config['project_name'])
# Remove existing project directory and make new dirs
project_dir = os.path.join("pages", config['project_name'])
if os.path.isdir(project_dir):
shutil.rmtree(project_dir)
os.makedirs(project_dir)
# Get and parse README file (project home)
get_readme_file(config, repo_meta, g)
# Get and parse wiki files
if repo_meta['has_wiki']:
get_wiki_files(config, repo_meta)
else:
logging.info("Project has no wiki.")
logging.info(
"Succesfully parsed %s/%s", config['user'],
config['project_name']
)
logging.info("Ready")
if __name__ == "__main__":
main()