8 from contextlib
import closing
11 def line_count(file_path
):
13 with
open(file_path
) as f
:
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
''):
24 return sha256
.hexdigest()
27 # TODO: timeout as a parameter or Settings
28 # TODO: Custom exception
29 def wait_for_file(path
):
32 while not os
.path
.exists(path
):
36 raise Exception("File still does not exists. Timeout expired")
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()
46 def __dummy_sigusr1_handler():
50 def sessiond_spawn(runtime
, opt_args
=""):
51 agent_port
= find_free_port()
52 previous_handler
= signal
.signal(signal
.SIGUSR1
, __dummy_sigusr1_handler
)
53 cmd
= "lttng-sessiond -vvv --verbose-consumer -S --agent-tcp-port {}".format(agent_port
)
54 cmd
= " ".join([cmd
, opt_args
])
55 sessiond
= runtime
.spawn_subprocess(cmd
)
56 signal
.sigtimedwait({signal
.SIGUSR1
}, 60)
57 previous_handler
= signal
.signal(signal
.SIGUSR1
, previous_handler
)
61 def relayd_spawn(runtime
, url
="localhost"):
63 Return a tuple (relayd_uuid, ctrl_port, data_port, live_port)
65 ports
= find_multiple_free_port(3)
66 data_port
= ports
.pop()
67 ctrl_port
= ports
.pop()
68 live_port
= ports
.pop()
70 base_cmd
= "lttng-relayd -vvv"
71 data_string
= "-D tcp://{}:{}".format(url
, data_port
)
72 ctrl_string
= "-C tcp://{}:{}".format(url
, ctrl_port
)
73 live_string
= "-L tcp://{}:{}".format(url
, live_port
)
75 cmd
= " ".join([base_cmd
, data_string
, ctrl_string
, live_string
])
76 relayd
= runtime
.spawn_subprocess(cmd
)
78 # Synchronization based on verbosity since no -S is available for
80 log_path
= runtime
.get_subprocess_stderr_path(relayd
)
82 # TODO: Move to settings.
83 ready_cue
= "Listener accepting live viewers connections"
84 # TODO: Move to settings.
87 for i
in range(timeout
):
88 if file_contains(log_path
, ready_cue
):
94 # Cleanup is performed by runtime
95 raise Exception("Relayd readyness timeout expired")
97 return (relayd
, ctrl_port
, data_port
, live_port
)
100 def find_free_port():
101 # There is no guarantee that the port will be free at runtime but should be
103 with
closing(socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)) as s
:
105 return s
.getsockname()[1]
108 def find_multiple_free_port(number
):
110 Return a list of supposedly free port
114 while(len(ports
) != number
):
115 port
= find_free_port()
122 def file_contains(file_path
, list_of_string
):
123 with
open(file_path
, 'r') as f
:
125 for s
in list_of_string
:
130 def find_dir(root
, name
):
132 Returns the absolute path or None.
135 for base
, dirs
, files
in os
.walk(root
):
137 if tmp
.endswith(name
):
138 abs_path
= os
.path
.abspath(os
.path
.join(base
, tmp
))
142 def find_file(root
, name
):
144 Returns the absolute path or None.
147 for base
, dirs
, files
in os
.walk(root
):
149 if tmp
.endswith(name
):
150 abs_path
= os
.path
.abspath(os
.path
.join(base
, tmp
))
154 def validate(xml_path
, xsd_path
):
156 xmlschema_doc
= etree
.parse(xsd_path
)
157 xmlschema
= etree
.XMLSchema(xmlschema_doc
)
159 xml_doc
= etree
.parse(xml_path
)
160 result
= xmlschema
.validate(xml_doc
)
164 def xpath_query(xml_file
, xpath
):
166 Return a list of xml node corresponding to the xpath. The list can be of lenght
169 with
open(xml_file
, 'r') as f
:
170 tree
= etree
.parse(f
)
171 root
= tree
.getroot()
172 # Remove all namespace
173 # https://stackoverflow.com/questions/18159221/remove-namespace-and-prefix-from-xml-in-python-using-lxml
174 for elem
in root
.getiterator():
175 if not hasattr(elem
.tag
, 'find'):
177 i
= elem
.tag
.find('}')
179 elem
.tag
= elem
.tag
[i
+1:]
181 return root
.xpath(xpath
)
This page took 0.064437 seconds and 5 git commands to generate.