Skip to content

Commit

Permalink
fix strings with escape sequences
Browse files Browse the repository at this point in the history
These are used as regex patterns and contain backslashes that python
tries to interpret as an escape sequence.

Starting with python3.12 this is recognized as a:

    SyntaxWarning: invalid escape sequence

This will eventually become a SyntaxError and will break the code.

As such, transforming these into a "raw string" by prepending them with
'r' is the correct solution.

Signed-off-by: Ondrej Lichtner <[email protected]>
  • Loading branch information
olichtne committed May 28, 2024
1 parent 139f55b commit ea2d588
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 92 deletions.
6 changes: 3 additions & 3 deletions lnst/Common/Colours.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def decorate_string(string, fg_colour=None, bg_colour=None, bold=False):
decorate_string(s, "red", "light-gray")
"""

extended_re = "^extended\(([0-9]+)\)$"
extended_re = r"^extended\(([0-9]+)\)$"

# parameters for the colouring functions (fg_num, bg_num)
# the bold flag is handled separately
Expand Down Expand Up @@ -161,7 +161,7 @@ def decorate_with_preset(string, preset):
return decorate_string(string, p[0], p[1], p[2])

def strip_colours(text):
return re.sub("\033\[[0-9]+(;[0-9]+){0,2}m", "", text)
return re.sub(r"\033\[[0-9]+(;[0-9]+){0,2}m", "", text)

def get_preset_conf(preset_name):
preset = PRESETS[preset_name]
Expand All @@ -174,7 +174,7 @@ def load_presets_from_config(lnst_config):
continue
fg, bg, bf = preset

extended_re = "^extended\([0-9]+\)$"
extended_re = r"^extended\([0-9]+\)$"

if fg == "default":
fg = None
Expand Down
4 changes: 2 additions & 2 deletions lnst/Common/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ def optionDirList(self, option, cfg_path):
return dirs

def optionTimeval(self, option, cfg_path):
timeval_re = "^(([0-9]+)days?)?\s*(([0-9]+)hours?)?\s*" \
"(([0-9]+)minutes?)?\s*(([0-9]+)seconds?)?$"
timeval_re = r"^(([0-9]+)days?)?\s*(([0-9]+)hours?)?\s*" \
r"(([0-9]+)minutes?)?\s*(([0-9]+)seconds?)?$"
timeval_match = re.match(timeval_re, option)
if timeval_match:
values = timeval_match.groups()
Expand Down
4 changes: 2 additions & 2 deletions lnst/Common/Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def type_check(self, value):
if not isinstance(value, str) or len(value) > 255:
raise ParamError("Value must be a valid hostname string")

hostname_re = ("^([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9])"
"(\.([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9]))*$")
hostname_re = (r"^([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9])"
r"(\.([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9]))*$")
if re.match(hostname_re, value, re.IGNORECASE):
return value
else:
Expand Down
8 changes: 4 additions & 4 deletions lnst/Common/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def kmod_in_use(modulename, tries = 1):
handle = open(mod_file, "r")
for line in handle:
match = re.match(r'^(\S+)\s\d+\s(\d+).*$', line)
if not match or not match.groups()[0] in re.split('\s+', modulename):
if not match or not match.groups()[0] in re.split(r'\s+', modulename):
continue
if int(match.groups()[1]) != 0:
ret = True
Expand All @@ -94,7 +94,7 @@ def kmod_loaded(modulename, tries = 1):
handle = open(mod_file, "r")
for line in handle:
match = re.match(r'^(\S+)\s\d+\s\d+\s\S+\s(Live|Loading|Unloading).*$', line)
if not match or not match.groups()[0] in re.split('\s+', modulename):
if not match or not match.groups()[0] in re.split(r'\s+', modulename):
continue
if match.groups()[1] == "Live":
ret = True
Expand All @@ -114,9 +114,9 @@ def int_it(val):

def bool_it(val):
if isinstance(val, str):
if re.match("^\s*(true|yes)", val, flags=re.IGNORECASE):
if re.match(r"^\s*(true|yes)", val, flags=re.IGNORECASE):
return True
elif re.match("^\s*(false|no)", val, flags=re.IGNORECASE):
elif re.match(r"^\s*(false|no)", val, flags=re.IGNORECASE):
return False
return True if int_it(val) else False

Expand Down
4 changes: 2 additions & 2 deletions lnst/Controller/AgentPoolManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def add_dir(self, pool_name, dir_path):
def add_file(self, pool_name, dir_path, dirent):
filepath = dir_path + "/" + dirent
pool = self._pools[pool_name]
if os.path.isfile(filepath) and re.search("\.xml$", filepath, re.I):
if os.path.isfile(filepath) and re.search(r"\.xml$", filepath, re.I):
dirname, basename = os.path.split(filepath)
m_id = re.sub("\.[xX][mM][lL]$", "", basename)
m_id = re.sub(r"\.[xX][mM][lL]$", "", basename)

parser = AgentMachineParser(filepath, self._ctl_config)
xml_data = parser.parse()
Expand Down
2 changes: 1 addition & 1 deletion lnst/Devices/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ def _read_coalescing_settings(self):
settings = {}
output, _ = exec_cmd("ethtool -c %s" % self.name, die_on_err=False)

regex = re.compile("^([a-z-]+):\s+(n/a|\d+)$")
regex = re.compile(r"^([a-z-]+):\s+(n/a|\d+)$")

for line in output.split('\n'):
if not (m := regex.match(line)):
Expand Down
2 changes: 1 addition & 1 deletion lnst/Recipes/ENRT/ConfigMixins/DevInterruptTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def pin_dev_interrupts(dev, cpus, policy=None):

def check_cpu_validity(host, cpus):
cpu_info = host.run("lscpu", job_level=ResultLevel.DEBUG).stdout
regex = "CPU\(s\): *([0-9]*)"
regex = r"CPU\(s\): *([0-9]*)"
num_cpus = int(re.search(regex, cpu_info).groups()[0])
for cpu in cpus:
if cpu < 0 or cpu > num_cpus - 1:
Expand Down
10 changes: 5 additions & 5 deletions lnst/Recipes/ENRT/GeneveLwtTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs = {}
if self.params.carrier_ipversion == "ipv4":
pa_kwargs["p_filter"] = "ip host {}".format(m1_carrier_ip)
grep_pattern = "IP "
grep_pattern = r"IP "
else:
pa_kwargs["p_filter"] = "ip6"
grep_pattern = "IP6 "
grep_pattern = r"IP6 "

grep_pattern += "{}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
grep_pattern += r"{}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
m1_carrier_ip, m2_carrier_ip
)

if isinstance(ip2, Ip4Address):
grep_pattern += "IP {} > {}: ICMP".format(ip1, ip2)
grep_pattern += r"IP {} > {}: ICMP".format(ip1, ip2)
elif isinstance(ip2, Ip6Address):
grep_pattern += "IP6 {} > {}: ICMP6".format(ip1, ip2)
grep_pattern += r"IP6 {} > {}: ICMP6".format(ip1, ip2)

pa_kwargs["grep_for"] = [grep_pattern]

Expand Down
6 changes: 3 additions & 3 deletions lnst/Recipes/ENRT/GeneveOvsTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs = {}
pa_kwargs["p_filter"] = "ip"

grep_pattern = "IP {}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
grep_pattern = r"IP {}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
m1_carrier_ip, m2_carrier_ip
)

if isinstance(ip2, Ip4Address):
grep_pattern += "IP {} > {}: ICMP".format(ip1, ip2)
grep_pattern += r"IP {} > {}: ICMP".format(ip1, ip2)
elif isinstance(ip2, Ip6Address):
grep_pattern += "IP6 {} > {}: ICMP6".format(ip1, ip2)
grep_pattern += r"IP6 {} > {}: ICMP6".format(ip1, ip2)

pa_kwargs["grep_for"] = [grep_pattern]

Expand Down
10 changes: 5 additions & 5 deletions lnst/Recipes/ENRT/GeneveTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,19 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs = {}
if self.params.carrier_ipversion == "ipv4":
pa_kwargs["p_filter"] = "ip host {}".format(m1_carrier_ip)
grep_pattern = "IP "
grep_pattern = r"IP "
else:
pa_kwargs["p_filter"] = "ip6"
grep_pattern = "IP6 "
grep_pattern = r"IP6 "

grep_pattern += "{}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
grep_pattern += r"{}\.[0-9]+ > {}\.[0-9]+: Geneve.*vni 0x4d2: ".format(
m1_carrier_ip, m2_carrier_ip
)

if isinstance(ip2, Ip4Address):
grep_pattern += "IP {} > {}: ICMP".format(ip1, ip2)
grep_pattern += r"IP {} > {}: ICMP".format(ip1, ip2)
elif isinstance(ip2, Ip6Address):
grep_pattern += "IP6 {} > {}: ICMP6".format(ip1, ip2)
grep_pattern += r"IP6 {} > {}: ICMP6".format(ip1, ip2)

pa_kwargs["grep_for"] = [grep_pattern]

Expand Down
12 changes: 6 additions & 6 deletions lnst/Recipes/ENRT/GreLwtTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
12 changes: 6 additions & 6 deletions lnst/Recipes/ENRT/GreOvsTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
14 changes: 7 additions & 7 deletions lnst/Recipes/ENRT/GreTunnelOverBondRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class GreTunnelOverBondRecipe(
MTUHWConfigMixin, PauseFramesHWConfigMixin, OffloadSubConfigMixin, BaseTunnelRecipe
):
"""
r"""
This class implements a recipe that configures a GRE tunnel between
two hosts that are connected through a bond device.
Expand Down Expand Up @@ -208,21 +208,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
12 changes: 6 additions & 6 deletions lnst/Recipes/ENRT/GreTunnelOverMacvlanRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
12 changes: 6 additions & 6 deletions lnst/Recipes/ENRT/GreTunnelOverVlanRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
12 changes: 6 additions & 6 deletions lnst/Recipes/ENRT/GreTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "proto gre"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}: GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = "{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
pat1 = r"{} > {}: GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}: GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
14 changes: 7 additions & 7 deletions lnst/Recipes/ENRT/Ip6GreNetnsTunnelRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class Ip6GreNetnsTunnelRecipe(
MTUHWConfigMixin, PauseFramesHWConfigMixin, OffloadSubConfigMixin, BaseTunnelRecipe
):
"""
r"""
This class implements a recipe that configures a IP6GRE tunnel between
two network namespaces on two hosts.
Expand Down Expand Up @@ -225,23 +225,23 @@ def get_packet_assert_config(self, ping_config):
pa_kwargs["p_filter"] = "ip6"

if isinstance(ip2, Ip4Address):
pat1 = "{} > {}:( DSTOPT)? GREv0, .* IP {} > {}: ICMP echo request".format(
pat1 = r"{} > {}:( DSTOPT)? GREv0, .* IP {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
pat2 = "{} > {}:( DSTOPT)? GREv0 \| {} > {}: ICMP echo request".format(
pat2 = r"{} > {}:( DSTOPT)? GREv0 \| {} > {}: ICMP echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
elif isinstance(ip2, Ip6Address):
pat1 = (
"{} > {}:( DSTOPT)? GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
r"{} > {}:( DSTOPT)? GREv0, .* IP6 {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
)
pat2 = "{} > {}:( DSTOPT)? GREv0 \| {} > {}: ICMP6, echo request".format(
pat2 = r"{} > {}:( DSTOPT)? GREv0 \| {} > {}: ICMP6, echo request".format(
m1_carrier_ip, m2_carrier_ip, ip1, ip2
)
grep_pattern = ["({})|({})".format(pat1, pat2)]
grep_pattern = [r"({})|({})".format(pat1, pat2)]
else:
raise Exception("The destination address is nor IPv4 or IPv6 address")

Expand Down
Loading

0 comments on commit ea2d588

Please sign in to comment.