text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Build and Launch iPhone Application in Simulator or install
# the application on the device via iTunes
#

import os, sys, uuid, subprocess, shutil, signal, string, traceback, imp, filecmp, inspect
import platform, time, re, run, glob, codecs, hashlib, datetime, plistlib
from compiler import Compiler, softlink_for_simulator
from projector import Projector
from xml.dom.minidom import parseString
from xml.etree.ElementTree import ElementTree
from os.path import join, splitext, split, exists
from tools import ensure_dev_path

# the template_dir is the path where this file lives on disk
template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))

# add the parent and the common directory so we can load libraries from those paths too
sys.path.append(os.path.join(template_dir,'../'))
sys.path.append(os.path.join(template_dir,'../common'))
sys.path.append(os.path.join(template_dir, '../module'))
script_ok = False

from tiapp import *
from css import csscompiler
import localecompiler
from module import ModuleDetector
from tools import *

ignoreFiles = ['.gitignore', '.cvsignore']
ignoreDirs = ['.git','.svn', 'CVS']

# need this so unicode works
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

def version_sort(a,b):
	x = float(a[0:3]) # ignore more than 2 places
	y = float(b[0:3]) # ignore more than 2 places
	if x > y:
		return -1
	if x < y:
		return 1
	return 0

# this will return the version of the iOS SDK that we have installed
def check_iphone_sdk(s):
	found = []
	output = run.run(["xcodebuild","-showsdks"],True,False)
	#print output
	if output:
		for line in output.split("\n"):
			if line[0:1] == '\t':
				line = line.strip()
				i = line.find('-sdk')
				if i < 0: continue
				type = line[0:i]
				cmd = line[i+5:]
				if cmd.find("iphoneos")==0:
					ver = cmd[8:]
					found.append(ver)
	# The sanity check doesn't have to be as thorough as prereq.
	if s in found:
		return s
	# Sanity check failed. Let's find something close.
	return sorted(found,version_sort)[0]

def dequote(s):
	if s[0:1] == '"':
		return s[1:-1]
	return s

# force kill the simulator if running
def kill_simulator():
	run.run(['/usr/bin/killall',"ios-sim"],True)
	run.run(['/usr/bin/killall',"iPhone Simulator"],True)

def write_project_property(f,prop,val):
	existing_val = read_project_property(f,prop)
	if existing_val!=val:
		fx = open(f,'w')
		fx.write("%s=%s\n"%(prop,val))
		fx.close()

def read_project_property(f,prop):
	if os.path.exists(f):
		contents = open(f).read()
		for line in contents.splitlines(False):
			(k,v) = line.split("=")
			if k == prop:
				return v
	return None

def read_project_appid(f):
	return read_project_property(f,'TI_APPID')

def read_project_version(f):
	return read_project_property(f,'TI_VERSION')

def infoplist_has_appid(f,appid):
	if os.path.exists(f):
		contents = codecs.open(f,encoding='utf-8').read()
		return contents.find(appid)>0
	return False

def copy_module_resources(source, target, copy_all=False, force=False):
	if not os.path.exists(os.path.expanduser(target)):
		os.makedirs(os.path.expanduser(target))
	for root, dirs, files in os.walk(source):
		for name in ignoreDirs:
			if name in dirs:
				dirs.remove(name)	# don't visit ignored directories
		for file in files:
			if copy_all==False and splitext(file)[-1] in ('.html', '.js', '.css', '.a', '.m', '.c', '.cpp', '.h', '.mm'):
				continue
			if file in ignoreFiles:
				continue
			from_ = os.path.join(root, file)
			to_ = os.path.expanduser(from_.replace(source, target, 1))
			to_directory = os.path.expanduser(split(to_)[0])
			if not exists(to_directory):
				os.makedirs(to_directory)
			# only copy if different filesize or doesn't exist
			if not os.path.exists(to_) or os.path.getsize(from_)!=os.path.getsize(to_) or force:
				if os.path.exists(to_): os.remove(to_)
				shutil.copyfile(from_, to_)

# WARNING: This could be a time bomb waiting to happen, because it mangles
# the app bundle name for NO REASON.  Or... does it?
def make_app_name(s):
	r = re.compile('[0-9a-zA-Z_]')
	buf = ''
	for i in s:
		if i=='-':
			buf+='_'
			continue
		if r.match(i)!=None:
			buf+=i
	# if name starts with number, we simply append a k to it
	if re.match('^[0-9]+',buf):
		buf = 'k%s' % buf
	return buf

def getText(nodelist):
	rc = ""
	for node in nodelist:
		if node.nodeType == node.TEXT_NODE:
			rc+=node.data
		elif node.nodeType == node.ELEMENT_NODE:
			rc+=getText(node.childNodes)
	return rc

def make_map(dict):
	props = {}
	curkey = None

	for i in dict.childNodes:
		if i.nodeType == 1:
			if i.nodeName == 'key':
				curkey = str(getText(i.childNodes)).strip()
			elif i.nodeName == 'dict':
				props[curkey] = make_map(i)
				curkey = None
			elif i.nodeName == 'array':
				s = i.getElementsByTagName('string')
				if len(s):
					txt = ''
					for t in s:
						txt+=getText(t.childNodes)
					props[curkey]=txt
				else:
					props[curkey]=None
				curkey = None
			else:
				if i.childNodes.length > 0:
					props[curkey] = getText(i.childNodes)
				else:
					props[curkey] = i.nodeName
				curkey = None

	return props

def dump_resources_listing(rootdir,out):
	out.write("\nFile listing for %s\n\n" % rootdir)
	total = 0
	for root, subFolders, files in os.walk(rootdir):
		for file in files:
			p = os.path.join(root,file)
			s = os.path.getsize(p)
			total+=s
			s = "[%.0f]" % s
			p = p[len(rootdir)+1:]
			if p.startswith('build/android') or p.startswith('build/mobileweb'): continue
			out.write("  %s %s\n" % (string.ljust(p,120),string.ljust(s,8)))
	out.write("-" * 130)
	out.write("\nTotal files: %.1f MB\n" % ((total/1024)/1024))
	out.write("\n")

def dump_infoplist(infoplist,out):
	plist = codecs.open(infoplist, encoding='utf-8').read()
	out.write("Contents of Info.plist\n\n")
	out.write(plist)
	out.write("\n")
	out.write("=" * 130)
	out.write("\n\n")

def read_provisioning_profile(f,o):
	f = open(f,'rb').read()
	b = f.index('<?xml')
	e = f.index('</plist>')
	xml_content = f[b:e+8]
	o.write("Reading provisioning profile:\n\n%s\n" % xml_content)
	dom = parseString(xml_content)
	dict = dom.getElementsByTagName('dict')[0]
	props = make_map(dict)
	return props

def get_aps_env(provisioning_profile):
	entitlements = provisioning_profile['Entitlements']
	if entitlements.has_key('aps-environment'):
		return entitlements['aps-environment']
	return None

def get_task_allow(provisioning_profile):
	entitlements = provisioning_profile['Entitlements']
	return entitlements['get-task-allow']

def get_app_prefix(provisioning_profile):
	appid_prefix = provisioning_profile['ApplicationIdentifierPrefix']
	return appid_prefix

def get_profile_uuid(provisioning_profile):
	return provisioning_profile['UUID']

def generate_customized_entitlements(provisioning_profile,appid,uuid,command,out):

	get_task_value = get_task_allow(provisioning_profile)
	aps_env = get_aps_env(provisioning_profile)

	buffer = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
"""

	app_prefix = None

	if command=='distribute':
		app_prefix = get_app_prefix(provisioning_profile)
		out.write("Using app_prefix = %s\n\n" % (app_prefix))
		buffer+="""
		<key>application-identifier</key>
		<string>%s.%s</string>
		""" % (app_prefix,appid)

	buffer+="<key>get-task-allow</key>\n		<%s/>" % get_task_value

	if aps_env!=None:
		buffer+="\n<key>aps-environment</key>\n		<string>%s</string>" % aps_env

	if command=='distribute':
		buffer+="""
		<key>keychain-access-groups</key>
		<array>
			<string>%s.%s</string>
		</array>
		""" % (app_prefix,appid)

	buffer+="""
	</dict>
