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
13
from __future__ import print_function
19
from subprocess import check_call, CalledProcessError
22
def get_local_settings():
23
"""Get local_settings."""
25
# Didn't find a better way to get the settings from the parent directory
27
f, f_path, descr = imp.find_module('../local_settings')
28
settings = imp.load_module('local_settings', f, f_path, descr)
29
except ImportError as why:
30
print("Couldn't find local_settings.py! ", why)
35
global SPHINX_DIR, BUILD_DIR
36
SPHINX_DIR = os.path.join(settings.WIDELANDS_SVN_DIR, 'doc/sphinx')
37
BUILD_DIR = os.path.join(settings.MEDIA_ROOT, 'documentation/html_temp')
42
def move_docs(settings):
43
"""Move the documentation created by sphinxdoc to the correct folder.
45
On unix systems the files were served from the symlink called
46
'html'. On Windows systems the files will only be copied in a folder
51
if os.name == 'posix':
53
# Creating symlinks is only available on unix systems
54
link_name = os.path.join(
55
settings.MEDIA_ROOT, 'documentation/html')
56
target_dir = os.path.join(
57
settings.MEDIA_ROOT, 'documentation/current')
59
if not os.path.exists(target_dir):
60
# only needed on first run
63
if os.path.exists(link_name):
64
# only needed if this script has already run
67
# Temporarily switch the symlink
68
os.symlink(BUILD_DIR, link_name)
70
shutil.rmtree(target_dir)
71
# Copy new build to current
72
shutil.copytree(BUILD_DIR, target_dir)
73
# Switch the link to current
75
os.symlink(target_dir, link_name)
79
# Non unix OS: Copy docs
81
target_dir = os.path.join(
82
settings.MEDIA_ROOT, 'documentation/html')
83
if os.path.exists(target_dir):
84
shutil.rmtree(target_dir)
85
shutil.copytree(BUILD_DIR, target_dir)
89
# The newly created directory is no longer needed
90
shutil.rmtree(BUILD_DIR)
93
def create_sphinxdoc():
94
"""Create the widelands source code documentation.
96
The Documenatation is build by sphinxdoc directly in the directory
97
'settings/MEDIA/documentation/html_temp'.
101
settings = get_local_settings()
103
if not os.path.exists(SPHINX_DIR):
104
print("Can't find the directory given by WIDELANDS_SVN_DIR in local_settings.py:\n", SPHINX_DIR)
107
if os.path.exists(os.path.join(SPHINX_DIR, 'build')):
109
# Clean the autogen* files created by extract_rst.py
110
# This has to be done because sometimes such a file remains after
111
# removing it from extract_rst. sphinx-build throughs an error then.
113
for f in glob.glob(os.path.join(SPHINX_DIR, 'source/autogen*')):
118
# Locally 'dirhtml' do not work because the staticfiles view disallow
119
# directory indexes, but 'dirhtml' gives nicer addresses in production
121
if hasattr(settings, 'DEBUG'):
122
# In production local_settings.py has no DEBUG statement
126
check_call(['python', os.path.join(SPHINX_DIR, 'extract_rst.py')])
127
check_call(['sphinx-build',
129
'-d', os.path.join(SPHINX_DIR, 'build/doctrees'),
130
'-c', os.path.join(BUILD_DIR, '../../../documentation'),
131
os.path.join(SPHINX_DIR, 'source'),
132
os.path.join(BUILD_DIR),
134
except CalledProcessError as why:
135
print('An error occured: {0}'.format(why))
140
if __name__ == '__main__':
142
from django.conf import settings
144
print('Are you running this script with activated virtual environment?')
147
sys.exit(create_sphinxdoc())