Base utils for xsd mi validation and saved sessions file
[deliverable/lttng-ivc.git] / lttng_ivc / utils / utils.py
CommitLineData
a028c28a 1import signal
cb87ff89 2import hashlib
bde0c540
JR
3import os
4import time
74eb7096
JR
5import socket
6
8fefa4a3 7from lxml import etree
74eb7096 8from contextlib import closing
a028c28a 9
8fefa4a3 10
85a9e11c
JR
11def line_count(file_path):
12 line_count = 0
13 with open(file_path) as f:
14 for line in f:
15 line_count += 1
16 return line_count
a028c28a
JR
17
18
cb87ff89
JR
19def sha256_checksum(filename, block_size=65536):
20 sha256 = hashlib.sha256()
21 with open(filename, 'rb') as f:
22 for block in iter(lambda: f.read(block_size), b''):
23 sha256.update(block)
24 return sha256.hexdigest()
25
26
bde0c540
JR
27# TODO: timeout as a parameter or Settings
28# TODO: Custom exception
29def wait_for_file(path):
30 i = 0
31 timeout = 60
32 while not os.path.exists(path):
33 time.sleep(1)
34 i = i + 1
35 if i > timeout:
36 raise Exception("File still does not exists. Timeout expired")
37
38
39# TODO: find better exception
40def create_empty_file(path):
41 if os.path.exists(path):
42 raise Exception("Path already exist")
43 open(path, 'w').close()
44
45
a028c28a
JR
46def __dummy_sigusr1_handler():
47 pass
48
49
50def sessiond_spawn(runtime):
74eb7096 51 agent_port = find_free_port()
29cf55b3 52 previous_handler = signal.signal(signal.SIGUSR1, __dummy_sigusr1_handler)
74eb7096 53 sessiond = runtime.spawn_subprocess("lttng-sessiond -vvv -S --agent-tcp-port {}".format(agent_port))
29cf55b3
JR
54 signal.sigtimedwait({signal.SIGUSR1}, 60)
55 previous_handler = signal.signal(signal.SIGUSR1, previous_handler)
56 return sessiond
57
58
74eb7096
JR
59def find_free_port():
60 # There is no guarantee that the port will be free at runtime but should be
61 # good enough
62 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
63 s.bind(('', 0))
64 return s.getsockname()[1]
29cf55b3 65
f4711e68 66
8fefa4a3
JR
67def file_contains(file_path, list_of_string):
68 with open(file_path, 'r') as f:
69 for line in f:
f4711e68
JR
70 for s in list_of_string:
71 if s in line:
72 return True
2094d672
JR
73
74
75def find_dir(root, name):
76 """
77 Returns the absolute path or None.
78 """
79 abs_path = None
80 for base, dirs, files in os.walk(root):
81 for tmp in dirs:
2094d672
JR
82 if tmp.endswith(name):
83 abs_path = os.path.abspath(os.path.join(base, tmp))
84 return abs_path
8fefa4a3
JR
85
86
816f8cc1
JR
87def find_file(root, name):
88 """
89 Returns the absolute path or None.
90 """
91 print(root)
92 print(name)
93 abs_path = None
94 for base, dirs, files in os.walk(root):
95 for tmp in files:
96 if tmp.endswith(name):
97 abs_path = os.path.abspath(os.path.join(base, tmp))
98 print(abs_path)
99 return abs_path
100
101
102def validate(xml_path, xsd_path):
103
104 xmlschema_doc = etree.parse(xsd_path)
105 xmlschema = etree.XMLSchema(xmlschema_doc)
106
107 xml_doc = etree.parse(xml_path)
108 result = xmlschema.validate(xml_doc)
109
110 return result
111
8fefa4a3
JR
112def xpath_query(xml_file, xpath):
113 """
114 Return a list of xml node corresponding to the xpath. The list can be of lenght
115 zero.
116 """
117 with open(xml_file, 'r') as f:
118 tree = etree.parse(f)
119 root = tree.getroot()
120 # Remove all namespace
121 # https://stackoverflow.com/questions/18159221/remove-namespace-and-prefix-from-xml-in-python-using-lxml
122 for elem in root.getiterator():
123 if not hasattr(elem.tag, 'find'):
124 continue
125 i = elem.tag.find('}')
126 if i >= 0:
127 elem.tag = elem.tag[i+1:]
128
129 return root.xpath(xpath)
This page took 0.0293 seconds and 5 git commands to generate.