summaryrefslogtreecommitdiffstats
path: root/awlsim/common/codevalidator.py
blob: c20d14aae727ae6b7085eab0a860f5f9aea07ee3 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - Code validator
#
# Copyright 2014-2016 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *

from awlsim.common.util import *

from awlsim.coreclient.client import *

import threading


class AwlValidator(object):
	"""Source code validation.
	"""

	__globalInstance = None

	_PORT_RANGE	= range(30000, 32000)
	_EXIT_THREAD	= -1

	@classmethod
	def startup(cls):
		if cls.__globalInstance:
			return cls.__globalInstance
		printDebug("AwlValidator: Startup")
		try:
			val = cls()
		except AwlSimError as e:
			printDebug("Failed to startup validator: %s" % str(e))
			return None
		cls.__globalInstance = val
		return val

	@classmethod
	def shutdown(cls):
		if cls.__globalInstance:
			cls.__globalInstance.doShutdown()
			cls.__globalInstance = None

	@classmethod
	def get(cls):
		return cls.startup()

	def __init__(self):
		self.__client = AwlSimClient()
		try:
			self.__client.setLoglevel(Logging.LOG_ERROR,
						  setClientSide=False,
						  setServerSide=True)
			self.__client.spawnServer(listenHost = "localhost",
						  listenPort = self._PORT_RANGE)
			self.__client.connectToServer(host = "localhost",
						      port = self.__client.serverProcessPort)
			self.__client.setLoglevel(Logging.LOG_NONE,
						  setClientSide=False,
						  setServerSide=True)
		except AwlSimError as e:
			self.doShutdown()
			raise e
		self.__lock = threading.Lock()
		self.__condition = threading.Condition(self.__lock)
		self.__job = None
		self.__exception = None
		self.__running = False
		self.__thread = threading.Thread(target=self.__thread)
		self.__thread.daemon = False
		self.__thread.start()

	def doShutdown(self):
		printDebug("AwlValidator: Shutdown")
		if self.__client:
			self.__client.shutdown()
			self.__client = None
		if self.__thread:
			with self.__lock:
				self.__job = self._EXIT_THREAD
				self.__condition.notify_all()
			self.__thread.join()
			self.__thread = None
			self.__exception = None
			self.__running = False

	def __runJob(self, job):
		(project,
		 symTabSources, libSelections, awlSources,
		 fupSources, kopSources) = job

		client = self.__client
		exception = None
		try:
			client.setRunState(False)
			client.reset()
			client.loadProject(project, loadSymTabs=False,
					   loadLibSelections=False,
					   loadSources=False,
					   loadFup=False,
					   loadKop=False)
			client.loadSymTabSources(symTabSources)
			client.loadLibraryBlocks(libSelections)
			client.loadAwlSources(awlSources)
			client.loadFupSources(fupSources)
			client.loadKopSources(kopSources)
			client.build()
			client.reset()
		except AwlSimError as e:
			exception = e
		with self.__lock:
			if self.__job is None:
				self.__running = False
			self.__exception = exception

	def __thread(self):
		"""This is the validation thread.
		"""
		while True:
			with self.__lock:
				if self.__job is None:
					self.__condition.wait()
				job, self.__job = self.__job, None
			if job is self._EXIT_THREAD:
				break
			if job:
				self.__runJob(job)

	def validate(self, project,
		     symTabSources, libSelections, awlSources,
		     fupSources, kopSources):
		"""Schedule a validation.
		Get the result with getState().
		"""
		if not project or not awlSources:
			return
		with self.__lock:
			if self.__job is self._EXIT_THREAD:
				return
			self.__job = (project,
				      symTabSources, libSelections, awlSources,
				      fupSources, kopSources)
			self.__running = True
			self.__condition.notify_all()

	def getState(self):
		"""Get the validation result.
		Returns a tuple (running, exception).
		"""
		with self.__lock:
			running = self.__running
			exception = self.__exception
		return running, exception
bues.ch cgit interface