-
Notifications
You must be signed in to change notification settings - Fork 2
/
compose.py
executable file
·162 lines (140 loc) · 6.21 KB
/
compose.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
"""
Compose documentation for NMDC workflows
"""
import os
import sys
import git
from shutil import copy2
from git.exc import GitCommandError
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
class NMDC_Composer:
"""NMDC Composer provides an interface to NMDC resources."""
def __init__(self, config):
self.__version__ = 0.1
config = yaml.load(config, Loader=Loader)
self.ROOT_DIR = os.path.abspath(config["root_dir"])
self.DOC_DIR = os.path.join(self.ROOT_DIR, "docs")
self.DOC_CHAPTER_DIR = os.path.join(self.DOC_DIR, "chapters")
self.DOC_IMG_DIR = os.path.join(self.DOC_DIR, "_static/images")
self.WORKFLOW_DIR = os.path.join(self.ROOT_DIR, "workflows")
self.WORKFLOWS = config["workflows"]
self.DOCUMENTATION_TEMPLATE_REPO = config["documentation_template_repo"]
def get_workflow_dir(self, workflow):
"""Get the absolute path of the local workflow repo"""
try:
assert workflow in self.WORKFLOWS.keys()
except AssertionError:
sys.stderr.write("{} is not a valid workflow".format(workflow))
sys.exit()
return (os.path.join(self.WORKFLOW_DIR, workflow))
def checkout_documentation_template(self):
remote_repo = self.DOCUMENTATION_TEMPLATE_REPO
local_repo = self.DOC_DIR
try:
git.Repo.clone_from(remote_repo, local_repo)
except GitCommandError as error:
if error.status == 128: # Do git pull from origin/master if local repo exists
print(f'Git pull from remote origin/master: {remote_repo}')
g = git.Git(local_repo)
g.pull('origin', 'master')
else:
msg = "Error in fectching documentation template repository.\n"
msg += " ".join(error.command)
msg += error.stderr
sys.exit(msg)
def checkout_workflow_repo(self, workflow):
"""Clone a workflow module from repo."""
try:
assert workflow in self.WORKFLOWS.keys()
except AssertionError:
sys.stderr.write("{} is not a valid workflow".format(workflow))
sys.exit()
remote_repo = self.WORKFLOWS[workflow]
local_repo = self.get_workflow_dir(workflow)
try:
git.Repo.clone_from(remote_repo, local_repo)
except GitCommandError as error:
if error.status == 128:
repo = git.Repo(local_repo)
branch = 'master' # Pull from master first
try:
repo.git.checkout(branch)
except GitCommandError:
branch = 'main' # Pull from main when no master branch
repo.git.checkout(branch)
else:
msg = ' '.join(error.command)
msg += error.stderr
sys.exit('Error in running git: {}'.format(msg))
def checkout_all_workflows(self, verbose=False):
for workflow in self.WORKFLOWS.keys():
if verbose:
print("Check out workflow {}".format(workflow))
self.checkout_workflow_repo(workflow)
def build_documentation(self, verbose=False):
"""
Build documentation from each module's doc directory.
0. Check out the documenation template repository
1. Check out the repo of individual workflows
2. Read, update, write rst files from workflow "docs" to "chapters"
3. Add workflow names before rst files
4. Check figures
5. Update index.rst to include all chapter files
"""
self.checkout_documentation_template()
chapters = []
for workflow in self.WORKFLOWS.keys():
self.checkout_workflow_repo(workflow)
local_repo = self.get_workflow_dir(workflow)
src_dir = os.path.join(local_repo, 'docs')
if not os.access(src_dir, os.W_OK):
sys.stderr.write("Cannot read from {}".format(src_dir))
for f in os.listdir(src_dir):
if f.endswith(".rst"):
src = os.path.join(src_dir, f)
tgt = "_".join([workflow, f])
chapters.append(tgt)
tgt = os.path.join(self.DOC_CHAPTER_DIR, tgt)
if verbose:
print("Updating {} to {}".format(src, tgt))
if not os.access(src, os.R_OK):
sys.exit('Cannot open rst file {}'.format(src))
with open(src, 'r') as fh:
contents = fh.readlines()
for i, l in enumerate(contents):
if ".. image:: " in l:
src_img_f = l.split("image:: ")[1].strip()
tgt_img_f = "_".join([workflow, src_img_f])
tgt_img_ref = os.path.join("../_static/images/", tgt_img_f)
tgt_img_f = os.path.join(self.DOC_IMG_DIR, tgt_img_f)
contents[i] = ".. image:: {}\n".format(tgt_img_ref)
copy2(os.path.join(src_dir, src_img_f), tgt_img_f)
with open(tgt, 'w') as fh:
fh.writelines(contents)
self.write_index_rst(chapters)
print("Run this command to generate docs/_build/latex/NMDCWorkflows.pdf:\n\tcd docs&&make latexpdf")
def write_index_rst(self, chapters):
"""Add index.rst """
index_rst = os.path.join(self.DOC_DIR, 'index.rst')
if not os.access(index_rst, os.W_OK):
sys.exit('Cannot write to {}'.format(index_rst))
with open(index_rst, 'w') as rst:
rst.write("""
Welcome to NMDC Workflows's documentation!
==========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
chapters/overview.rst
""")
for chapter in chapters:
rst.write(" chapters/{}\n".format(chapter))
if __name__ == '__main__':
config = "nmdc_doc.yaml"
with open(config, "r") as config:
manager = NMDC_Composer(config)
manager.build_documentation(verbose=False)