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 12 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
17 changes: 16 additions & 1 deletion readthedocs/projects/tasks/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
rebuilding documentation.
"""
import os
import shutil
import signal
import socket
import subprocess
from dataclasses import dataclass, field
from pathlib import Path

import structlog
from celery import Task
Expand Down Expand Up @@ -40,6 +42,7 @@
from readthedocs.builds.utils import memcache_lock
from readthedocs.config.config import BuildConfigV2
from readthedocs.config.exceptions import ConfigError
from readthedocs.core.utils.filesystem import assert_path_is_inside_docroot
from readthedocs.doc_builder.director import BuildDirector
from readthedocs.doc_builder.environments import (
DockerBuildEnvironment,
Expand Down Expand Up @@ -605,7 +608,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 +630,17 @@ def get_valid_artifact_types(self):
},
)

# 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(".")
path = Path(artifact_directory) / filename
destination = (
Path(artifact_directory) / f"{self.data.project.slug}.{extension}"
)
assert_path_is_inside_docroot(path)
humitos marked this conversation as resolved.
Show resolved Hide resolved
shutil.move(path, destination)

# 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 @@ -403,6 +404,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 @@ -417,6 +419,7 @@ def test_successful_build(
send_external_build_status,
build_complete,
index_build,
shutilmock,
):
load_yaml_config.return_value = get_build_config(
{
Expand All @@ -433,12 +436,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 @@ -452,6 +459,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