]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Scripts changed to use database.
[linux-conf-perf.git] / scripts / utils.py
1 import os
2 import sys
3 import subprocess
4 import time
5 import hashlib
6 import re
7 from conf import conf
8 from conf import sf
9 import exceptions
10
11 def build_symbol_map():
12         """Generates global variable smap from symbol_map_file.
13         When file not exists, MissingFile exception is raised.
14         """
15         global smap
16         try:
17                 smap
18         except NameError:
19                 # Check if symbol_map_file exist
20                 if not os.path.isfile(sf(conf.symbol_map_file)):
21                         raise exceptions.MissingFile(sf(conf.symbol_map_file),
22                                         "Run parse_kconfig to generate it.")
23
24                 smap = dict()
25                 with open(sf(conf.symbol_map_file)) as f:
26                         for lnn in f:
27                                 w = lnn.rstrip().split(sep=':')
28                                 smap[int(w[0])] = w[1]
29
30
31 def callsubprocess(process_name, process, show_output = True,
32                 return_output = False, env=os.environ, allowed_exit_codes = [0],
33                 allow_all_exit_codes = False):
34         sprc = subprocess.Popen(process, stdout = subprocess.PIPE, env = env)
35
36         try:
37                 os.mkdir(os.path.join(sf(conf.log_folder), process_name))
38         except OSError:
39                 pass
40
41         rtn = []
42         with open(os.path.join(sf(conf.log_folder),
43                         process_name, time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
44                         "a") as f:
45                 f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::\n')
46                 for linen in sprc.stdout:
47                         line = linen.decode(sys.getdefaultencoding())
48                         f.write(line)
49                         if show_output:
50                                 print(line, end="")
51                         if return_output:
52                                 rtn.append(line.rstrip())
53
54         rtncode = sprc.wait()
55         if rtncode not in allowed_exit_codes and not allow_all_exit_codes:
56                 raise exceptions.ProcessFailed(process, rtncode)
57         return rtn
58
59 def get_kernel_env():
60         env = dict(os.environ)
61         env.update(conf.kernel_env)
62         return env