~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to documentation/management/commands/create_docs.py

  • Committer: franku
  • Date: 2018-05-08 19:33:19 UTC
  • mto: This revision was merged to the branch mainline in revision 496.
  • Revision ID: somal@arcor.de-20180508193319-tyt9kdv0dcghrhhp
run pyformat; some tweaks

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
18
import os
17
19
import sys
18
 
import imp
19
20
import shutil
20
21
import glob
21
 
from subprocess import check_call, CalledProcessError
 
22
 
22
23
 
23
24
class Command(BaseCommand):
24
25
    help = 'Create the source code documenation.'
25
 
        
26
 
    def move_docs(self, build_dir):
 
26
 
 
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__)
 
35
 
 
36
    def move_docs(self):
27
37
        """Move the documentation created by sphinxdoc to the correct folder.
28
 
    
 
38
 
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
31
 
        called 'html'.
32
 
    
 
40
        'html'. On Windows systems the files will only be copied in a
 
41
        folder called 'html'.
 
42
 
33
43
        """
34
 
    
 
44
 
35
45
        if os.name == 'posix':
 
46
            # Creating symlinks is only available on unix systems
36
47
            try:
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')
42
 
    
 
52
 
43
53
                if not os.path.exists(target_dir):
44
54
                    # only needed on first run
45
55
                    os.mkdir(target_dir)
46
 
    
 
56
 
47
57
                if os.path.exists(link_name):
48
58
                    # only needed if this script has already run
49
59
                    os.remove(link_name)
50
 
    
 
60
 
51
61
                # Temporarily switch the symlink
52
 
                os.symlink(build_dir, link_name)
 
62
                os.symlink(self.build_dir, link_name)
53
63
                # Remove current
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)
70
80
            except:
71
81
                raise
72
 
    
73
 
        # The newly created directory is no longer needed
74
 
        shutil.rmtree(build_dir)
75
 
    
76
 
    
 
82
 
 
83
        # The newly build directory is no longer needed
 
84
        shutil.rmtree(self.build_dir)
 
85
 
77
86
    def handle(self, *args, **options):
78
87
        """Create the widelands source code documentation.
79
 
    
 
88
 
80
89
        The Documenatation is build by sphinxdoc in the directory
81
90
        'settings/MEDIA/documentation/html_temp'.
82
 
    
 
91
 
83
92
        """
84
 
    
85
 
        sphinx_dir = os.path.join(settings.WIDELANDS_SVN_DIR, 'doc/sphinx')
86
 
        build_dir = os.path.join(settings.MEDIA_ROOT, 'documentation/html_temp')
87
 
    
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)
 
93
 
 
94
        if not os.path.exists(self.sphinx_dir):
 
95
            print(
 
96
                "Can't find the directory given by WIDELANDS_SVN_DIR in local_settings.py:\n", self.sphinx_dir)
90
97
            sys.exit(1)
91
 
    
92
 
        if os.path.exists(os.path.join(sphinx_dir, 'build')):
93
 
    
 
98
 
 
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
 
103
            # then.
97
104
            try:
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*')):
99
106
                    os.remove(f)
100
107
            except OSError:
101
108
                raise
102
 
    
 
109
 
103
110
        # Locally 'dirhtml' do not work because the staticfiles view disallow
104
111
        # directory indexes, but 'dirhtml' gives nicer addresses in production
105
112
        builder = 'html'
106
113
        if not settings.DEBUG:
107
114
            # In production DEBUG is False
108
115
            builder = 'dirhtml'
109
 
    
 
116
 
110
117
        try:
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',
113
121
                        '-b', builder,
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),
118
126
                        ])
119
127
        except CalledProcessError as why:
120
128
            print('An error occured: {0}'.format(why))
121
129
            sys.exit(1)
122
 
    
123
 
        self.move_docs(build_dir)
 
130
 
 
131
        self.move_docs()