aboutsummaryrefslogtreecommitdiff
path: root/scripts/utils.py
blob: f6a4720e2f15d25b152ff7d8a411ff89f106547a (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
import os
import sys
import subprocess
import time
import hashlib
import signal
import re
from threading import Thread
from conf import conf
from conf import sf
import exceptions

def build_symbol_map():
	"""Generates global variable smap from symbol_map_file.
	When file not exists, MissingFile exception is raised.
	"""
	global smap
	try:
		smap
	except NameError:
		# Check if symbol_map_file exist
		if not os.path.isfile(sf(conf.symbol_map_file)):
			raise exceptions.MissingFile(sf(conf.symbol_map_file),
					"Run parse_kconfig to generate it.")

		smap = dict()
		with open(sf(conf.symbol_map_file)) as f:
			for lnn in f:
				w = lnn.rstrip().split(sep=':')
				smap[int(w[0])] = w[1]

class __subprocess_timer__(Thread):
	def __init__(self, sprc, timeout):
		Thread.__init__(self, name='subprocess_timer')
		self.sprc = sprc
		self.last = time.time()
		self.exitit = False
		self.timeout = timeout
		self.timeouted = False
		if timeout > 0:
			self.start()
	def output(self):
		self.last = time.time()
	def exit(self):
		self.exitit = True
		return self.timeouted
	def run(self):
		while not self.exitit:
			now = time.time()
			if (now - self.last) >= self.timeout:
				self.timeouted = True
				os.kill(self.sprc.pid, signal.SIGTERM)
				return
			time.sleep(1)

def callsubprocess(process_name, process, show_output = True,
		return_output = False, env=os.environ, allowed_exit_codes = [0],
		allow_all_exit_codes = False, stdin = None, timeout = -1):
	sprc = subprocess.Popen(process, stdout = subprocess.PIPE,
			stderr = subprocess.STDOUT, stdin = subprocess.PIPE, env = env)


	try:
		os.mkdir(os.path.join(sf(conf.log_folder), process_name))
	except OSError:
		pass

	if stdin != None:
		for ln in stdin:
			sprc.stdin.write(bytes(ln + '\n', sys.getdefaultencoding()))
			sprc.stdin.flush()
		sprc.stdin.close()

	rtn = []
	timerout = __subprocess_timer__(sprc, timeout)
	with open(os.path.join(sf(conf.log_folder),
			process_name, time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
			"a") as f:
		f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::\n')
		for linen in sprc.stdout:
			timerout.output()
			line = linen.decode(sys.getdefaultencoding())
			f.write(line)
			if show_output:
				print(line, end="")
			if return_output:
				rtn.append(line.rstrip())
	if timerout.exit():
		raise exceptions.ProcessTimeout(process_name, rtn)
	rtncode = sprc.wait()
	if rtncode not in allowed_exit_codes and not allow_all_exit_codes:
		raise exceptions.ProcessFailed(process, rtncode, rtn)
	return rtn

def get_kernel_env():
	env = dict(os.environ)
	env.update(conf.kernel_env)
	return env

def __dirty_repo__(path):
	cwd = os.getcwd()
	os.chdir(conf.absroot)
	out = subprocess.check_output(conf.git_describe_cmd)
	if re.search('dirty', out.decode(sys.getdefaultencoding())):
		raise exceptions.DirtyRepository(path)

def dirtycheck():
	__dirty_repo__(conf.absroot)
	__dirty_repo__(conf.linux_sources)