#coding=UTF-8

# See BuildArchetypes for details on environment
# BuildDemos needs git in PATH and depends on gitpython library
# gitpython can be installed with python installer script "pip":
# pip install gitpython	
#
# Deployment dependency: requests
# pip install requests
# Deploy depends on .deployUrl and .deployCredentials files in home folder

import sys, os
from os.path import join, isfile
from fnmatch import fnmatch
from xml.etree.ElementTree import ElementTree

# Validated demos. name -> git url
demos = {
	"dashboard" : "https://github.com/vaadin/dashboard-demo.git",
	"parking" : "https://github.com/vaadin/parking-demo.git",
	"addressbook" : "https://github.com/vaadin/addressbook.git",
	"grid-gwt" : "https://github.com/vaadin/grid-gwt.git"
}

def checkout(folder, url):
	Repo.clone_from(url, join(resultPath, folder))

if __name__ == "__main__":
	# Do imports.	
	try:
		from git import Repo
	except:
		print("BuildDemos depends on gitpython. Install it with `pip install gitpython`")
		sys.exit(1)
	from BuildHelpers import updateRepositories, mavenValidate, copyWarFiles, getLogFile, removeDir, getArgs, mavenInstall, resultPath, readPomFile, parser
	from DeployHelpers import deployWar

	# Add command line argument for staging repos
	parser.add_argument("--repo", type=str, help="Staging repository URL", default=None)

	# Add command line agrument for ignoring failing demos
	parser.add_argument("--ignore", type=str, help="Ignored demos", default="")

	args = getArgs()
	if hasattr(args, "artifactPath") and args.artifactPath is not None:
		version = False
		basePath = args.artifactPath
		poms = []
		for root, dirs, files in os.walk(basePath):
			for name in files:
				if fnmatch(name, "*.pom"):
					poms.append(join(root, name))
		for pom in poms:
			jarFile = pom.replace(".pom", ".jar")
			if isfile(jarFile):
				mavenInstall(pom, jarFile)
			else:
				mavenInstall(pom)
			if "vaadin-server" in pom:
				pomXml, nameSpace = readPomFile(pom)
				for version in pomXml.getroot().findall("./{%s}version" % (nameSpace)):
					args.version = version.text
	demosFailed = False
	ignoredDemos = args.ignore.split(",")
	
	for demo in demos:
		print("Validating demo %s" % (demo))
		try:
			checkout(demo, demos[demo])
			if hasattr(args, "repo") and args.repo is not None:
				updateRepositories(join(resultPath, demo), args.repo)
			mavenValidate(demo, logFile=getLogFile(demo))
			resultWars = copyWarFiles(demo)
			for war in resultWars:
				try:
					deployWar(war)
				except Exception as e:
					print("War %s failed to deploy: %s" % (war, e))
					demosFailed = True
			print("%s demo validation succeeded!" % (demo))
		except Exception as e:
			print("%s demo validation failed: %s" % (demo, e))
			if demo not in ignoredDemos:
				demosFailed = True
		removeDir(demo)
		print("")
	if demosFailed:
		sys.exit(1)

@@ -752,16 +752,106 @@
 gwt.git%22
+,%0A%09%22sampler%22 : %22demos/sampler%22%0A#%09%22my-demo%22 : (%22my_demo_url_or_path%22, %22my-demo-dev-branch%22)
 %0A%7D%0A%0Adef 
@@ -870,16 +870,39 @@
 der, url
+, repoBranch = %22master%22
 ):%0A%09Repo
@@ -942,16 +942,37 @@
  folder)
+, branch = repoBranch
 )%0A%0Aif __
@@ -2346,34 +2346,129 @@
 %0A%09%09%09
-checkout(demo, demos%5Bdemo%5D
+repo = demos%5Bdemo%5D%0A%09%09%09if (isinstance(repo, tuple)):%0A%09%09%09%09checkout(demo, repo%5B0%5D, repo%5B1%5D)%0A%09%09%09else:%0A%09%09%09%09checkout(demo, repo
 )%0A%09%09

Add sampler to BuildDemos, support specifying branch for demo