import os, os.path
import sys
import shutil
import logging
import subprocess
import webbrowser
import getpass
import imp
import base64
import traceback
import socket
import tempfile
import tarfile
import zipfile
import urllib
import boto
from .config import Config
from .utils import *
from .page import Page
from .listener import Listener
from .file import File
from .server import Server, RequestHandler
from .browser import browserReload, browserReloadCSS
class Site(object):
	def __init__(self, path):
		self.path = path
		self.paths = {
			'config': os.path.join(path, 'config.json'),
			'build': os.path.join(path, '.build'),
			'pages': os.path.join(path, 'pages'),
			'templates': os.path.join(path, 'templates'),
			'plugins': os.path.join(path, 'plugins'),
			'static': os.path.join(path, 'static'),
			'script': os.path.join(os.getcwd(), __file__)
		self.config = Config(self.paths['config'])
	def setup(self):
		Configure django to use both our template and pages folder as locations
		to look for included templates.
			from django.conf import settings
				TEMPLATE_DIRS=[self.paths['templates'], self.paths['pages']],
	def verify(self):
		Check if this path looks like a Cactus website
		for p in ['pages', 'static', 'templates', 'plugins']:
			if not os.path.isdir(os.path.join(self.path, p)):'This does not look like a (complete) cactus project (missing "%s" subfolder)', p)
	def bootstrap(self, skeleton=None):
		Bootstrap a new project at a given path. If provided, the skeleton argument will be used as the basis for the new cactus project, in place of the default skeleton. If provided, the argument can be a filesystem path to a directory, a tarfile, a zipfile, or a URL which retrieves a tarfile or a zipfile.
		skeletonArchive = skeletonFile = None
		if skeleton is None:
			from .skeleton import data"Building from data")
			temp = tempfile.NamedTemporaryFile(delete=False, suffix='.tar.gz')
			skeletonArchive =, mode='r')
		elif os.path.isfile(skeleton):
			skeletonFile = skeleton
			# Assume it's a URL
			skeletonFile, headers = urllib.urlretrieve(skeleton)
		if skeletonFile:
			if tarfile.is_tarfile(skeletonFile):
				skeletonArchive =, mode='r')
			elif zipfile.is_zipfile(skeletonFile):
				skeletonArchive = zipfile.ZipFile(skeletonFile)
				logging.error("File %s is an unknown file archive type. At this time, skeleton argument must be a directory, a zipfile, or a tarball." % skeletonFile)
		if skeletonArchive:
			skeletonArchive.close()'New project generated at %s', self.path)
		elif os.path.isdir(skeleton):
			shutil.copytree(skeleton, self.path)'New project generated at %s', self.path)
			logging.error("Cannot process skeleton '%s'. At this time, skeleton argument must be a directory, a zipfile, or a tarball." % skeleton)
	def context(self):
		Base context for the site: all the html pages.
		return {'CACTUS': {'pages': [p for p in self.pages() if p.path.endswith('.html')]}}
	def clean(self):
		Remove all build files.
		if os.path.isdir(self.paths['build']):
	def build(self):
		Generate fresh site from templates.
		# Set up django settings
		# Bust the context cache
		self._contextCache = self.context()
		# Load the plugin code, because we want fresh plugin code on build
		# refreshes if we're running the web server with listen.
		self.loadPlugins()'Plugins: %s', ', '.join([ for p in self._plugins]))
		self.pluginMethod('preBuild', self)
		# Make sure the build path exists
		if not os.path.exists(self.paths['build']):
		# Copy the static files
		# Render the pages to their output files
		# Comment for non threaded building, crashes randomly
		multiMap = map
		multiMap(lambda p:, self.pages())
		self.pluginMethod('postBuild', self)
	def buildStatic(self):
		Copy static files to build folder. If platform supports symlinking
		(Windows doesn't, except for admins) we do that for speed instead.
		staticBuildPath = os.path.join(self.paths['build'], 'static')
    		if not hasattr(self, 'nosymlink') and callable(getattr(os, "symlink", None)):
			# If there is a folder, replace it with a symlink
			if os.path.lexists(staticBuildPath) and not os.path.exists(staticBuildPath):
			if not os.path.lexists(staticBuildPath):
				os.symlink(self.paths['static'], staticBuildPath)
				if os.path.exists(staticBuildPath):
					shutil.rmtree(staticBuildPath) # copytree fails if destination exists
				shutil.copytree(self.paths['static'], staticBuildPath)
			except Exception:'*** Error copying %s to %s' % (self.paths['static'], staticBuildPath))
	def ignorePatterns(self):
		# Page filters
		defaultPatterns = [".*", "*~"]
		configPatterns  = self.config.get("ignore")
		# Add the config values to the default ignore list
		if type(configPatterns) is types.ListType:
			defaultPatterns += configPatterns
		return defaultPatterns
	def pages(self):
		List of pages.
		paths = fileList(self.paths['pages'], relative=True)
		# Filter out the ignored paths
		paths = filterPaths(paths, self.ignorePatterns())
		return [Page(self, p) for p in paths]
	def serve(self, browser=True, port=8000):
		Start a http server and rebuild on changes.
		self.clean()'Running webserver at for %s' % (port, self.paths['build']))'Type control-c to exit')
		def rebuild(changes):'*** Rebuilding (%s changed)' % self.path)
			# We will pause the listener while building so scripts that alter the output
			# like coffeescript and less don't trigger the listener again immediately.
			except Exception, e:'*** Error while building\n%s', e)
			# When we have changes, we want to refresh the browser tabs with the updates.
			# Mostly we just refresh the browser except when there are just css changes,
			# then we reload the css in place.
			if  len(changes["added"]) == 0 and \
				len(changes["deleted"]) == 0 and \
				set(map(lambda x: os.path.splitext(x)[1], changes["changed"])) == set([".css"]):
				browserReloadCSS('' % port)
				browserReload('' % port)
		self.listener = Listener(self.path, rebuild, ignore=lambda x: '/.build/' in x)
			httpd = Server(("", port), RequestHandler)
		except socket.error, e:'Could not start webserver, port is in use. To use another port:')'  cactus serve %s' % (int(port) + 1))
		if browser is True:'' % port)
		except (KeyboardInterrupt, SystemExit):
			httpd.server_close()'See you!')
	def upload(self):
		Upload the site to the server.
		# Make sure we have internet
		if not internetWorking():'There does not seem to be internet here, check your connection')
		logging.debug('Start upload')
		logging.debug('Start preDeploy')
		self.pluginMethod('preDeploy', self)
		logging.debug('End preDeploy')
		# Get access information from the config or the user
		awsAccessKey = self.config.get('aws-access-key') or \
			raw_input('Amazon access key ( ').strip()
		awsSecretKey = getpassword('aws', awsAccessKey) or \
			getpass._raw_input('Amazon secret access key (will be saved in keychain): ').strip()
		# Try to fetch the buckets with the given credentials
		connection = boto.connect_s3(awsAccessKey.strip(), awsSecretKey.strip())
		logging.debug('Start get_all_buckets')
		# Exit if the information was not correct
			buckets = connection.get_all_buckets()
		except:'Invalid login credentials, please try again...')
		logging.debug('end get_all_buckets')
		# If it was correct, save it for the future
		self.config.set('aws-access-key', awsAccessKey)
		setpassword('aws', awsAccessKey, awsSecretKey)
		awsBucketName = self.config.get('aws-bucket-name') or \
			raw_input('S3 bucket name ( ').strip().lower()
		if awsBucketName not in [ for b in buckets]:
			if raw_input('Bucket does not exist, create it? (y/n): ') == 'y':
				logging.debug('Start create_bucket')
					self.bucket = connection.create_bucket(awsBucketName, policy='public-read')
				except boto.exception.S3CreateError, e:'Bucket with name %s already is used by someone else, please try again with another name' % awsBucketName)
				logging.debug('end create_bucket')
				# Configure S3 to use the index.html and error.html files for indexes and 404/500s.
				self.bucket.configure_website('index.html', 'error.html')
				self.config.set('aws-bucket-website', self.bucket.get_website_endpoint())
				self.config.set('aws-bucket-name', awsBucketName)
				self.config.write()'Bucket %s was selected with website endpoint %s' % (self.config.get('aws-bucket-name'), self.config.get('aws-bucket-website')))'You can learn more about s3 (like pointing to your own domain) here:')
			else: return
			# Grab a reference to the existing bucket
			for b in buckets:
				if == awsBucketName:
					self.bucket = b
		self.config.set('aws-bucket-website', self.bucket.get_website_endpoint())
		self.config.set('aws-bucket-name', awsBucketName)
		self.config.write()'Uploading site to bucket %s' % awsBucketName)
		# Upload all files concurrently in a thread pool
		totalFiles = multiMap(lambda p: p.upload(self.bucket), self.files())
		changedFiles = [r for r in totalFiles if r['changed'] == True]
		self.pluginMethod('postDeploy', self)
		# Display done message and some statistics'\nDone\n')'%s total files with a size of %s' % \
			(len(totalFiles), fileSize(sum([r['size'] for r in totalFiles]))))'%s changed files with a size of %s' % \
			(len(changedFiles), fileSize(sum([r['size'] for r in changedFiles]))))'\nhttp://%s\n' % self.config.get('aws-bucket-website'))
	def files(self):
		List of build files.
		paths = fileList(self.paths['build'], relative=True)
		paths = filterPaths(paths, self.ignorePatterns())
		return [File(self, p) for p in paths]
	def loadPlugins(self, force=False):
		Load plugins from the plugins directory and import the code.
		plugins = []
		# Figure out the files that can possibly be plugins
		for pluginPath in fileList(self.paths['plugins']):
			if not pluginPath.endswith('.py'):
			if 'disabled' in pluginPath:
			pluginHandle = os.path.splitext(os.path.basename(pluginPath))[0]
			# Try to load the code from a plugin
				plugin = imp.load_source('plugin_%s' % pluginHandle, pluginPath)
			except Exception, e:'Error: Could not load plugin at path %s\n%s' % (pluginPath, e))
			# Set an id based on the file name = pluginHandle
		# Sort the plugins by their defined order (optional)
		def getOrder(plugin):
			if hasattr(plugin, 'ORDER'):
				return plugin.ORDER
			return -1
		self._plugins = sorted(plugins, key=getOrder)
	def pluginMethod(self, method, *args, **kwargs):
		Run this method on all plugins
		if not hasattr(self, '_plugins'):
		for plugin in self._plugins:
			if hasattr(plugin, method):
				getattr(plugin, method)(*args, **kwargs)