</plist>"""

	return buffer

def xcode_version():
	output = run.run(['xcodebuild','-version'],True,False)
	if output:
		versionLine = output.split('\n')[0]
		return float(versionLine.split(' ')[1].rpartition('.')[0])

def distribute_xc3(uuid, provisioning_profile, name, log):
	# starting in 4.0, apple now requires submission through XCode
	# this code mimics what xcode does on its own to package the
	# application for the app uploader process
	log.write("Creating distribution for xcode3...\n");
	archive_uuid = str(uuid.uuid4()).upper()
	archive_dir = os.path.join(os.path.expanduser("~/Library/MobileDevice/Archived Applications"),archive_uuid)
	archive_app_dir = os.path.join(archive_dir,"%s.app" % name)
	archive_appdsym_dir = os.path.join(archive_dir,"%s.app.dSYM" % name)
	os.makedirs(archive_app_dir)
	os.makedirs(archive_appdsym_dir)

	os.system('ditto "%s.app" "%s"' % (name,archive_app_dir))
	os.system('ditto "%s.app.dSYM" "%s"' % (name,archive_appdsym_dir))

	archive_plist = os.path.join(archive_dir,'ArchiveInfo.plist')
	log.write("Writing archive plist to: %s\n\n" % archive_plist)

	profile_uuid = get_profile_uuid(provisioning_profile)

	os.system("/usr/bin/plutil -convert xml1 -o \"%s\" \"%s\"" % (os.path.join(archive_dir,'Info.xml.plist'),os.path.join(archive_app_dir,'Info.plist')))
	p = plistlib.readPlist(os.path.join(archive_dir,'Info.xml.plist'))
	archive_metadata = {
		'CFBundleIdentifier':p['CFBundleIdentifier'],
		'CFBundleVersion':p['CFBundleVersion'],
		'XCApplicationFilename':'%s.app' %name,
		'XCApplicationName':name,
		'XCArchivedDate': time.time() - 978307200.0,
		'XCArchiveUUID':archive_uuid,
		'XCInfoPlist' : p,
		'XCProfileUUID': profile_uuid
	}
	log.write("%s\n\n" % archive_metadata)
	plistlib.writePlist(archive_metadata,archive_plist)
	os.remove(os.path.join(archive_dir,'Info.xml.plist'))

def distribute_xc4(name, icon, log):
	# Locations of bundle, app binary, dsym info
	log.write("Creating distribution for xcode4...\n");
	timestamp = datetime.datetime.now()
	date = timestamp.date().isoformat()
	time = timestamp.time().strftime('%H-%M-%S')
	archive_name = os.path.join(date,'%s_%s' % (name, time))
	archive_bundle = os.path.join(os.path.expanduser("~/Library/Developer/Xcode/Archives"),"%s.xcarchive" % archive_name)
	archive_app = os.path.join(archive_bundle,"Products","Applications","%s.app" % name)
	archive_dsym = os.path.join(archive_bundle,"dSYM")

	# create directories
	if not os.access(archive_bundle, os.F_OK): os.makedirs(archive_bundle)
	if not os.access(archive_app, os.F_OK): os.makedirs(archive_app)
	if not os.access(archive_dsym, os.F_OK): os.makedirs(archive_dsym)

	# copy app bundles into the approps. places
	os.system('ditto "%s.app" "%s"' % (name,archive_app))
	os.system('ditto "%s.app.dSYM" "%s"' % (name,archive_dsym))

	# plist processing time - this is the biggest difference from XC3.
	archive_info_plist = os.path.join(archive_bundle,'Info.plist')
	log.write("Writing archive plist to: %s\n\n" % archive_info_plist)

	# load existing plist values so that we can use them in generating the archive
	# plist
	os.system('/usr/bin/plutil -convert xml1 -o "%s" "%s"' % (os.path.join(archive_bundle,'Info.xml.plist'),os.path.join(archive_app,'Info.plist')))
	project_info_plist = plistlib.readPlist(os.path.join(archive_bundle,'Info.xml.plist'))
	appbundle = "Applications/%s.app" % name
	# NOTE: We chop off the end '.' of 'CFBundleVersion' to provide the 'short' version
	version = project_info_plist['CFBundleVersion']
	app_version_ = version.split('.')
	if(len(app_version_) > 3):
		version = app_version_[0]+'.'+app_version_[1]+'.'+app_version_[2]
	archive_info = {
		'ApplicationProperties' : {
			'ApplicationPath' : appbundle,
			'CFBundleIdentifier' : project_info_plist['CFBundleIdentifier'],
			'CFBundleShortVersionString' : version,
			'IconPaths' : [os.path.join(appbundle,icon), os.path.join(appbundle,icon)]
		},
		'ArchiveVersion' : float(1),
		'CreationDate' : datetime.datetime.utcnow(),
		'Name' : name,
		'SchemeName' : name
	}

	# write out the archive plist and clean up
	log.write("%s\n\n" % archive_info)
	plistlib.writePlist(archive_info,archive_info_plist)
	os.remove(os.path.join(archive_bundle,'Info.xml.plist'))

	# Workaround for dumb xcode4 bug that doesn't update the organizer unless
	# files are touched in a very specific manner
	temp = os.path.join(os.path.expanduser("~/Library/Developer/Xcode/Archives"),"temp")
	os.rename(archive_bundle,temp)
	os.rename(temp,archive_bundle)

def is_indexing_enabled(tiapp, simulator_dir, **kwargs):
	# darwin versions:
	# - 9.x: Leopard (10.5)
	# - 10.x: Snow Leopard (10.6)
	# - 11.x: Lion (10.7)

	# for testing purposes
	platform_release = kwargs.get("platform_release", platform.release())
	darwin_version = [int(n) for n in platform_release.split(".")]

	enable_mdfind = True
	if tiapp.has_app_property('ti.ios.enablemdfind'):
		enable_mdfind = tiapp.to_bool(tiapp.get_app_property('ti.ios.enablemdfind'))

	# mdfind is specifically disabled, so don't use it
	if not enable_mdfind:
		return False

	# pre-Leopard, mdfind / mdutil don't exist
	if darwin_version[0] < 10:
		return False

	# for testing purposes
	indexer_status = kwargs.get("indexer_status")
	if indexer_status == None:
		indexer_status = run.run(['mdutil', '-a', '-s'], True)

	# An error occurred running mdutil, play it safe
	if indexer_status == None:
		return False

	lines = indexer_status.splitlines()
	mount_point_status = {}
	for i in range(0, len(lines), 2):
		mount_point = lines[i].rstrip(':')
		if len(lines) > (i+1):
			status = lines[i+1].strip('\t.')
			# Only add mount points that the simulator_dir starts with
			if simulator_dir.startswith(mount_point):
				mount_point_status[mount_point] = status
		# mdutil must be disabled if we don't get the right amount of output
		else:
			return False

	if len(mount_point_status) > 0:
		# There may be multiple volumes that have a mount point that the
		# simulator_dir matches, so the one with the longest length
		# *should* be the most specific / correct mount point.
		mount_points = mount_point_status.keys()
		mount_points.sort(lambda a, b: cmp(len(b), len(a)))
		status = mount_point_status[mount_points[0]]

		if 'Indexing enabled' in status:
			return True

	return False

HEADER = """
#import <Foundation/Foundation.h>
"""

DEFAULTS_IMPL_HEADER= """#import "TiUtils.h"
#import "ApplicationDefaults.h"

@implementation ApplicationDefaults

