Commit 73baf7a1 authored by Marco Mariani's avatar Marco Mariani

createWrapper()

parent ea9f28cc
...@@ -35,6 +35,8 @@ import urlparse ...@@ -35,6 +35,8 @@ import urlparse
import pkg_resources import pkg_resources
import zc.buildout import zc.buildout
from slapos.recipe.librecipe import shlex
class GenericBaseRecipe(object): class GenericBaseRecipe(object):
"""Boilerplate class for all Buildout recipes providing helpful methods like """Boilerplate class for all Buildout recipes providing helpful methods like
creating configuration file, creating wrappers, generating passwords, etc. creating configuration file, creating wrappers, generating passwords, etc.
...@@ -107,6 +109,25 @@ class GenericBaseRecipe(object): ...@@ -107,6 +109,25 @@ class GenericBaseRecipe(object):
path, arguments=arguments)[0] path, arguments=arguments)[0]
return script return script
def createWrapper(self, name, command, parameters):
"""Creates a very simple (one command) shell script. Takes care of quoting."""
q = shlex.quote
lines = [
'#!/bin/sh',
shlex.quote(command)
]
for param in parameters:
if len(lines[-1]) < 30:
lines[-1] += ' ' + shlex.quote(param)
else:
lines[-1] += ' \\'
lines.append('\t' + shlex.quote(param))
content = '\n'.join(lines) + '\n'
return self.createFile(name, content, 0700)
def createDirectory(self, parent, name, mode=0700): def createDirectory(self, parent, name, mode=0700):
path = os.path.join(parent, name) path = os.path.join(parent, name)
if not os.path.exists(path): if not os.path.exists(path):
......
# -*- coding: utf-8 -*-
"""
backported part of shlex.py from Python 3.3
"""
import re
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', 256).search
def quote(s):
"""Return a shell-escaped version of the string *s*."""
if not s:
return "''"
if _find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment