summaryrefslogtreecommitdiff
path: root/scripts/builder/main.py
blob: ecdbf6af1aee824e39559340acebf48e746d91ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys
import os
import json
import shutil
import pprint
import codecs
import itertools
from collections   import deque

#import frontendBuilder
import repository

pp = pprint.PrettyPrinter(indent=4, depth=4)

#--------------------------------------------------------------------

def scriptDir ():
	return  os.path.dirname(sys.argv[0])

def projectBaseDir ():
	return os.path.abspath(scriptDir() + '/../..')

def projectTargetDir(): 
	return projectBaseDir() + '/target/'

#--------------------------------------------------------------------

def createFolder (path):
	if not os.path.exists(path):
		os.makedirs(path)

#--------------------------------------------------------------------

def loadSettings (component, module):
	# print "MODULE: " + module

	if '.' in module:
		moduleComponents = module.split('.')
		module = moduleComponents[0]
		submodule = moduleComponents[1]
	else:
		submodule = module

	#settings = codecs.open(projectBaseDir() + os.sep + component + os.sep + module + os.sep + 'properties' + os.sep + submodule + '.properties.json', 'r', 'utf-8')
	settings = codecs.open(os.path.join(projectBaseDir(), component, module, 'properties', submodule + '.properties.json'), 'r', 'utf-8')
	result = json.load(settings)
	settings.close

	return result

#====================================================================
# 
# def assembleFrontend (frontend, versions):
# 	result = {}
# 	settings = loadSettings('frontend', frontend)
# 	builder = frontendBuilder.FrontendBuilder(frontend, settings, projectBaseDir())
# 	
# 	for version in versions:
# 		if version == 'install':
# 			result[version] = builder.assembleInstallVersion()
# 		elif version == 'debug':
# 			result[version] = builder.assembleDebugVersion()
# 		else:
# 			raise Exception('unrecognized version: ' + version)
# 	
# 	return result
# 
#====================================================================

def assembleBackend (backend, frontends, versions):
	settings = loadSettings('backend', backend)
	
	builderModuleName = backend + 'Builder'
	builderClassName  = backend.capitalize() + 'Builder'
	#print ("BUILD BACKENDS - module: " + builderModuleName + " , class: " + builderClassName)
	builderModule  = __import__(builderModuleName)
	builderClass   = getattr(builderModule, builderClassName)
	
	backendBuilder = builderClass(projectTargetDir(), frontends, versions, settings)
	backendBuilder.run()	

#====================================================================

def build (settings, repository):
	frontends = []
	
	if repository.areTherePendingChanges():
		if 'install' in settings['versions']:
			raise Exception("repository has pending changes, can't 'install'")
		else:
			print "\nWARNING: repository has pending changes\n"

	for frontend in settings['frontends']:
		normalizedFrontendName = frontend.replace(".", "_")
		builderModuleName = normalizedFrontendName + 'Builder'
		builderClassName  = normalizedFrontendName.title() + 'Builder'

		#print ("BUILD FRONTEND - module: " + builderModuleName + " , class: " + builderClassName)
		builderModule  = __import__(builderModuleName)
		builderClass   = getattr(builderModule, builderClassName)
		builder = builderClass(frontend, loadSettings('frontend', frontend), repository.version())
		#builder = frontendBuilder.FrontendBuilder(frontend, loadSettings('frontend', frontend), repository.version())
		frontends.append(builder)

	for backend in settings['backends']:
		assembleBackend(backend, frontends, settings['versions'])

#--------------------------------------------------------------------

def clean ():
	# print "cleaning up …"
	if os.path.exists(projectTargetDir()):
		shutil.rmtree(projectTargetDir())

#--------------------------------------------------------------------

def usage (message):
	if message != None:
		print "ERROR: " + message
	
	print
	# print "build clean"
	# print "build clean install"
	print "build install --ALL"
	print "build install debug --ALL"
	print "build install debug development --ALL"
	# print "build clean install debug --ALL"
	print "build install debug --backends php python --frontends beta gamma"
	print "build install debug development --backends php python --frontends beta gamma gamma.mobile"
	exit(1)

#--------------------------------------------------------------------

def allFrontends ():
	return ['beta', 'gamma',  'gamma.mobile']

def allBackends ():
	return ['php',  'python']

#--------------------------------------------------------------------

def main ():
	settings = {}
	parameters = list(itertools.islice(sys.argv, 1, None))

	sys.path.append(os.path.join(scriptDir(), 'backends'))
	sys.path.append(os.path.join(scriptDir(), 'frontends'))
	currentRepository = repository.repositoryWithPath(projectBaseDir())

	clean()
	versions = list(itertools.takewhile(lambda x: not x.startswith('--'), parameters))
	settings['versions']  = versions;		#['debug', 'install', 'development', 'checksum']
	parameters = deque(itertools.dropwhile(lambda x: not x.startswith('--'), parameters))
	
	if len(parameters) > 0:
		parameter = parameters.popleft()
		if parameter == "--ALL":
			settings['frontends'] = allFrontends()
			settings['backends']  = allBackends()
		else:
			while parameter != None:
				values = list(itertools.takewhile(lambda x: not x.startswith('--'), parameters))
			
				if parameter == "--backends":
					settings['backends'] = values
				elif parameter == "--frontends":
					settings['frontends'] = values
			
				parameters = deque(itertools.dropwhile(lambda x: not x.startswith('--'), parameters))
				if parameters:
					parameter = parameters.popleft()
				else:
					parameter = None
	
		if 'checksum' in settings['versions']:
			if not 'backends' in settings:
				settings['backends'] = []
			settings['backends'].append('checksum')
		
		if (not settings.has_key('versions')):
			usage("missing 'versions'")
		if (not settings.has_key('frontends')):
			usage("missing 'frontends'")
		if (not settings.has_key('backends')):
			usage("missing 'backends'")
		
		build(settings, currentRepository)
	else:
		usage("Suggestions on how to call the 'build' script:")


if __name__ == "__main__":
	main()