Skip to content

Commit e455e4f

Browse files
authored
Update docs build to support the new antsibull-docs 2.15.0 features (#2001)
* Provide information for antsibull-docs 2.15.0. * I don't think 'named' will ever get implemented. * The paths might be relative to the old CWD. * Show which commands are run.
1 parent d499606 commit e455e4f

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

hacking/build_library/build_ansible/command_plugins/docs_build.py

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class NoSuchFile(Exception):
3939
# Helpers
4040
#
4141

42-
def find_latest_ansible_dir(build_data_working):
42+
def find_latest_ansible_dir(build_data_working) -> tuple[str, "packaging.version.Version"]:
4343
"""Find the most recent ansible major version."""
4444
# imports here so that they don't cause unnecessary deps for all of the plugins
4545
from packaging.version import InvalidVersion, Version
4646

4747
ansible_directories = glob.glob(os.path.join(build_data_working, '[0-9.]*'))
4848

4949
# Find the latest ansible version directory
50-
latest = None
50+
latest_dir = None
5151
latest_ver = Version('0')
5252
for directory_name in (d for d in ansible_directories if os.path.isdir(d)):
5353
try:
@@ -61,12 +61,12 @@ def find_latest_ansible_dir(build_data_working):
6161

6262
if new_version > latest_ver:
6363
latest_ver = new_version
64-
latest = directory_name
64+
latest_dir = directory_name
6565

66-
if latest is None:
66+
if latest_dir is None:
6767
raise NoSuchFile('Could not find an ansible data directory in {0}'.format(build_data_working))
6868

69-
return latest
69+
return latest_dir, latest_ver
7070

7171

7272
def parse_deps_file(filename):
@@ -84,7 +84,7 @@ def write_deps_file(filename, deps_data):
8484
f.write(f'{key}: {value}\n')
8585

8686

87-
def find_latest_deps_file(build_data_working, ansible_version):
87+
def find_latest_deps_file(build_data_working, ansible_version: str) -> tuple[str, "packaging.version.Version"]:
8888
"""Find the most recent ansible deps file for the given ansible major version."""
8989
# imports here so that they don't cause unnecessary deps for all of the plugins
9090
from packaging.version import Version
@@ -95,19 +95,19 @@ def find_latest_deps_file(build_data_working, ansible_version):
9595
raise Exception('No deps files exist for version {0}'.format(ansible_version))
9696

9797
# Find the latest version of the deps file for this major version
98-
latest = None
98+
latest_deps_file = None
9999
latest_ver = Version('0')
100100
for filename in deps_files:
101101
deps_data = parse_deps_file(filename)
102102
new_version = Version(deps_data['_ansible_version'])
103103
if new_version > latest_ver:
104104
latest_ver = new_version
105-
latest = filename
105+
latest_deps_file = filename
106106

107-
if latest is None:
107+
if latest_deps_file is None:
108108
raise NoSuchFile('Could not find an ansible deps file in {0}'.format(data_dir))
109109

110-
return latest
110+
return latest_deps_file, latest_ver
111111

112112

113113
#
@@ -133,9 +133,18 @@ def generate_core_docs(args):
133133
f.write(yaml.dump(deps_file_contents))
134134

135135
# Generate the plugin rst
136-
return antsibull_docs.run(['antsibull-docs', 'stable', '--deps-file', modified_deps_file,
137-
'--ansible-core-source', str(args.top_dir),
138-
'--dest-dir', args.output_dir])
136+
full_command = [
137+
'antsibull-docs',
138+
'stable',
139+
'--deps-file',
140+
modified_deps_file,
141+
'--ansible-core-source',
142+
str(args.top_dir),
143+
'--dest-dir',
144+
args.output_dir,
145+
]
146+
print(f"Running {full_command!r}:")
147+
return antsibull_docs.run(full_command)
139148

140149
# If we make this more than just a driver for antsibull:
141150
# Run other rst generation
@@ -164,16 +173,21 @@ def generate_full_docs(args):
164173
if args.ansible_build_data:
165174
build_data_working = args.ansible_build_data
166175

167-
ansible_version = args.ansible_version
176+
ansible_version: str = args.ansible_version
168177
if ansible_version is None:
169-
ansible_version = find_latest_ansible_dir(build_data_working)
170-
params = ['devel', '--pieces-file', os.path.join(ansible_version, 'ansible.in')]
178+
devel_dir, devel_version = find_latest_ansible_dir(build_data_working)
179+
params = ['devel', '--pieces-file', 'ansible.in', '--major-version', str(devel_version.major)]
180+
cwd = str(devel_dir)
171181
else:
172-
latest_filename = find_latest_deps_file(build_data_working, ansible_version)
182+
latest_deps_file, ansible_version_ver = find_latest_deps_file(build_data_working, ansible_version)
183+
deps_dir = os.path.dirname(latest_deps_file)
173184

174185
# Make a copy of the deps file so that we can set the ansible-core version we'll use
175186
modified_deps_file = os.path.join(tmp_dir, 'ansible.deps')
176-
shutil.copyfile(latest_filename, modified_deps_file)
187+
shutil.copyfile(latest_deps_file, modified_deps_file)
188+
189+
# Make a copy of collection-meta.yaml
190+
shutil.copyfile(os.path.join(deps_dir, 'collection-meta.yaml'), os.path.join(tmp_dir, 'collection-meta.yaml'))
177191

178192
# Put our version of ansible-core into the deps file
179193
deps_data = parse_deps_file(modified_deps_file)
@@ -182,12 +196,23 @@ def generate_full_docs(args):
182196

183197
write_deps_file(modified_deps_file, deps_data)
184198

185-
params = ['stable', '--deps-file', modified_deps_file]
199+
params = ['stable', '--deps-file', 'ansible.deps', '--version', str(ansible_version_ver)]
200+
cwd = str(tmp_dir)
186201

187-
# Generate the plugin rst
188-
return antsibull_docs.run(['antsibull-docs'] + params +
189-
['--ansible-core-source', str(args.top_dir),
190-
'--dest-dir', args.output_dir])
202+
old_cwd = os.getcwd()
203+
try:
204+
os.chdir(cwd)
205+
# Generate the plugin rst
206+
full_command = ['antsibull-docs'] + params + [
207+
'--ansible-core-source',
208+
os.path.join(old_cwd, str(args.top_dir)),
209+
'--dest-dir',
210+
os.path.join(old_cwd, args.output_dir),
211+
]
212+
print(f"Running {full_command!r} in {cwd!r}:")
213+
return antsibull_docs.run(full_command)
214+
finally:
215+
os.chdir(old_cwd)
191216

192217
# If we make this more than just a driver for antsibull:
193218
# Run other rst generation
@@ -199,7 +224,6 @@ class CollectionPluginDocs(Command):
199224
_ACTION_HELP = """Action to perform.
200225
full: Regenerate the rst for the full ansible website.
201226
core: Regenerate the rst for plugins in ansible-core and then build the website.
202-
named: Regenerate the rst for the named plugins and then build the website.
203227
"""
204228

205229
@classmethod
@@ -212,7 +236,7 @@ def init_parser(cls, add_parser):
212236
' hierarchy.')
213237
# I think we should make the actions a subparser but need to look in git history and see if
214238
# we tried that and changed it for some reason.
215-
parser.add_argument('action', action='store', choices=('full', 'core', 'named'),
239+
parser.add_argument('action', action='store', choices=('full', 'core'),
216240
default='full', help=cls._ACTION_HELP)
217241
parser.add_argument("-o", "--output-dir", action="store", dest="output_dir",
218242
default=DEFAULT_OUTPUT_DIR,
@@ -249,7 +273,7 @@ def main(args):
249273

250274
if args.action == 'core':
251275
return generate_core_docs(args)
252-
# args.action == 'named' (Invalid actions are caught by argparse)
253-
raise NotImplementedError('Building docs for specific files is not yet implemented')
276+
277+
raise NotImplementedError('New actions have to be explicitly supported by the code')
254278

255279
# return 0

0 commit comments

Comments
 (0)