2015-10-05 18:12:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2019-10-09 09:02:30 +00:00
|
|
|
import sys
|
2016-05-02 15:13:23 +00:00
|
|
|
import re
|
2019-10-09 09:02:30 +00:00
|
|
|
import shlex
|
2016-04-25 14:36:20 +00:00
|
|
|
import textwrap
|
2016-09-27 15:15:59 +00:00
|
|
|
import posixpath
|
2024-06-17 14:41:40 +00:00
|
|
|
import socket
|
|
|
|
import errno
|
2016-09-27 15:15:59 +00:00
|
|
|
|
2015-10-05 18:12:20 +00:00
|
|
|
|
|
|
|
def force_unix_path(path):
|
|
|
|
"""
|
|
|
|
:param path: Path to convert
|
|
|
|
"""
|
|
|
|
|
2016-09-27 15:15:59 +00:00
|
|
|
path = path.replace("\\", "/")
|
|
|
|
return posixpath.normpath(path)
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
|
|
|
2016-05-28 05:00:05 +00:00
|
|
|
def macaddress_to_int(mac_address):
|
2016-04-25 14:36:20 +00:00
|
|
|
"""
|
|
|
|
Convert a macaddress with the format 00:0c:29:11:b0:0a to a int
|
|
|
|
|
2016-05-28 05:00:05 +00:00
|
|
|
:param mac_address: The mac address
|
|
|
|
|
2016-04-25 14:36:20 +00:00
|
|
|
:returns: Integer
|
|
|
|
"""
|
2016-05-28 05:00:05 +00:00
|
|
|
return int(mac_address.replace(":", ""), 16)
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def int_to_macaddress(integer):
|
|
|
|
"""
|
2016-05-28 05:00:05 +00:00
|
|
|
Convert an integer to a mac address
|
2016-04-25 14:36:20 +00:00
|
|
|
"""
|
|
|
|
return ":".join(textwrap.wrap("%012x" % (integer), width=2))
|
2016-05-02 15:13:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_version(version):
|
|
|
|
"""
|
2016-05-06 15:03:28 +00:00
|
|
|
Return a comparable tuple from a version string. We try to force tuple to semver with version like 1.2.0
|
2016-05-02 15:13:23 +00:00
|
|
|
|
|
|
|
Replace pkg_resources.parse_version which now display a warning when use for comparing version with tuple
|
|
|
|
|
|
|
|
:returns: Version string as comparable tuple
|
|
|
|
"""
|
|
|
|
|
|
|
|
release_type_found = False
|
2024-04-23 09:52:28 +00:00
|
|
|
version_infos = re.split(r'(\.|[a-z]+)', version)
|
2016-05-02 15:13:23 +00:00
|
|
|
version = []
|
|
|
|
for info in version_infos:
|
|
|
|
if info == '.' or len(info) == 0:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
info = int(info)
|
2016-05-06 15:03:28 +00:00
|
|
|
# We pad with zero to compare only on string
|
|
|
|
# This avoid issue when comparing version with different length
|
|
|
|
version.append("%06d" % (info,))
|
2016-05-02 15:13:23 +00:00
|
|
|
except ValueError:
|
2016-05-06 15:03:28 +00:00
|
|
|
# Force to a version with three number
|
|
|
|
if len(version) == 1:
|
|
|
|
version.append("00000")
|
|
|
|
if len(version) == 2:
|
|
|
|
version.append("000000")
|
2016-05-18 09:23:45 +00:00
|
|
|
# We want rc to be at lower level than dev version
|
2016-05-02 15:13:23 +00:00
|
|
|
if info == 'rc':
|
|
|
|
info = 'c'
|
|
|
|
version.append(info)
|
|
|
|
release_type_found = True
|
|
|
|
if release_type_found is False:
|
2016-05-06 15:03:28 +00:00
|
|
|
# Force to a version with three number
|
|
|
|
if len(version) == 1:
|
|
|
|
version.append("00000")
|
|
|
|
if len(version) == 2:
|
|
|
|
version.append("000000")
|
2016-05-02 15:13:23 +00:00
|
|
|
version.append("final")
|
|
|
|
return tuple(version)
|
2019-10-09 09:02:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def shlex_quote(s):
|
|
|
|
"""
|
|
|
|
Compatible shlex_quote to handle case where Windows needs double quotes around file names, not single quotes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"')
|
|
|
|
else:
|
|
|
|
return shlex.quote(s)
|
2024-06-17 14:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_ipv6_enabled() -> bool:
|
|
|
|
|
|
|
|
if not socket.has_ipv6:
|
|
|
|
return False # the socket library has no support for IPv6
|
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.bind(("::1", 0))
|
|
|
|
return True
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno in (errno.EADDRNOTAVAIL, errno.EAFNOSUPPORT):
|
|
|
|
# EADDRNOTAVAIL is the errno if IPv6 modules/drivers are loaded but disabled.
|
|
|
|
# EAFNOSUPPORT is the errno if IPv6 modules/drivers are not loaded at all.
|
|
|
|
return False
|
|
|
|
if e.errno == errno.EADDRINUSE:
|
|
|
|
return True
|
|
|
|
raise
|