6a5608c908c36a8fea3f61d4817edfca0d3a24af
[deliverable/lttng-ivc.git] / lttng_ivc / utils / utils.py
1 import signal
2 import hashlib
3 import os
4 import time
5 import socket
6
7 from lxml import etree
8 from contextlib import closing
9
10
11 def 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
17
18
19 def 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
27 # TODO: timeout as a parameter or Settings
28 # TODO: Custom exception
29 def 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
40 def create_empty_file(path):
41 if os.path.exists(path):
42 raise Exception("Path already exist")
43 open(path, 'w').close()
44
45
46 def __dummy_sigusr1_handler():
47 pass
48
49
50 def sessiond_spawn(runtime):
51 agent_port = find_free_port()
52 previous_handler = signal.signal(signal.SIGUSR1, __dummy_sigusr1_handler)
53 sessiond = runtime.spawn_subprocess("lttng-sessiond -vvv -S --agent-tcp-port {}".format(agent_port))
54 signal.sigtimedwait({signal.SIGUSR1}, 60)
55 previous_handler = signal.signal(signal.SIGUSR1, previous_handler)
56 return sessiond
57
58
59 def relayd_spawn(runtime, url="localhost"):
60 """
61 Return a tuple (relayd_uuid, ctrl_port, data_port, live_port)
62 """
63 ports = find_multiple_free_port(3)
64 data_port = ports.pop()
65 ctrl_port = ports.pop()
66 live_port = ports.pop()
67
68 base_cmd = "lttng-relayd -vvv"
69 data_string = "-D tcp://{}:{}".format(url, data_port)
70 ctrl_string = "-C tcp://{}:{}".format(url, ctrl_port)
71 live_string = "-L tcp://{}:{}".format(url, live_port)
72
73 cmd = " ".join([base_cmd, data_string, ctrl_string, live_string])
74 relayd = runtime.spawn_subprocess(cmd)
75
76 # Synchronization based on verbosity since no -S is available for
77 # lttng-relayd yet.
78 log_path = runtime.get_subprocess_stderr_path(relayd)
79
80 # TODO: Move to settings.
81 ready_cue = "Listener accepting live viewers connections"
82 # TODO: Move to settings.
83 timeout = 60
84 ready = False
85 for i in range(timeout):
86 if file_contains(log_path, ready_cue):
87 ready = True
88 break
89 time.sleep(1)
90
91 if not ready:
92 # Cleanup is performed by runtime
93 raise Exception("Relayd readyness timeout expired")
94
95 return (relayd, ctrl_port, data_port, live_port)
96
97
98 def find_free_port():
99 # There is no guarantee that the port will be free at runtime but should be
100 # good enough
101 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
102 s.bind(('', 0))
103 return s.getsockname()[1]
104
105
106 def find_multiple_free_port(number):
107 """
108 Return a list of supposedly free port
109 """
110 assert(number >= 0)
111 ports = []
112 while(len(ports) != number):
113 port = find_free_port()
114 if port in ports:
115 continue
116 ports.append(port)
117 return ports
118
119
120 def file_contains(file_path, list_of_string):
121 with open(file_path, 'r') as f:
122 for line in f:
123 for s in list_of_string:
124 if s in line:
125 return True
126
127
128 def find_dir(root, name):
129 """
130 Returns the absolute path or None.
131 """
132 abs_path = None
133 for base, dirs, files in os.walk(root):
134 for tmp in dirs:
135 if tmp.endswith(name):
136 abs_path = os.path.abspath(os.path.join(base, tmp))
137 return abs_path
138
139
140 def find_file(root, name):
141 """
142 Returns the absolute path or None.
143 """
144 abs_path = None
145 for base, dirs, files in os.walk(root):
146 for tmp in files:
147 if tmp.endswith(name):
148 abs_path = os.path.abspath(os.path.join(base, tmp))
149 return abs_path
150
151
152 def validate(xml_path, xsd_path):
153
154 xmlschema_doc = etree.parse(xsd_path)
155 xmlschema = etree.XMLSchema(xmlschema_doc)
156
157 xml_doc = etree.parse(xml_path)
158 result = xmlschema.validate(xml_doc)
159
160 return result
161
162 def xpath_query(xml_file, xpath):
163 """
164 Return a list of xml node corresponding to the xpath. The list can be of lenght
165 zero.
166 """
167 with open(xml_file, 'r') as f:
168 tree = etree.parse(f)
169 root = tree.getroot()
170 # Remove all namespace
171 # https://stackoverflow.com/questions/18159221/remove-namespace-and-prefix-from-xml-in-python-using-lxml
172 for elem in root.getiterator():
173 if not hasattr(elem.tag, 'find'):
174 continue
175 i = elem.tag.find('}')
176 if i >= 0:
177 elem.tag = elem.tag[i+1:]
178
179 return root.xpath(xpath)
This page took 0.035085 seconds and 4 git commands to generate.