2
# -*- coding: utf-8 -*-
4
"""Create the source code documenation.
6
This script covers all needed steps to create or recreate the widelands
7
source code documentation.
9
Needed dependency: Sphinx
14
from django.core.management.base import BaseCommand, CommandError
15
from django.conf import settings
16
from subprocess import check_call, CalledProcessError
17
from documentation import conf
24
class Command(BaseCommand):
25
help = 'Create the source code documenation.'
27
def __init__(self, *args, **kwargs):
28
super(Command, self).__init__(*args, **kwargs)
30
# Define the main directories used in this class
31
self.sphinx_dir = os.path.join(
32
settings.WIDELANDS_SVN_DIR, 'doc/sphinx')
33
self.build_dir = os.path.join(
34
settings.MEDIA_ROOT, 'documentation/html_temp')
35
self.sphinx_conf_dir = os.path.dirname(conf.__file__)
38
"""Move the documentation created by sphinxdoc to the correct folder.
40
On unix systems the files were served from the symlink called
41
'html'. On Windows systems the files will only be copied in a
46
if os.name == 'posix':
47
# Creating symlinks is only available on unix systems
49
link_name = os.path.join(
50
settings.MEDIA_ROOT, 'documentation/html')
51
target_dir = os.path.join(
52
settings.MEDIA_ROOT, 'documentation/current')
54
if not os.path.exists(target_dir):
55
# only needed on first run
58
if os.path.exists(link_name):
59
# only needed if this script has already run
62
# Temporarily switch the symlink
63
os.symlink(self.build_dir, link_name)
65
shutil.rmtree(target_dir)
66
# Copy new build to current
67
shutil.copytree(self.build_dir, target_dir)
68
# Switch the link to current
70
os.symlink(target_dir, link_name)
74
# Non unix OS: Copy docs
76
target_dir = os.path.join(
77
settings.MEDIA_ROOT, 'documentation/html')
78
if os.path.exists(target_dir):
79
shutil.rmtree(target_dir)
80
shutil.copytree(self.build_dir, target_dir)
85
# The new build directory is no longer needed
86
shutil.rmtree(self.build_dir)
90
def handle(self, *args, **options):
91
"""Create the widelands source code documentation.
93
The Documenatation is built by sphinxdoc in the directory
94
'settings/MEDIA/documentation/html_temp'.
98
if not os.path.exists(self.sphinx_dir):
100
"Can't find the directory given by WIDELANDS_SVN_DIR in local_settings.py:\n", self.sphinx_dir)
103
if os.path.exists(os.path.join(self.sphinx_dir, 'build')):
104
# Clean the autogen* files created by extract_rst.py
105
# This has to be done because sometimes such a file remains after
106
# removing it from extract_rst.
108
for f in glob.glob(os.path.join(self.sphinx_dir, 'source/autogen*')):
113
# Locally 'dirhtml' do not work because the staticfiles view disallow
114
# directory indexes, but 'dirhtml' gives nicer addresses in production
116
if not settings.DEBUG:
117
# In production DEBUG is False
121
check_call(['python', os.path.join(
122
self.sphinx_dir, 'extract_rst.py')])
123
check_call(['sphinx-build',
125
'-d', os.path.join(self.sphinx_dir, 'build/doctrees'),
126
'-c', self.sphinx_conf_dir,
127
os.path.join(self.sphinx_dir, 'source'),
130
except CalledProcessError as why:
131
print('An error occured: {0}'.format(why))