Skip to content

Commit d9f5345

Browse files
carlescufinashif
authored andcommitted
doc: extract_content: Allow for absolute paths in deps
The dependencies in the scripts are figures, images, includes or literal includes. Typically those dependencies are placed relative to the .rst file that is being copied, but in some instances you might want them to refer to an absolute path which, in fact, is relative to Sphinx's top-level source directory. In this case, and as long as the top-level source directory is provided, do not emit a warning. Signed-off-by: Carles Cufi <[email protected]>
1 parent ad17e41 commit d9f5345

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

doc/scripts/extract_content.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
Content = collections.namedtuple('Content', 'outputs output_dirs')
4141

4242

43-
def src_deps(zephyr_base, src_file, dest):
43+
def src_deps(zephyr_base, src_file, dest, src_root):
4444
# - zephyr_base: the ZEPHYR_BASE directory containing src_file
4545
# - src_file: path to a source file in the documentation
4646
# - dest: path to the top-level output/destination directory
47+
# - src_root: path to the Sphinx top-level source directory
4748
#
4849
# Return a list of Output objects which contain src_file's
4950
# additional dependencies, as they should be copied into
@@ -89,8 +90,22 @@ def src_deps(zephyr_base, src_file, dest):
8990
if not m:
9091
continue
9192

92-
dep_rel = m.group('dep_rel') # relative to src_dir
93+
dep_rel = m.group('dep_rel') # relative to src_dir or absolute
9394
dep_src = path.abspath(path.join(src_dir, dep_rel))
95+
if path.isabs(dep_rel):
96+
# Not a relative path, check if it's absolute if we have been
97+
# provided with a sphinx source directory root
98+
if not src_root:
99+
print("Absolute path to file:", dep_rel, "\n referenced by:",
100+
src_file, "with no --sphinx-src-root", file=sys.stderr)
101+
continue
102+
# Make it really relative
103+
dep_rel = '.' + dep_rel
104+
dep_src = path.abspath(path.join(src_root, dep_rel))
105+
if path.isfile(dep_src):
106+
# File found, but no need to copy it since it's part
107+
# of Sphinx's top-level source directory
108+
continue
94109
if not path.isfile(dep_src):
95110
print("File not found:", dep_src, "\n referenced by:",
96111
src_file, file=sys.stderr)
@@ -102,7 +117,7 @@ def src_deps(zephyr_base, src_file, dest):
102117
return deps
103118

104119

105-
def find_content(zephyr_base, src, dest, fnfilter, ignore):
120+
def find_content(zephyr_base, src, dest, fnfilter, ignore, src_root):
106121
# Create a list of Outputs to copy over, and new directories we
107122
# might need to make to contain them. Don't copy any files or
108123
# otherwise modify dest.
@@ -128,7 +143,7 @@ def find_content(zephyr_base, src, dest, fnfilter, ignore):
128143
# directories for dependencies are tracked too.
129144
for src_rel in sources:
130145
src_abs = path.join(dirpath, src_rel)
131-
deps = src_deps(zephyr_base, src_abs, dest)
146+
deps = src_deps(zephyr_base, src_abs, dest, src_root)
132147

133148
for depdir in (path.dirname(d.dst) for d in deps):
134149
output_dirs.add(depdir)
@@ -168,6 +183,11 @@ def main():
168183
parser.add_argument('--ignore', action='append',
169184
help='''Source directories to ignore when copying
170185
files. This may be given multiple times.''')
186+
parser.add_argument('--sphinx-src-root',
187+
help='''If given, absolute paths for dependencies are
188+
resolved using this root, which is the Sphinx top-level
189+
source directory as passed to sphinx-build.''')
190+
171191
parser.add_argument('content_config', nargs='+',
172192
help='''A glob:source:destination specification
173193
for content to extract. The "glob" is a documentation
@@ -191,7 +211,8 @@ def main():
191211
content_config = [cfg.split(':', 2) for cfg in args.content_config]
192212
outputs = set()
193213
for fnfilter, source, dest in content_config:
194-
content = find_content(zephyr_base, source, dest, fnfilter, ignore)
214+
content = find_content(zephyr_base, source, dest, fnfilter, ignore,
215+
args.sphinx_src_root)
195216
if not args.just_outputs:
196217
extract_content(content)
197218
outputs |= set(content.outputs)

0 commit comments

Comments
 (0)