diff --git a/contrib/inventory/apache-libcloud.py b/contrib/inventory/apache-libcloud.py index 92f799895f..7d6bba2515 100755 --- a/contrib/inventory/apache-libcloud.py +++ b/contrib/inventory/apache-libcloud.py @@ -327,7 +327,7 @@ class LibcloudInventory(object): used as Ansible groups ''' - return re.sub("[^A-Za-z0-9\-]", "_", word) + return re.sub(r"[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): ''' diff --git a/contrib/inventory/apstra_aos.py b/contrib/inventory/apstra_aos.py index e8752183cd..2ffd3290f0 100755 --- a/contrib/inventory/apstra_aos.py +++ b/contrib/inventory/apstra_aos.py @@ -572,7 +572,7 @@ class AosInventory(object): - Converting to lowercase """ - rx = re.compile('\W+') + rx = re.compile(r'\W+') clean_group = rx.sub('_', group_name).lower() return clean_group diff --git a/contrib/inventory/azure_rm.py b/contrib/inventory/azure_rm.py index 6c79d8b9a2..d790a0a02f 100755 --- a/contrib/inventory/azure_rm.py +++ b/contrib/inventory/azure_rm.py @@ -842,9 +842,9 @@ class AzureInventory(object): def _to_safe(self, word): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' - regex = "[^A-Za-z0-9\_" + regex = r"[^A-Za-z0-9\_" if not self.replace_dash_in_groups: - regex += "\-" + regex += r"\-" return re.sub(regex + "]", "_", word) diff --git a/contrib/inventory/cloudforms.py b/contrib/inventory/cloudforms.py index 69c149bfc5..ecf10a4119 100755 --- a/contrib/inventory/cloudforms.py +++ b/contrib/inventory/cloudforms.py @@ -444,7 +444,7 @@ class CloudFormsInventory(object): Converts 'bad' characters in a string to underscores so they can be used as Ansible groups """ if self.cloudforms_clean_group_keys: - regex = "[^A-Za-z0-9\_]" + regex = r"[^A-Za-z0-9\_]" return re.sub(regex, "_", word.replace(" ", "")) else: return word diff --git a/contrib/inventory/cobbler.py b/contrib/inventory/cobbler.py index 896fc73588..0443bb3c8c 100755 --- a/contrib/inventory/cobbler.py +++ b/contrib/inventory/cobbler.py @@ -271,7 +271,7 @@ class CobblerInventory(object): def to_safe(self, word): """ Converts 'bad' characters in a string to underscores so they can be used as Ansible groups """ - return re.sub("[^A-Za-z0-9\-]", "_", word) + return re.sub(r"[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): """ Converts a dict to a JSON object and dumps it as a formatted string """ diff --git a/contrib/inventory/collins.py b/contrib/inventory/collins.py index bcba11cc1c..a6af0d7302 100755 --- a/contrib/inventory/collins.py +++ b/contrib/inventory/collins.py @@ -423,7 +423,7 @@ class CollinsInventory(object): """ Converts 'bad' characters in a string to underscores so they can be used as Ansible groups """ - return re.sub("[^A-Za-z0-9\-]", "_", word) + return re.sub(r"[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): """ Converts a dict to a JSON object and dumps it as a formatted string """ diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 8f3a104fdf..c0c2b4e127 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -394,7 +394,7 @@ class ConsulInventory(object): def to_safe(self, word): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' - return re.sub('[^A-Za-z0-9\-\.]', '_', word) + return re.sub(r'[^A-Za-z0-9\-\.]', '_', word) def sanitize_dict(self, d): diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index a53a7df937..23fb2c6259 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -460,7 +460,7 @@ or environment variables (DO_API_TOKEN)\n''') def to_safe(self, word): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' - return re.sub("[^A-Za-z0-9\-\.]", "_", word) + return re.sub(r"[^A-Za-z0-9\-\.]", "_", word) def do_namespace(self, data): ''' Returns a copy of the dictionary with all the keys put in a 'do_' namespace ''' diff --git a/contrib/inventory/docker.py b/contrib/inventory/docker.py index 461e0d6817..24986e2d58 100755 --- a/contrib/inventory/docker.py +++ b/contrib/inventory/docker.py @@ -656,7 +656,7 @@ class DockerInventory(object): self.hostvars[name].update(facts) def _slugify(self, value): - return 'docker_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_')) + return 'docker_%s' % (re.sub(r'[^\w-]', '_', value).lower().lstrip('_')) def get_hosts(self, config): ''' diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 825a1732db..42cc7a58c4 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -1680,9 +1680,9 @@ class Ec2Inventory(object): def to_safe(self, word): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' - regex = "[^A-Za-z0-9\_" + regex = r"[^A-Za-z0-9\_" if not self.replace_dash_in_groups: - regex += "\-" + regex += r"\-" return re.sub(regex + "]", "_", word) def json_format_dict(self, data, pretty=False): diff --git a/contrib/inventory/fleet.py b/contrib/inventory/fleet.py index cf8f647669..e749aa6905 100755 --- a/contrib/inventory/fleet.py +++ b/contrib/inventory/fleet.py @@ -61,7 +61,7 @@ def get_ssh_config(): def list_running_boxes(): boxes = [] for line in subprocess.check_output(["fleetctl", "list-machines"]).split('\n'): - matcher = re.search("[^\s]+[\s]+([^\s]+).+", line) + matcher = re.search(r"[^\s]+[\s]+([^\s]+).+", line) if matcher and matcher.group(1) != "IP": boxes.append(matcher.group(1)) diff --git a/contrib/inventory/foreman.py b/contrib/inventory/foreman.py index b21ca11be8..a83c094d95 100755 --- a/contrib/inventory/foreman.py +++ b/contrib/inventory/foreman.py @@ -264,7 +264,7 @@ class ForemanInventory(object): >>> ForemanInventory.to_safe("foo-bar baz") 'foo_barbaz' ''' - regex = "[^A-Za-z0-9\_]" + regex = r"[^A-Za-z0-9\_]" return re.sub(regex, "_", word.replace(" ", "")) def update_cache(self): diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py index 0784b35d7b..0472ecfce0 100755 --- a/contrib/inventory/linode.py +++ b/contrib/inventory/linode.py @@ -338,7 +338,7 @@ class LinodeInventory(object): def to_safe(self, word): """Escapes any characters that would be invalid in an ansible group name.""" - return re.sub("[^A-Za-z0-9\-]", "_", word) + return re.sub(r"[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): """Converts a dict to a JSON object and dumps it as a formatted string.""" diff --git a/contrib/inventory/packet_net.py b/contrib/inventory/packet_net.py index 583ba04cf0..0b1a1f5730 100755 --- a/contrib/inventory/packet_net.py +++ b/contrib/inventory/packet_net.py @@ -480,9 +480,9 @@ class PacketInventory(object): def to_safe(self, word): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups ''' - regex = "[^A-Za-z0-9\_" + regex = r"[^A-Za-z0-9\_" if not self.replace_dash_in_groups: - regex += "\-" + regex += r"\-" return re.sub(regex + "]", "_", word) def json_format_dict(self, data, pretty=False): diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index a9e1be054b..0d0c7b63fd 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -189,7 +189,7 @@ p = load_config_file() def rax_slugify(value): - return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_')) + return 'rax_%s' % (re.sub(r'[^\w-]', '_', value).lower().lstrip('_')) def to_dict(obj): diff --git a/contrib/inventory/rudder.py b/contrib/inventory/rudder.py index 50f1caeaaa..30cab1a21d 100755 --- a/contrib/inventory/rudder.py +++ b/contrib/inventory/rudder.py @@ -292,7 +292,7 @@ class RudderInventory(object): ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible variable names ''' - return re.sub('[^A-Za-z0-9\_]', '_', word) + return re.sub(r'[^A-Za-z0-9\_]', '_', word) # Run the script RudderInventory() diff --git a/contrib/inventory/softlayer.py b/contrib/inventory/softlayer.py index b1e915f74a..4bece43475 100755 --- a/contrib/inventory/softlayer.py +++ b/contrib/inventory/softlayer.py @@ -91,7 +91,7 @@ class SoftLayerInventory(object): def to_safe(self, word): '''Converts 'bad' characters in a string to underscores so they can be used as Ansible groups''' - return re.sub("[^A-Za-z0-9\-\.]", "_", word) + return re.sub(r"[^A-Za-z0-9\-\.]", "_", word) def push(self, my_dict, key, element): '''Push an element onto an array that may not have been defined in the dict''' diff --git a/contrib/inventory/vagrant.py b/contrib/inventory/vagrant.py index 37a6741b09..c6e0ff3b75 100755 --- a/contrib/inventory/vagrant.py +++ b/contrib/inventory/vagrant.py @@ -79,7 +79,7 @@ def list_running_boxes(): boxes = [] for line in output: - matcher = re.search("([^\s]+)[\s]+running \(.+", line) + matcher = re.search(r"([^\s]+)[\s]+running \(.+", line) if matcher: boxes.append(matcher.group(1)) diff --git a/contrib/inventory/windows_azure.py b/contrib/inventory/windows_azure.py index 5398b54032..2e32e75204 100755 --- a/contrib/inventory/windows_azure.py +++ b/contrib/inventory/windows_azure.py @@ -271,7 +271,7 @@ class AzureInventory(object): def to_safe(self, word): """Escapes any characters that would be invalid in an ansible group name.""" - return re.sub("[^A-Za-z0-9\-]", "_", word) + return re.sub(r"[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): """Converts a dict to a JSON object and dumps it as a formatted string.""" diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index d23c95acda..ea8de8dbb8 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -627,9 +627,9 @@ def _find_module_utils(module_name, b_module_data, module_path, module_args, tas module_style = 'new' module_substyle = 'powershell' b_module_data = b_module_data.replace(REPLACER_WINDOWS, b'#Requires -Module Ansible.ModuleUtils.Legacy') - elif re.search(b'#Requires \-Module', b_module_data, re.IGNORECASE) \ - or re.search(b'#Requires \-Version', b_module_data, re.IGNORECASE)\ - or re.search(b'#AnsibleRequires \-OSVersion', b_module_data, re.IGNORECASE): + elif re.search(b'#Requires -Module', b_module_data, re.IGNORECASE) \ + or re.search(b'#Requires -Version', b_module_data, re.IGNORECASE)\ + or re.search(b'#AnsibleRequires -OSVersion', b_module_data, re.IGNORECASE): module_style = 'new' module_substyle = 'powershell' elif REPLACER_JSONARGS in b_module_data: @@ -796,9 +796,9 @@ def _find_module_utils(module_name, b_module_data, module_path, module_args, tas min_ps_version = None requires_module_list = re.compile(to_bytes(r'(?i)^#\s*requires\s+\-module(?:s?)\s*(Ansible\.ModuleUtils\..+)')) - requires_ps_version = re.compile(to_bytes('(?i)^#requires\s+\-version\s+([0-9]+(\.[0-9]+){0,3})$')) - requires_os_version = re.compile(to_bytes('(?i)^#ansiblerequires\s+\-osversion\s+([0-9]+(\.[0-9]+){0,3})$')) - requires_become = re.compile(to_bytes('(?i)^#ansiblerequires\s+\-become$')) + requires_ps_version = re.compile(to_bytes(r'(?i)^#requires\s+\-version\s+([0-9]+(\.[0-9]+){0,3})$')) + requires_os_version = re.compile(to_bytes(r'(?i)^#ansiblerequires\s+\-osversion\s+([0-9]+(\.[0-9]+){0,3})$')) + requires_become = re.compile(to_bytes(r'(?i)^#ansiblerequires\s+\-become$')) for line in lines: module_util_line_match = requires_module_list.match(line) diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index aab983c27d..caac03a68d 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -39,7 +39,7 @@ except ImportError: from ansible.utils.display import Display display = Display() -IGNORED_ALWAYS = [b"^\.", b"^host_vars$", b"^group_vars$", b"^vars_plugins$"] +IGNORED_ALWAYS = [br"^\.", b"^host_vars$", b"^group_vars$", b"^vars_plugins$"] IGNORED_PATTERNS = [to_bytes(x) for x in C.INVENTORY_IGNORE_PATTERNS] IGNORED_EXTS = [b'%s$' % to_bytes(re.escape(x)) for x in C.INVENTORY_IGNORE_EXTS] diff --git a/lib/ansible/module_utils/azure_rm_common.py b/lib/ansible/module_utils/azure_rm_common.py index f40ddfcc8e..56a8ee4ebc 100644 --- a/lib/ansible/module_utils/azure_rm_common.py +++ b/lib/ansible/module_utils/azure_rm_common.py @@ -71,8 +71,8 @@ AZURE_COMMON_REQUIRED_IF = [ ANSIBLE_USER_AGENT = 'Ansible/{0}'.format(ANSIBLE_VERSION) CLOUDSHELL_USER_AGENT_KEY = 'AZURE_HTTP_USER_AGENT' -CIDR_PATTERN = re.compile("(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1" - "[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))") +CIDR_PATTERN = re.compile(r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1" + r"[0-9]{2}|2[0-4][0-9]|25[0-5])(/([0-9]|[1-2][0-9]|3[0-2]))") AZURE_SUCCESS_STATE = "Succeeded" AZURE_FAILED_STATE = "Failed" diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 3e32ed9301..dfdd2df7d5 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -586,7 +586,7 @@ def human_to_bytes(number, default_unit=None, isbits=False): ex: human_to_bytes('10M') <=> human_to_bytes(10, 'M') ''' - m = re.search('^\s*(\d*\.?\d*)\s*([A-Za-z]+)?', str(number), flags=re.IGNORECASE) + m = re.search(r'^\s*(\d*\.?\d*)\s*([A-Za-z]+)?', str(number), flags=re.IGNORECASE) if m is None: raise ValueError("human_to_bytes() can't interpret following string: %s" % str(number)) try: diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py index 805e4ed907..37e1e0fbfa 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -79,7 +79,7 @@ DOCKER_REQUIRED_TOGETHER = [ ] DEFAULT_DOCKER_REGISTRY = 'https://index.docker.io/v1/' -EMAIL_REGEX = '[^@]+@[^@]+\.[^@]+' +EMAIL_REGEX = r'[^@]+@[^@]+\.[^@]+' BYTE_SUFFIXES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] diff --git a/lib/ansible/module_utils/facts/hardware/freebsd.py b/lib/ansible/module_utils/facts/hardware/freebsd.py index ee894b01e0..0e7002b486 100644 --- a/lib/ansible/module_utils/facts/hardware/freebsd.py +++ b/lib/ansible/module_utils/facts/hardware/freebsd.py @@ -136,8 +136,8 @@ class FreeBSDHardware(Hardware): sysdir = '/dev' device_facts['devices'] = {} - drives = re.compile('(ada?\d+|da\d+|a?cd\d+)') # TODO: rc, disks, err = self.module.run_command("/sbin/sysctl kern.disks") - slices = re.compile('(ada?\d+s\d+\w*|da\d+s\d+\w*)') + drives = re.compile(r'(ada?\d+|da\d+|a?cd\d+)') # TODO: rc, disks, err = self.module.run_command("/sbin/sysctl kern.disks") + slices = re.compile(r'(ada?\d+s\d+\w*|da\d+s\d+\w*)') if os.path.isdir(sysdir): dirlist = sorted(os.listdir(sysdir)) for device in dirlist: diff --git a/lib/ansible/module_utils/facts/hardware/hpux.py b/lib/ansible/module_utils/facts/hardware/hpux.py index b56302c3dc..e57480c0ce 100644 --- a/lib/ansible/module_utils/facts/hardware/hpux.py +++ b/lib/ansible/module_utils/facts/hardware/hpux.py @@ -127,7 +127,7 @@ class HPUXHardware(Hardware): memory_facts['memtotal_mb'] = int(data) / 256 else: rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep Memory", use_unsafe_shell=True) - data = re.search('Memory[\ :=]*([0-9]*).*MB.*', out).groups()[0].strip() + data = re.search(r'Memory[\ :=]*([0-9]*).*MB.*', out).groups()[0].strip() memory_facts['memtotal_mb'] = int(data) rc, out, err = self.module.run_command("/usr/sbin/swapinfo -m -d -f -q") memory_facts['swaptotal_mb'] = int(out.strip()) diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py index eb1d0fc914..32fc1c0a4c 100644 --- a/lib/ansible/module_utils/facts/hardware/linux.py +++ b/lib/ansible/module_utils/facts/hardware/linux.py @@ -568,7 +568,7 @@ class LinuxHardware(Hardware): device = "/dev/%s" % (block) rc, drivedata, err = self.module.run_command([sg_inq, device]) if rc == 0: - serial = re.search("Unit serial number:\s+(\w+)", drivedata) + serial = re.search(r"Unit serial number:\s+(\w+)", drivedata) if serial: d['serial'] = serial.group(1) @@ -585,7 +585,7 @@ class LinuxHardware(Hardware): d['partitions'] = {} for folder in os.listdir(sysdir): - m = re.search("(" + diskname + "\d+)", folder) + m = re.search("(" + diskname + r"\d+)", folder) if m: part = {} partname = m.group(1) @@ -611,7 +611,7 @@ class LinuxHardware(Hardware): d['scheduler_mode'] = "" scheduler = get_file_content(sysdir + "/queue/scheduler") if scheduler is not None: - m = re.match(".*?(\[(.*)\])", scheduler) + m = re.match(r".*?(\[(.*)\])", scheduler) if m: d['scheduler_mode'] = m.group(2) @@ -626,11 +626,11 @@ class LinuxHardware(Hardware): d['host'] = "" # domains are numbered (0 to ffff), bus (0 to ff), slot (0 to 1f), and function (0 to 7). - m = re.match(".+/([a-f0-9]{4}:[a-f0-9]{2}:[0|1][a-f0-9]\.[0-7])/", sysdir) + m = re.match(r".+/([a-f0-9]{4}:[a-f0-9]{2}:[0|1][a-f0-9]\.[0-7])/", sysdir) if m and pcidata: pciid = m.group(1) did = re.escape(pciid) - m = re.search("^" + did + "\s(.*)$", pcidata, re.MULTILINE) + m = re.search("^" + did + r"\s(.*)$", pcidata, re.MULTILINE) if m: d['host'] = m.group(1) diff --git a/lib/ansible/module_utils/facts/network/aix.py b/lib/ansible/module_utils/facts/network/aix.py index 0ea53d3ca3..46bbb68f67 100644 --- a/lib/ansible/module_utils/facts/network/aix.py +++ b/lib/ansible/module_utils/facts/network/aix.py @@ -73,7 +73,7 @@ class AIXNetwork(GenericBsdIfconfigNetwork): words = line.split() # only this condition differs from GenericBsdIfconfigNetwork - if re.match('^\w*\d*:', line): + if re.match(r'^\w*\d*:', line): current_if = self.parse_interface_line(words) interfaces[current_if['device']] = current_if elif words[0].startswith('options='): diff --git a/lib/ansible/module_utils/facts/network/generic_bsd.py b/lib/ansible/module_utils/facts/network/generic_bsd.py index dab3004822..3be10be25e 100644 --- a/lib/ansible/module_utils/facts/network/generic_bsd.py +++ b/lib/ansible/module_utils/facts/network/generic_bsd.py @@ -121,7 +121,7 @@ class GenericBsdIfconfigNetwork(Network): if words[0] == 'pass': continue - elif re.match('^\S', line) and len(words) > 3: + elif re.match(r'^\S', line) and len(words) > 3: current_if = self.parse_interface_line(words) interfaces[current_if['device']] = current_if elif words[0].startswith('options='): diff --git a/lib/ansible/module_utils/facts/network/linux.py b/lib/ansible/module_utils/facts/network/linux.py index deb69883f6..95e3bfd5ff 100644 --- a/lib/ansible/module_utils/facts/network/linux.py +++ b/lib/ansible/module_utils/facts/network/linux.py @@ -297,9 +297,9 @@ class LinuxNetwork(Network): args = [ethtool_path, '-T', device] rc, stdout, stderr = self.module.run_command(args, errors='surrogate_then_replace') if rc == 0: - data['timestamping'] = [m.lower() for m in re.findall('SOF_TIMESTAMPING_(\w+)', stdout)] - data['hw_timestamp_filters'] = [m.lower() for m in re.findall('HWTSTAMP_FILTER_(\w+)', stdout)] - m = re.search('PTP Hardware Clock: (\d+)', stdout) + data['timestamping'] = [m.lower() for m in re.findall(r'SOF_TIMESTAMPING_(\w+)', stdout)] + data['hw_timestamp_filters'] = [m.lower() for m in re.findall(r'HWTSTAMP_FILTER_(\w+)', stdout)] + m = re.search(r'PTP Hardware Clock: (\d+)', stdout) if m: data['phc_index'] = int(m.groups()[0]) diff --git a/lib/ansible/module_utils/facts/network/sunos.py b/lib/ansible/module_utils/facts/network/sunos.py index dec5888b81..adba14c684 100644 --- a/lib/ansible/module_utils/facts/network/sunos.py +++ b/lib/ansible/module_utils/facts/network/sunos.py @@ -50,7 +50,7 @@ class SunOSNetwork(GenericBsdIfconfigNetwork): if line: words = line.split() - if re.match('^\S', line) and len(words) > 3: + if re.match(r'^\S', line) and len(words) > 3: current_if = self.parse_interface_line(words, current_if, interfaces) interfaces[current_if['device']] = current_if elif words[0].startswith('options='): diff --git a/lib/ansible/module_utils/facts/sysctl.py b/lib/ansible/module_utils/facts/sysctl.py index 2446dab2f7..2b2ca43b5b 100644 --- a/lib/ansible/module_utils/facts/sysctl.py +++ b/lib/ansible/module_utils/facts/sysctl.py @@ -29,7 +29,7 @@ def get_sysctl(module, prefixes): for line in out.splitlines(): if not line: continue - (key, value) = re.split('\s?=\s?|: ', line, maxsplit=1) + (key, value) = re.split(r'\s?=\s?|: ', line, maxsplit=1) sysctl[key] = value.strip() return sysctl diff --git a/lib/ansible/module_utils/facts/system/distribution.py b/lib/ansible/module_utils/facts/system/distribution.py index 48314baea0..3ba448d080 100644 --- a/lib/ansible/module_utils/facts/system/distribution.py +++ b/lib/ansible/module_utils/facts/system/distribution.py @@ -208,7 +208,7 @@ class DistributionFiles: if 'Slackware' not in data: return False, slackware_facts # TODO: remove slackware_facts['distribution'] = name - version = re.findall('\w+[.]\w+', data) + version = re.findall(r'\w+[.]\w+', data) if version: slackware_facts['distribution_version'] = version[0] return True, slackware_facts @@ -251,16 +251,16 @@ class DistributionFiles: if distribution: suse_facts['distribution'] = distribution.group(1).strip('"') # example pattern are 13.04 13.0 13 - distribution_version = re.search('^VERSION_ID="?([0-9]+\.?[0-9]*)"?', line) + distribution_version = re.search(r'^VERSION_ID="?([0-9]+\.?[0-9]*)"?', line) if distribution_version: suse_facts['distribution_version'] = distribution_version.group(1) if 'open' in data.lower(): - release = re.search('^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) + release = re.search(r'^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) if release: suse_facts['distribution_release'] = release.groups()[0] elif 'enterprise' in data.lower() and 'VERSION_ID' in line: # SLES doesn't got funny release names - release = re.search('^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) + release = re.search(r'^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) if release.group(1): release = release.group(1) else: @@ -294,7 +294,7 @@ class DistributionFiles: debian_facts = {} if 'Debian' in data or 'Raspbian' in data: debian_facts['distribution'] = 'Debian' - release = re.search("PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data) + release = re.search(r"PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data) if release: debian_facts['distribution_release'] = release.groups()[0] @@ -475,8 +475,8 @@ class Distribution(object): def get_distribution_HPUX(self): hpux_facts = {} - rc, out, err = self.module.run_command("/usr/sbin/swlist |egrep 'HPUX.*OE.*[AB].[0-9]+\.[0-9]+'", use_unsafe_shell=True) - data = re.search('HPUX.*OE.*([AB].[0-9]+\.[0-9]+)\.([0-9]+).*', out) + rc, out, err = self.module.run_command(r"/usr/sbin/swlist |egrep 'HPUX.*OE.*[AB].[0-9]+\.[0-9]+'", use_unsafe_shell=True) + data = re.search(r'HPUX.*OE.*([AB].[0-9]+\.[0-9]+)\.([0-9]+).*', out) if data: hpux_facts['distribution_version'] = data.groups()[0] hpux_facts['distribution_release'] = data.groups()[1] @@ -495,7 +495,7 @@ class Distribution(object): def get_distribution_FreeBSD(self): freebsd_facts = {} freebsd_facts['distribution_release'] = platform.release() - data = re.search('(\d+)\.(\d+)-RELEASE.*', freebsd_facts['distribution_release']) + data = re.search(r'(\d+)\.(\d+)-RELEASE.*', freebsd_facts['distribution_release']) if data: freebsd_facts['distribution_major_version'] = data.group(1) freebsd_facts['distribution_version'] = '%s.%s' % (data.group(1), data.group(2)) @@ -505,7 +505,7 @@ class Distribution(object): openbsd_facts = {} openbsd_facts['distribution_version'] = platform.release() rc, out, err = self.module.run_command("/sbin/sysctl -n kern.version") - match = re.match('OpenBSD\s[0-9]+.[0-9]+-(\S+)\s.*', out) + match = re.match(r'OpenBSD\s[0-9]+.[0-9]+-(\S+)\s.*', out) if match: openbsd_facts['distribution_release'] = match.groups()[0] else: diff --git a/lib/ansible/module_utils/facts/virtual/linux.py b/lib/ansible/module_utils/facts/virtual/linux.py index 9ce9132078..68f3cfe0a2 100644 --- a/lib/ansible/module_utils/facts/virtual/linux.py +++ b/lib/ansible/module_utils/facts/virtual/linux.py @@ -144,9 +144,9 @@ class LinuxVirtual(Virtual): if os.path.exists('/proc/self/status'): for line in get_file_lines('/proc/self/status'): - if re.match('^VxID: \d+', line): + if re.match(r'^VxID: \d+', line): virtual_facts['virtualization_type'] = 'linux_vserver' - if re.match('^VxID: 0', line): + if re.match(r'^VxID: 0', line): virtual_facts['virtualization_role'] = 'host' else: virtual_facts['virtualization_role'] = 'guest' diff --git a/lib/ansible/module_utils/known_hosts.py b/lib/ansible/module_utils/known_hosts.py index defdc11b7f..5e04a6b216 100644 --- a/lib/ansible/module_utils/known_hosts.py +++ b/lib/ansible/module_utils/known_hosts.py @@ -58,7 +58,7 @@ def get_fqdn_and_port(repo_url): fqdn = None port = None - ipv6_re = re.compile('(\[[^]]*\])(?::([0-9]+))?') + ipv6_re = re.compile(r'(\[[^]]*\])(?::([0-9]+))?') if "@" in repo_url and "://" not in repo_url: # most likely an user@host:path or user@host/path type URL repo_url = repo_url.split("@", 1)[1] diff --git a/lib/ansible/module_utils/netcfg.py b/lib/ansible/module_utils/netcfg.py index 5f0896df6e..0b1742ef11 100644 --- a/lib/ansible/module_utils/netcfg.py +++ b/lib/ansible/module_utils/netcfg.py @@ -35,9 +35,9 @@ from ansible.module_utils.network_common import to_list DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/', 'echo'] DEFAULT_IGNORE_LINES_RE = set([ - re.compile("Using \d+ out of \d+ bytes"), - re.compile("Building configuration"), - re.compile("Current configuration : \d+ bytes") + re.compile(r"Using \d+ out of \d+ bytes"), + re.compile(r"Building configuration"), + re.compile(r"Current configuration : \d+ bytes") ]) diff --git a/lib/ansible/module_utils/network_common.py b/lib/ansible/module_utils/network_common.py index 8bbe0e40a0..cf3faaf91b 100644 --- a/lib/ansible/module_utils/network_common.py +++ b/lib/ansible/module_utils/network_common.py @@ -305,7 +305,7 @@ def dict_merge(base, other): def conditional(expr, val, cast=None): - match = re.match('^(.+)\((.+)\)$', str(expr), re.I) + match = re.match(r'^(.+)\((.+)\)$', str(expr), re.I) if match: op, arg = match.groups() else: diff --git a/lib/ansible/module_utils/rax.py b/lib/ansible/module_utils/rax.py index 5c7ea66927..ea32de316e 100644 --- a/lib/ansible/module_utils/rax.py +++ b/lib/ansible/module_utils/rax.py @@ -51,7 +51,7 @@ SERVICE_NET_ID = "11111111-1111-1111-1111-111111111111" def rax_slugify(value): """Prepend a key with rax_ and normalize the key name""" - return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_')) + return 'rax_%s' % (re.sub(r'[^\w-]', '_', value).lower().lstrip('_')) def rax_clb_node_to_dict(obj): diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py index 1d37166fcd..636abe656f 100644 --- a/lib/ansible/playbook/play_context.py +++ b/lib/ansible/playbook/play_context.py @@ -501,7 +501,7 @@ class PlayContext(Base): # passing code ref to examine prompt as simple string comparisson isn't good enough with su def detect_su_prompt(b_data): - b_password_string = b"|".join([b'(\w+\'s )?' + x for x in b_SU_PROMPT_LOCALIZATIONS]) + b_password_string = b"|".join([br'(\w+\'s )?' + x for x in b_SU_PROMPT_LOCALIZATIONS]) # Colon or unicode fullwidth colon b_password_string = b_password_string + to_bytes(u' ?(:|:) ?') b_SU_PROMPT_LOCALIZATIONS_RE = re.compile(b_password_string, flags=re.IGNORECASE) diff --git a/lib/ansible/plugins/callback/junit.py b/lib/ansible/plugins/callback/junit.py index 4948426fcb..58ca714631 100644 --- a/lib/ansible/plugins/callback/junit.py +++ b/lib/ansible/plugins/callback/junit.py @@ -171,7 +171,7 @@ class CallbackModule(CallbackBase): duration = host_data.finish - task_data.start if self._task_class == 'true': - junit_classname = re.sub('\.yml:[0-9]+$', '', task_data.path) + junit_classname = re.sub(r'\.yml:[0-9]+$', '', task_data.path) else: junit_classname = task_data.path diff --git a/lib/ansible/plugins/connection/docker.py b/lib/ansible/plugins/connection/docker.py index bf7d629960..a05de0294d 100644 --- a/lib/ansible/plugins/connection/docker.py +++ b/lib/ansible/plugins/connection/docker.py @@ -115,7 +115,7 @@ class Connection(ConnectionBase): @staticmethod def _sanitize_version(version): - return re.sub(u'[^0-9a-zA-Z\.]', u'', version) + return re.sub(u'[^0-9a-zA-Z.]', u'', version) def _old_docker_version(self): cmd_args = [] diff --git a/lib/ansible/plugins/inventory/__init__.py b/lib/ansible/plugins/inventory/__init__.py index 5d2600e57e..06898736db 100644 --- a/lib/ansible/plugins/inventory/__init__.py +++ b/lib/ansible/plugins/inventory/__init__.py @@ -39,7 +39,7 @@ except ImportError: from ansible.utils.display import Display display = Display() -_SAFE_GROUP = re.compile("[^A-Za-z0-9\_]") +_SAFE_GROUP = re.compile("[^A-Za-z0-9_]") # Helper methods diff --git a/lib/ansible/utils/module_docs_fragments/netapp.py b/lib/ansible/utils/module_docs_fragments/netapp.py index 8737214332..2a71b0c06d 100644 --- a/lib/ansible/utils/module_docs_fragments/netapp.py +++ b/lib/ansible/utils/module_docs_fragments/netapp.py @@ -51,7 +51,7 @@ requirements: - netapp-lib (2015.9.25). Install using 'pip install netapp-lib' notes: - - The modules prefixed with C(netapp\_cdot) are built to support the ONTAP storage platform. + - The modules prefixed with C(netapp\\_cdot) are built to support the ONTAP storage platform. """ @@ -75,7 +75,7 @@ requirements: - solidfire-sdk-python (1.1.0.92) notes: - - The modules prefixed with C(sf\_) are built to support the SolidFire storage platform. + - The modules prefixed with C(sf\\_) are built to support the SolidFire storage platform. """ diff --git a/test/sanity/validate-modules/main.py b/test/sanity/validate-modules/main.py index 66cb8ebe78..f3f41df35a 100755 --- a/test/sanity/validate-modules/main.py +++ b/test/sanity/validate-modules/main.py @@ -70,7 +70,7 @@ BLACKLIST_IMPORTS = { 'ansible.module_utils.urls instead') } }, - 'boto(?:\.|$)': { + r'boto(?:\.|$)': { 'new_only': True, 'error': { 'code': 204, diff --git a/test/units/module_utils/facts/test_collector.py b/test/units/module_utils/facts/test_collector.py index 4e2b0cef50..2fbcfe6f0e 100644 --- a/test/units/module_utils/facts/test_collector.py +++ b/test/units/module_utils/facts/test_collector.py @@ -269,7 +269,7 @@ class TestGetCollectorNames(unittest.TestCase): minimal_gather_subset = frozenset(['my_fact']) self.assertRaisesRegexp(TypeError, - 'Bad subset .* given to Ansible.*allowed\:.*all,.*my_fact.*', + r'Bad subset .* given to Ansible.*allowed\:.*all,.*my_fact.*', collector.get_collector_names, valid_subsets=valid_subsets, minimal_gather_subset=minimal_gather_subset, @@ -341,7 +341,7 @@ class TestCollectorClassesFromGatherSubset(unittest.TestCase): # something claims 'unknown_collector' is a valid gather_subset, but there is # no FactCollector mapped to 'unknown_collector' self.assertRaisesRegexp(TypeError, - 'Bad subset.*unknown_collector.*given to Ansible.*allowed\:.*all,.*env.*', + r'Bad subset.*unknown_collector.*given to Ansible.*allowed\:.*all,.*env.*', self._classes, all_collector_classes=default_collectors.collectors, gather_subset=['env', 'unknown_collector']) diff --git a/test/units/parsing/vault/test_vault.py b/test/units/parsing/vault/test_vault.py index 814bcce191..495d7e709f 100644 --- a/test/units/parsing/vault/test_vault.py +++ b/test/units/parsing/vault/test_vault.py @@ -285,7 +285,7 @@ class TestScriptVaultSecret(unittest.TestCase): with patch.object(secret, 'loader') as mock_loader: mock_loader.is_executable = MagicMock(return_value=True) self.assertRaisesRegexp(errors.AnsibleError, - 'Vault password script.*returned non-zero \(%s\): %s' % (rc, stderr), + r'Vault password script.*returned non-zero \(%s\): %s' % (rc, stderr), secret.load) diff --git a/test/units/playbook/test_conditional.py b/test/units/playbook/test_conditional.py index 5184186104..164cccd55c 100644 --- a/test/units/playbook/test_conditional.py +++ b/test/units/playbook/test_conditional.py @@ -99,7 +99,7 @@ class TestConditional(unittest.TestCase): when = [u"some_dict.some_dict_key1 == hostvars['host3']"] # self._eval_con(when, variables) self.assertRaisesRegexp(errors.AnsibleError, - "The conditional check 'some_dict.some_dict_key1 == hostvars\['host3'\]' failed", + r"The conditional check 'some_dict.some_dict_key1 == hostvars\['host3'\]' failed", # "The conditional check 'some_dict.some_dict_key1 == hostvars['host3']' failed", # "The conditional check 'some_dict.some_dict_key1 == hostvars['host3']' failed.", self._eval_con, diff --git a/test/units/template/test_templar.py b/test/units/template/test_templar.py index 13e957961d..d6642f7253 100644 --- a/test/units/template/test_templar.py +++ b/test/units/template/test_templar.py @@ -307,7 +307,7 @@ class TestTemplarMisc(BaseTemplar, unittest.TestCase): class TestTemplarLookup(BaseTemplar, unittest.TestCase): def test_lookup_missing_plugin(self): self.assertRaisesRegexp(AnsibleError, - 'lookup plugin \(not_a_real_lookup_plugin\) not found', + r'lookup plugin \(not_a_real_lookup_plugin\) not found', self.templar._lookup, 'not_a_real_lookup_plugin', 'an_arg', a_keyword_arg='a_keyword_arg_value')