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