1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create the source code documenation.
This script covers all needed steps to create or recreate the widelands
source code documentation.
Needed dependency: Sphinx
"""
from __future__ import print_function
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from subprocess import check_call, CalledProcessError
from documentation import conf
import os
import sys
import shutil
import glob
class Command(BaseCommand):
help = 'Create the source code documenation.'
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
# Define the main directories used in this class
self.sphinx_dir = os.path.join(
settings.WIDELANDS_SVN_DIR, 'doc/sphinx')
self.build_dir = os.path.join(
settings.MEDIA_ROOT, 'documentation/html_temp')
self.sphinx_conf_dir = os.path.dirname(conf.__file__)
def move_docs(self):
"""Move the documentation created by sphinxdoc to the correct folder.
On unix systems the files were served from the symlink called
'html'. On Windows systems the files will only be copied in a
folder called 'html'.
"""
if os.name == 'posix':
# Creating symlinks is only available on unix systems
try:
link_name = os.path.join(
settings.MEDIA_ROOT, 'documentation/html')
target_dir = os.path.join(
settings.MEDIA_ROOT, 'documentation/current')
if not os.path.exists(target_dir):
# only needed on first run
os.mkdir(target_dir)
if os.path.exists(link_name):
# only needed if this script has already run
os.remove(link_name)
# Temporarily switch the symlink
os.symlink(self.build_dir, link_name)
# Remove current
shutil.rmtree(target_dir)
# Copy new build to current
shutil.copytree(self.build_dir, target_dir)
# Switch the link to current
os.remove(link_name)
os.symlink(target_dir, link_name)
except:
raise
else:
# Non unix OS: Copy docs
try:
target_dir = os.path.join(
settings.MEDIA_ROOT, 'documentation/html')
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
shutil.copytree(self.build_dir, target_dir)
except:
raise
try:
# The new build directory is no longer needed
shutil.rmtree(self.build_dir)
except:
raise
def handle(self, *args, **options):
"""Create the widelands source code documentation.
The Documenatation is build by sphinxdoc in the directory
'settings/MEDIA/documentation/html_temp'.
"""
if not os.path.exists(self.sphinx_dir):
print(
"Can't find the directory given by WIDELANDS_SVN_DIR in local_settings.py:\n", self.sphinx_dir)
sys.exit(1)
if os.path.exists(os.path.join(self.sphinx_dir, 'build')):
# Clean the autogen* files created by extract_rst.py
# This has to be done because sometimes such a file remains after
# removing it from extract_rst.
try:
for f in glob.glob(os.path.join(self.sphinx_dir, 'source/autogen*')):
os.remove(f)
except OSError:
raise
# Locally 'dirhtml' do not work because the staticfiles view disallow
# directory indexes, but 'dirhtml' gives nicer addresses in production
builder = 'html'
if not settings.DEBUG:
# In production DEBUG is False
builder = 'dirhtml'
try:
check_call(['python', os.path.join(
self.sphinx_dir, 'extract_rst.py')])
check_call(['sphinx-build',
'-b', builder,
'-d', os.path.join(self.sphinx_dir, 'build/doctrees'),
'-c', self.sphinx_conf_dir,
os.path.join(self.sphinx_dir, 'source'),
os.path.join(self.build_dir),
])
except CalledProcessError as why:
print('An error occured: {0}'.format(why))
sys.exit(1)
self.move_docs()
|