summaryrefslogtreecommitdiff
path: root/scripts/builder/backendBuilder.py
blob: 5b9167819fb460722c6c6def0fab8b1a8f8e4f6d (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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys, os, json
import shutil
import hashlib

import main

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


class BackendBuilder(object):
	
	def __init__ (self, projectTargetDir, frontends, versions, settings):
		self.projectTargetDir = projectTargetDir
		self.frontends = frontends
		self.versions = versions
		self.settings = settings

	# --------------------------------------------------------------------------
	
	def name (self):
		raise NotImplementedError()

	
	def relativePath (self):
		raise NotImplementedError()

	
	def compileCode (self):
		raise NotImplementedError()

	
	def createPackage (self):
		raise NotImplementedError()

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

	def sourceFolder (self):
		return os.path.join(main.projectBaseDir() , 'backend', self.relativePath(), 'src')


	def tempFolder (self):
		return os.path.join(self.projectTargetDir, '.tmp', self.relativePath())
		

	def frontEndTempFolder (self):
		return self.tempFolder()


	def developmentTargetFolder (self):
		return os.path.join(self.projectTargetDir, 'development', self.relativePath())

	def targetFolder (self):
		return os.path.join(self.projectTargetDir, self.relativePath())
		
	# --------------------------------------------------------------------------
	
	def writeToFolder (self, folder, filename, content):
		file = open(os.path.join(folder, filename), 'w')
		file.write(content.encode('utf-8'))
		file.close()
		

	def configureIndexContent (self, indexContent, requestPathPrefix = ".."):
		result = indexContent
		result = result.replace( '@request.path@',    requestPathPrefix + '/' + self.settings['request.path']    )
		result = result.replace( '@should.pay.toll@', self.settings['should.pay.toll'] )

		return result
	

	def logChecksums (self, content, message):
		md5Digest		= hashlib.md5(content.encode('utf-8')).hexdigest()
		shaDigest		= hashlib.sha1(content.encode('utf-8')).hexdigest()
		sha256Digest	= hashlib.sha256(content.encode('utf-8')).hexdigest()
		print message + ": " + md5Digest + " (md5)"
		print message + ": " + shaDigest + " (sha1)"
		print message + ": " + sha256Digest + " (sha256)"
		
	
	def shouldCompileCode (self):
		return ('debug' in self.versions) or ('install' in self.versions)


	def run (self):
		print self.name() + " - RUN"

		if self.shouldCompileCode():
			self.compileCode()
		
			for frontend in self.frontends:
				if (frontend.module == frontend.submodule):
					submoduleExtension = ''
				else:
					submoduleExtension = '.' + frontend.submodule

				main.createFolder(os.path.join(self.frontEndTempFolder(), frontend.module))

				if 'debug' in self.versions:
					frontend.copyResourcesToFolder(self.frontEndTempFolder())

					index = self.configureIndexContent(frontend.assemble(assemblyMode='DEBUG', versionType='DEBUG'))
					self.writeToFolder(self.frontEndTempFolder(), os.path.join(frontend.module, 'index_debug' + submoduleExtension + '.html'), index)
				
				if 'install' in self.versions:
					index = self.configureIndexContent(frontend.assemble())
					self.writeToFolder(self.frontEndTempFolder(), os.path.join(frontend.module, 'index' + submoduleExtension + '.html'), index)

					self.logChecksums(index, "[" + self.name() + " - " + frontend.module + "] index" + submoduleExtension + ".html checksum")
			
			self.createPackage()

		if 'development' in self.versions:
			for frontend in self.frontends:
				if (frontend.module == frontend.submodule):
					submoduleExtension = ''
				else:
					submoduleExtension = '.' + frontend.submodule

				print "FRONTEND module: " + frontend.module
				print "FRONTEND submodule: " + frontend.submodule

				main.createFolder(os.path.join(self.developmentTargetFolder(), frontend.module))

				index = self.configureIndexContent(frontend.assemble(assemblyMode='DEVELOPMENT', versionType='DEBUG'), self.settings['development.settings']['url'])
				self.writeToFolder(self.developmentTargetFolder(), os.path.join(frontend.module, 'index' + submoduleExtension + '.html'), index)

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