virtual-env.cfg 2.56 KB
Newer Older
1 2 3 4 5 6
[buildout]
extends =
	../pygolang/buildout.cfg

[virtual-env-base]
recipe = slapos.recipe.build
7 8
name = ${:_buildout_section_name_}
default-instance = true
9 10 11
init =
  from zc.buildout.easy_install import working_set
  import os
12
  name = options['name']
13 14 15 16
  eggs = options.get('eggs')
  self.message = options.get('message')
  environment = options.get('environment')
  scripts = options.get('scripts')
17

18
  eggs_template = """
19 20
  [.%(name)s.install-eggs]
  recipe = zc.recipe.egg
21 22
  eggs =
    %(eggs)s
23 24 25 26
  %(scripts)s

  [.%(name)s.install-interpreter]
  <= python-interpreter
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  eggs +=
    %(eggs)s
  """

  instance_template = """
  [.%(name)s.instance]
  recipe = slapos.recipe.template
  output = ${buildout:directory}/instance.cfg
  depends = $%(cookbook)s
  inline =
    [buildout]
    parts = publish

    eggs-directory = ${buildout:eggs-directory}
    develop-eggs-directory = ${buildout:develop-eggs-directory}

    [publish]
    recipe = slapos.cookbook:publish
    activate-script = %(location)s
  """

  if eggs:
    self.buildout.parse(eggs_template % {
      "eggs": "\n  ".join(e.strip() for e in eggs.splitlines()),
      "name": name,
      "scripts": "scripts = " + scripts if scripts else "",
    })
54 55

  if is_true(options.get('default-instance')):
56 57 58 59 60 61 62 63 64 65
    self.buildout.parse(instance_template % {
      "cookbook": "{slapos-cookbook:recipe}",
      "location": location,
      "name": name,
    })

  env = {
    "PATH": self.buildout['buildout']['bin-directory'] + ":\$PATH",
    "PS1": "\"(" + self.name + ") \$PS1\"",
  }
66

67 68 69 70
  if environment:
    for line in environment.splitlines():
      key, value = line.split("=", 1)
      env[key.strip()] = value.strip()
71

72
  self.env = env
73

74
install =
75 76 77 78 79 80
  message = ""
  if self.message:
    message = "\n  echo " + "\n  echo ".join(
      "%r" % line for line in self.message.splitlines())
    message += "\n  echo \'\'"

81 82
  with open(location, "w") as f:
    f.write(options['template'] % {
83 84
      "env": " ".join("%s %s" % (k, v) for k, v in self.env.items()),
      "message": message,
85 86 87 88 89
    })

# Template virtual env for bash shell in posix
[virtual-env-base:posix]
template =
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  if type deactivate > /dev/null 2>&1
  then
    echo "deactivate current environment first"
  else
    deactivate () {
      set %(env)s
      while [ "$1" ]; do
        eval "if [ \"\$_OLD_VENV_$1\" ]; then $1=\$_OLD_VENV_$1; else unset $1; fi; unset \$_OLD_VENV_$1"
        shift
        shift
      done
      unset -f deactivate
    }

    set %(env)s
105
    while [ "$1" ]; do
106 107 108
      eval "_OLD_VENV_$1=\$$1"
      eval "export $1=\"$2\""
      shift
109 110
      shift
    done
111 112
    %(message)s
  fi