|
1 | 1 | """Defines serializers for each of our models."""
|
2 | 2 |
|
| 3 | +import re |
| 4 | + |
3 | 5 | from allauth.socialaccount.models import SocialAccount
|
| 6 | +from django.conf import settings |
4 | 7 | from rest_framework import serializers
|
5 | 8 |
|
6 | 9 | from readthedocs.builds.models import Build, BuildCommandResult, Version
|
@@ -127,11 +130,41 @@ class VersionAdminSerializer(VersionSerializer):
|
127 | 130 | class BuildCommandSerializer(serializers.ModelSerializer):
|
128 | 131 |
|
129 | 132 | run_time = serializers.ReadOnlyField()
|
| 133 | + command = serializers.SerializerMethodField() |
130 | 134 |
|
131 | 135 | class Meta:
|
132 | 136 | model = BuildCommandResult
|
133 | 137 | exclude = []
|
134 | 138 |
|
| 139 | + def get_command(self, obj): |
| 140 | + # HACK: remove unreadable paths from the command outputs when returning it from the API. |
| 141 | + # We could make this change at build level, but we want to avoid undoable issues from now |
| 142 | + # and hack a small solution to fix the immediate problem. |
| 143 | + # |
| 144 | + # This converts: |
| 145 | + # $ /usr/src/app/checkouts/readthedocs.org/user_builds/ |
| 146 | + # <container_hash>/<project_slug>/envs/<version_slug>/bin/python |
| 147 | + # $ /home/docs/checkouts/readthedocs.org/user_builds/ |
| 148 | + # <project_slug>/envs/<version_slug>/bin/python |
| 149 | + # into |
| 150 | + # $ python |
| 151 | + project_slug = obj.build.version.project.slug |
| 152 | + version_slug = obj.build.version.slug |
| 153 | + docroot = settings.DOCROOT.rstrip("/") # remove trailing '/' |
| 154 | + |
| 155 | + # Remove Docker hash from DOCROOT when running it locally |
| 156 | + # DOCROOT contains the Docker container hash (e.g. b7703d1b5854). |
| 157 | + # We have to remove it from the DOCROOT it self since it changes each time |
| 158 | + # we spin up a new Docker instance locally. |
| 159 | + container_hash = "/" |
| 160 | + if settings.RTD_DOCKER_COMPOSE: |
| 161 | + docroot = re.sub("/[0-9a-z]+/?$", "", settings.DOCROOT, count=1) |
| 162 | + container_hash = "/[0-9a-z]+/" |
| 163 | + |
| 164 | + regex = f"{docroot}{container_hash}{project_slug}/envs/{version_slug}(/bin/)?" |
| 165 | + command = re.sub(regex, "", obj.command, count=1) |
| 166 | + return command |
| 167 | + |
135 | 168 |
|
136 | 169 | class BuildSerializer(serializers.ModelSerializer):
|
137 | 170 |
|
|
0 commit comments