Attached is a Django module for func that allows people to pull settings data as well as do basic management functions. See, I'm alive after all! -- "An organization that treats its programmers as morons will soon have programmers that are willing and able to act like morons only." -Bjarne Stroustrup
From 4c3a3a8378496e5a52ce4cd630381a9c3db8d912 Mon Sep 17 00:00:00 2001 From: Steve 'Ashcrow' Milner <stevem@xxxxxxxxxxxx> Date: Tue, 7 Oct 2008 14:57:11 -0400 Subject: [PATCH] patch to add some basic support for pulling data out of and manipulating django applications --- func/minion/modules/djangoctl.py | 128 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 128 insertions(+), 0 deletions(-) create mode 100644 func/minion/modules/djangoctl.py diff --git a/func/minion/modules/djangoctl.py b/func/minion/modules/djangoctl.py new file mode 100644 index 0000000..9404f21 --- /dev/null +++ b/func/minion/modules/djangoctl.py @@ -0,0 +1,128 @@ +# +# Copyright 2008 +# Steve 'Ashcrow' Milner <smilner@xxxxxxxxxx> +# +# 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()} -- 1.5.4.3
_______________________________________________ Func-list mailing list Func-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/func-list