From 4860f523d45df502e2d1005df3db4121c7795fe0 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Thu, 17 May 2018 18:47:16 +0200 Subject: [PATCH] Fix invalid escape sequence warnings Starting with Python 3.6, invalid escape sequences in string literals are now deprecated[1]. This influence also automatic style checkers like pycodestyle which starting with 2.4.0 complains about invalid escape sequences (W605)[2]. Let's fix all those warnings at once by using raw strings where possible and adding additional \ where not. Footnotes: 1 - https://docs.python.org/3/whatsnew/3.6.html#deprecated-python-behavior 2 - https://github.com/PyCQA/pycodestyle/pull/676 Change-Id: I009a366fd8342edfd30890df6fe8e1fca88bf3cc Signed-off-by: Krzysztof Opasiak --- integration/tests/examples/examples/client.py | 8 ++++---- .../examples/examples/example_generation.py | 8 ++++---- integration/tests/integration/tests/util/rpc.py | 2 +- .../tests/integration/tests/util/services.py | 6 +++--- tools/install_venv.py | 2 +- tools/trove-pylint.py | 3 ++- trove/common/apischema.py | 6 +++--- trove/common/db/cassandra/models.py | 2 +- trove/common/db/mongodb/models.py | 2 +- trove/common/db/mysql/models.py | 4 ++-- trove/common/wsgi.py | 8 ++++---- trove/guestagent/common/configuration.py | 2 +- trove/guestagent/common/guestagent_utils.py | 2 +- .../datastore/experimental/couchbase/system.py | 10 +++++----- .../datastore/experimental/db2/system.py | 4 ++-- .../datastore/experimental/postgresql/service.py | 2 +- .../datastore/experimental/vertica/system.py | 2 +- trove/guestagent/pkg.py | 6 +++--- .../backup/experimental/cassandra_impl.py | 4 ++-- .../backup/experimental/postgresql_impl.py | 4 ++-- trove/guestagent/strategies/backup/mysql_impl.py | 3 ++- .../restore/experimental/postgresql_impl.py | 4 ++-- trove/tests/api/mgmt/hosts.py | 4 ++-- trove/tests/fakes/guestagent.py | 2 +- .../guestagent/test_operating_system.py | 16 ++++++++-------- trove/tests/util/mysql.py | 10 +++++----- 26 files changed, 64 insertions(+), 62 deletions(-) diff --git a/integration/tests/examples/examples/client.py b/integration/tests/examples/examples/client.py index e52d1833d6..598e9bcd5f 100644 --- a/integration/tests/examples/examples/client.py +++ b/integration/tests/examples/examples/client.py @@ -72,12 +72,12 @@ class SnippetWriter(object): def _indent_xml(self, my_string): my_string = my_string.encode("utf-8") # convert to plain string without indents and spaces - my_re = re.compile('>\s+([^\s])', re.DOTALL) - my_string = myre.sub('>\g<1>', my_string) + my_re = re.compile(r'>\s+([^\s])', re.DOTALL) + my_string = myre.sub(r'>\g<1>', my_string) my_string = xml.dom.minidom.parseString(my_string).toprettyxml() # remove line breaks - my_re = re.compile('>\n\s+([^<>\s].*?)\n\s+\g<1>\n\s+([^<>\s].*?)\n\s+\g<1>\s+([^\s])', re.DOTALL) - my_string = myre.sub('>\g<1>', my_string) + my_re = re.compile(r'>\s+([^\s])', re.DOTALL) + my_string = myre.sub(r'>\g<1>', my_string) my_string = xml.dom.minidom.parseString(my_string).toprettyxml() # remove line breaks - my_re = re.compile('>\n\s+([^<>\s].*?)\n\s+\g<1>\n\s+([^<>\s].*?)\n\s+\g<1>\d+\.?\d*)") + accept_version_re = re.compile(r".*?application/vnd.openstack.trove" + r"(\+.+?)?;" + r"version=(?P\d+\.?\d*)") match = accept_version_re.search(accept_header) return match.group("version_no") if match else None @utils.cached_property def url_version(self): - versioned_url_re = re.compile("/v(?P\d+\.?\d*)") + versioned_url_re = re.compile(r"/v(?P\d+\.?\d*)") match = versioned_url_re.search(self.path) return match.group("version_no") if match else None diff --git a/trove/guestagent/common/configuration.py b/trove/guestagent/common/configuration.py index 8b98686694..0ee8c1be25 100644 --- a/trove/guestagent/common/configuration.py +++ b/trove/guestagent/common/configuration.py @@ -295,7 +295,7 @@ class ImportOverrideStrategy(ConfigurationOverrideStrategy): within their set got applied. """ - FILE_NAME_PATTERN = '%s-([0-9]+)-%s\.%s$' + FILE_NAME_PATTERN = r'%s-([0-9]+)-%s\.%s$' def __init__(self, revision_dir, revision_ext): """ diff --git a/trove/guestagent/common/guestagent_utils.py b/trove/guestagent/common/guestagent_utils.py index d408a3491b..6945be7283 100644 --- a/trove/guestagent/common/guestagent_utils.py +++ b/trove/guestagent/common/guestagent_utils.py @@ -108,7 +108,7 @@ def to_bytes(value): """Convert numbers with a byte suffix to bytes. """ if isinstance(value, six.string_types): - pattern = re.compile('^(\d+)([K,M,G]{1})$') + pattern = re.compile(r'^(\d+)([K,M,G]{1})$') match = pattern.match(value) if match: value = match.group(1) diff --git a/trove/guestagent/datastore/experimental/couchbase/system.py b/trove/guestagent/datastore/experimental/couchbase/system.py index e9b3059c91..4b266f66fe 100644 --- a/trove/guestagent/datastore/experimental/couchbase/system.py +++ b/trove/guestagent/datastore/experimental/couchbase/system.py @@ -43,8 +43,8 @@ cmd_update_sysctl_conf = ('echo "vm.swappiness = 0" | sudo tee -a ' '/etc/sysctl.conf') cmd_reset_pwd = 'sudo /opt/couchbase/bin/cbreset_password %(IP)s:8091' pwd_file = COUCHBASE_CONF_DIR + SECRET_KEY -cmd_get_password_from_config = """sudo /opt/couchbase/bin/erl -noinput -eval \ -'case file:read_file("/opt/couchbase/var/lib/couchbase/config/config.dat") \ -of {ok, B} -> io:format("~p~n", [binary_to_term(B)]) end.' \ --run init stop | grep '\[{"root",\[{password,' | awk -F\\" '{print $4}' -""" +cmd_get_password_from_config = ( + r"""sudo /opt/couchbase/bin/erl -noinput -eval 'case file:read_file(""" + r""""/opt/couchbase/var/lib/couchbase/config/config.dat") of {ok, B} ->""" + r"""io:format("~p~n", [binary_to_term(B)]) end.' -run init stop""" + r""" | grep '\[{"root",\[{password,' | awk -F\" '{print $4}'""") diff --git a/trove/guestagent/datastore/experimental/db2/system.py b/trove/guestagent/datastore/experimental/db2/system.py index 64c7da3163..28e10f5110 100644 --- a/trove/guestagent/datastore/experimental/db2/system.py +++ b/trove/guestagent/datastore/experimental/db2/system.py @@ -61,11 +61,11 @@ RESTORE_OFFLINE_DB = ( "db2 restore database %(dbname)s from " + DB2_BACKUP_DIR) GET_DB_SIZE = ( "db2 +o connect to %(dbname)s;" - "db2 call get_dbsize_info\(?, ?, ?, -1\) | " + r"db2 call get_dbsize_info\(?, ?, ?, -1\) | " "grep -A1 'DATABASESIZE' | grep 'Parameter Value' | sed 's/.*[:]//' |" " tr -d '\n'; db2 +o connect reset") GET_DB_NAMES = ("find /home/db2inst1/db2inst1/backup/ -type f -name '*.001' |" - " grep -Po \"(?<=backup/)[^.']*(?=\.)\"") + " grep -Po \"(?<=backup/)[^.']*(?=\\.)\"") GET_DBM_CONFIGURATION = "db2 get dbm configuration > %(dbm_config)s" UPDATE_DBM_CONFIGURATION = ("db2 update database manager configuration using " "%(parameter)s %(value)s") diff --git a/trove/guestagent/datastore/experimental/postgresql/service.py b/trove/guestagent/datastore/experimental/postgresql/service.py index a18d76ed3a..6c8959ee87 100644 --- a/trove/guestagent/datastore/experimental/postgresql/service.py +++ b/trove/guestagent/datastore/experimental/postgresql/service.py @@ -445,7 +445,7 @@ class PgSqlApp(object): """ r = operating_system.read_file(self.pgsql_recovery_config, as_root=True) - regexp = re.compile("host=(\d+.\d+.\d+.\d+) ") + regexp = re.compile(r"host=(\d+.\d+.\d+.\d+) ") m = regexp.search(r) return m.group(1) diff --git a/trove/guestagent/datastore/experimental/vertica/system.py b/trove/guestagent/datastore/experimental/vertica/system.py index 66c7df893f..104d9c294e 100644 --- a/trove/guestagent/datastore/experimental/vertica/system.py +++ b/trove/guestagent/datastore/experimental/vertica/system.py @@ -100,7 +100,7 @@ class VSqlError(object): stderr looks like: "ERROR 3117: Division by zero" :param stderr: string from executing statement via vsql """ - parse = re.match("^(ERROR|WARNING) (\d+): (.+)$", stderr) + parse = re.match(r"^(ERROR|WARNING) (\d+): (.+)$", stderr) if not parse: raise ValueError(_("VSql stderr %(msg)s not recognized.") % {'msg': stderr}) diff --git a/trove/guestagent/pkg.py b/trove/guestagent/pkg.py index 06734c4197..4938eebb2e 100644 --- a/trove/guestagent/pkg.py +++ b/trove/guestagent/pkg.py @@ -202,7 +202,7 @@ class RedhatPackagerMixin(RPMPackagerMixin): """ cmd = "sudo yum --color=never -y install %s" % " ".join(packages) - output_expects = ['\[sudo\] password for .*:', + output_expects = [r'\[sudo\] password for .*:', 'No package (.*) available.', ('file .* from install of .* conflicts with file' ' from package (.*?)\r\n'), @@ -243,7 +243,7 @@ class RedhatPackagerMixin(RPMPackagerMixin): """ cmd = "sudo yum --color=never -y remove %s" % package_name LOG.debug("Running package remove command: %s", cmd) - output_expects = ['\[sudo\] password for .*:', + output_expects = [r'\[sudo\] password for .*:', 'No Packages marked for removal', 'Removed:'] i, match = self.pexpect_run(cmd, output_expects, time_out) @@ -395,7 +395,7 @@ class DebianPackagerMixin(BasePackagerMixin): def pkg_version(self, package_name): std_out = getoutput("apt-cache", "policy", package_name) for line in std_out.split("\n"): - m = re.match("\s+Installed: (.*)", line) + m = re.match(r"\s+Installed: (.*)", line) if m: version = m.group(1) if version == "(none)": diff --git a/trove/guestagent/strategies/backup/experimental/cassandra_impl.py b/trove/guestagent/strategies/backup/experimental/cassandra_impl.py index 8066475bfb..33398ef276 100644 --- a/trove/guestagent/strategies/backup/experimental/cassandra_impl.py +++ b/trove/guestagent/strategies/backup/experimental/cassandra_impl.py @@ -102,8 +102,8 @@ class NodetoolSnapshot(base.BackupRunner): snapshot_name) snapshot_files = operating_system.list_files_in_directory( data_dir, recursive=True, include_dirs=False, - pattern='.*/snapshots/%s/.*\.%s' % (snapshot_name, - self._SNAPSHOT_EXTENSION), + pattern=r'.*/snapshots/%s/.*\.%s' % (snapshot_name, + self._SNAPSHOT_EXTENSION), as_root=True) num_snapshot_files = len(snapshot_files) LOG.debug('Found %(num)d snapshot (*.%(ext)s) files.', diff --git a/trove/guestagent/strategies/backup/experimental/postgresql_impl.py b/trove/guestagent/strategies/backup/experimental/postgresql_impl.py index 672223b777..8c1478cd62 100644 --- a/trove/guestagent/strategies/backup/experimental/postgresql_impl.py +++ b/trove/guestagent/strategies/backup/experimental/postgresql_impl.py @@ -125,8 +125,8 @@ class PgBaseBackup(base.BackupRunner, PgBaseBackupUtil): operating_system.chmod( metadata_file, FileMode(add=[stat.S_IROTH]), as_root=True) - start_re = re.compile("START WAL LOCATION: (.*) \(file (.*)\)") - stop_re = re.compile("STOP WAL LOCATION: (.*) \(file (.*)\)") + start_re = re.compile(r"START WAL LOCATION: (.*) \(file (.*)\)") + stop_re = re.compile(r"STOP WAL LOCATION: (.*) \(file (.*)\)") checkpt_re = re.compile("CHECKPOINT LOCATION: (.*)") label_re = re.compile("LABEL: (.*)") diff --git a/trove/guestagent/strategies/backup/mysql_impl.py b/trove/guestagent/strategies/backup/mysql_impl.py index 8c51fc7a31..be50a0eec4 100644 --- a/trove/guestagent/strategies/backup/mysql_impl.py +++ b/trove/guestagent/strategies/backup/mysql_impl.py @@ -85,7 +85,8 @@ class InnoBackupEx(base.BackupRunner): def metadata(self): LOG.debug('Getting metadata from backup.') meta = {} - lsn = re.compile("The latest check point \(for incremental\): '(\d+)'") + lsn = re.compile(r"The latest check point \(for incremental\): " + r"'(\d+)'") with open('/tmp/innobackupex.log', 'r') as backup_log: output = backup_log.read() match = lsn.search(output) diff --git a/trove/guestagent/strategies/restore/experimental/postgresql_impl.py b/trove/guestagent/strategies/restore/experimental/postgresql_impl.py index f5080c736f..c7cf0d63f2 100644 --- a/trove/guestagent/strategies/restore/experimental/postgresql_impl.py +++ b/trove/guestagent/strategies/restore/experimental/postgresql_impl.py @@ -37,7 +37,7 @@ class PgDump(base.RestoreRunner): base_restore_cmd = 'psql -U os_admin' IGNORED_ERROR_PATTERNS = [ - re.compile("ERROR:\s*role \"postgres\" already exists"), + re.compile(r'ERROR:\s*role "postgres" already exists'), ] def restore(self): @@ -96,7 +96,7 @@ class PgBaseBackup(base.RestoreRunner): base_restore_cmd = '' IGNORED_ERROR_PATTERNS = [ - re.compile("ERROR:\s*role \"postgres\" already exists"), + re.compile(r'ERROR:\s*role "postgres" already exists'), ] def __init__(self, *args, **kwargs): diff --git a/trove/tests/api/mgmt/hosts.py b/trove/tests/api/mgmt/hosts.py index d057319284..37754864b2 100644 --- a/trove/tests/api/mgmt/hosts.py +++ b/trove/tests/api/mgmt/hosts.py @@ -67,7 +67,7 @@ class HostsBeforeInstanceCreation(object): @test(depends_on=[test_empty_index_host_list]) def test_empty_index_host_list_single(self): - self.host.name = self.host.name.replace(".", "\.") + self.host.name = self.host.name.replace(".", r"\.") result = self.client.hosts.get(self.host) assert_not_equal(result, None, "Get host should not be empty for: %s" % self.host) @@ -126,7 +126,7 @@ class HostsMgmtCommands(object): @test(depends_on=[test_index_host_list]) def test_index_host_list_single(self): - self.host.name = self.host.name.replace(".", "\.") + self.host.name = self.host.name.replace(".", r"\.") result = self.client.hosts.get(self.host) assert_not_equal(result, None, "list hosts should not be empty: %s" % str(result)) diff --git a/trove/tests/fakes/guestagent.py b/trove/tests/fakes/guestagent.py index fbfb6e401f..b75397da3b 100644 --- a/trove/tests/fakes/guestagent.py +++ b/trove/tests/fakes/guestagent.py @@ -66,7 +66,7 @@ class FakeGuest(object): self.version += 1 def _check_username(self, username): - unsupported_chars = re.compile("^\s|\s$|'|\"|;|`|,|/|\\\\") + unsupported_chars = re.compile(r"""^\s|\s$|'|"|;|`|,|/|\\""") if (not username or unsupported_chars.search(username) or ("%r" % username).find("\\") != -1): diff --git a/trove/tests/unittests/guestagent/test_operating_system.py b/trove/tests/unittests/guestagent/test_operating_system.py index adb66fa6ac..9079ec343b 100644 --- a/trove/tests/unittests/guestagent/test_operating_system.py +++ b/trove/tests/unittests/guestagent/test_operating_system.py @@ -1146,35 +1146,35 @@ class TestOperatingSystem(trove_testtools.TestCase): # Only '*.txt' in the top directory. self._assert_list_files( - root_path, False, '.*\.txt$', False, all_paths, 3) + root_path, False, r'.*\.txt$', False, all_paths, 3) # Only '*.txt' (including directories) in the top directory. self._assert_list_files( - root_path, False, '.*\.txt$', True, all_paths, 3) + root_path, False, r'.*\.txt$', True, all_paths, 3) # Only '*.txt' recursive. self._assert_list_files( - root_path, True, '.*\.txt$', True, all_paths, 9) + root_path, True, r'.*\.txt$', True, all_paths, 9) # Only '*.txt' (including directories) recursive. self._assert_list_files( - root_path, True, '.*\.txt$', False, all_paths, 9) + root_path, True, r'.*\.txt$', False, all_paths, 9) # Only extension-less files in the top directory. self._assert_list_files( - root_path, False, '[^\.]*$', False, all_paths, 3) + root_path, False, r'[^\.]*$', False, all_paths, 3) # Only extension-less files recursive. self._assert_list_files( - root_path, True, '[^\.]*$', False, all_paths, 9) + root_path, True, r'[^\.]*$', False, all_paths, 9) # Non-existing extension in the top directory. self._assert_list_files( - root_path, False, '.*\.bak$', False, all_paths, 0) + root_path, False, r'.*\.bak$', False, all_paths, 0) # Non-existing extension recursive. self._assert_list_files( - root_path, True, '.*\.bak$', False, all_paths, 0) + root_path, True, r'.*\.bak$', False, all_paths, 0) finally: try: os.remove(root_path) diff --git a/trove/tests/util/mysql.py b/trove/tests/util/mysql.py index 2d91433796..8d8c5de39b 100644 --- a/trove/tests/util/mysql.py +++ b/trove/tests/util/mysql.py @@ -73,10 +73,10 @@ class SqlAlchemyConnection(object): @staticmethod def _exception_is_permissions_issue(msg): """Assert message cited a permissions issue and not something else.""" - pos_error = re.compile(".*Host '[\w\.]*' is not allowed to connect to " - "this MySQL server.*") + pos_error = re.compile(r".*Host '[\w\.]*' is not allowed to connect " + "to this MySQL server.*") pos_error1 = re.compile(".*Access denied for user " - "'[\w\*\!\@\#\^\&]*'@'[\w\.]*'.*") + r"'[\w\*\!\@\#\^\&]*'@'[\w\.]*'.*") if (pos_error.match(msg) or pos_error1.match(msg)): return True @@ -130,7 +130,7 @@ class PexpectMySqlConnection(object): cmd = '%s %s' % (tests.SSH_CMD, ssh_args) self.proc = pexpect.spawn(cmd) print(cmd) - self.proc.expect(":~\$", timeout=self.TIME_OUT) + self.proc.expect(r":~\$", timeout=self.TIME_OUT) cmd2 = "mysql --host '%s' -u '%s' '-p%s'\n" % \ (self.host, self.user, self.password) print(cmd2) @@ -152,7 +152,7 @@ class PexpectMySqlConnection(object): self.proc.close() def execute(self, cmd): - self.proc.send(cmd + "\G\n") + self.proc.send(cmd + "\\G\n") outcome = self.proc.expect(['Empty set', 'mysql>'], timeout=self.TIME_OUT) if outcome == 0: