summaryrefslogtreecommitdiffstats
path: root/func/minion/modules/djangoctl.py
blob: 9404f2197d44d05ca5554b25a8297fab8bd5397a (plain)
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
#
# Copyright 2008
# Steve 'Ashcrow' Milner <smilner@redhat.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
Django control module.

See http://www.djangoproject.com/ for more information about Django.
"""

import exceptions
import os
import sys
import types

import func_module
import sub_process


class Django(func_module.FuncModule):

    version = "0.0.1"
    api_version = "0.0.1"
    description = "Django control module."

    def _verify_path(self, project_path):
        """
        Verifies the project path exists and is readable.

        project_path is the path to the Django project.
        """
        # Verify path exists
        if not os.access(project_path, os.F_OK):
            raise exceptions.OSError(
                "Path %s doesn't exist." % project_path)
        # .. and that we can read it
        if not os.access(project_path, os.R_OK):
            raise exceptions.OSError(
                "Path %s is not readable." % project_path)
        return True

    def _get_settings(self, project_path):
        """
        Gets the settings object.

        project_path is the path to the Django project.
        """

        self._verify_path(project_path)
        # Append the settings to the path
        sys.path.append(project_path)
        import settings
        del sys.path[-1]
        return settings

    def settings(self, project_path):
        """
        Simple gets the settings for a specific application.

        project_path is the path to the Django project.
        """
        self._verify_path(project_path)
        settings = self._get_settings(project_path)
        items = {}
        # for each item in settings attempt to append the setting to items
        for item in dir(settings):
            # ... assuming it isn't _*
            if item[1] != '_':
                setting = getattr(settings, item)
                # See if we can directly append
                if type(setting) in [types.TupleType,
                                     types.StringType,
                                     types.DictType,
                                     types.ListType]:
                    items[item] = setting
                else:
                    # Else force it to a string
                    items[item] = str(setting)
        # Remove the settings from the path
        return items

    def setting(self, project_path, a_setting):
        """
        Returns a single setting.

        project_path is the path to the Django project.
        """
        items = self.settings(project_path)
        return items[a_setting]

    def manage(self, project_path, command):
        """
        Executes django-admin.py adding the path and settings
        variables for a command.

        project_path is the path to the Django project.
        command is the command to run.
        """
        self._verify_path(project_path)
        settings = self._get_settings(project_path)

        command = "django-admin.py %s --pythonpath=%s --settings=settings" % (
                      command, project_path)
        cmdref = sub_process.Popen(command.split(), stdout=sub_process.PIPE,
                                   stderr=sub_process.PIPE, shell=False)
        data = cmdref.communicate()
        return (cmdref.returncode, data[0], data[1])

    def environment(self, project_path):
        """
        Gets the environment information that the project is running on.

        project_path is the path to the Django project.
        """
        self._verify_path(project_path)
        import platform
        from django import get_version

        return {'django_version': get_version(),
                'uname': platform.uname(),
                'dist': platform.dist(),
                'python_version': platform.python_version()}