-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpublish.py
174 lines (146 loc) · 6.01 KB
/
publish.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
#!/usr/bin/python3
#
# This publishes a new version of the web site from the "ndocs" directory
# to the "docs" directory. It should be run from "ndocs".
#
# The "app_version" should be updated with the release information.
#
import os
import os.path
import re
import shutil
import subprocess
import sys
# Current version string. Used for text substitution.
app_version = "1.0.5"
pkg_version = app_version
# ----- nothing configurable below -----
# Output directory. The directory will be completely removed and regenerated.
output_dir = "../docs"
#output_dir = "../pdocs" # DEBUG
# Subdirectory with files that will be copied to the top of the project.
top_dir = "top"
# List of directories to exclude from recursive copy.
copy_exclude_dirs = [ top_dir ]
# List of file patterns to exclude from recursive copy.
copy_exclude_files = [
re.compile(r"^.*\.py$"), # python scripts
re.compile(r"^incl-.*$"), # HTML include files
re.compile(r"^\..*\.swp$"), # VIM temporary files
re.compile(r"topic-list.txt") # prev/next order file
]
# Text variable substitutions. Performed on ".html" and ".md".
text_subst = [
( re.compile(r"\${VERSION}"), app_version ),
( re.compile(r"\${PKG_VERSION}"), pkg_version )
]
def copy_file(inpath, outpath, do_subst):
"""
Copy the contents of a file, optionally performing a variable
substitution.
inpath: path to input file
outpath: path to output file
do_subst: if True, perform variable substitution
"""
if do_subst:
with open(inpath, "r", encoding="utf-8") as infile:
filedata = infile.read()
if do_subst:
for subst in text_subst:
filedata = re.sub(subst[0], subst[1], filedata)
with open(outpath, "x", encoding="utf-8") as outfile:
outfile.write(filedata)
else:
shutil.copyfile(inpath, outpath)
def main():
""" main """
# See if we're in the right place.
if not os.path.isfile("publish.py"):
print("Failed: run this script from the ndocs directory.")
sys.exit(1)
# Quick check to see if the source and destination directories are at the
# same level in the hierarchy. This is really just a way to check that
# our removal of the target directory isn't stepping outside the project
# area. It can be removed if you want to install elsewhere.
curdir = os.path.normpath(os.getcwd())
outdir = os.path.normpath(os.path.join(os.getcwd(), output_dir))
if (os.path.dirname(curdir) != os.path.dirname(outdir)):
raise Exception("source and target directory are at different levels")
# Remove and create the output directory.
print("--- Removing output directory", outdir)
shutil.rmtree(outdir, True)
print("--- Creating output directory", outdir)
os.mkdir(outdir)
# Do prev/next patching for tutorials.
print("--- Configuring tutorial prev/next links")
for tutdir in ["cli-tutorial", "gui-tutorial"]:
result = subprocess.run(["py", "prevnext.py", tutdir],
capture_output=True, encoding="utf-8")
if result.returncode != 0:
print("FAILED prevnext in", tutdir)
print("STDOUT: ", result.stdout)
print("STDERR: ", result.stderr)
sys.exit(1)
# Walk through the directory hierarchy.
print("--- Copying files", outdir)
for root, dirs, files in os.walk(os.path.curdir):
# Sort the lists to make the output more consistent.
dirs.sort()
files.sort()
# Is this directory excluded from copying?
rootbasename = os.path.basename(root)
if rootbasename in copy_exclude_dirs:
print(" - copy not traversing:", rootbasename)
continue
# According to the python docs, if # "topdown=True" then we can
# modify the list during the traversal. Use this to prune the
# directory list. https://stackoverflow.com/a/19859907/294248
dirs[:] = [d for d in dirs if d not in copy_exclude_dirs]
# Create target directory.
for dir in dirs:
fulldir = os.path.join(root, dir)
newdir = os.path.relpath(os.path.join(outdir, fulldir))
print(" + creating directory:", newdir)
os.mkdir(newdir)
for file in files:
fullname = os.path.normpath(os.path.join(root, file))
# See if it's in the exclusion list.
exclude = False
for regexp in copy_exclude_files:
if regexp.match(file):
print(" - not copying:", fullname)
exclude = True
break;
if exclude:
continue
# Check the filename extension.
rootext = os.path.splitext(file)
do_subst = False
if rootext[1] == ".html":
# Do special processing for HTML.
print(" * performing block replace:", fullname)
result = subprocess.run(["py", "block-repl.py", fullname],
capture_output=True, encoding="utf-8")
if result.returncode != 0:
print("FAILED:", result.stderr)
result.check_returncode()
do_subst = True
elif rootext[1] == ".md":
do_subst = True
# Copy the file.
outname = os.path.relpath(os.path.join(outdir, fullname))
print(" + copying:", fullname, "to", outname)
copy_file(fullname, outname, do_subst)
# Copy files to the top level.
outdir = ".."
print("--- Copying top files to", os.path.normpath(os.path.join(os.getcwd(), outdir)))
topfiles = os.listdir(top_dir)
for file in topfiles:
inpath = os.path.join(top_dir, file)
outpath = os.path.join(outdir, file)
if os.path.isfile(outpath):
os.remove(outpath)
copy_file(inpath, outpath, True)
#print(" WOULD copy", inpath, "-->", outpath)
sys.exit(0)
main() # does not return