Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: rename PDF/ePUB filename to valid one automatically #11198

Merged
merged 15 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion readthedocs/projects/tasks/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
rebuilding documentation.
"""
import os
import shutil
import signal
import socket
import subprocess
Expand Down Expand Up @@ -605,7 +606,8 @@ def get_valid_artifact_types(self):
# These output format does not support multiple files yet.
# In case multiple files are found, the upload for this format is not performed.
if artifact_type in ARTIFACT_TYPES_WITHOUT_MULTIPLE_FILES_SUPPORT:
artifact_format_files = len(os.listdir(artifact_directory))
list_dir = os.listdir(artifact_directory)
artifact_format_files = len(list_dir)
if artifact_format_files > 1:
log.error(
"Multiple files are not supported for this format. "
Expand All @@ -626,6 +628,22 @@ def get_valid_artifact_types(self):
},
)

if artifact_format_files == 1:
humitos marked this conversation as resolved.
Show resolved Hide resolved
# Rename file as "<project_slug>-<version_slug>.<artifact_type>",
# which is the filename that Proxito serves for offline formats
filename = list_dir[0]
_, extension = filename.rsplit(".")
shutil.move(
humitos marked this conversation as resolved.
Show resolved Hide resolved
os.path.join(
artifact_directory,
list_dir[0],
),
os.path.join(
artifact_directory,
f"{self.data.project.slug}.{extension}",
),
)

# If all the conditions were met, the artifact is valid
valid_artifacts.append(artifact_type)

Expand Down
27 changes: 26 additions & 1 deletion readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pathlib
import textwrap
import uuid
from unittest import mock

import django_dynamic_fixture as fixture
Expand Down Expand Up @@ -399,6 +400,7 @@ def test_get_env_vars(self, load_yaml_config, build_environment, config, externa
expected_build_env_vars["PRIVATE_TOKEN"] = "a1b2c3"
assert build_env_vars == expected_build_env_vars

@mock.patch("readthedocs.projects.tasks.builds.shutil")
@mock.patch("readthedocs.projects.tasks.builds.index_build")
@mock.patch("readthedocs.projects.tasks.builds.build_complete")
@mock.patch("readthedocs.projects.tasks.builds.send_external_build_status")
Expand All @@ -413,6 +415,7 @@ def test_successful_build(
send_external_build_status,
build_complete,
index_build,
shutilmock,
):
load_yaml_config.return_value = get_build_config(
{
Expand All @@ -429,12 +432,16 @@ def test_successful_build(
# Create the artifact paths, so it's detected by the builder
os.makedirs(self.project.artifact_path(version=self.version.slug, type_="html"))
os.makedirs(self.project.artifact_path(version=self.version.slug, type_="json"))
filename = str(uuid.uuid4())
for f in ("htmlzip", "epub", "pdf"):
extension = "zip" if f == "htmlzip" else f
os.makedirs(self.project.artifact_path(version=self.version.slug, type_=f))
pathlib.Path(
os.path.join(
self.project.artifact_path(version=self.version.slug, type_=f),
f"{self.project.slug}.{f}",
# Use a random name for the offline format.
# We will automatically rename this file to filename El Proxito expects.
f"{filename}.{extension}",
)
).touch()

Expand All @@ -448,6 +455,24 @@ def test_successful_build(

self._trigger_update_docs_task()

# Offline formats were renamed to the correct filename.
shutilmock.move.assert_has_calls(
[
mock.call(
f"/tmp/readthedocs-tests/git-repository/_readthedocs/epub/{filename}.epub",
f"/tmp/readthedocs-tests/git-repository/_readthedocs/epub/{self.project.slug}.epub",
),
mock.call(
f"/tmp/readthedocs-tests/git-repository/_readthedocs/htmlzip/{filename}.zip",
f"/tmp/readthedocs-tests/git-repository/_readthedocs/htmlzip/{self.project.slug}.zip",
),
mock.call(
f"/tmp/readthedocs-tests/git-repository/_readthedocs/pdf/{filename}.pdf",
f"/tmp/readthedocs-tests/git-repository/_readthedocs/pdf/{self.project.slug}.pdf",
),
]
)

# It has to be called twice, ``before_start`` and ``after_return``
clean_build.assert_has_calls(
[mock.call(mock.ANY), mock.call(mock.ANY)] # the argument is an APIVersion
Expand Down