diff options
author | Max Illfelder <illfelder@users.noreply.github.com> | 2018-10-24 14:06:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-24 14:06:23 -0700 |
commit | 127357168fc41ca1726a42d2b9cd6a922ce1c31a (patch) | |
tree | 2a3c831d8a276a7b59156fe3d53045967b38f7bc | |
parent | d007637872aa16f082f1af4f8d8d9a09360c9b7d (diff) | |
download | google-compute-image-packages-2fa.tar.gz |
Improvements to resolved deprecation warnings. (#678)2fa
- Create backwards compatible parser.
- Move off of deprecated methods.
- Fixing PyPI build errors.
- Fix linter warnings.
- Fix the flake8 linter to prevent contradictory linter warnings.
-rw-r--r-- | google_compute_engine/accounts/accounts_utils.py | 4 | ||||
-rw-r--r-- | google_compute_engine/accounts/oslogin_utils.py | 4 | ||||
-rw-r--r-- | google_compute_engine/compat.py | 6 | ||||
-rw-r--r-- | google_compute_engine/config_manager.py | 6 | ||||
-rw-r--r-- | google_compute_engine/distro_lib/el_7/tests/utils_test.py | 8 | ||||
-rw-r--r-- | google_compute_engine/instance_setup/instance_config.py | 6 | ||||
-rwxr-xr-x | google_compute_engine/instance_setup/instance_setup.py | 12 | ||||
-rw-r--r-- | google_compute_engine/instance_setup/tests/instance_config_test.py | 14 | ||||
-rw-r--r-- | google_compute_engine/metadata_scripts/script_retriever.py | 4 | ||||
-rw-r--r-- | google_compute_engine/metadata_watcher.py | 4 | ||||
-rw-r--r-- | google_compute_engine/tests/config_manager_test.py | 4 | ||||
-rw-r--r-- | tox.ini | 2 |
12 files changed, 40 insertions, 34 deletions
diff --git a/google_compute_engine/accounts/accounts_utils.py b/google_compute_engine/accounts/accounts_utils.py index 8c0741d..01d348d 100644 --- a/google_compute_engine/accounts/accounts_utils.py +++ b/google_compute_engine/accounts/accounts_utils.py @@ -338,8 +338,8 @@ class AccountsUtils(object): if not self._GetUser(user): # User does not exist. Attempt to create the user and add them to the # appropriate user groups. - if not (self._AddUser(user) and - self._UpdateUserGroups(user, self.groups)): + if not (self._AddUser(user) + and self._UpdateUserGroups(user, self.groups)): return False # Add the user to the google sudoers group. if not self._UpdateSudoer(user, sudoer=True): diff --git a/google_compute_engine/accounts/oslogin_utils.py b/google_compute_engine/accounts/oslogin_utils.py index e8cf520..a7e2d50 100644 --- a/google_compute_engine/accounts/oslogin_utils.py +++ b/google_compute_engine/accounts/oslogin_utils.py @@ -126,5 +126,5 @@ class OsLoginUtils(object): return self._RunOsLoginControl('activate') or self._RunOsLoginNssCache() else: self.logger.info('Deactivating OS Login.') - return (self._RunOsLoginControl('deactivate') or - self._RemoveOsLoginNssCache()) + return (self._RunOsLoginControl('deactivate') + or self._RemoveOsLoginNssCache()) diff --git a/google_compute_engine/compat.py b/google_compute_engine/compat.py index 4efbeee..3063917 100644 --- a/google_compute_engine/compat.py +++ b/google_compute_engine/compat.py @@ -102,3 +102,9 @@ if sys.version_info < (2, 7, 9): subprocess.check_call(command) urlretrieve.urlretrieve = curlretrieve + +if sys.version_info < (3, 2): + parser.SafeConfigParser.read_file = parser.SafeConfigParser.readfp + parser.Parser = parser.SafeConfigParser +else: + parser.Parser = parser.ConfigParser diff --git a/google_compute_engine/config_manager.py b/google_compute_engine/config_manager.py index c5a2036..1057bf1 100644 --- a/google_compute_engine/config_manager.py +++ b/google_compute_engine/config_manager.py @@ -37,7 +37,7 @@ class ConfigManager(object): """ self.config_file = config_file or CONFIG self.config_header = config_header - self.config = parser.SafeConfigParser() + self.config = parser.Parser() self.config.read(self.config_file) def _AddHeader(self, fp): @@ -76,8 +76,8 @@ class ConfigManager(object): Returns: bool, True if the option is enabled or not set. """ - return (not self.config.has_option(section, option) or - self.config.getboolean(section, option)) + return (not self.config.has_option(section, option) + or self.config.getboolean(section, option)) def SetOption(self, section, option, value, overwrite=True): """Set the value of an option in the config file. diff --git a/google_compute_engine/distro_lib/el_7/tests/utils_test.py b/google_compute_engine/distro_lib/el_7/tests/utils_test.py index 17f81d0..d726c3d 100644 --- a/google_compute_engine/distro_lib/el_7/tests/utils_test.py +++ b/google_compute_engine/distro_lib/el_7/tests/utils_test.py @@ -53,22 +53,22 @@ class UtilsTest(unittest.TestCase): # Write a value for an existing config without overriding it. utils.Utils._ModifyInterface( self.mock_setup, config_file, 'A', 'aardvark', replace=False) - self.assertEquals(open(config_file).readlines(), config_content) + self.assertEqual(open(config_file).readlines(), config_content) # Write a value for a config that is not already set. utils.Utils._ModifyInterface( self.mock_setup, config_file, 'C', 'none', replace=False) config_content.append('C=none\n') - self.assertEquals(open(config_file).readlines(), config_content) + self.assertEqual(open(config_file).readlines(), config_content) # Write a value for an existing config with replacement. utils.Utils._ModifyInterface( self.mock_setup, config_file, 'A', 'aardvark', replace=True) config_content[1] = 'A=aardvark\n' - self.assertEquals(open(config_file).readlines(), config_content) + self.assertEqual(open(config_file).readlines(), config_content) # Write a value for an existing config with multiple occurrences. utils.Utils._ModifyInterface( self.mock_setup, config_file, 'B', '"banana"', replace=True) config_content[2] = config_content[3] = 'B="banana"\n' - self.assertEquals(open(config_file).readlines(), config_content) + self.assertEqual(open(config_file).readlines(), config_content) @mock.patch('google_compute_engine.distro_lib.el_7.utils.os.path.exists') def testDisableNetworkManager(self, mock_exists): diff --git a/google_compute_engine/instance_setup/instance_config.py b/google_compute_engine/instance_setup/instance_config.py index 782f6b0..1562fab 100644 --- a/google_compute_engine/instance_setup/instance_config.py +++ b/google_compute_engine/instance_setup/instance_config.py @@ -126,9 +126,9 @@ class InstanceConfig(config_manager.ConfigManager): config_files = [self.instance_config, self.instance_config_distro] config_defaults = [] if self.instance_config_metadata: - config = parser.SafeConfigParser() + config = parser.Parser() try: - config.readfp(stringio.StringIO(self.instance_config_metadata)) + config.read_file(stringio.StringIO(self.instance_config_metadata)) except parser.Error as e: self.logger.error('Error parsing metadata configs: %s', str(e)) else: @@ -136,7 +136,7 @@ class InstanceConfig(config_manager.ConfigManager): dict((s, dict(config.items(s))) for s in config.sections())) for config_file in config_files: if os.path.exists(config_file): - config = parser.SafeConfigParser() + config = parser.Parser() try: config.read(config_file) except parser.Error as e: diff --git a/google_compute_engine/instance_setup/instance_setup.py b/google_compute_engine/instance_setup/instance_setup.py index 78f4e00..0a4301f 100755 --- a/google_compute_engine/instance_setup/instance_setup.py +++ b/google_compute_engine/instance_setup/instance_setup.py @@ -87,8 +87,8 @@ class InstanceSetup(object): project_data = {} self.logger.warning('Project attributes were not found.') - return (instance_data.get('google-instance-configs') or - project_data.get('google-instance-configs')) + return (instance_data.get('google-instance-configs') + or project_data.get('google-instance-configs')) def _RunScript(self, script): """Run a script and log the streamed script output. @@ -147,12 +147,12 @@ class InstanceSetup(object): # Instance setup systemd scripts block sshd from starting. if os.path.exists(constants.LOCALBASE + '/bin/systemctl'): return - elif (os.path.exists('/etc/init.d/ssh') or - os.path.exists('/etc/init/ssh.conf')): + elif (os.path.exists('/etc/init.d/ssh') + or os.path.exists('/etc/init/ssh.conf')): subprocess.call(['service', 'ssh', 'start']) subprocess.call(['service', 'ssh', 'reload']) - elif (os.path.exists('/etc/init.d/sshd') or - os.path.exists('/etc/init/sshd.conf')): + elif (os.path.exists('/etc/init.d/sshd') + or os.path.exists('/etc/init/sshd.conf')): subprocess.call(['service', 'sshd', 'start']) subprocess.call(['service', 'sshd', 'reload']) diff --git a/google_compute_engine/instance_setup/tests/instance_config_test.py b/google_compute_engine/instance_setup/tests/instance_config_test.py index a40dcbf..eb91adb 100644 --- a/google_compute_engine/instance_setup/tests/instance_config_test.py +++ b/google_compute_engine/instance_setup/tests/instance_config_test.py @@ -72,9 +72,9 @@ class InstanceConfigTest(unittest.TestCase): @mock.patch('google_compute_engine.instance_setup.instance_config.config_manager.ConfigManager.SetOption') @mock.patch('google_compute_engine.instance_setup.instance_config.config_manager.ConfigManager.__init__') def testInstanceConfigExists(self, mock_init, mock_set, mock_exists): - config_parser = instance_config.parser.SafeConfigParser() + config_parser = instance_config.parser.Parser() config_metadata = '[first]\na = true' - mock_config = mock.create_autospec(instance_config.parser.SafeConfigParser) + mock_config = mock.create_autospec(instance_config.parser.Parser) with mock.patch( 'google_compute_engine.instance_setup.instance_config' '.parser') as mock_parser: @@ -82,7 +82,7 @@ class InstanceConfigTest(unittest.TestCase): mock_config.sections = mock.Mock() mock_config.sections.return_value = ['a', 'b'] mock_config.items = lambda key: {'key: %s' % key: 'value: %s' % key} - mock_parser.SafeConfigParser.side_effect = [ + mock_parser.Parser.side_effect = [ config_parser, mock_config, mock_config] mocks = mock.Mock() mocks.attach_mock(mock_init, 'init') @@ -98,13 +98,13 @@ class InstanceConfigTest(unittest.TestCase): expected_calls = [ mock.call.init( config_file='template', config_header='/tmp/test.py template'), - mock.call.parser.SafeConfigParser(), + mock.call.parser.Parser(), mock.call.exists('config'), - mock.call.parser.SafeConfigParser(), + mock.call.parser.Parser(), mock.call.config.read('config'), mock.call.config.sections(), mock.call.exists('distro'), - mock.call.parser.SafeConfigParser(), + mock.call.parser.Parser(), mock.call.config.read('distro'), mock.call.config.sections(), mock.call.set('first', 'a', 'true', overwrite=False), @@ -120,7 +120,7 @@ class InstanceConfigTest(unittest.TestCase): ] self.assertEqual(mocks.mock_calls, expected_calls) - @mock.patch('google_compute_engine.instance_setup.instance_config.parser.SafeConfigParser.read') + @mock.patch('google_compute_engine.instance_setup.instance_config.parser.Parser.read') @mock.patch('google_compute_engine.instance_setup.instance_config.os.path.exists') @mock.patch('google_compute_engine.instance_setup.instance_config.config_manager.ConfigManager.SetOption') @mock.patch('google_compute_engine.instance_setup.instance_config.config_manager.ConfigManager.__init__') diff --git a/google_compute_engine/metadata_scripts/script_retriever.py b/google_compute_engine/metadata_scripts/script_retriever.py index 96d84ac..4c531e5 100644 --- a/google_compute_engine/metadata_scripts/script_retriever.py +++ b/google_compute_engine/metadata_scripts/script_retriever.py @@ -220,5 +220,5 @@ class ScriptRetriever(object): project_data = None self.logger.warning('Project attributes were not found.') - return (self._GetAttributeScripts(instance_data, dest_dir) or - self._GetAttributeScripts(project_data, dest_dir)) + return (self._GetAttributeScripts(instance_data, dest_dir) + or self._GetAttributeScripts(project_data, dest_dir)) diff --git a/google_compute_engine/metadata_watcher.py b/google_compute_engine/metadata_watcher.py index 1f088d9..72a12f9 100644 --- a/google_compute_engine/metadata_watcher.py +++ b/google_compute_engine/metadata_watcher.py @@ -50,8 +50,8 @@ def RetryOnUnavailable(func): response = func(*args, **kwargs) except (httpclient.HTTPException, socket.error, urlerror.URLError) as e: time.sleep(5) - if (isinstance(e, urlerror.HTTPError) and - e.getcode() == httpclient.SERVICE_UNAVAILABLE): + if (isinstance(e, urlerror.HTTPError) + and e.getcode() == httpclient.SERVICE_UNAVAILABLE): continue elif isinstance(e, socket.timeout): continue diff --git a/google_compute_engine/tests/config_manager_test.py b/google_compute_engine/tests/config_manager_test.py index 2a708d4..185989a 100644 --- a/google_compute_engine/tests/config_manager_test.py +++ b/google_compute_engine/tests/config_manager_test.py @@ -55,8 +55,8 @@ class ConfigManagerTest(unittest.TestCase): self.mock_config = mock.Mock() self.mock_config.has_option.side_effect = _HasOption self.mock_config.has_section.side_effect = _HasSection - config_manager.parser.SafeConfigParser = mock.Mock() - config_manager.parser.SafeConfigParser.return_value = self.mock_config + config_manager.parser.Parser = mock.Mock() + config_manager.parser.Parser.return_value = self.mock_config self.config_file = 'test.cfg' self.config_header = 'Config file header.' @@ -52,7 +52,7 @@ commands = # E302 expected 2 blank lines, found 1 # E501 line too long # F401 imported but unused -ignore = E111,E114,E121,E125,E128,E129,E226,E231,E261,E302,E501,F401 +ignore = E111,E114,E121,E125,E128,E129,E226,E231,E261,E302,E501,F401,W503 exclude = .git, .tox, |