From dbe13b17e8ba0f717a131caa6e6c9002903a03e1 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 19 Mar 2015 13:24:00 +0100 Subject: [PATCH] Leverage timeutils, drop strtime() usage This patch remove some code that tried to parse time in different way by leveraging timeutils instead. It also drops strtime() usage as it's going to be deprecated in oslo_utils (see I8b5119e64369ccac3423dccc04421f99912df733). Change-Id: Icb2906ccd4b381f80064e0f1348fc64e389031dd --- HACKING.rst | 1 + cinder/backup/drivers/swift.py | 2 +- cinder/context.py | 6 +++--- cinder/hacking/checks.py | 8 ++++++++ cinder/tests/unit/test_hacking.py | 6 ++++++ cinder/volume/drivers/solidfire.py | 10 +++++----- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 54dcecdb2..199d05b5d 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -21,6 +21,7 @@ Cinder Specific Commandments - [C303] Ensure that there are no 'print()' statements in code that is being committed. - [C304] Enforce no use of LOG.audit messages. LOG.info should be used instead. - [C305] Prevent use of deprecated contextlib.nested. +- [C306] timeutils.strtime() must not be used (deprecated). General diff --git a/cinder/backup/drivers/swift.py b/cinder/backup/drivers/swift.py index 70d3d263c..178823e6d 100644 --- a/cinder/backup/drivers/swift.py +++ b/cinder/backup/drivers/swift.py @@ -274,7 +274,7 @@ class SwiftBackupDriver(chunkeddriver.ChunkedBackupDriver): az = 'az_%s' % self.az backup_name = '%s_backup_%s' % (az, backup['id']) volume = 'volume_%s' % (backup['volume_id']) - timestamp = timeutils.strtime(fmt="%Y%m%d%H%M%S") + timestamp = timeutils.utcnow().strftime("%Y%m%d%H%M%S") prefix = volume + '/' + timestamp + '/' + backup_name LOG.debug('generate_object_name_prefix: %s', prefix) return prefix diff --git a/cinder/context.py b/cinder/context.py index 88139ad61..9370cdad5 100644 --- a/cinder/context.py +++ b/cinder/context.py @@ -69,8 +69,8 @@ class RequestContext(context.RequestContext): self.remote_address = remote_address if not timestamp: timestamp = timeutils.utcnow() - if isinstance(timestamp, basestring): - timestamp = timeutils.parse_strtime(timestamp) + elif isinstance(timestamp, basestring): + timestamp = timeutils.parse_isotime(timestamp) self.timestamp = timestamp self.quota_class = quota_class @@ -115,7 +115,7 @@ class RequestContext(context.RequestContext): 'read_deleted': self.read_deleted, 'roles': self.roles, 'remote_address': self.remote_address, - 'timestamp': timeutils.strtime(self.timestamp), + 'timestamp': timeutils.isotime(self.timestamp, True), 'quota_class': self.quota_class, 'service_catalog': self.service_catalog, 'request_id': self.request_id} diff --git a/cinder/hacking/checks.py b/cinder/hacking/checks.py index 9b2451411..71ca919fe 100644 --- a/cinder/hacking/checks.py +++ b/cinder/hacking/checks.py @@ -219,6 +219,13 @@ def check_no_contextlib_nested(logical_line): yield(0, msg) +def check_timeutils_strtime(logical_line): + msg = ("C306: Found timeutils.strtime(). " + "Please use oslo_utils.timeutils.isotime() or datetime.strftime()") + if 'timeutils.strtime' in logical_line: + yield(0, msg) + + def factory(register): register(no_vi_headers) register(no_translate_debug_logs) @@ -227,6 +234,7 @@ def factory(register): register(check_assert_called_once) register(check_oslo_namespace_imports) register(check_datetime_now) + register(check_timeutils_strtime) register(validate_log_translations) register(check_unicode_usage) register(check_no_print_statements) diff --git a/cinder/tests/unit/test_hacking.py b/cinder/tests/unit/test_hacking.py index 400506d6b..141485109 100644 --- a/cinder/tests/unit/test_hacking.py +++ b/cinder/tests/unit/test_hacking.py @@ -184,6 +184,12 @@ class HackingTestCase(test.TestCase): self.assertEqual(0, len(list(checks.check_datetime_now( "datetime.now() # noqa", True)))) + def test_check_timeutils_strtime(self): + self.assertEqual(1, len(list(checks.check_timeutils_strtime( + "timeutils.strtime")))) + self.assertEqual(0, len(list(checks.check_timeutils_strtime( + "strftime")))) + def test_validate_log_translations(self): self.assertEqual(1, len(list(checks.validate_log_translations( "LOG.info('foo')", "foo.py")))) diff --git a/cinder/volume/drivers/solidfire.py b/cinder/volume/drivers/solidfire.py index 27d0d0829..e50643302 100644 --- a/cinder/volume/drivers/solidfire.py +++ b/cinder/volume/drivers/solidfire.py @@ -394,7 +394,7 @@ class SolidFireDriver(san.SanISCSIDriver): # to set any that were provided params = {'volumeID': sf_volume_id} - create_time = timeutils.strtime(v_ref['created_at']) + create_time = timeutils.isotime(v_ref['created_at'], True) attributes = {'uuid': v_ref['id'], 'is_clone': 'True', 'src_uuid': src_uuid, @@ -698,7 +698,7 @@ class SolidFireDriver(san.SanISCSIDriver): if type_id is not None: qos = self._set_qos_by_volume_type(ctxt, type_id) - create_time = timeutils.strtime(volume['created_at']) + create_time = timeutils.isotime(volume['created_at'], True) attributes = {'uuid': volume['id'], 'is_clone': 'False', 'created_at': create_time} @@ -1006,7 +1006,7 @@ class SolidFireDriver(san.SanISCSIDriver): raise exception.VolumeNotFound(volume_id=volume['id']) attributes = sf_vol['attributes'] - attributes['retyped_at'] = timeutils.strtime() + attributes['retyped_at'] = timeutils.isotime(subsecond=True) params = {'volumeID': sf_vol['volumeID']} qos = self._set_qos_by_volume_type(ctxt, new_type['id']) @@ -1053,7 +1053,7 @@ class SolidFireDriver(san.SanISCSIDriver): if type_id is not None: qos = self._set_qos_by_volume_type(ctxt, type_id) - import_time = timeutils.strtime(volume['created_at']) + import_time = timeutils.isotime(volume['created_at'], True) attributes = {'uuid': volume['id'], 'is_clone': 'False', 'os_imported_at': import_time, @@ -1113,7 +1113,7 @@ class SolidFireDriver(san.SanISCSIDriver): if sf_vol is None: raise exception.VolumeNotFound(volume_id=volume['id']) - export_time = timeutils.strtime() + export_time = timeutils.isotime(subsecond=True) attributes = sf_vol['attributes'] attributes['os_exported_at'] = export_time params = {'volumeID': int(sf_vol['volumeID']), -- 2.45.2