-
Notifications
You must be signed in to change notification settings - Fork 21
/
port.py
85 lines (67 loc) · 3 KB
/
port.py
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
# Copyright (C) 2016 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).
# This file is part of pytest-elasticsearch.
# pytest-elasticsearch is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pytest-elasticsearch is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with pytest-elasticsearch. If not, see <http://www.gnu.org/licenses/>.
"""Helpers for port-for package."""
from itertools import chain
import port_for
class InvalidPortsDefinition(ValueError):
"""Exception raised if ports definition is not a valid string."""
def __init__(self, ports):
"""Construct InvalidPortsDefinition exception."""
super().__init__(
'Unknown format of ports: {0}.\n'.format(ports) + (
'You should provide a ports range "[(4000,5000)]"'
'or "(4000,5000)" or a comma-separated ports set'
'"[{4000,5000,6000}]" or list of ints "[400,5000,6000,8000]"'
'or all of them "[(20000, 30000), {48889, 50121}, 4000, 4004]"'
)
)
def get_port(ports):
"""
Retun a random available port.
If there's only one port passed (e.g. 5000 or '5000') function
does not check if port is available.
it there's -1 passed as an argument, function returns None.
When a range or list of ports is passed `port_for` external package
is used in order to find a free port.
:param str|int|tuple|set|list port:
exact port (e.g. '8000', 8000)
randomly selected port (None) - any random available port
[(2000,3000)] or (2000,3000) - random available port from a given range
[{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports
[(2000,3000), {4002,4003}] -random of given orange and set
:returns: a random free port
:raises: ValueError
"""
if ports == -1:
return None
if not ports:
return port_for.select_random(None)
try:
return int(ports)
except TypeError:
pass
ports_set = set()
try:
if not isinstance(ports, list):
ports = [ports]
ranges = port_for.utils.ranges_to_set(filter_by_type(ports, tuple))
nums = set(filter_by_type(ports, int))
sets = set(chain(*filter_by_type(ports, (set, frozenset))))
ports_set = ports_set.union(ranges, sets, nums)
except ValueError as exc:
raise InvalidPortsDefinition from exc
return port_for.select_random(ports_set)
def filter_by_type(lst, type_of):
"""Return a list of elements with given type."""
return [e for e in lst if isinstance(e, type_of)]