Skip to content

Commit 0cd46c5

Browse files
committed
Add static metadata output on project save
* Uses project translations to build supported language list * Saves to metadata.json on project save/build, uploads to app servers
1 parent 63e26c2 commit 0cd46c5

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

readthedocs/projects/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from projects import constants
1818
from projects.exceptions import ProjectImportError
1919
from projects.templatetags.projects_tags import sort_version_aware
20-
from projects.utils import highest_version as _highest, make_api_version, symlink
20+
from projects.utils import (highest_version as _highest, make_api_version,
21+
symlink, update_static_metadata)
2122
from taggit.managers import TaggableManager
2223
from tastyapi.slum import api
2324

@@ -292,6 +293,10 @@ def save(self, *args, **kwargs):
292293
symlink(project=self.slug)
293294
except Exception, e:
294295
log.error('failed to symlink project', exc_info=True)
296+
try:
297+
update_static_metadata(project_pk=self.pk)
298+
except Exception:
299+
log.error('failed to update static metadata', exc_info=True)
295300
return obj
296301

297302
def get_absolute_url(self):
@@ -570,6 +575,12 @@ def rtd_build_path(self, version="latest"):
570575
"""
571576
return os.path.join(self.doc_path, 'rtd-builds', version)
572577

578+
def static_metadata_path(self):
579+
"""
580+
The path to the static metadata JSON settings file
581+
"""
582+
return os.path.join(self.doc_path, 'metadata.json')
583+
573584
def conf_file(self, version='latest'):
574585
if self.conf_py_file:
575586
log.debug('Inserting conf.py file path from model')

readthedocs/projects/tasks.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tasks related to projects, including fetching repository code, cleaning
22
``conf.py`` files, and rebuilding documentation.
3-
43
"""
54
import datetime
65
import fnmatch
@@ -34,7 +33,8 @@
3433
make_api_version, make_api_project)
3534
from tastyapi import client as tastyapi_client
3635
from tastyapi import api, apiv2
37-
from core.utils import copy_to_app_servers, run_on_app_servers
36+
from core.utils import (copy_to_app_servers, copy_file_to_app_servers,
37+
run_on_app_servers)
3838

3939
ghetto_hack = re.compile(
4040
r'(?P<key>.*)\s*=\s*u?\[?[\'\"](?P<value>.*)[\'\"]\]?')
@@ -771,6 +771,48 @@ def remove_symlink_single_version(version):
771771
symlink = version.project.single_version_symlink_path()
772772
run_on_app_servers('rm %s' % symlink)
773773

774+
def update_static_metadata(project_pk):
775+
"""Update static metadata JSON file
776+
777+
Metadata settings include the following project settings:
778+
779+
version
780+
The default version for the project, default: `latest`
781+
782+
language
783+
The default language for the project, default: `en`
784+
785+
languages
786+
List of languages built by linked translation projects.
787+
"""
788+
project_base = apiv2.project(project_pk)
789+
project_data = project_base.get()
790+
project = make_api_project(project_data)
791+
translations = project_base.translations.get()['translations']
792+
languages = set([
793+
translation['language']
794+
for translation in translations
795+
if 'language' in translation
796+
])
797+
# Convert to JSON safe types
798+
metadata = {
799+
'version': project.default_version,
800+
'language': project.language,
801+
'languages': list(languages)
802+
}
803+
try:
804+
path = project.static_metadata_path()
805+
fh = open(path, 'w')
806+
json.dump(metadata, fh)
807+
fh.close()
808+
copy_file_to_app_servers(path, path)
809+
except IOError as e:
810+
log.debug(LOG_TEMPLATE.format(
811+
project=project.slug,
812+
version='',
813+
msg='Cannot write to metadata.json: {0}'.format(e)
814+
))
815+
774816
def send_notifications(version, build):
775817
#zenircbot_notification(version.id)
776818
for hook in version.project.webhook_notifications.all():

readthedocs/projects/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def symlink(project, version='latest'):
3434
tasks.symlink_cnames(v)
3535
tasks.symlink_translations(v)
3636

37+
def update_static_metadata(project_pk):
38+
from projects import tasks
39+
log.info("Updating static metadata")
40+
tasks.update_static_metadata(project_pk)
41+
3742
def find_file(file):
3843
"""Find matching filenames in the current directory and its subdirectories,
3944
and return a list of matching filenames.

0 commit comments

Comments
 (0)