Clean imports in code

This patch set modifies lines which are importing objects
instead of modules. As per openstack import guide lines, user should
import modules in a file not objects.
Also, imports was regrouped and placed in alphabetical order.

http://docs.openstack.org/developer/hacking/#imports

Change-Id: I192b8bf6f50a85e19640975f0902a014c4ba0878
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
This commit is contained in:
Cao Xuan Hoang 2016-09-06 11:38:54 +07:00 committed by Ruslan Aliev
parent 85ab97121e
commit 61ee6a331a
7 changed files with 63 additions and 60 deletions

View File

@ -18,15 +18,15 @@ limitations under the License.
import abc import abc
import json import json
import multiprocessing import multiprocessing
from multiprocessing.queues import SimpleQueue from multiprocessing import queues
import shutil import shutil
import six
# PyCharm will not recognize queue. Puts red squiggle line under it. That's OK.
from six.moves import queue
import tempfile import tempfile
import time import time
from oslo_log import log from oslo_log import log
import six
# PyCharm will not recognize queue. Puts red squiggle line under it. That's OK.
from six.moves import queue
from freezer.exceptions import engine as engine_exceptions from freezer.exceptions import engine as engine_exceptions
from freezer.storage import base from freezer.storage import base
@ -179,8 +179,7 @@ class BackupEngine(object):
if got_exception: if got_exception:
raise engine_exceptions.EngineException( raise engine_exceptions.EngineException(
"Engine error. Failed to backup." "Engine error. Failed to backup.")
)
with open(freezer_meta, mode='wb') as b_file: with open(freezer_meta, mode='wb') as b_file:
b_file.write(json.dumps(self.metadata())) b_file.write(json.dumps(self.metadata()))
@ -242,7 +241,7 @@ class BackupEngine(object):
max_level = max(backups.keys()) max_level = max(backups.keys())
# Use SimpleQueue because Queue does not work on Mac OS X. # Use SimpleQueue because Queue does not work on Mac OS X.
read_except_queue = SimpleQueue() read_except_queue = queues.SimpleQueue()
for level in range(0, max_level + 1): for level in range(0, max_level + 1):
backup = backups[level] backup = backups[level]
@ -259,7 +258,7 @@ class BackupEngine(object):
# Start the tar pipe consumer process # Start the tar pipe consumer process
# Use SimpleQueue because Queue does not work on Mac OS X. # Use SimpleQueue because Queue does not work on Mac OS X.
write_except_queue = SimpleQueue() write_except_queue = queues.SimpleQueue()
tar_stream = multiprocessing.Process( tar_stream = multiprocessing.Process(
target=self.restore_level, target=self.restore_level,
@ -290,8 +289,7 @@ class BackupEngine(object):
if tar_stream.exitcode or got_exception: if tar_stream.exitcode or got_exception:
raise engine_exceptions.EngineException( raise engine_exceptions.EngineException(
"Engine error. Failed to restore." "Engine error. Failed to restore.")
)
LOG.info( LOG.info(
'Restore completed successfully for backup name ' 'Restore completed successfully for backup name '

View File

@ -15,23 +15,24 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
import abc import abc
import datetime import datetime
import os import os
import six
import sys import sys
import time import time
from freezer.openstack import backup
from freezer.openstack import restore
from freezer.snapshot import snapshot
from freezer.utils.checksum import CheckSum
from freezer.utils import exec_cmd
from freezer.utils import utils
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
from oslo_utils import importutils from oslo_utils import importutils
import six
from freezer.openstack import backup
from freezer.openstack import restore
from freezer.snapshot import snapshot
from freezer.utils import checksum
from freezer.utils import exec_cmd
from freezer.utils import utils
CONF = cfg.CONF CONF = cfg.CONF
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -196,7 +197,7 @@ class BackupJob(Job):
if self.conf.consistency_check: if self.conf.consistency_check:
ignorelinks = (self.conf.dereference_symlink == 'none' or ignorelinks = (self.conf.dereference_symlink == 'none' or
self.conf.dereference_symlink == 'hard') self.conf.dereference_symlink == 'hard')
consistency_checksum = CheckSum( consistency_checksum = checksum.CheckSum(
filepath, ignorelinks=ignorelinks).compute() filepath, ignorelinks=ignorelinks).compute()
LOG.info('Computed checksum for consistency {0}'. LOG.info('Computed checksum for consistency {0}'.
format(consistency_checksum)) format(consistency_checksum))
@ -274,8 +275,8 @@ class RestoreJob(Job):
try: try:
if conf.consistency_checksum: if conf.consistency_checksum:
backup_checksum = conf.consistency_checksum backup_checksum = conf.consistency_checksum
restore_checksum = CheckSum(restore_abs_path, restore_checksum = checksum.CheckSum(restore_abs_path,
ignorelinks=True) ignorelinks=True)
if restore_checksum.compare(backup_checksum): if restore_checksum.compare(backup_checksum):
LOG.info('Consistency check success.') LOG.info('Consistency check success.')
else: else:

View File

@ -14,16 +14,16 @@
# limitations under the License. # limitations under the License.
import os import os
import swiftclient
import time import time
from cinderclient.client import Client as cinder_client from cinderclient import client as cinder_client
from glanceclient.client import Client as glance_client from glanceclient import client as glance_client
from keystoneauth1 import loading from keystoneauth1 import loading
from keystoneauth1 import session from keystoneauth1 import session
from novaclient.client import Client as nova_client from novaclient import client as nova_client
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
import swiftclient
from freezer.utils import utils from freezer.utils import utils
@ -78,8 +78,8 @@ class OSClientManager(object):
Use pre-initialized session to create an instance of nova client. Use pre-initialized session to create an instance of nova client.
:return: novaclient instance :return: novaclient instance
""" """
self.nova = nova_client(self.compute_version, session=self.sess, self.nova = nova_client.Client(self.compute_version, session=self.sess,
**self.client_kwargs) **self.client_kwargs)
return self.nova return self.nova
def create_glance(self): def create_glance(self):
@ -92,8 +92,9 @@ class OSClientManager(object):
if 'insecure' in self.client_kwargs.keys(): if 'insecure' in self.client_kwargs.keys():
self.client_kwargs.pop('insecure') self.client_kwargs.pop('insecure')
self.glance = glance_client(self.image_version, session=self.sess, self.glance = glance_client.Client(self.image_version,
**self.client_kwargs) session=self.sess,
**self.client_kwargs)
return self.glance return self.glance
def create_cinder(self): def create_cinder(self):
@ -101,8 +102,9 @@ class OSClientManager(object):
Use pre-initialized session to create an instance of cinder client. Use pre-initialized session to create an instance of cinder client.
:return: cinderclient instance :return: cinderclient instance
""" """
self.cinder = cinder_client(self.volume_version, session=self.sess, self.cinder = cinder_client.Client(self.volume_version,
**self.client_kwargs) session=self.sess,
**self.client_kwargs)
return self.cinder return self.cinder
def create_swift(self): def create_swift(self):

View File

@ -15,15 +15,16 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
import six
from distutils import spawn
import sys import sys
import threading import threading
import time import time
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers import background
from distutils import spawn
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
import six
from freezer.apiclient import client from freezer.apiclient import client
from freezer.scheduler import arguments from freezer.scheduler import arguments
@ -32,7 +33,6 @@ from freezer.scheduler import shell
from freezer.scheduler import utils from freezer.scheduler import utils
from freezer.utils import winutils from freezer.utils import winutils
if winutils.is_windows(): if winutils.is_windows():
from win_daemon import Daemon from win_daemon import Daemon
from win_daemon import NoDaemon from win_daemon import NoDaemon
@ -67,8 +67,10 @@ class FreezerScheduler(object):
'default': {'type': 'threadpool', 'max_workers': 1}, 'default': {'type': 'threadpool', 'max_workers': 1},
'threadpool': {'type': 'threadpool', 'max_workers': 10} 'threadpool': {'type': 'threadpool', 'max_workers': 10}
} }
self.scheduler = BackgroundScheduler(job_defaults=job_defaults, self.scheduler = background.BackgroundScheduler(
executors=executors) job_defaults=job_defaults,
executors=executors)
if self.client: if self.client:
self.scheduler.add_job(self.poll, 'interval', self.scheduler.add_job(self.poll, 'interval',
seconds=interval, id='api_poll', seconds=interval, id='api_poll',

View File

@ -18,21 +18,19 @@ limitations under the License.
import json import json
import os import os
import prettytable
import six import six
from freezer.utils import utils as freezer_utils
import utils import utils
from freezer.utils.utils import DateTime
from prettytable import PrettyTable
try: try:
from betterprint import pprint from betterprint import pprint
except Exception: except Exception:
def pprint(doc): def pprint(doc):
print(json.dumps(doc, indent=4)) print(json.dumps(doc, indent=4))
from freezer.utils import utils as freezer_utils
def do_session_remove_job(client, args): def do_session_remove_job(client, args):
""" """
@ -70,7 +68,8 @@ def do_session_list_job(client, args):
raise Exception("Parameter --session required") raise Exception("Parameter --session required")
session_doc = client.sessions.get(args.session_id) session_doc = client.sessions.get(args.session_id)
jobs = session_doc.get('jobs', {}) jobs = session_doc.get('jobs', {})
table = PrettyTable(["job_id", "status", "result", "client_id"]) table = prettytable.PrettyTable(["job_id", "status", "result",
"client_id"])
for job_id, job_data in six.iteritems(jobs): for job_id, job_data in six.iteritems(jobs):
table.add_row([job_id, table.add_row([job_id,
job_data['status'], job_data['status'],
@ -120,8 +119,8 @@ def do_session_list(client, args):
:return: None :return: None
""" """
table = PrettyTable(["session_id", "tag", "status", table = prettytable.PrettyTable(["session_id", "tag", "status",
"description", "jobs", "last_start"]) "description", "jobs", "last_start"])
session_docs = client.sessions.list() session_docs = client.sessions.list()
offset = 0 offset = 0
while session_docs: while session_docs:
@ -208,8 +207,9 @@ def _job_list(client, args):
def do_job_list(client, args): def do_job_list(client, args):
table = PrettyTable(["job_id", "client-id", "description", "# actions", table = prettytable.PrettyTable(["job_id", "client-id", "description",
"status", "event", "result", "session_id"]) "# actions", "status", "event",
"result", "session_id"])
for doc in _job_list(client, args): for doc in _job_list(client, args):
job_scheduling = doc.get('job_schedule', {}) job_scheduling = doc.get('job_schedule', {})
job_status = job_scheduling.get('status', '') job_status = job_scheduling.get('status', '')
@ -227,7 +227,7 @@ def do_job_list(client, args):
def do_client_list(client, args): def do_client_list(client, args):
table = PrettyTable(["client_id", "hostname", "description"]) table = prettytable.PrettyTable(["client_id", "hostname", "description"])
l = client.registration.list() l = client.registration.list()
offset = 0 offset = 0
while l: while l:
@ -249,7 +249,7 @@ def do_backup_list(client, args):
else: else:
fields = ["backup uuid", "container", "backup name", "timestamp", fields = ["backup uuid", "container", "backup name", "timestamp",
"level", "path"] "level", "path"]
table = PrettyTable(fields) table = prettytable.PrettyTable(fields)
l = list_func() l = list_func()
offset = 0 offset = 0
while l: while l:
@ -264,14 +264,14 @@ def do_backup_list(client, args):
metadata_doc.get('container', ''), metadata_doc.get('container', ''),
metadata_doc.get('hostname', ''), metadata_doc.get('hostname', ''),
metadata_doc.get('backup_name', ''), metadata_doc.get('backup_name', ''),
str(DateTime(timestamp)), str(freezer_utils.DateTime(timestamp)),
metadata_doc.get('curr_backup_level', ''), metadata_doc.get('curr_backup_level', ''),
metadata_doc.get('fs_real_path', '')] metadata_doc.get('fs_real_path', '')]
else: else:
row = [doc['backup_uuid'], row = [doc['backup_uuid'],
metadata_doc.get('container', ''), metadata_doc.get('container', ''),
metadata_doc.get('backup_name', ''), metadata_doc.get('backup_name', ''),
str(DateTime(timestamp)), str(freezer_utils.DateTime(timestamp)),
metadata_doc.get('curr_backup_level', ''), metadata_doc.get('curr_backup_level', ''),
metadata_doc.get('fs_real_path', '')] metadata_doc.get('fs_real_path', '')]
table.add_row(row) table.add_row(row)

View File

@ -12,13 +12,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from oslo_log import log
# PyCharm will not recognize queue. Puts red squiggle line under it. That's OK. # PyCharm will not recognize queue. Puts red squiggle line under it. That's OK.
from six.moves import queue from six.moves import queue
from oslo_log import log
from freezer.storage import base from freezer.storage import base
from freezer.storage.exceptions import StorageException from freezer.storage import exceptions
from freezer.utils import streaming from freezer.utils import streaming
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -62,7 +61,8 @@ class MultipleStorage(base.Storage):
got_exception) got_exception)
if (got_exception): if (got_exception):
raise StorageException("Storage error. Failed to backup.") raise exceptions.StorageException(
"Storage error. Failed to backup.")
def get_level_zero(self, def get_level_zero(self,
engine, engine,

View File

@ -15,8 +15,8 @@
import hashlib import hashlib
import os import os
from six.moves import StringIO import six
from six import PY2 # True if running on Python 2 from six import moves
from freezer.utils import utils from freezer.utils import utils
@ -119,7 +119,7 @@ class CheckSum(object):
# Need to use string-escape for Python 2 non-unicode strings. For # Need to use string-escape for Python 2 non-unicode strings. For
# Python 2 unicode strings and all Python 3 strings, we need to use # Python 2 unicode strings and all Python 3 strings, we need to use
# unicode-escape. The effect of them is the same. # unicode-escape. The effect of them is the same.
if PY2 and isinstance(buf, str): if six.PY2 and isinstance(buf, str):
buf = buf.encode('string-escape') buf = buf.encode('string-escape')
else: else:
buf = buf.encode('unicode-escape') buf = buf.encode('unicode-escape')
@ -132,7 +132,7 @@ class CheckSum(object):
""" """
:return: the hash for a given string :return: the hash for a given string
""" """
fd = StringIO(string) fd = moves.StringIO(string)
return self.hashfile(fd) return self.hashfile(fd)
def compute(self): def compute(self):