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 all 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
1 change: 1 addition & 0 deletions readthedocs/core/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def assert_path_is_inside_docroot(path):
log.error(
"Suspicious operation outside the docroot directory.",
path_resolved=str(resolved_path),
docroot=settings.DOCROOT,
)
raise SuspiciousFileOperation(path)

Expand Down
18 changes: 17 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,18 @@ 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
assert_path_is_inside_docroot(destination)
shutil.move(path, destination)

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

Expand Down
43 changes: 42 additions & 1 deletion readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import pathlib
import textwrap
import uuid
from pathlib import Path
from unittest import mock

import django_dynamic_fixture as fixture
import pytest
from django.conf import settings
from django.test.utils import override_settings

from readthedocs.builds.constants import (
BUILD_STATUS_FAILURE,
Expand Down Expand Up @@ -260,6 +263,7 @@ def test_build_respects_formats_mkdocs(self, build_docs_class, load_yaml_config)

build_docs_class.assert_called_once_with("mkdocs") # HTML builder

@override_settings(DOCROOT="/tmp/readthedocs-tests/git-repository/")
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")
def test_build_updates_documentation_type(self, load_yaml_config):
assert self.version.documentation_type == "sphinx"
Expand Down Expand Up @@ -403,6 +407,8 @@ 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

@override_settings(DOCROOT="/tmp/readthedocs-tests/git-repository/")
@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 +423,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 +440,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 +463,36 @@ def test_successful_build(

self._trigger_update_docs_task()

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

# 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