]> git.evergreen-ils.org Git - working/random.git/blob - deploy.py
typo repair patch from James Fournie
[working/random.git] / deploy.py
1 #!/usr/bin/python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2007-2008  King County Library System
4 # Bill Erickson <erickson@esilibrary.com>
5
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.
10
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 # -----------------------------------------------------------------------
16
17 import os, sys, getopt, re, constrictor.properties
18
19 # tell django where to find the settings file
20 os.environ['DJANGO_SETTINGS_MODULE'] = 'constrictor_gui.settings'
21
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
28
29
30 ops, args = getopt.getopt(sys.argv[1:], 'fu')
31 options = dict( (k,v) for k,v in ops )
32
33
34 def buildDjangoDB():
35
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 '''
39
40     global options
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':
44             sys.exit(0)
45
46     if os.path.exists(settings.DATABASE_NAME):
47         os.remove(settings.DATABASE_NAME)
48
49     if os.system('python manage.py syncdb') != 0:
50         # tell django to build the base tables
51         sys.stderr.write('Error syncing database...')
52         sys.exit(1)
53
54     # create the admin user
55     admin = User.objects.create_user('admin', 'admin@example.org', 'constrictor')
56     admin.is_staff = True
57     admin.is_superuser = True
58     admin.save()
59
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()
63
64
65 def getXMLAttr(node, name, ns=None):
66     for (k, v) in node.attributes.items():
67         if k == name:
68             return v
69     return None
70
71
72
73 def loadModuleConfigs():
74     ''' Returns the DOM nodes for the XML configs if a config is found '''
75
76     props = constrictor.properties.Properties.get_properties()
77     scriptDirs = props.get_property('constrictor.scriptDirs').split(',')
78     configs = []
79     for d in scriptDirs:
80         conf = None
81         try:
82             confFile = "%s/%s" % (d, constrictor.utils.CONFIG_FILENAME)
83             print "Parsing config file: " + confFile
84             conf = xml.dom.minidom.parse(confFile)
85             configs.append(conf)
86         except:
87             continue
88     return configs
89
90 def updateDjangoPlugins():
91     configs = loadModuleConfigs()
92
93     for conf in configs:
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)
99         plugin.save()
100
101         updateDjangoScripts(plugin, conf)
102         updateDjangoProperties(plugin, conf)
103
104 def updateDjangoScripts(plugin, conf):
105
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
109
110         if getXMLAttr(scriptNode, 'publish')+''.lower() != 'true':
111             continue
112         name = getXMLAttr(scriptNode, 'name')
113
114         # see if this script already exists in the database
115         existing = Script.objects.filter(name=name)
116         if len(existing) > 0: continue
117
118         print 'Registering script "%s" for module "%s"' % (name, plugin.name)
119
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
124             
125         script = Script(plugin_id=plugin.id, name=name, description=desc or '')
126         script.save()
127
128 def updateDjangoProperties(plugin, conf):
129
130     for propNode in conf.getElementsByTagName('property'):
131
132         if getXMLAttr(propNode, 'publish')+''.lower() != 'true':
133             continue
134
135         name = getXMLAttr(propNode, 'name')
136         existing = Property.objects.filter(name=name)
137         if len(existing) > 0: continue
138
139         print 'Registering property "%s" for module "%s"' % (name, plugin.name)
140
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
145             
146         property = Property(plugin_id=plugin.id, name=name, description=desc or '')
147         property.save()
148
149
150
151
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'):
157     buildDjangoDB()
158 else:
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()]))
162
163 os.chdir(basedir)
164 updateDjangoPlugins();
165
166