Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added initialization and fixed script.

* Fixed init script which was missing a few options

* Removed schema.xml after initial copy

* Added initialization and environment-vars option
  • Loading branch information...
commit 36a1629f79ff2cfd5445759b0ceec6dc19a4beee 1 parent 15d56f8
@simonedeponti simonedeponti authored
View
6 README.rst
@@ -92,6 +92,12 @@ java-args
Optional. Extra arguments to pass to the Java Virtual Machine (JVM): this
are typically all the parameters you'd pass after the jar file.
+initialization
+ Optional chunk of code that is needed prior to Django bootstrapping.
+
+environment-vars
+ Environment variables needed prior to Django bootstrapping.
+
Development
-----------
View
97 src/abstract/recipe/haystack_solr/__init__.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-import os, shutil
+import os, stat, re, shutil
from tempita import Template
from zc.buildout import UserError
from zc.recipe.egg import Egg
@@ -27,7 +27,8 @@ def __init__(self, buildout, name, options):
options['eggs'] = options.get(
'eggs',
self.buildout['buildout'].get('eggs', '')
- ).strip().split()
+ )
+ self.eggs = options['eggs'].strip().split()
options['django-settings'] = options.get('django-settings', '').strip()
options['django-settings-file'] = options.get(
@@ -43,11 +44,20 @@ def __init__(self, buildout, name, options):
options['java-opts'] = options.get('java-opts', '')
options['java-args'] = options.get('java-args', '')
+ @staticmethod
+ def listify(data):
+ lines = []
+ for raw_line in data.splitlines():
+ line = raw_line.strip()
+ if line != '':
+ lines.append(line)
+ return lines
+
def parse_java_opts(self):
"""Parsed the java opts and arguments from `options`. """
cmd = [
'java',
- '-Dsolr.solr.home=%s' % self.part_dir,
+ '-Dsolr.solr.home=%s' % os.path.join(self.part_dir, 'solr'),
'-Dsolr.solr.dataDir=%s' % self.data_dir
]
if self.options['java-opts']:
@@ -70,10 +80,11 @@ def generate(self, src, dst, **kwargs):
namespace = self.get_namespace()
namespace.update(kwargs)
# pylint: disable-msg=C0321
- with open(src, 'rb') as src_stream, open(dst, 'rb') as dst_stream:
+ with open(src, 'rb') as src_stream, open(dst, 'wb') as dst_stream:
template = Template(src_stream.read())
dst_stream.write(template.substitute(**namespace))
+ # pylint: disable-msg=R0912,R0914
def install(self, update=False):
"""installer
"""
@@ -82,7 +93,7 @@ def install(self, update=False):
if not update:
for path in parts:
- if not os.path.exists(path):
+ if os.path.exists(path):
shutil.rmtree(path)
# Copy the solr files
@@ -101,7 +112,7 @@ def install(self, update=False):
logdir = dirs[1]
- for path in dirs:
+ for path in parts + dirs:
if not os.path.exists(path):
os.makedirs(path)
@@ -114,27 +125,75 @@ def install(self, update=False):
'dir="../..',
'dir="%s' % self.options['solr-location']
)
- with open(solrconfig_path, 'rb') as dst:
+ with open(solrconfig_path, 'wb') as dst:
dst.write(solrconfig)
else:
shutil.copy(self.options['solr-config'], solrconfig_path)
eggs = Egg(self.buildout, self.options['recipe'], self.options)
- requisites = [ 'django-haystack', 'Django' ]
__, working_set = eggs.working_set(
- requisites.extend(self.options['eggs'])
+ self.eggs
)
- self.generate(
- src=os.path.join(TEMPLATE_DIR, 'templates', 'solr.tmpl'),
- dst=os.path.join(self.buildout['buildout']['bin-directory'],
- self.name),
- schema_file=os.path.join(
+ # The initialization code is expressed as a list of lines
+ initialization = []
+
+ # Gets the initialization code: the tricky part here is to preserve
+ # indentation. This is obtained by getting all the lines and then
+ # finding the initial whitespace common to all lines, excluding blank
+ # lines: the obtained whitespace is then subtracted from all lines.
+ raw_code = []
+ residual_whitespace = None
+ whitespace_regex = re.compile(r'^([ ]+)')
+ for line in self.options.get("initialization", "").splitlines():
+ if line != "":
+ m = whitespace_regex.search(line)
+ if m is None:
+ initial_whitespace = 0
+ else:
+ initial_whitespace = len(m.group(1))
+ if residual_whitespace is None or \
+ initial_whitespace < residual_whitespace:
+ residual_whitespace = initial_whitespace
+ raw_code.append(line)
+ for line in raw_code:
+ initialization.append(line[residual_whitespace:])
+
+ # Gets the environment-vars option and generates code to set the
+ # enviroment variables via os.environ
+ environment_vars = []
+ for line in self.listify(self.options.get("environment-vars", "")):
+ try:
+ var_name, raw_value = line.split(" ", 1)
+ except ValueError:
+ raise RuntimeError(
+ "Bad environment-vars contents: %s" % line
+ )
+ environment_vars.append(
+ 'os.environ["%s"] = r"%s"' % (
+ var_name,
+ raw_value.strip()
+ )
+ )
+ if len(environment_vars) > 0:
+ initialization.append("import os")
+ initialization.extend(environment_vars)
+
+ script_file = os.path.join(self.buildout['buildout']['bin-directory'],
+ self.name)
+ schema_file = os.path.join(
self.part_dir,
'solr',
'conf',
'schema.xml'
- ),
+ )
+ if os.path.exists(schema_file):
+ os.remove(schema_file)
+ self.generate(
+ src=os.path.join(TEMPLATE_DIR, 'solr.tmpl'),
+ dst=script_file,
+ schema_file=schema_file,
+ executable=self.buildout['buildout']['executable'],
extrapaths=[dist.location for dist in working_set],
pidfile=os.path.join(self.var_dir, 'solr.pid'),
logfile=os.path.join(logdir, 'solr.log'),
@@ -142,7 +201,13 @@ def install(self, update=False):
basedir=self.part_dir,
djangosettings=self.options['django-settings'],
djangosettings_file=self.options['django-settings-file'],
- startcmd=self.parse_java_opts()
+ startcmd=self.parse_java_opts(),
+ initialization=initialization
+ )
+
+ os.chmod(
+ script_file,
+ stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
)
# returns installed files
View
11 src/abstract/recipe/haystack_solr/templates/solr.tmpl
@@ -1,3 +1,5 @@
+#!{{executable}}
+import sys
sys.path[0:0] = {{extrapaths}}
from subprocess import Popen
import sys, os
@@ -20,7 +22,7 @@ def create_schema():
from django.core.management import setup_environ
try:
imp.acquire_lock()
- mod = imp.load_source('_django_settings', '{{django_settings_file}}')
+ mod = imp.load_source('_django_settings', '{{djangosettings_file}}')
except Exception, e:
imp.release_lock()
sys.stderr.write("Error loading the settings module '%s': %s"
@@ -29,9 +31,12 @@ def create_schema():
imp.release_lock()
# Setup settings
setup_environ(mod, '_django_settings')
-{{endif}}{{if django_settings}}
- os.environ['DJANGO_SETTINGS_MODULE'] = '{{django_settings}}'
+{{endif}}{{if djangosettings}}
+ os.environ['DJANGO_SETTINGS_MODULE'] = '{{djangosettings}}'
{{endif}}
+ {{for line in initialization}}
+ {{line}}
+ {{endfor}}
from django.core.management import ManagementUtility
utility = ManagementUtility([
'manage.py',

0 comments on commit 36a1629

Please sign in to comment.
Something went wrong with that request. Please try again.