2 # -----------------------------------------------------------------------
3 # Copyright (C) 2007-2008 King County Library System
4 # Bill Erickson <erickson@esilibrary.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 3
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
17 import os, sys, getopt, re, constrictor.properties
19 # tell django where to find the settings file
20 os.environ['DJANGO_SETTINGS_MODULE'] = 'constrictor_gui.settings'
22 from django.contrib.auth.models import User
23 from constrictor_gui.control.models import Drone, Script, Plugin, Property
24 from constrictor.utils import load_props
25 import constrictor_gui.settings as settings
26 from constrictor.log import *
27 import xml.dom.minidom
30 ops, args = getopt.getopt(sys.argv[1:], 'fu')
31 options = dict( (k,v) for k,v in ops )
36 ''' Re-synchronizes the Django db from the django models.
37 re-creates the admin user.
38 If there are no drones configured, we create a default drone at 127.0.0.1 '''
41 if not options.has_key('-f'):
42 r = raw_input('This will destroy the existing django database. continue? [yes/no] ')
43 if r.lower() != 'yes':
46 if os.path.exists(settings.DATABASE_NAME):
47 os.remove(settings.DATABASE_NAME)
49 if os.system('python manage.py syncdb') != 0:
50 # tell django to build the base tables
51 sys.stderr.write('Error syncing database...')
54 # create the admin user
55 admin = User.objects.create_user('admin', 'admin@example.org', 'constrictor')
57 admin.is_superuser = True
60 # create a default drone if there are none
61 if len(Drone.objects.all()) == 0:
62 Drone(address='127.0.0.1', port=constrictor.utils.DEFAULT_PORT, enabled=True).save()
65 def getXMLAttr(node, name, ns=None):
66 for (k, v) in node.attributes.items():
73 def loadModuleConfigs():
74 ''' Returns the DOM nodes for the XML configs if a config is found '''
76 props = constrictor.properties.Properties.get_properties()
77 scriptDirs = props.get_property('constrictor.scriptDirs').split(',')
82 confFile = "%s/%s" % (d, constrictor.utils.CONFIG_FILENAME)
83 print "Parsing config file: " + confFile
84 conf = xml.dom.minidom.parse(confFile)
90 def updateDjangoPlugins():
91 configs = loadModuleConfigs()
94 name = getXMLAttr(conf.documentElement, 'plugin')
95 desc = conf.documentElement.getElementsByTagName('desc')[0]
96 desc = desc.childNodes[0].nodeValue
97 print "Registering plugin %s" % name
98 plugin = Plugin(name=name, description=desc)
101 updateDjangoScripts(plugin, conf)
102 updateDjangoProperties(plugin, conf)
104 def updateDjangoScripts(plugin, conf):
106 for scriptNode in conf.getElementsByTagName('script'):
107 # read the scripts from the config and insert them into
108 # the django db if publish is true
110 if getXMLAttr(scriptNode, 'publish')+''.lower() != 'true':
112 name = getXMLAttr(scriptNode, 'name')
114 # see if this script already exists in the database
115 existing = Script.objects.filter(name=name)
116 if len(existing) > 0: continue
118 print 'Registering script "%s" for module "%s"' % (name, plugin.name)
120 #if it does not exist in the db, create it
121 desc = scriptNode.getElementsByTagName('desc')[0]
122 if desc and len(desc.childNodes) > 0:
123 desc = desc.childNodes[0].nodeValue
125 script = Script(plugin_id=plugin.id, name=name, description=desc or '')
128 def updateDjangoProperties(plugin, conf):
130 for propNode in conf.getElementsByTagName('property'):
132 if getXMLAttr(propNode, 'publish')+''.lower() != 'true':
135 name = getXMLAttr(propNode, 'name')
136 existing = Property.objects.filter(name=name)
137 if len(existing) > 0: continue
139 print 'Registering property "%s" for module "%s"' % (name, plugin.name)
141 #if it does not exist in the db, create it
142 desc = propNode.getElementsByTagName('desc')[0]
143 if desc and len(desc.childNodes) > 0:
144 desc = desc.childNodes[0].nodeValue
146 property = Property(plugin_id=plugin.id, name=name, description=desc or '')
152 load_props('constrictor.properties')
153 basedir = os.getcwd()
154 os.chdir('constrictor_gui')
155 os.environ['PYTHONPATH'] = os.path.join(os.path.abspath(''), '..')
156 if not options.has_key('-u'):
159 # force a db connection so changing dirs won't affect the db
160 print "We already have the following scripts: %s" \
161 % str(tuple([s.name for s in Script.objects.all()]))
164 updateDjangoPlugins();