# -*- coding: utf-8 -*-
# Carlo Pires <carlopires@gmail.com>
# from http://pypi.python.org/pypi/appfy.recipe.gae/0.9.3
# Qua 16 Mar 2011 09:23:36 BRT 

"""
Installs a python executable and several SDK scripts in the buildout
directory: appcfg, bulkload_client, bulkloader, dev_appserver and
remote_api_shell.

It also allows to set default values to start the dev_appserver.

This recipe extends `zc.recipe.egg.Scripts <http://pypi.python.org/pypi/zc.recipe.egg>`_,
so all the options from that recipe are also valid.

Options
~~~~~~~

:sdk-directory: Path to the App Engine SDK directory. It can be an
	absolute path or a reference to the `appfy.recipe.gae:sdk` destination
	option. Default is `${buildout:parts-directory}/google_appengine`.
:appcfg-script: Name of the appcfg script to be installed in the bin
	directory.. Default is `appcfg`.
:bulkload_client-script: Name of the bulkloader script to be installed in
	the bin directory. Default is `bulkload_client`.
:bulkloader-script: Name of the bulkloader script to be installed in
	the bin directory. Default is `bulkloader`.
:dev_appserver-script: Name of the dev_appserver script to be installed in
	the bin directory. Default is `dev_appserver`.
:remote_api_shell-script: Name of the remote_api_shell script to be
	installed in the bin directory. Default is `remote_api_shell`.
:config-file: Configuration file with the default values to use in
	scripts. Default is `gaetools.cfg`.
:extra-paths: Extra paths to include in sys.path for generated scripts.

Example
~~~~~~~

::

	[tools]
	recipe = tipfyrecipes:tools
	sdk-directory = ${gae_sdk:destination}/google_appengine

	# Add these paths to sys.path in the generated scripts.
	extra-paths =
		src/lib
		src/distlib

To set default values to start the dev_appserver, create a section
`dev_appserver` in the defined configuration file (`gaetools.cfg` by
default). For example::

	[dev_appserver]
	# Set default values to start the dev_appserver. All options from the
	# command line are allowed. They are inserted at the beginning of the
	# arguments. Values are used as they are; don't use variables here.
	recipe = tipfyrecipes.gae:tools
	defaults =
		--datastore_path=var/data.store
		--history_path=var/history.store
		--blobstore_path=var/blob.store
		src

Each option should be set in a separate line, as displayed above. Options
provided when calling dev_appserver will override the default values.
"""
import logging
import os

import zc.recipe.egg

from tipfyrecipes import get_relative_path

BASE = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(
	os.path.abspath(os.path.realpath(__file__))))))

class InstallGAETools(zc.recipe.egg.Scripts):
	def __init__(self, buildout, name, opts):
		self.parts_dir = buildout['buildout']['parts-directory']
		self.buildout_dir = buildout['buildout']['directory']

		# Set default values.
		join = os.path.join
		defaults = {
			'sdk-directory': join(self.parts_dir, 'google_appengine'),
			'config-file':   join(self.buildout_dir, 'gaetools.cfg'),
			'interpreter':   'python',
			'extra-paths':   '',
			'eggs':		  '',
		}
		defaults.update(opts)
		opts = defaults

		# Set normalized paths.
		self.config_file = os.path.abspath(opts['config-file'])
		self.sdk_dir = os.path.abspath(opts['sdk-directory'])

		# Set the scripts to be generated.
		scripts = ['appcfg',
				   'bulkload_client',
				   'bulkloader',
				   'dev_appserver',
				   'remote_api_shell']

		self.scripts = [(s, opts.get(s + '-script', s)) for s in scripts]

		# Add the SDK and this recipe package to the path.
		opts['extra-paths'] += '\n%s\n%s' % (BASE, self.sdk_dir)

		# Set a flag to use relative paths.
		self.use_rel_paths = opts.get('relative-paths',
			buildout['buildout'].get('relative-paths', 'false')) == 'true'

		super(InstallGAETools, self).__init__(buildout, name, opts)

	def install(self):
		"""Creates the scripts."""
		entry_points =['%s=tipfyrecipes.gae.scripts:%s' % (scriptname,
			function) for function, scriptname in self.scripts]

		if self.use_rel_paths is not True:
			# base won't be set if we are using absolute paths.
			initialization = ['base = %r' % self.buildout_dir]
		else:
			initialization = []

		initialization.append('gae = %s' % self.get_path(self.sdk_dir))
		initialization.append('cfg = %s' % self.get_path(self.config_file))

		self.options.update({
			'entry-points':   ' '.join(entry_points),
			'initialization': '\n'.join(initialization),
			'arguments':	  'base, gae, cfg',
		})

		return super(InstallGAETools, self).install()

	def get_path(self, path):
		if self.use_rel_paths is True:
			return get_relative_path(path, self.buildout['buildout']['directory'])
		else:
			return '%r' % os.path.abspath(path)

	update = install
