Skip to content

Commit 01d385d

Browse files
markmcdcopybara-github
authored andcommitted
Support generating docs on Windows.
We use `os.path.join` to build URLs from relative paths in a number of places. This doesn't work on Windows, where the path separator is different. Rather than depending on an external package like `urlpath`, I've replaced `os.path.join` with `posixpath.join` and where needed, used os-split and posix-join to re-encode. Windows doesn't throw `OSError` on missing files either, so fixed that too. This *should* fix tensorflow#53216. Tested with `generate2` on my Windows box. PiperOrigin-RevId: 419944483
1 parent ca2035c commit 01d385d

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,7 @@ def build(self, output_dir):
871871
str(work_py_dir / self._short_name.replace('.', '/') /
872872
'_api_cache.json'))
873873

874-
try:
875-
os.makedirs(output_dir)
876-
except OSError as e:
877-
if e.strerror != 'File exists':
878-
raise
874+
os.makedirs(output_dir, exist_ok=True)
879875

880876
# Typical results are something like:
881877
#

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
# ==============================================================================
1616
"""Turn Python docstrings into Markdown for TensorFlow documentation."""
1717

18-
import ast
1918
import collections
2019
import dataclasses
2120
import enum
2221
import html
2322
import inspect
2423
import itertools
2524
import os
25+
import posixpath
2626
import pprint
2727
import re
2828
import textwrap
@@ -98,7 +98,7 @@ def documentation_path(full_name, is_fragment=False):
9898
if is_fragment:
9999
parts, fragment = parts[:-1], parts[-1]
100100

101-
result = os.path.join(*parts) + '.md'
101+
result = posixpath.join(*parts) + '.md'
102102

103103
if is_fragment:
104104
result = result + '#' + fragment
@@ -1437,6 +1437,8 @@ def docs_for_object(
14371437

14381438
relative_path = os.path.relpath(
14391439
path='.', start=os.path.dirname(documentation_path(full_name)) or '.')
1440+
# Convert from OS-specific path to URL/POSIX path.
1441+
relative_path = posixpath.join(*relative_path.split(os.path.sep))
14401442

14411443
with parser_config.reference_resolver.temp_prefix(relative_path):
14421444
page_info.set_doc(
@@ -1497,6 +1499,9 @@ def _get_defined_in(
14971499
continue
14981500
else:
14991501
code_url_prefix = temp_prefix
1502+
# rel_path is currently a platform-specific path, so we need to convert
1503+
# it to a posix path (for lack of a URL path).
1504+
rel_path = posixpath.join(*rel_path.split(os.path.sep))
15001505
break
15011506

15021507
# No link if the file was not found in a `base_dir`, or the prefix is None.
@@ -1534,10 +1539,10 @@ def _get_defined_in(
15341539
elif re.match(r'.*_pb2\.py$', rel_path):
15351540
# The _pb2.py files all appear right next to their defining .proto file.
15361541
rel_path = rel_path[:-7] + '.proto'
1537-
return _FileLocation(base_url=os.path.join(code_url_prefix, rel_path))
1542+
return _FileLocation(base_url=posixpath.join(code_url_prefix, rel_path))
15381543
else:
15391544
return _FileLocation(
1540-
base_url=os.path.join(code_url_prefix, rel_path),
1545+
base_url=posixpath.join(code_url_prefix, rel_path),
15411546
start_line=start_line,
15421547
end_line=end_line)
15431548

tools/tensorflow_docs/api_generator/reference_resolver.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import html
2121
import json
2222
import os
23+
import posixpath
2324
import re
2425

2526
from typing import Dict, List, Optional
@@ -350,7 +351,7 @@ def reference_to_url(self, ref_full_name):
350351

351352
if self._link_prefix is None:
352353
raise ValueError('you must set the `link_prefix`')
353-
url = os.path.join(self._link_prefix, rel_path)
354+
url = posixpath.join(self._link_prefix, rel_path)
354355
return url
355356

356357
def _one_ref(self, match, full_name=None):
@@ -411,6 +412,6 @@ def _cc_link(self, string, link_text):
411412
# relative_path_to_root gets you to api_docs/python, we go from there
412413
# to api_docs/cc, and then add ret.
413414
cc_relative_path = os.path.normpath(
414-
os.path.join(self._link_prefix, '../cc', ret))
415+
posixpath.join(self._link_prefix, '../cc', ret))
415416

416417
return f'<a href="{cc_relative_path}"><code>{link_text}</code></a>'

0 commit comments

Comments
 (0)