+ (NSMutableDictionary*) copyDefaults
{
    NSMutableDictionary * _property = [[NSMutableDictionary alloc] init];\n
"""

FOOTER ="""
@end
"""

def copy_tiapp_properties(project_dir):
	tiapp = ElementTree()
	src_root = os.path.dirname(sys.argv[0])
	assets_tiappxml = os.path.join(project_dir,'tiapp.xml')
	if not os.path.exists(assets_tiappxml):
		shutil.copy(os.path.join(project_dir, 'tiapp.xml'), assets_tiappxml)
	tiapp.parse(open(assets_tiappxml, 'r'))
	impf = open("ApplicationDefaults.m",'w+')
	appl_default = os.path.join(project_dir,'build','iphone','Classes','ApplicationDefaults.m')
	impf.write(HEADER)
	impf.write(DEFAULTS_IMPL_HEADER)
	for property_el in tiapp.findall("property"):
		name = property_el.get("name")
		type = property_el.get("type")
		value = property_el.text
		if name == None: continue
		if value == None: value = ""
		if type == "string":
			impf.write("""    [_property setObject:[TiUtils stringValue:@"%s"] forKey:@"%s"];\n"""%(value,name))
		elif type == "bool":
			impf.write("""    [_property setObject:[NSNumber numberWithBool:[TiUtils boolValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
		elif type == "int":
			impf.write("""    [_property setObject:[NSNumber numberWithInt:[TiUtils intValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
		elif type == "double":
			impf.write("""    [_property setObject:[NSNumber numberWithDouble:[TiUtils doubleValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
		elif type == None:
			impf.write("""    [_property setObject:[TiUtils stringValue:@"%s"] forKey:@"%s"];\n"""%(value,name))
		else:
			print """[WARN] Cannot set property "%s" , type "%s" not supported""" % (name,type)
	if (len(tiapp.findall("property")) > 0) :
		impf.write("\n    return _property;\n}")
	else:
		impf.write("\n    return NULL;\n}")
	impf.write(FOOTER)
	impf.close()
	if open(appl_default,'r').read() == open('ApplicationDefaults.m','r').read():
		os.remove('ApplicationDefaults.m')
		return False
	else:
		shutil.copyfile('ApplicationDefaults.m',appl_default)
		os.remove('ApplicationDefaults.m')
		return True

def cleanup_app_logfiles(tiapp, log_id, iphone_version):
	print "[DEBUG] finding old log files"
	sys.stdout.flush()
	simulator_dir = os.path.expanduser('~/Library/Application Support/iPhone Simulator/%s' % iphone_version)

	# No need to clean if the directory doesn't exist
	if not os.path.exists(simulator_dir):
		return

	results = None

	# If the indexer is enabled, we can use spotlight for faster searching
	if is_indexing_enabled(tiapp, simulator_dir):
		print "[DEBUG] Searching for old log files with mdfind..."
		sys.stdout.flush()
		results = run.run(['mdfind',
			'-onlyin', simulator_dir,
			'-name', '%s.log' % log_id
		], True)

	# Indexer is disabled, revert to manual crawling
	if results == None:
		print "[DEBUG] Searching for log files without mdfind..."
		sys.stdout.flush()
		def find_all_log_files(folder, fname):
			results = []
			for root, dirs, files in os.walk(os.path.expanduser(folder)):
				for file in files:
					if fname==file:
						fullpath = os.path.join(root, file)
						results.append(fullpath)
			return results
		for f in find_all_log_files(simulator_dir, '%s.log' % log_id):
			print "[DEBUG] removing old log file: %s" % f
			sys.stdout.flush()
			os.remove(f)
	else:
		for i in results.splitlines(False):
			print "[DEBUG] removing old log file: %s" % i
			os.remove(i)

#
# this script is invoked from our tooling but you can run from command line too if
# you know the arguments
#
# the current pattern is <command> [arguments]
#
# where the arguments are dependent on the command being passed
#
def main(args):
	global script_ok
	argc = len(args)
	if argc < 2 or argc==2 and (args[1]=='--help' or args[1]=='-h'):
		print "%s <command> <version> <project_dir> <appid> <name> [options]" % os.path.basename(args[0])
		print
		print "available commands: "
		print
		print "  install       install the app to itunes for testing on iphone"
		print "  simulator     build and run on the iphone simulator"
		print "  adhoc         build for adhoc distribution"
		print "  distribute    build final distribution bundle"
		print "  xcode         build from within xcode"
		print "  run           build and run app from project folder"

		sys.exit(1)

	print "[INFO] One moment, building ..."
	sys.stdout.flush()
	start_time = time.time()
	command = args[1].decode("utf-8")
	ensure_dev_path()

	target = 'Debug'
	deploytype = 'development'
	devicefamily = 'iphone'
	debug = False
	build_only = False
	simulator = False
	xcode_build = False
	force_xcode = False
	simtype = devicefamily

	# when you run from xcode, we'll pass xcode as the command and the
	# xcode script will simply pass some additional args as well as xcode
	# will add some additional useful stuff to the ENVIRONMENT and we pull
	# those values out here
	if command == 'xcode':
		xcode_build = True
		src_root = os.environ['SOURCE_ROOT']
		project_dir = os.path.abspath(os.path.join(src_root,'../','../'))
		name = os.environ['PROJECT_NAME']
		target = os.environ['CONFIGURATION']
		appid = os.environ['TI_APPID']
		arch = os.environ['CURRENT_ARCH']
		sdk_name = os.environ['SDK_NAME']
		iphone_version = sdk_name.replace('iphoneos','').replace('iphonesimulator','')
		# SUPPORTED_DEVICE_FAMILIES 1 or 2 or both
		# TARGETED_DEVICE_FAMILY 1 or 2
		target_device = os.environ['TARGETED_DEVICE_FAMILY']
		if target_device == '1':
			devicefamily = 'iphone'
		elif target_device == '2':
			devicefamily = 'ipad'
		elif target_device == '1,2':
			devicefamily = 'universal'
		if arch == 'i386':
			# simulator always indicates simulator
			deploytype = 'development'
		else:
			# if arch!=i386 indicates a build for device
			if target=='Debug':
				# non-simulator + debug build indicates test on device
				deploytype = 'test'
			else:
				# non-simulator + release build indicates package for distribution
				deploytype = 'production'
		#Ensure the localization files are copied in the application directory
		out_dir = os.path.join(os.environ['TARGET_BUILD_DIR'],os.environ['CONTENTS_FOLDER_PATH'])
		localecompiler.LocaleCompiler(name,project_dir,devicefamily,deploytype,out_dir).compile()
		compiler = Compiler(project_dir,appid,name,deploytype)
		compiler.compileProject(xcode_build,devicefamily,iphone_version)
		script_ok = True
		sys.exit(0)
	else:
		# the run command is when you run from titanium using the run command
		# and it will run the project in the current directory immediately in the simulator
		# from the command line
		if command == 'run':
			if argc < 3:
				print "Usage: %s run <project_dir> [ios_version]" % os.path.basename(args[0])
				sys.exit(1)
			if argc == 3:
				iphone_version = check_iphone_sdk('4.0')
			else:
				iphone_version = dequote(args[3].decode("utf-8"))
			project_dir = os.path.expanduser(dequote(args[2].decode("utf-8")))
			iphonesim = os.path.abspath(os.path.join(template_dir,'ios-sim'))
			iphone_dir = os.path.abspath(os.path.join(project_dir,'build','iphone'))
			tiapp_xml = os.path.join(project_dir,'tiapp.xml')
			ti = TiAppXML(tiapp_xml)
			appid = ti.properties['id']
			name = ti.properties['name']
			command = 'simulator' # switch it so that the rest of the stuff works
		else:
			iphone_version = dequote(args[2].decode("utf-8"))
			iphonesim = os.path.abspath(os.path.join(template_dir,'ios-sim'))
			project_dir = os.path.expanduser(dequote(args[3].decode("utf-8")))
			appid = dequote(args[4].decode("utf-8"))
			name = dequote(args[5].decode("utf-8"))
			tiapp_xml = os.path.join(project_dir,'tiapp.xml')
			ti = TiAppXML(tiapp_xml)

		app_name = make_app_name(name)
		iphone_dir = os.path.abspath(os.path.join(project_dir,'build','iphone'))

		# We need to create the iphone dir if necessary, now that
		# the tiapp.xml allows build target selection
		if not os.path.isdir(iphone_dir):
			if os.path.exists(iphone_dir):
				os.remove(iphone_dir)
			os.makedirs(iphone_dir)

		project_xcconfig = os.path.join(iphone_dir,'project.xcconfig')
		target = 'Release'
		ostype = 'os'
		version_file = None
		log_id = None
		provisioning_profile = None
		debughost = None
		debugport = None
		postbuild_modules = []

		# starting in 1.4, you don't need to actually keep the build/iphone directory
		# if we don't find it, we'll just simply re-generate it
		if not os.path.exists(iphone_dir):
			from iphone import IPhone
			print "[INFO] Detected missing project but that's OK. re-creating it..."
			iphone_creator = IPhone(name,appid)
			iphone_creator.create(iphone_dir,True)
			sys.stdout.flush()

		# we use different arguments dependent on the command
		# pluck those out here
		if command == 'distribute':
			iphone_version = check_iphone_sdk(iphone_version)
			link_version = iphone_version
			dist_keychain = None
			appuuid = dequote(args[6].decode("utf-8"))
			dist_name = dequote(args[7].decode("utf-8"))
			output_dir = os.path.expanduser(dequote(args[8].decode("utf-8")))
			if argc > 9:
				devicefamily = dequote(args[9].decode("utf-8"))
			if argc > 10:
				dist_keychain = dequote(args[10].decode("utf-8"))
			print "[INFO] Switching to production mode for distribution"
			deploytype = 'production'
		elif command in ['simulator', 'build']:
			link_version = check_iphone_sdk(iphone_version)
			deploytype = 'development'
			debug = True
			simulator = command == 'simulator'
			build_only = command == 'build'
			target = 'Debug'
			ostype = 'simulator'
			if argc > 6:
				devicefamily = dequote(args[6].decode("utf-8"))
			if argc > 7:
				simtype = dequote(args[7].decode("utf-8"))
			else:
				# 'universal' helpfully translates into iPhone here... just in case.
				simtype = devicefamily
			if argc > 8:
				# this is host:port from the debugger
				debughost = dequote(args[8].decode("utf-8"))
				if debughost=='':
					debughost = None
					debugport = None
				else:
					debughost,debugport = debughost.split(":")
		elif command in ['install', 'adhoc']:
			iphone_version = check_iphone_sdk(iphone_version)
			link_version = iphone_version
			dist_keychain = None
			appuuid = dequote(args[6].decode("utf-8"))
			dist_name = dequote(args[7].decode("utf-8"))
			if argc > 8:
				devicefamily = dequote(args[8].decode("utf-8"))
			if argc > 9:
				dist_keychain = dequote(args[9].decode("utf-8"))
			if command == 'install':
				target = 'Debug'
				deploytype = 'test'
			elif command == 'adhoc':
				target = 'Release'
				deploytype = 'production'

		# setup up the useful directories we need in the script
		build_out_dir = os.path.abspath(os.path.join(iphone_dir,'build'))
		build_dir = os.path.abspath(os.path.join(build_out_dir,'%s-iphone%s'%(target,ostype)))
		app_dir = os.path.abspath(os.path.join(build_dir,name+'.app'))
		binary = os.path.join(app_dir,name)
		sdk_version = os.path.basename(os.path.abspath(os.path.join(template_dir,'../')))
		iphone_resources_dir = os.path.join(iphone_dir,'Resources')
		version_file = os.path.join(iphone_resources_dir,'.version')
		force_rebuild = read_project_version(project_xcconfig)!=sdk_version or not os.path.exists(version_file)
		infoplist = os.path.join(iphone_dir,'Info.plist')
		githash = None
		custom_fonts = []

		# if we're not running in the simulator we want to clean out the build directory
		if command!='simulator' and os.path.exists(build_out_dir):
			shutil.rmtree(build_out_dir)
		if not os.path.exists(build_out_dir):
			os.makedirs(build_out_dir)
		# write out the build log, useful for debugging
		o = codecs.open(os.path.join(build_out_dir,'build.log'),'w',encoding='utf-8')
		def log(msg):
			print msg
			o.write(msg)
		try:
			buildtime = datetime.datetime.now()
			o.write("%s\n" % ("="*80))
			o.write("Appcelerator Titanium Diagnostics Build Log\n")
			o.write("The contents of this file are useful to send to Appcelerator Support if\n")
			o.write("reporting an issue to help us understand your environment, build settings\n")
			o.write("and aid in debugging. Please attach this log to any issue that you report.\n")
			o.write("%s\n\n" % ("="*80))
			o.write("Starting build at %s\n\n" % buildtime.strftime("%m/%d/%y %H:%M"))

			# write out the build versions info
			versions_txt = read_config(os.path.join(template_dir,'..','version.txt'))
			o.write("Build details:\n\n")
			for key in versions_txt:
				o.write("   %s=%s\n" % (key,versions_txt[key]))
			o.write("\n\n")

			if versions_txt.has_key('githash'):
				githash = versions_txt['githash']

			o.write("Script arguments:\n")
			for arg in args:
				o.write(unicode("   %s\n" % arg, 'utf-8'))
			o.write("\n")
			o.write("Building from: %s\n" % template_dir)
			o.write("Platform: %s\n\n" % platform.version())

			# print out path to debug
			xcode_path=run.run(["/usr/bin/xcode-select","-print-path"],True,False)
			if xcode_path:
				o.write("Xcode path is: %s\n" % xcode_path)
			else:
				o.write("Xcode path undetermined\n")

			# find the module directory relative to the root of the SDK
			titanium_dir = os.path.abspath(os.path.join(template_dir,'..','..','..','..'))
			tp_module_dir = os.path.abspath(os.path.join(titanium_dir,'modules','iphone'))
			force_destroy_build = command!='simulator'

			detector = ModuleDetector(project_dir)
			missing_modules, modules = detector.find_app_modules(ti, 'iphone')
			module_lib_search_path, module_asset_dirs = locate_modules(modules, project_dir, app_dir, log)
			common_js_modules = []

			if len(missing_modules) != 0:
				print '[ERROR] Could not find the following required iOS modules:'
				for module in missing_modules:
					print "[ERROR]\tid: %s\tversion: %s" % (module['id'], module['version'])
				exit(1)

			# search for modules that the project is using
			# and make sure we add them to the compile
			for module in modules:
				if module.js:
					common_js_modules.append(module)
					continue
				module_id = module.manifest.moduleid.lower()
				module_version = module.manifest.version
				module_lib_name = ('lib%s.a' % module_id).lower()
				# check first in the local project
				local_module_lib = os.path.join(project_dir, 'modules', 'iphone', module_lib_name)
				local = False
				if os.path.exists(local_module_lib):
					module_lib_search_path.append([module_lib_name, local_module_lib])
					local = True
					log("[INFO] Detected third-party module: %s" % (local_module_lib))
				else:
					if module.lib is None:
						module_lib_path = module.get_resource(module_lib_name)
						log("[ERROR] Third-party module: %s/%s missing library at %s" % (module_id, module_version, module_lib_path))
						sys.exit(1)
					module_lib_search_path.append([module_lib_name, os.path.abspath(module.lib).rsplit('/',1)[0]])
					log("[INFO] Detected third-party module: %s/%s" % (module_id, module_version))
				force_xcode = True

				if not local:
					# copy module resources
					img_dir = module.get_resource('assets', 'images')
					if os.path.exists(img_dir):
						dest_img_dir = os.path.join(app_dir, 'modules', module_id, 'images')
						if not os.path.exists(dest_img_dir):
							os.makedirs(dest_img_dir)
						module_asset_dirs.append([img_dir, dest_img_dir])

					# copy in any module assets
					module_assets_dir = module.get_resource('assets')
					if os.path.exists(module_assets_dir):
						module_dir = os.path.join(app_dir, 'modules', module_id)
						module_asset_dirs.append([module_assets_dir, module_dir])

			full_version = sdk_version
			if 'version' in versions_txt:
				full_version = versions_txt['version']
				if 'timestamp' in versions_txt or 'githash' in versions_txt:
					full_version += ' ('
					if 'timestamp' in versions_txt:
						full_version += '%s' % versions_txt['timestamp']
					if 'githash' in versions_txt:
						full_version += ' %s' % versions_txt['githash']
					full_version += ')'

			print "[INFO] Titanium SDK version: %s" % full_version
			print "[INFO] iPhone Device family: %s" % devicefamily
			print "[INFO] iPhone SDK version: %s" % iphone_version

			if simulator or build_only:
				print "[INFO] iPhone simulated device: %s" % simtype
				# during simulator we need to copy in standard built-in module files
				# since we might not run the compiler on subsequent launches
				for module_name in ('facebook','ui'):
					img_dir = os.path.join(template_dir,'modules',module_name,'images')
					dest_img_dir = os.path.join(app_dir,'modules',module_name,'images')
					if not os.path.exists(dest_img_dir):
						os.makedirs(dest_img_dir)
					module_asset_dirs.append([img_dir,dest_img_dir])

				# when in simulator since we point to the resources directory, we need
				# to explicitly copy over any files
				ird = os.path.join(project_dir,'Resources','iphone')
				if os.path.exists(ird):
					module_asset_dirs.append([ird,app_dir])

				# We also need to copy over the contents of 'platform/iphone'
				platform_iphone = os.path.join(project_dir,'platform','iphone')
				if os.path.exists(platform_iphone):
					module_asset_dirs.append([platform_iphone,app_dir])

				for ext in ('ttf','otf'):
					for f in glob.glob('%s/*.%s' % (os.path.join(project_dir,'Resources'),ext)):
						custom_fonts.append(f)

			if not (simulator or build_only):
				version = ti.properties['version']
				# we want to make sure in debug mode the version always changes
				version = "%s.%d" % (version,time.time())
				if (deploytype != 'production'):
					ti.properties['version']=version
				pp = os.path.expanduser("~/Library/MobileDevice/Provisioning Profiles/%s.mobileprovision" % appuuid)
				provisioning_profile = read_provisioning_profile(pp,o)

			create_info_plist(ti, template_dir, project_dir, infoplist)

			applogo = None
			clean_build = False

			# check to see if the appid is different (or not specified) - we need to re-generate
			if read_project_appid(project_xcconfig)!=appid or not infoplist_has_appid(infoplist,appid):
				clean_build = True
				force_xcode = True

			new_lib_hash = None
			lib_hash = None
			existing_git_hash = None

			# this code simply tries and detect if we're building a different
			# version of the project (or same version but built from different git hash)
			# and if so, make sure we force rebuild so to propagate any code changes in
			# source code (either upgrade or downgrade)
			if os.path.exists(app_dir):
				if os.path.exists(version_file):
					line = open(version_file).read().strip()
					lines = line.split(",")
					v = lines[0]
					log_id = lines[1]
					if len(lines) > 2:
						lib_hash = lines[2]
						existing_git_hash = lines[3]
					if lib_hash==None:
						force_rebuild = True
					else:
						if template_dir==v and force_rebuild==False:
							force_rebuild = False
						else:
							log_id = None
				else:
					force_rebuild = True

			else:
				force_rebuild = True

			o.write("\ngithash=%s, existing_git_hash=%s\n" %(githash,existing_git_hash))

			if githash!=existing_git_hash:
				force_rebuild = True

			# we want to read the md5 of the libTiCore.a library since it must match
			# the current one we're building and if not, we need to force a rebuild since
			# that means we've copied in a different version of the library and we need
			# to rebuild clean to avoid linking errors
			source_lib=os.path.join(template_dir,'libTiCore.a')
			fd = open(source_lib,'rb')
			m = hashlib.md5()
			m.update(fd.read(1024)) # just read 1K, it's binary
			new_lib_hash = m.hexdigest()
			fd.close()

			if new_lib_hash!=lib_hash:
				force_rebuild=True
				o.write("forcing rebuild since libhash (%s) not matching (%s)\n" % (lib_hash,new_lib_hash))

			lib_hash=new_lib_hash

			# when we force rebuild, we need to re-compile and re-copy source, libs etc
			if force_rebuild:
				o.write("Performing full rebuild\n")
				print "[INFO] Performing full rebuild. This will take a little bit. Hold tight..."
				sys.stdout.flush()
				project = Projector(name,sdk_version,template_dir,project_dir,appid)
				project.create(template_dir,iphone_dir)
				force_xcode = True
				if os.path.exists(app_dir): shutil.rmtree(app_dir)
				# we have to re-copy if we have a custom version
				create_info_plist(ti, template_dir, project_dir, infoplist)
				# since compiler will generate the module dependencies, we need to
				# attempt to compile to get it correct for the first time.
				compiler = Compiler(project_dir,appid,name,deploytype)
				compiler.compileProject(xcode_build,devicefamily,iphone_version,True)
			else:
				if simulator:
					softlink_for_simulator(project_dir,app_dir)
				contents="TI_VERSION=%s\n"% sdk_version
				contents+="TI_SDK_DIR=%s\n" % template_dir.replace(sdk_version,'$(TI_VERSION)')
				contents+="TI_APPID=%s\n" % appid
				contents+="OTHER_LDFLAGS[sdk=iphoneos*]=$(inherited) -weak_framework iAd\n"
				contents+="OTHER_LDFLAGS[sdk=iphonesimulator*]=$(inherited) -weak_framework iAd\n"
				contents+="#include \"module\"\n"
				xcconfig = open(project_xcconfig,'w+')
				xccontents = xcconfig.read()
				if contents!=xccontents:
					o.write("writing contents of %s:\n\n%s\n" % (project_xcconfig,contents))
					o.write("old contents\n\n%s\n" % (xccontents))
					xcconfig.write(contents)
					xcconfig.close()
				else:
					o.write("Skipping writing contents of xcconfig %s\n" % project_xcconfig)

			# write out any modules into the xcode project
			# this must be done after project create above or this will be overriden
			link_modules(module_lib_search_path, name, iphone_dir)

			cwd = os.getcwd()

			# check to see if the symlink exists and that it points to the
			# right version of the library
			libticore = os.path.join(template_dir,'libTiCore.a')
			make_link = True
			symlink = os.path.join(iphone_dir,'lib','libTiCore.a')
			if os.path.islink(symlink):
				path = os.path.realpath(symlink)
				if path.find(sdk_version) > 0:
					make_link = False
			if make_link:
				libdir = os.path.join(iphone_dir,'lib')
				if not os.path.exists(libdir): os.makedirs(libdir)
				os.chdir(libdir)
				# a broken link will not return true on os.path.exists
				# so we need to use brute force
				try:
					os.unlink("libTiCore.a")
				except:
					pass
				try:
					os.symlink(libticore,"libTiCore.a")
				except:
					pass
				os.chdir(cwd)

			# if the lib doesn't exist, force a rebuild since it's a new build
			if not os.path.exists(os.path.join(iphone_dir,'lib','libtiverify.a')):
				shutil.copy(os.path.join(template_dir,'libtiverify.a'),os.path.join(iphone_dir,'lib','libtiverify.a'))

			if not os.path.exists(os.path.join(iphone_dir,'lib','libti_ios_debugger.a')):
				shutil.copy(os.path.join(template_dir,'libti_ios_debugger.a'),os.path.join(iphone_dir,'lib','libti_ios_debugger.a'))

			# compile JSS files
			cssc = csscompiler.CSSCompiler(os.path.join(project_dir,'Resources'),devicefamily,appid)
			app_stylesheet = os.path.join(iphone_dir,'Resources','stylesheet.plist')
			asf = codecs.open(app_stylesheet,'w','utf-8')
			asf.write(cssc.code)
			asf.close()

			# compile debugger file
			debug_plist = os.path.join(iphone_dir,'Resources','debugger.plist')

			# Force an xcodebuild if the debugger.plist has changed
			force_xcode = write_debugger_plist(debughost, debugport, template_dir, debug_plist)

			if command not in ['simulator', 'build']:
				# compile plist into binary format so it's faster to load
				# we can be slow on simulator
				os.system("/usr/bin/plutil -convert binary1 \"%s\"" % app_stylesheet)

			o.write("Generated the following stylecode code:\n\n")
			o.write(cssc.code)
			o.write("\n")

			# generate the Info.plist file with the appropriate device family
			if devicefamily!=None:
				applogo = ti.generate_infoplist(infoplist,appid,devicefamily,project_dir,iphone_version)
			else:
				applogo = ti.generate_infoplist(infoplist,appid,'iphone',project_dir,iphone_version)

			# attempt to load any compiler plugins
			if len(ti.properties['plugins']) > 0:
				local_compiler_dir = os.path.abspath(os.path.join(project_dir,'plugins'))
				tp_compiler_dir = os.path.abspath(os.path.join(titanium_dir,'plugins'))
				if not os.path.exists(tp_compiler_dir) and not os.path.exists(local_compiler_dir):
					o.write("+ Missing plugins directory at %s\n" % tp_compiler_dir)
					print "[ERROR] Build Failed (Missing plugins directory). Please see output for more details"
					sys.stdout.flush()
					sys.exit(1)
				compiler_config = {
					'platform':'ios',
					'devicefamily':devicefamily,
					'simtype':simtype,
					'tiapp':ti,
					'project_dir':project_dir,
					'titanium_dir':titanium_dir,
					'appid':appid,
					'iphone_version':iphone_version,
					'template_dir':template_dir,
					'project_name':name,
					'command':command,
					'deploytype':deploytype,
					'build_dir':build_dir,
					'app_name':app_name,
					'app_dir':app_dir,
					'iphone_dir':iphone_dir
				}
				for plugin in ti.properties['plugins']:
					local_plugin_file = os.path.join(local_compiler_dir,plugin['name'],'plugin.py')
					plugin_file = os.path.join(tp_compiler_dir,plugin['name'],plugin['version'],'plugin.py')
					if not os.path.exists(local_plugin_file) and not os.path.exists(plugin_file):
						o.write("+ Missing plugin at %s (checked %s also)\n" % (plugin_file,local_plugin_file))
						print "[ERROR] Build Failed (Missing plugin for %s). Please see output for more details" % plugin['name']
						sys.stdout.flush()
						sys.exit(1)
					o.write("+ Detected plugin: %s/%s\n" % (plugin['name'],plugin['version']))
					print "[INFO] Detected compiler plugin: %s/%s" % (plugin['name'],plugin['version'])
					code_path = plugin_file
					if os.path.exists(local_plugin_file):
						code_path = local_plugin_file
					o.write("+ Loading compiler plugin at %s\n" % code_path)
					compiler_config['plugin']=plugin
					fin = open(code_path, 'rb')
					m = hashlib.md5()
					m.update(open(code_path,'rb').read())
					code_hash = m.hexdigest()
					p = imp.load_source(code_hash, code_path, fin)
					module_functions = dict(inspect.getmembers(p, inspect.isfunction))
					if module_functions.has_key('postbuild'):
						print "[DBEUG] Plugin has postbuild"
						o.write("+ Plugin has postbuild")
						postbuild_modules.append((plugin['name'], p))
					p.compile(compiler_config)
					fin.close()

			try:
				os.chdir(iphone_dir)

				# we always target backwards to 4.0 even when we use a later
				# version iOS SDK. this ensures our code will run on old devices
				# no matter which SDK we compile with
				deploy_target = "IPHONEOS_DEPLOYMENT_TARGET=4.0"
				device_target = 'TARGETED_DEVICE_FAMILY=1'  # this is non-sensical, but you can't pass empty string

				# clean means we need to nuke the build
				if clean_build or force_destroy_build:
					print "[INFO] Performing clean build"
					o.write("Performing clean build...\n")
					if os.path.exists(app_dir):
						shutil.rmtree(app_dir)

				if not os.path.exists(app_dir): os.makedirs(app_dir)

				# compile localization files
				# Using app_name here will cause the locale to be put in the WRONG bundle!!
				localecompiler.LocaleCompiler(name,project_dir,devicefamily,deploytype).compile()

				# copy any module resources
				if len(module_asset_dirs)>0:
					for e in module_asset_dirs:
						copy_module_resources(e[0],e[1],True)

				# copy CommonJS modules
				for module in common_js_modules:
					#module_id = module.manifest.moduleid.lower()
					#module_dir = os.path.join(app_dir, 'modules', module_id)
					#if os.path.exists(module_dir) is False:
					#	os.makedirs(module_dir)
					shutil.copy(module.js, app_dir)

				# copy artworks, if appropriate
				if command in ['adhoc', 'install', 'distribute']:
					artworks = ['iTunesArtwork', 'iTunesArtwork@2x']
					for artwork in artworks:
						if os.path.exists(os.path.join(project_dir, artwork)):
							shutil.copy(os.path.join(project_dir, artwork), app_dir)

				# copy any custom fonts in (only runs in simulator)
				# since we need to make them live in the bundle in simulator
				if len(custom_fonts)>0:
					for f in custom_fonts:
						font = os.path.basename(f)
						app_font_path = os.path.join(app_dir, font)
						print "[INFO] Detected custom font: %s" % font
						if os.path.exists(app_font_path):
							os.remove(app_font_path)
						try:
							shutil.copy(f,app_dir)
						except shutil.Error, e:
							print "[WARN] Not copying %s: %s" % (font, e)

				# dump out project file info
				if command not in ['simulator', 'build']:
					dump_resources_listing(project_dir,o)
					dump_infoplist(infoplist,o)

				install_logo(ti, applogo, project_dir, template_dir, app_dir)
				install_defaults(project_dir, template_dir, iphone_resources_dir)

				extra_args = None

				recompile = copy_tiapp_properties(project_dir)
				# if the anything changed in the application defaults then we have to force  a xcode build.
				if recompile == True:
					force_xcode = recompile

				if devicefamily!=None:
					# Meet the minimum requirements for ipad when necessary
					if devicefamily == 'ipad' or devicefamily == 'universal':
						device_target="TARGETED_DEVICE_FAMILY=2"
						# NOTE: this is very important to run on device -- i dunno why
						# xcode warns that 3.2 needs only armv7, but if we don't pass in
						# armv6 we get crashes on device
						extra_args = ["VALID_ARCHS=armv6 armv7 i386"]
					# Additionally, if we're universal, change the device family target
					if devicefamily == 'universal':
						device_target="TARGETED_DEVICE_FAMILY=1,2"

				kroll_coverage = ""
				if ti.has_app_property("ti.ios.enablecoverage"):
					enable_coverage = ti.to_bool(ti.get_app_property("ti.ios.enablecoverage"))
					if enable_coverage:
						kroll_coverage = "KROLL_COVERAGE=1"

				def execute_xcode(sdk,extras,print_output=True):

					config = name
					if devicefamily=='ipad':
						config = "%s-iPad" % config
					if devicefamily=='universal':
						config = "%s-universal" % config

					# these are the arguments for running a command line xcode build
					args = ["xcodebuild","-target",config,"-configuration",target,"-sdk",sdk]
					if extras!=None and len(extras)>0:
						args += extras
					args += [deploy_target,device_target]
					if extra_args!=None and len(extra_args)>0:
						args += extra_args

					o.write("Starting Xcode compile with the following arguments:\n\n")
					for arg in args: o.write("    %s\n" % arg)
					o.write("\napp_id = %s\n" % appid)
					o.write("\n\n")
					o.flush()

					if print_output:
						print "[DEBUG] compile checkpoint: %0.2f seconds" % (time.time()-start_time)
						print "[INFO] Executing XCode build..."
						print "[BEGIN_VERBOSE] Executing XCode Compiler  <span>[toggle output]</span>"

					# h/t cbarber for this; occasionally the PCH header info gets out of sync
					# with the PCH file if you do the "wrong thing" and xcode isn't
					# smart enough to pick up these changes (since the PCH file hasn't 'changed').
					run.run(['touch', '%s_Prefix.pch' % ti.properties['name']], debug=False)

					output = run.run(args,False,False,o)

					if print_output:
						print output
						print "[END_VERBOSE]"
						sys.stdout.flush()

					# Output already written by run.run
					#o.write(output)

					# check to make sure the user doesn't have a custom build location
					# configured in Xcode which currently causes issues with titanium
					idx = output.find("TARGET_BUILD_DIR ")
					if idx > 0:
						endidx = output.find("\n",idx)
						if endidx > 0:
							target_build_dir = dequote(output[idx+17:endidx].strip())
							if not os.path.samefile(target_build_dir,build_dir):
								o.write("+ TARGET_BUILD_DIR = %s\n" % target_build_dir)
								print "[ERROR] Your TARGET_BUILD_DIR is incorrectly set. Most likely you have configured in Xcode a customized build location. Titanium does not currently support this configuration."
								print "[ERROR] Expected dir %s, was: %s" % (build_dir,target_build_dir)
								sys.stdout.flush()
								sys.exit(1)

					# look for build error
					if output.find("** BUILD FAILED **")!=-1 or output.find("ld returned 1")!=-1 or output.find("The following build commands failed:")!=-1:
						o.write("+ Detected build failure\n")
						print "[ERROR] Build Failed. Please see output for more details"
						sys.stdout.flush()
						sys.exit(1)

					o.write("+ Looking for application binary at %s\n" % binary)

					# make sure binary exists
					if not os.path.exists(binary):
						o.write("+ Missing application binary at %s\n" % binary)
						print "[ERROR] Build Failed (Missing app at %s). Please see output for more details" % binary
						sys.stdout.flush()
						sys.exit(1)

					# look for a code signing error
					error = re.findall(r'Code Sign error:(.*)',output)
					if len(error) > 0:
						o.write("+ Detected code sign error: %s\n" % error[0])
						print "[ERROR] Code sign error: %s" % error[0].strip()
						sys.stdout.flush()
						sys.exit(1)

				def run_postbuild():
					try:
						if postbuild_modules:
							for p in postbuild_modules:
								o.write("Running postbuild %s" % p[0])
								print "[INFO] Running postbuild %s..." % p[0]
								p[1].postbuild()
					except Exception,e:
						o.write("Error in post-build: %s" % e)
						print "[ERROR] Error in post-build: %s" % e

				# build the final release distribution
				args = []

				if command not in ['simulator', 'build']:
					# allow the project to have its own custom entitlements
					custom_entitlements = os.path.join(project_dir,"Entitlements.plist")
					entitlements_contents = None
					if os.path.exists(custom_entitlements):
						entitlements_contents = open(custom_entitlements).read()
						o.write("Found custom entitlements: %s\n" % custom_entitlements)
					else:
						# attempt to customize it by reading prov profile
						entitlements_contents = generate_customized_entitlements(provisioning_profile,appid,appuuid,command,o)
					o.write("Generated the following entitlements:\n\n%s\n\n" % entitlements_contents)
					f=open(os.path.join(iphone_resources_dir,'Entitlements.plist'),'w+')
					f.write(entitlements_contents)
					f.close()
					args+=["CODE_SIGN_ENTITLEMENTS=Resources/Entitlements.plist"]

				# only build if force rebuild (different version) or
				# the app hasn't yet been built initially
				if ti.properties['guid']!=log_id or force_xcode:
					log_id = ti.properties['guid']
					f = open(version_file,'w+')
					f.write("%s,%s,%s,%s" % (template_dir,log_id,lib_hash,githash))
					f.close()

				# both simulator and build require an xcodebuild
				if command in ['simulator', 'build']:
					debugstr = ''
					if debughost:
						debugstr = 'DEBUGGER_ENABLED=1'

					if force_rebuild or force_xcode or not os.path.exists(binary):
						execute_xcode("iphonesimulator%s" % link_version,["GCC_PREPROCESSOR_DEFINITIONS=__LOG__ID__=%s DEPLOYTYPE=development TI_DEVELOPMENT=1 DEBUG=1 TI_VERSION=%s %s %s" % (log_id,sdk_version,debugstr,kroll_coverage)],False)

					run_postbuild()

					o.write("Finishing build\n")

				if command == 'simulator':
					# first make sure it's not running
					kill_simulator()
					#Give the kill command time to finish
					time.sleep(2)

					# sometimes the simulator doesn't remove old log files
					# in which case we get our logging jacked - we need to remove
					# them before running the simulator

					cleanup_app_logfiles(ti, log_id, iphone_version)

					sim = None

					# this handler will simply catch when the simulator exits
					# so we can exit this script
					def handler(signum, frame):
						global script_ok
						print "[INFO] Simulator is exiting"

						if not log == None:
							try:
								os.system("kill -2 %s" % str(log.pid))
							except:
								pass
						if not sim == None and signum!=3:
							try:
								os.system("kill -3 %s" % str(sim.pid))
							except:
								pass

						kill_simulator()
						script_ok = True
						sys.exit(0)

					# make sure we're going to stop this script whenever
					# the simulator exits
					signal.signal(signal.SIGHUP, handler)
					signal.signal(signal.SIGINT, handler)
					signal.signal(signal.SIGQUIT, handler)
					signal.signal(signal.SIGABRT, handler)
					signal.signal(signal.SIGTERM, handler)

					print "[INFO] Launching application in Simulator"

					sys.stdout.flush()
					sys.stderr.flush()

					# set the DYLD_FRAMEWORK_PATH environment variable for the following Popen iphonesim command
					# this allows the XCode developer folder to be arbitrarily named
					xcodeselectpath = os.popen("/usr/bin/xcode-select -print-path").readline().rstrip('\n')
					iphoneprivateframeworkspath = xcodeselectpath + '/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks'
					os.putenv('DYLD_FRAMEWORK_PATH', iphoneprivateframeworkspath)

					# launch the simulator

					# Awkard arg handling; we need to take 'retina' to be a device type,
					# even though it's really not (it's a combination of device type and configuration).
					# So we translate it into two args:
					if simtype == 'retina':
						# Manually overrule retina type if we're an ipad
						if devicefamily == 'ipad':
							simtype = 'ipad'
						else:
							simtype = 'iphone --retina'
					if devicefamily==None:
						sim = subprocess.Popen("\"%s\" launch \"%s\" --sdk %s" % (iphonesim,app_dir,iphone_version),shell=True,cwd=template_dir)
					else:
						sim = subprocess.Popen("\"%s\" launch \"%s\" --sdk %s --family %s" % (iphonesim,app_dir,iphone_version,simtype),shell=True,cwd=template_dir)

					# activate the simulator window
					command = 'osascript -e "tell application \\\"%s/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app\\\" to activate"'
					os.system(command%xcodeselectpath)

					end_time = time.time()-start_time

					print "[INFO] Launched application in Simulator (%0.2f seconds)" % end_time
					sys.stdout.flush()
					sys.stderr.flush()

					# give the simulator a bit to get started and up and running before
					# starting the logger
					time.sleep(2)

					logger = os.path.realpath(os.path.join(template_dir,'logger.py'))

					# start the logger tail process. this will simply read the output
					# from the logs and stream them back to Titanium Developer on the console
					log = subprocess.Popen([
					  	logger,
						str(log_id)+'.log',
						iphone_version
					])

					# wait (blocking this script) until the simulator exits
					try:
						os.waitpid(sim.pid,0)
					except SystemExit:
						# If the user terminates the app here, it's via a
						# soft kill of some kind (i.e. like what TiDev does)
						# and so we should suppress the usual error message.
						# Fixes #2086
						pass

					print "[INFO] Application has exited from Simulator"

					# in this case, the user has exited the simulator itself
					# and not clicked Stop Emulator from within Developer so we kill
					# our tail log process but let simulator keep running
					if not log == None:
						try:
							os.system("kill -2 %s" % str(log.pid))
						except:
							pass

					script_ok = True

				###########################################################################
				# END OF SIMULATOR COMMAND
				###########################################################################

				#
				# this command is run for installing an app on device or packaging for adhoc distribution
				#
				elif command in ['install', 'adhoc']:

					debugstr = ''
					if debughost:
						debugstr = 'DEBUGGER_ENABLED=1'

					args += [
						"GCC_PREPROCESSOR_DEFINITIONS=DEPLOYTYPE=test TI_TEST=1 %s %s" % (debugstr, kroll_coverage),
						"PROVISIONING_PROFILE=%s" % appuuid
					]

					if command == 'install':
						args += ["CODE_SIGN_IDENTITY=iPhone Developer: %s" % dist_name]
					elif command == 'adhoc':
						args += ["CODE_SIGN_IDENTITY=iPhone Distribution: %s" % dist_name]

					if dist_keychain is not None:
						args += ["OTHER_CODE_SIGN_FLAGS=--keychain %s" % dist_keychain]

					args += ["DEPLOYMENT_POSTPROCESSING=YES"]

					execute_xcode("iphoneos%s" % iphone_version,args,False)

					if command == 'install':
						print "[INFO] Installing application in iTunes ... one moment"
						sys.stdout.flush()

					dev_path = run.run(['xcode-select','-print-path'],True,False).rstrip()
					package_path = os.path.join(dev_path,'Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication')

					if os.path.exists(package_path):
						o.write("+ Preparing to run %s\n"%package_path)
						output = run.run([package_path,app_dir],True)
						o.write("+ Finished running %s\n"%package_path)
						if output: o.write(output)

					# for install, launch itunes with the app
					ipa = os.path.join(os.path.dirname(app_dir),"%s.ipa" % name)
					o.write("+ IPA file should be at %s\n" % ipa);

					# it appears that sometimes this command above fails on certain installs
					# or is missing. let's just open if we have it otherwise, open the app
					# directory
					if not os.path.exists(ipa):
						# just open the app dir itself
						o.write("+ IPA didn't exist at %s\n" % ipa)
						o.write("+ Will try and open %s\n" % app_dir)
						ipa = app_dir

					if command == 'install':
						# to force iTunes to install our app, we simply open the IPA
						# file in itunes
						cmd = "open -b com.apple.itunes \"%s\"" % ipa
						o.write("+ Executing the command: %s\n" % cmd)
						os.system(cmd)
						o.write("+ After executing the command: %s\n" % cmd)

						# now run our applescript to tell itunes to sync to get
						# the application on the phone
						ass = os.path.join(template_dir,'itunes_sync.scpt')
						cmd = "osascript \"%s\"" % ass
						o.write("+ Executing the command: %s\n" % cmd)
						os.system(cmd)
						o.write("+ After executing the command: %s\n" % cmd)

						print "[INFO] iTunes sync initiated"

						o.write("Finishing build\n")

					sys.stdout.flush()
					script_ok = True

					run_postbuild()

				###########################################################################
				# END OF INSTALL/ADHOC COMMAND
				###########################################################################

				#
				# this command is run for packaging an app for distribution
				#
				elif command == 'distribute':

					deploytype = "production"

					args += [
						"GCC_PREPROCESSOR_DEFINITIONS=DEPLOYTYPE=%s TI_PRODUCTION=1" % deploytype,
						"PROVISIONING_PROFILE=%s" % appuuid,
						"CODE_SIGN_IDENTITY=iPhone Distribution: %s" % dist_name,
						"DEPLOYMENT_POSTPROCESSING=YES"
					]

					if dist_keychain is not None:
						args += ["OTHER_CODE_SIGN_FLAGS=--keychain %s" % dist_keychain]

					execute_xcode("iphoneos%s" % iphone_version,args,False)

					# switch to app_bundle for zip
					os.chdir(build_dir)
					if xcode_version() >= 4.0:
						distribute_xc4(name, applogo, o)
					else:
						distribute_xc3(uuid, provisioning_profile, name, o)

					# open xcode + organizer after packaging
					# Have to force the right xcode open...
					xc_path = run.run(['xcode-select','-print-path'],True,False).rstrip()
					xc_app_index = xc_path.find('/Xcode.app/')
					if (xc_app_index >= 0):
						xc_path = xc_path[0:xc_app_index+10]
					else:
						xc_path = os.path.join(xc_path,'Applications','Xcode.app')
					o.write("Launching xcode: %s\n" % xc_path)
					os.system('open -a %s' % xc_path)

					ass = os.path.join(template_dir,'xcode_organizer.scpt')
					cmd = "osascript \"%s\"" % ass
					os.system(cmd)

					o.write("Finishing build\n")
					script_ok = True

					run_postbuild()

				###########################################################################
				# END OF DISTRIBUTE COMMAND
				###########################################################################

			finally:
				os.chdir(cwd)
		except:
			print "[ERROR] Error: %s" % traceback.format_exc()
			if not script_ok:
				o.write("\nException detected in script:\n")
				traceback.print_exc(file=o)
				o.close()
				sys.exit(1)
			else:
				o.close()

if __name__ == "__main__":
	main(sys.argv)
	sys.exit(0)


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:id="@+id/data_passway"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_color"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="@color/btn_color"
        android:textSize="@dimen/size_little"></TextView>
</LinearLayout>

redpajama_set_name: RedPajamaGithub


text:
package com.slothnull.android.medox.broadcastreceiver;

/**
 * Created by Mahmoud Shaheen
 * Project: Medox
 * Licence: MIT
 */

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.util.Log;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.slothnull.android.medox.service.IndicatorsService;
import com.slothnull.android.medox.service.LocationService;
import com.slothnull.android.medox.service.ShakeService;

public class BootBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BootBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Broadcast received");
        FirebaseUser auth = FirebaseAuth.getInstance().getCurrentUser();
        SharedPreferences sharedPreferences =
                context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        if (auth != null){
            final String appType = sharedPreferences.getString("appType", "");
            Log.i(TAG, appType);
            if (appType.equals("senior")) {
                enableServices(context);
                triggerServices(context);
            } else { //user not signed or Care type or undefined app type
                return;
            }
        }
    }
    private void enableServices(Context context){
        PackageManager pm = context.getPackageManager();
        Log.i(TAG, "enable");

        try{
            pm.setComponentEnabledSetting(
                    new ComponentName(context, ShakeService.class),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

        }catch(Exception e){
            Log.e(TAG, "error enabling Shake Service");
        }
        try{
            pm.setComponentEnabledSetting(
                    new ComponentName(context, LocationService.class),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

        }catch(Exception e){
            Log.e(TAG, "error enabling Location Service");
        }
        try{
            pm.setComponentEnabledSetting(
                    new ComponentName(context, IndicatorsService.class),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

        }catch(Exception e){
            Log.e(TAG, "error enabling Indicators Service");
        }
    }
    public void triggerServices(Context context){
        Log.i(TAG, "trigger");

        try{
            Intent shake = new Intent(context, ShakeService.class);
            context.startService(shake);
        }catch(Exception e){
            Log.e(TAG, "error Triggering Shake Service");
        }
        try{
            Intent location = new Intent(context, LocationService.class);
            context.startService(location);
        }catch(Exception e){
            Log.e(TAG, "error Triggering Location Service");
        }
        try{
            Intent indicators = new Intent(context, IndicatorsService.class);
            context.startService(indicators);
        }catch(Exception e){
            Log.e(TAG, "error Triggering Indicators Service");
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright (C) 2017 Muhammad Tayyab Akram

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="84dp"
              android:background="@android:color/black"
              android:orientation="vertical"
              android:padding="1dp">

    <TextView
        android:id="@+id/text_view_glyph_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:gravity="center"
        android:lines="1"
        android:text="0001"
        android:textColor="@android:color/black"
        android:textSize="14sp"/>

    <ImageView
        android:id="@+id/image_view_glyph_shape"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"/>
</LinearLayout>


text:
set -x
set -e

# Install wkhtmltopdf dependencies
apt-get update
apt-get install -y --no-install-recommends xvfb  libxrender1 libfontconfig1 libxext6 libssl1.0
#FIXME:  wkhtmltopdf depends on legacy libssl so we are removing new one
apt-get remove -y libssl1.1

cd /tmp
wget -O wk.txz https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar xf wk.txz
cp -r wkhtmltox/* /usr/

# Copy runtime files
cp -frv /build/files/* / || true

# Clean up APT when done.
source /usr/local/build_scripts/cleanup_apt.sh


text:
public class SomeClass {
}


text:
class Competitor:
    def __init__(self, name, school, place, elo):
        self.name = name
        self.school = school
        self.place = place
        self.elo = elo

    def __eq__(self, other):
        return self.name == other.name and \
               self.school == other.school

    def __hash__(self):
        return hash((self.name, self.school))

# Used as a class for example.py, and is not relevant to the core algorithm

class Athlete:
    def __init__(self, name, school):
        self.name = name
        self.school = school

    def __eq__(self, other):
        return self.name == other.name and \
               self.school == other.school

    def __hash__(self):
        return hash((self.name, self.school))

    def __repr__(self):
        return self.name

class Meet:
    competitors = []

    def addCompetitor(self, name, place, elo, school):
        player = Competitor(name, school, place, elo)
        self.competitors.append(player)


text:
package org.bedework.util.security.keys;

import org.bedework.util.config.ConfInfo;
import org.bedework.util.config.ConfigBase;

/** Information for outbound http connections
 *
 * @author Mike Douglass
 */
@ConfInfo(elementName = "genkeys",
          type = "edu.rpi.cmt.security.keys.GenKeysConfig")
public class GenKeysConfigImpl
        extends ConfigBase<GenKeysConfigImpl>
        implements GenKeysConfig {
  private String privKeyFileName;
  private String publicKeyFileName;

  @Override
  public void setPrivKeyFileName(final String val) {
    privKeyFileName = val;
  }

  @Override
  public String getPrivKeyFileName() {
    return privKeyFileName;
  }

  @Override
  public void setPublicKeyFileName(final String val) {
    publicKeyFileName = val;
  }

  @Override
  public String getPublicKeyFileName() {
    return publicKeyFileName;
  }
}


text:
package request

import (
	"fmt"
	"time"

	"github.com/codeforsanjose/open311-gateway/adapters/citysourced/logs"
	"github.com/codeforsanjose/open311-gateway/adapters/citysourced/telemetry"
)

var (
	log = logs.Log
)

// Report is the RPC container struct for the Report.Create service.  This service creates
// a new 311 report.
type Report struct{}

// processer is the interface used to run all the common request processing steps (see runRequest()).
type processer interface {
	convertRequest() error
	process() error
	convertResponse() (int, error)
	fail(err error) error
	getIDS() string
	getRoute() string
	String() string
}

// runRequest runs all of the common request processing operations.
func runRequest(r processer) error {
	id := r.getIDS()
	telemetry.SendRPC(id, "open", r.getRoute(), "", 0, time.Now())

	if err := r.convertRequest(); err != nil {
		telemetry.SendRPC(id, "error", r.getRoute(), "", 0, time.Now())
		return r.fail(err)
	}
	if err := r.process(); err != nil {
		telemetry.SendRPC(id, "error", r.getRoute(), "", 0, time.Now())
		return r.fail(err)
	}
	resultCount, err := r.convertResponse()
	if err != nil {
		telemetry.SendRPC(id, "error", r.getRoute(), "", 0, time.Now())
		return r.fail(err)
	}
	// telemetry.SendRPC(r.getID(), data.AdapterName(), "", "", "adp-send")
	telemetry.SendRPC(id, "done", r.getRoute(), "", resultCount, time.Now())
	log.Debugf("Request COMPLETED:%s\n", r.String())
	return nil
}

// Init should be called at startup to initialize the request package.
func Init(keyfile string) error {
	stdHeader.Load(&stdHeader, keyfile)
	fmt.Printf("After initializing the Header:\n%s\n", stdHeader.String())
	return nil
}


text:
package ma.glasnost.orika.test.boundmapperfacade;

import java.util.Arrays;

import ma.glasnost.orika.BoundMapperFacade;
import ma.glasnost.orika.test.MappingUtil;
import ma.glasnost.orika.test.array.ArrayTestCaseClasses;
import ma.glasnost.orika.test.array.ArrayTestCaseClasses.A;
import ma.glasnost.orika.test.array.ArrayTestCaseClasses.B;
import ma.glasnost.orika.test.array.ArrayTestCaseClasses.C;
import ma.glasnost.orika.test.array.ArrayTestCaseClasses.D;

import org.junit.Assert;
import org.junit.Test;

public class ArrayTestCase {

    @Test
    public void testSimplePrimitiveArray() {
        ArrayTestCaseClasses.A source =  new ArrayTestCaseClasses.A();
        byte[] buffer = new byte[]{1,2,3,4};
        source.setBuffer(buffer);

        BoundMapperFacade<A,B> mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(ArrayTestCaseClasses.A.class, ArrayTestCaseClasses.B.class);

        ArrayTestCaseClasses.B destination = mapperFacade.map(source);

        Assert.assertArrayEquals(source.getBuffer(), destination.getBuffer());

    }

    @Test
    public void testSimplePrimitiveToWrapperArray() {
        ArrayTestCaseClasses.A source =  new ArrayTestCaseClasses.A();
        byte[] buffer = new byte[]{1,2,3,4};
        source.setBuffer(buffer);

        BoundMapperFacade<A,C> mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(A.class, C.class);

        ArrayTestCaseClasses.C destination = mapperFacade.map(source);

        Assert.assertArrayEquals(new Byte[]{1,2,3,4}, destination.getBuffer());
    }

    @Test
    public void testArrayToList() {
        BoundMapperFacade<A, D> mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(A.class, D.class);

    	ArrayTestCaseClasses.A source =  new ArrayTestCaseClasses.A();
        byte[] buffer = new byte[]{1,2,3,4};
        source.setBuffer(buffer);

        D destination = mapperFacade.map(source);

        Assert.assertEquals(Arrays.asList((byte)1,(byte)2,(byte)3,(byte)4), destination.getBuffer());

    }

    @Test
    public void testListToArray() {
        BoundMapperFacade<A,D> mapperFacade = MappingUtil.getMapperFactory().getMapperFacade(A.class, D.class);

    	D source =  new D();
        source.setBuffer(Arrays.asList((byte)1,(byte)2,(byte)3,(byte)4));

        A destination = mapperFacade.mapReverse(source);

        Assert.assertArrayEquals(new byte[] {(byte)1,(byte)2,(byte)3,(byte)4}, destination.getBuffer());

    }

    @Test
    public void testMappingArrayOfString() {

        Product p = new Product();
        p.setTags(new String[] { "music", "sport" });

        ProductDTO productDTO = MappingUtil.getMapperFactory().getMapperFacade(Product.class, ProductDTO.class).map(p);

        Assert.assertArrayEquals(p.getTags(), productDTO.getTags());
    }

    public static class Product {

        private String[] tags;

        public String[] getTags() {
            return tags;
        }

        public void setTags(String[] tags) {
            this.tags = tags;
        }

    }

    public static class ProductDTO {

        private String[] tags;

        public String[] getTags() {
            return tags;
        }

        public void setTags(String[] tags) {
            this.tags = tags;
        }

    }
}


text:
By default Infinitude will store thermostat state files in this directory in JSON and XML formats.

To change the directory used, specify the $config->{store_base} key in the infinitude script.
Any get(key) set(key,value) store should work with Infinitude. If you would prefer to use a
different persistence mechanism, assign the $store object to one of your choosing.


text:
package com.cloud.agent.api;

public class DeleteLogicalSwitchAnswer extends Answer {

    public DeleteLogicalSwitchAnswer(final Command command, final boolean success, final String details) {
        super(command, success, details);
    }

    public DeleteLogicalSwitchAnswer(final Command command, final Exception e) {
        super(command, e);
    }
}


text:
<!doctype html>

  <title>GDG Twin Cities Home</title>

  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
  <meta name="mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">

  <link rel="import" href="components/paper-checkbox/paper-checkbox.html">
  <link rel="import" href="components/font-roboto/roboto.html">
  <link rel="import" href="custom_components/organizer-list.html">
  <link rel="import" href="custom_components/organizer-card.html">
  <link rel="import" href="footer.html">

GDG Twin Cities

Our big event that is coming on February 6th, 2016, is DevFestMN.  We'd love to see you there.  Find out more are devfest.mn

Our monthly events are managed on Meetup.com sign up!

Organizers

  <organizer-list show="all"></organizer-list>

Organizer Emeritus

       <organizer-card gplusid="StephenFluin">

Stephen Fluin

Fantsy McPantsy Android Developer Advocate at The Googles.

        </organizer-card>

       <organizer-card gplusid="BradArmstrong">

Brad Armstrong

Android Team manager at Code42

        </organizer-card>

  <page-footer></page-footer>


text:
namespace Machete.HL7Schema.V26
{
    using HL7;

    /// <summary>
    /// LCC (Segment) - Location Charge Code
    /// </summary>
    public interface LCC :
        HL7V26Segment
    {
        /// <summary>
        /// LCC-1: Primary Key Value - LCC
        /// </summary>
        Value<PL> PrimaryKeyValue { get; }

        /// <summary>
        /// LCC-2: Location Department
        /// </summary>
        Value<CWE> LocationDepartment { get; }

        /// <summary>
        /// LCC-3: Accommodation Type
        /// </summary>
        ValueList<CWE> AccommodationType { get; }

        /// <summary>
        /// LCC-4: Charge Code
        /// </summary>
        ValueList<CWE> ChargeCode { get; }
    }
}


text:
export default class LoadOptions {
	public addToHistory?: boolean
	public forceReload?: boolean
}


text:
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900' rel='stylesheet' type='text/css'>
    <meta name="viewport" content="initial-scale=1" />
    <title ng-bind="pageTitle"></title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">
    <base href="/">
    <title ng-bind="pageTitle"></title>

    <md-toolbar layout="row" class="md-default-theme md-whiteframe-z1" hide-gt-sm>

        <md-button ng-click="toggleSidenav('left')" hide-gt-sm class="md-icon-button">
          <md-icon aria-label="Menu" md-svg-icon="https://s3-us-west-2.amazonaws.com/s.cdpn.io/68133/menu.svg"></md-icon>
        </md-button>

Map App

    </md-toolbar>

            <ui-view></ui-view>


text:
package org.mifos.application.servicefacade;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.mifos.accounts.financial.business.GLCodeEntity;
import org.mifos.accounts.financial.business.service.GeneralLedgerDao;
import org.mifos.accounts.productdefinition.business.InterestCalcTypeEntity;
import org.mifos.accounts.productdefinition.business.PrdApplicableMasterEntity;
import org.mifos.accounts.productdefinition.business.PrdStatusEntity;
import org.mifos.accounts.productdefinition.business.ProductCategoryBO;
import org.mifos.accounts.productdefinition.business.RecommendedAmntUnitEntity;
import org.mifos.accounts.productdefinition.business.SavingsOfferingBO;
import org.mifos.accounts.productdefinition.business.SavingsTypeEntity;
import org.mifos.accounts.productdefinition.persistence.LoanProductDao;
import org.mifos.accounts.productdefinition.persistence.PrdOfferingPersistence;
import org.mifos.accounts.productdefinition.persistence.SavingsProductDao;
import org.mifos.accounts.productdefinition.util.helpers.ApplicableTo;
import org.mifos.accounts.productdefinition.util.helpers.InterestCalcType;
import org.mifos.accounts.productdefinition.util.helpers.PrdStatus;
import org.mifos.accounts.productdefinition.util.helpers.ProductDefinitionConstants;
import org.mifos.accounts.productdefinition.util.helpers.RecommendedAmountUnit;
import org.mifos.accounts.productdefinition.util.helpers.SavingsType;
import org.mifos.application.master.business.MifosCurrency;
import org.mifos.application.meeting.business.MeetingBO;
import org.mifos.application.meeting.exceptions.MeetingException;
import org.mifos.application.meeting.util.helpers.MeetingType;
import org.mifos.application.meeting.util.helpers.RecurrenceType;
import org.mifos.core.MifosRuntimeException;
import org.mifos.dto.domain.ProductDetailsDto;
import org.mifos.dto.domain.SavingsProductDto;
import org.mifos.framework.exceptions.PersistenceException;
import org.mifos.framework.util.helpers.Money;
import org.mifos.security.MifosUser;

public class SavingsProductAssembler {

    private final LoanProductDao loanProductDao;
    private final GeneralLedgerDao generalLedgerDao;
    private final SavingsProductDao savingsProductDao;

    public SavingsProductAssembler(LoanProductDao loanProductDao, SavingsProductDao savingsProductDao, GeneralLedgerDao generalLedgerDao) {
        this.loanProductDao = loanProductDao;
        this.savingsProductDao = savingsProductDao;
        this.generalLedgerDao = generalLedgerDao;
    }

    public SavingsOfferingBO fromDto(MifosUser user, SavingsProductDto savingsProductRequest) {

        try {
            // FIXME - keithw - this is general assembler common to both savings and loans i.e. all products. so
            // ProductDao and ProductAssembler
            ProductDetailsDto productDetails = savingsProductRequest.getProductDetails();
            String name = productDetails.getName();
            String shortName = productDetails.getShortName();
            String description = productDetails.getDescription();
            Integer category = productDetails.getCategory();

            ProductCategoryBO productCategory = this.loanProductDao.findActiveProductCategoryById(category);
            DateTime startDate = productDetails.getStartDate();
            DateTime endDate = productDetails.getEndDate();
            ApplicableTo applicableTo = ApplicableTo.fromInt(productDetails.getApplicableFor());
            PrdApplicableMasterEntity applicableToEntity = this.loanProductDao.findApplicableProductType(applicableTo);

            PrdStatusEntity activeStatus = new PrdOfferingPersistence().getPrdStatus(PrdStatus.SAVINGS_ACTIVE);
            PrdStatusEntity inActiveStatus = new PrdOfferingPersistence().getPrdStatus(PrdStatus.SAVINGS_INACTIVE);

            PrdStatusEntity selectedStatus = activeStatus;
            if (productDetails.getStatus() != null && inActiveStatus.getOfferingStatusId().equals(productDetails.getStatus().shortValue())) {
                selectedStatus = inActiveStatus;
            }

            String globalNum = generateProductGlobalNum(user);

            // savings specific
            SavingsType savingsType = SavingsType.fromInt(savingsProductRequest.getDepositType());
            SavingsTypeEntity savingsTypeEntity = this.loanProductDao.retrieveSavingsType(savingsType);

            RecommendedAmntUnitEntity recommendedAmntUnitEntity = null;
            if (savingsProductRequest.getGroupMandatorySavingsType() != null) {
                RecommendedAmountUnit recommendedAmountType = RecommendedAmountUnit.fromInt(savingsProductRequest.getGroupMandatorySavingsType());
                recommendedAmntUnitEntity = this.loanProductDao.retrieveRecommendedAmountType(recommendedAmountType);
            }

            Money amountForDeposit = new Money(Money.getDefaultCurrency(), BigDecimal.valueOf(savingsProductRequest.getAmountForDeposit()));
            Money maxWithdrawal = new Money(Money.getDefaultCurrency(), BigDecimal.valueOf(savingsProductRequest.getMaxWithdrawal()));

            // interest specific
            BigDecimal interestRate = savingsProductRequest.getInterestRate();

            InterestCalcType interestCalcType = InterestCalcType.fromInt(savingsProductRequest.getInterestCalculationType());
            InterestCalcTypeEntity interestCalcTypeEntity = this.savingsProductDao.retrieveInterestCalcType(interestCalcType);

            RecurrenceType recurrence = RecurrenceType.fromInt(savingsProductRequest.getInterestCalculationFrequencyPeriod().shortValue());
            Integer every = savingsProductRequest.getInterestCalculationFrequency();
            MeetingBO interestCalculationMeeting = new MeetingBO(recurrence, every.shortValue(), new Date(), MeetingType.SAVINGS_INTEREST_CALCULATION_TIME_PERIOD);

            Integer interestPostingEveryMonthFreq = savingsProductRequest.getInterestPostingMonthlyFrequency();
            MeetingBO interestPostingMeeting = new MeetingBO(RecurrenceType.MONTHLY, interestPostingEveryMonthFreq.shortValue(), new Date(), MeetingType.SAVINGS_INTEREST_POSTING);

            Money minAmountForCalculation = new Money(Money.getDefaultCurrency(), savingsProductRequest.getMinBalanceForInterestCalculation());

            GLCodeEntity depositGlEntity = this.generalLedgerDao.findGlCodeById(savingsProductRequest.getDepositGlCode().shortValue());
            GLCodeEntity interestGlEntity = this.generalLedgerDao.findGlCodeById(savingsProductRequest.getInterestGlCode().shortValue());

            MifosCurrency currency = Money.getDefaultCurrency();
            return SavingsOfferingBO.createNew(user.getUserId(), globalNum, name, shortName, description,
                        productCategory, startDate, endDate, applicableToEntity, currency, selectedStatus,
                        savingsTypeEntity, recommendedAmntUnitEntity, amountForDeposit, maxWithdrawal,
                        interestRate, interestCalcTypeEntity, interestCalculationMeeting, interestPostingMeeting, minAmountForCalculation, depositGlEntity, interestGlEntity);

        } catch (PersistenceException e) {
            throw new MifosRuntimeException(e);
        } catch (MeetingException e) {
            throw new MifosRuntimeException(e);
        }
    }

    private String generateProductGlobalNum(MifosUser user) {
        StringBuilder globalPrdOfferingNum = new StringBuilder();
        globalPrdOfferingNum.append(user.getBranchId());
        globalPrdOfferingNum.append("-");

        try {
            Short maxPrdID = new PrdOfferingPersistence().getMaxPrdOffering();
            globalPrdOfferingNum.append(StringUtils.leftPad(String.valueOf(maxPrdID != null ? maxPrdID + 1
                    : ProductDefinitionConstants.DEFAULTMAX + 1), 3, '0'));
            return globalPrdOfferingNum.toString();
        } catch (PersistenceException e) {
            throw new MifosRuntimeException(e);
        }
    }
}


text:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

			<title>Docs For Class AmfphpCustomClassConverter</title>
			<link rel="stylesheet" href="../media/stylesheet.css" />
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>

Class AmfphpCustomClassConverter

Description

					Description |
													Vars (details)
						|											Methods (details)

Converts data from incoming packets with explicit types to custom classes.

If the vclass is not found, the object is unmodified.  Sets the explicit type marker in the data of the outgoing packets.  If the explicit type marker is already set in an outgoing object, the value is left as is.  This works for nested objects.  This is to support services and plugins setting the explicit type themselves.

author: Ariel Sommeria-Klein

			Located in /Amfphp/Plugins/AmfphpCustomClassConverter/AmfphpCustomClassConverter.php (line 23)

Variable Summary

			Description |
						Vars (details)
							|
									Methods (details)

					array
					$customClassFolderPaths

Method Summary

			Description |
																		Vars (details)

				|
						Methods (details)

											AmfphpCustomClassConverter
										__construct
											([ $config = null])

											mixed
										convertToTyped
											(mixed $obj)

											mixed
										filterDeserializedRequest
											(mixed $deserializedRequest)

											mixed
										filterDeserializedResponse
											(mixed $deserializedResponse)

											mixed
										markExplicitType
											(mixed $obj)

Variables

			Description |
										Vars (details)

										|
									Methods (details)

			array
			$customClassFolderPaths
						(line 29)

paths to folders containing custom classes(relative or absolute)

var: of paths

access: public

Methods

			Description |
															Vars (details)
																	Methods (details)

		Constructor __construct (line 35)

constructor.

access: public

		AmfphpCustomClassConverter

			__construct

					([ $config = null])

				array
				$config: optional key/value pairs in an associative array. Used to override default configuration values.

		convertToTyped (line 81)

if the object contains an explicit type marker, this method attempts to convert it to its typed counterpart  if the typed class is already available, then simply creates a new instance of it. If not,  attempts to load the file from the available service folders.

If then the class is still not available, the object is not converted  note: This is not a recursive function. Rather the recusrion is handled by Amfphp_Core_Amf_Util::applyFunctionToContainedObjects.  must be public so that Amfphp_Core_Amf_Util::applyFunctionToContainedObjects can call it

access: public

		mixed

			convertToTyped

					(mixed $obj)

				mixed
				$obj

		filterDeserializedRequest (line 54)

converts untyped objects to their typed counterparts. Loads the class if necessary

access: public

		mixed

			filterDeserializedRequest

					(mixed $deserializedRequest)

				mixed
				$deserializedRequest

		filterDeserializedResponse (line 65)

looks at the outgoing packet and sets the explicit type field so that the serializer sends it properly

access: public

		mixed

			filterDeserializedResponse

					(mixed $deserializedResponse)

				mixed
				$deserializedResponse

		markExplicitType (line 124)

sets the the explicit type marker on the object and its sub-objects. This is only done if it not already set, as in some cases  the service class might want to do this manually.

note: This is not a recursive function. Rather the recusrion is handled by Amfphp_Core_Amf_Util::applyFunctionToContainedObjects.  must be public so that Amfphp_Core_Amf_Util::applyFunctionToContainedObjects can call it

access: public

		mixed

			markExplicitType

					(mixed $obj)

				mixed
				$obj

		Documentation generated on Thu, 24 Mar 2011 16:55:37 +0100 by phpDocumentor 1.4.3


text:
module SnlAdmin
  module UsersHelper
  end
end


text:
namespace download {

using TaskFinishedCallback = base::OnceCallback<void(bool)>;

// A class to manage the calls made to the TaskScheduler, that abstracts away
// the details of the TaskScheduler from the calling code. The tasks can run
// independently of each other as long as they have different |task_type|.
// Scheduling another task of same |task_type| before the task is started will
// overwrite the params of the scheduled task.
class TaskManagerImpl : public TaskManager {
 public:
  explicit TaskManagerImpl(std::unique_ptr<TaskScheduler> task_scheduler);

  TaskManagerImpl(const TaskManagerImpl&) = delete;
  TaskManagerImpl& operator=(const TaskManagerImpl&) = delete;

  ~TaskManagerImpl() override;

  // Called to schedule a new task. Overwrites the params if a task of the same
  // type is already scheduled. If the task is currently running, it will cache
  // the params and schedule the task after the completion/stopping of the
  // current task.
  void ScheduleTask(DownloadTaskType task_type,
                    const TaskParams& params) override;

  // Called to unschedule a scheduled task of the given type if it is not yet
  // started. Doesn't cancel the currently running task.
  void UnscheduleTask(DownloadTaskType task_type) override;

  // Called when the system starts a scheduled task. The callback will be cached
  // by the class and run after receiving a call to NotifyTaskFinished().
  void OnStartScheduledTask(DownloadTaskType task_type,
                            TaskFinishedCallback callback) override;

  // Called when the system decides to stop an already running task.
  void OnStopScheduledTask(DownloadTaskType task_type) override;

  // Should be called once the task is complete. The callback passed through
  // OnStartScheduledTask() will be run in order to notify that the task is done
  // and the system should reschedule the task with the original params if
  // |needs_reschedule| is true. If there are pending params for a new task, a
  // new task will be scheduled immediately and reschedule logic will not be
  // run.
  void NotifyTaskFinished(DownloadTaskType task_type,
                          bool needs_reschedule) override;

 private:
  // Whether a task of the given type is already running.
  bool IsRunningTask(DownloadTaskType task_type) const;

  std::unique_ptr<TaskScheduler> task_scheduler_;

  // Contains the params for the currently running task, which gets cleared
  // after the task is completed or stopped.
  std::map<DownloadTaskType, TaskParams> current_task_params_;

  // Contains params for the task to be scheduled. If a task is currently
  // running, the new task will be scheduled after the current task is finished
  // or immediately if there is no task running right now. The params will move
  // to current_task_params_ when the task is started.
  std::map<DownloadTaskType, TaskParams> pending_task_params_;

  // Contains the callbacks passed through the OnStartScheduledTask(). These
  // will be cleared when the task is completed or stopped by the system.
  std::map<DownloadTaskType, TaskFinishedCallback> task_finished_callbacks_;
};

}  // namespace download

#endif  // COMPONENTS_DOWNLOAD_PUBLIC_TASK_TASK_MANAGER_IMPL_H_


text:
'use strict';

var models = require('../models');
var Product = models.Product;
var Category = models.Category;
var Review = models.Review;
var handler = require('./handler');
var multer = require('multer');
var fs = require('fs');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './server/public/images/products/');
    },
    filename: function (req, file, cb) {
        var datetimestamp = Date.now();
        cb(null, file.fieldname + "-" + datetimestamp + "." +
            file.originalname.split(".")[file.originalname.split(".").length - 1]);
    }
});

var upload = multer({ storage: storage }).single('file');

// get all products from database
exports.getAll = function (req, res) {
	//Product.find({}, handler(res));
	Product.find({}).populate('category').exec(handler(res));
};

// "POST" add new product to db
exports.addNew = function (req, res) {

	upload(req, res, function (err) {
		if (err) {
			return handler(res)(err);
		}

		// create new product and add it to database
		var product = req.body.productInfo;

		var newProduct = {
			title: product.name,
			price: product.price,
			stock: product.amount,
			sale: product.sale,
			hot: product.hot,
			specifications: product.specifications,
			description: product.description,
			imageUrl: "images/products/" + req.file.filename,
			category: product.category
		};

		Product.create(newProduct, handler(res));
	});

};

// get product by id
exports.getById = function (req, res) {
	Product.findOne({ slug: req.params.id }).populate('category').exec(handler(res));
};

// delete product by id
exports.deleteById = function (req, res) {
	Product.findByIdAndRemove({ _id: req.params.id }, function (error, product) {
		// delete product image
		var imageUrl = product.imageUrl;

		// check if the product image is exist
		fs.stat('server/public/' + imageUrl, function (err, stats) {
			if(!err) {
				// delete the image
				fs.unlink('server/public/' + imageUrl, function (err) {
					handler(res)(error, product); // send response back
				});
			}
			else {
				handler(res)(error, product); // send response back
			}
		});
	});
};

// update product and return it
exports.updateById = function (req, res) {
	Product.findByIdAndUpdate(
		req.params.id,
		{ $set: req.body },
		{ new: true, runValidators: true },
		handler(res)
	);
};

exports.getByCategoryId = function (req, res) {

	var category = req.params.id;
	// get all children
	Category.find({ ancestors: category }, function (err, result) {
		var categories = result;
		categories.push(category);
		Product.find({ category: { $in: categories } }, handler(res));
	});

};

// search products using product title
exports.search = function (req, res) {

	var text = req.params.text;

	Product.find(
		{ $text: { $search: text } },
		{ score: { $meta: 'textScore' } }
	)
	.sort({ score: { $meta: 'textScore' } })
	.limit(10)
	.populate('category')
	.exec(handler(res));

};

exports.addReview = function (req, res) {

	var productId = req.params.id;
	var review = req.body.review;
	Product.findOne({ _id: productId }, function (err, product) {
		if(err) {
			return handler(res)(err);
		}

		product.addReview(review, handler(res));

	});

};

exports.getReviews = function (req, res) {
	var prodId = req.params.id;
	Review.find({ product: prodId }).populate('user').exec(handler(res));
};