From 35d648c9cfc1dc1f6540b483ed36f74ddeb42b5e Mon Sep 17 00:00:00 2001 From: Nguyen Hung Phuong Date: Thu, 25 Aug 2016 10:27:33 +0700 Subject: [PATCH] Clean imports in code In some part in the code we import objects. In the Openstack style guidelines they recommend to import only modules. http://docs.openstack.org/developer/hacking/#imports Change-Id: Icae231f06f3c4fd15256f06a464d3ba3e2845e33 --- browbeat.py | 18 +++++++++--------- lib/Connmon.py | 4 ++-- lib/Elastic.py | 4 ++-- lib/PerfKit.py | 16 ++++++++-------- lib/Rally.py | 26 +++++++++++++------------- lib/Shaker.py | 26 +++++++++++++------------- lib/Tools.py | 6 +++--- lib/WorkloadBase.py | 15 +++++++-------- 8 files changed, 57 insertions(+), 58 deletions(-) diff --git a/browbeat.py b/browbeat.py index ca7827f30..7f7128ef2 100755 --- a/browbeat.py +++ b/browbeat.py @@ -12,10 +12,10 @@ # limitations under the License. from lib.Elastic import browbeat_uuid -from lib.PerfKit import PerfKit -from lib.Rally import Rally -from lib.Shaker import Shaker -from lib.WorkloadBase import WorkloadBase +from lib import PerfKit +from lib import Rally +from lib import Shaker +from lib import WorkloadBase import argparse import logging import sys @@ -55,13 +55,13 @@ def validate_yaml(config, _logger): def _run_workload_provider(provider, config): _logger = logging.getLogger('browbeat') if provider == "perfkit": - perfkit = PerfKit(config) + perfkit = PerfKit.PerfKit(config) perfkit.start_workloads() elif provider == "rally": - rally = Rally(config) + rally = Rally.Rally(config) rally.start_workloads() elif provider == "shaker": - shaker = Shaker(config) + shaker = Shaker.Shaker(config) shaker.run_shaker() else: _logger.error("Unknown workload provider: {}".format(provider)) @@ -128,10 +128,10 @@ def main(): else: _logger.error("{} is missing in {}".format(wkld_provider, _cli_args.setup)) result_dir = _config['browbeat']['results'] - WorkloadBase.print_report(result_dir, time_stamp) + WorkloadBase.WorkloadBase.print_report(result_dir, time_stamp) _logger.info("Saved browbeat result summary to {}".format( os.path.join(result_dir,time_stamp + '.' + 'report'))) - WorkloadBase.print_summary() + WorkloadBase.WorkloadBase.print_summary() _logger.info("Browbeat Finished, UUID: {}".format(browbeat_uuid)) if __name__ == '__main__': diff --git a/lib/Connmon.py b/lib/Connmon.py index 0073915b0..ed1ca0502 100644 --- a/lib/Connmon.py +++ b/lib/Connmon.py @@ -10,7 +10,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from Tools import Tools +import Tools import os import logging import shutil @@ -20,7 +20,7 @@ class Connmon: def __init__(self, config): self.logger = logging.getLogger('browbeat.Connmon') self.config = config - self.tools = Tools(self.config) + self.tools = Tools.Tools(self.config) return None # Start connmond diff --git a/lib/Elastic.py b/lib/Elastic.py index 2276bad4c..3fe220430 100644 --- a/lib/Elastic.py +++ b/lib/Elastic.py @@ -10,7 +10,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from elasticsearch import Elasticsearch +import elasticsearch import logging import json import datetime @@ -27,7 +27,7 @@ class Elastic: def __init__(self, config, workload, tool="browbeat"): self.config = config self.logger = logging.getLogger('browbeat.Elastic') - self.es = Elasticsearch([ + self.es = elasticsearch.Elasticsearch([ {'host': self.config['elasticsearch']['host'], 'port': self.config['elasticsearch']['port']}], send_get_body_as='POST' diff --git a/lib/PerfKit.py b/lib/PerfKit.py index a8c0d4dd8..6f9671dba 100644 --- a/lib/PerfKit.py +++ b/lib/PerfKit.py @@ -10,28 +10,28 @@ # See the License for the specific language governing permissions and # limitations under the License. -from Connmon import Connmon -from Grafana import Grafana -from Tools import Tools -from WorkloadBase import WorkloadBase +import Connmon import datetime import glob +import Grafana import logging import os import shutil import subprocess import time +import Tools +import WorkloadBase -class PerfKit(WorkloadBase): +class PerfKit(WorkloadBase.WorkloadBase): def __init__(self, config): self.logger = logging.getLogger('browbeat.PerfKit') self.config = config self.error_count = 0 - self.tools = Tools(self.config) - self.connmon = Connmon(self.config) - self.grafana = Grafana(self.config) + self.tools = Tools.Tools(self.config) + self.connmon = Connmon.Connmon(self.config) + self.grafana = Grafana.Grafana(self.config) self.test_count = 0 self.scenario_count = 0 self.pass_count = 0 diff --git a/lib/Rally.py b/lib/Rally.py index 1cd61c4ab..47c539ef6 100644 --- a/lib/Rally.py +++ b/lib/Rally.py @@ -10,30 +10,30 @@ # See the License for the specific language governing permissions and # limitations under the License. -from Connmon import Connmon -from Tools import Tools -from collections import OrderedDict -from Grafana import Grafana -from WorkloadBase import WorkloadBase -from Elastic import Elastic +import collections +import Connmon import datetime +import Elastic import glob +import Grafana import logging import os +import re import shutil import time -import re +import Tools +import WorkloadBase -class Rally(WorkloadBase): +class Rally(WorkloadBase.WorkloadBase): def __init__(self, config, hosts=None): self.logger = logging.getLogger('browbeat.Rally') self.config = config - self.tools = Tools(self.config) - self.connmon = Connmon(self.config) - self.grafana = Grafana(self.config) - self.elastic = Elastic(self.config, self.__class__.__name__.lower()) + self.tools = Tools.Tools(self.config) + self.connmon = Connmon.Connmon(self.config) + self.grafana = Grafana.Grafana(self.config) + self.elastic = Elastic.Elastic(self.config, self.__class__.__name__.lower()) self.error_count = 0 self.pass_count = 0 self.test_count = 0 @@ -173,7 +173,7 @@ class Rally(WorkloadBase): def start_workloads(self): """Iterates through all rally scenarios in browbeat yaml config file""" - results = OrderedDict() + results = collections.OrderedDict() self.logger.info("Starting Rally workloads") es_ts = datetime.datetime.utcnow() dir_ts = es_ts.strftime("%Y%m%d-%H%M%S") diff --git a/lib/Shaker.py b/lib/Shaker.py index 3899e9137..0ce901b4d 100644 --- a/lib/Shaker.py +++ b/lib/Shaker.py @@ -10,28 +10,28 @@ # See the License for the specific language governing permissions and # limitations under the License. -from Tools import Tools -from Grafana import Grafana -from WorkloadBase import WorkloadBase -from Elastic import Elastic -from collections import OrderedDict -import yaml -import logging +import collections import datetime -import os +import Elastic +import Grafana import json +import logging +import os import time +import Tools import uuid +import WorkloadBase +import yaml -class Shaker(WorkloadBase): +class Shaker(WorkloadBase.WorkloadBase): def __init__(self, config): self.logger = logging.getLogger('browbeat.Shaker') self.config = config - self.tools = Tools(self.config) - self.grafana = Grafana(self.config) - self.elastic = Elastic(self.config, self.__class__.__name__.lower()) + self.tools = Tools.Tools(self.config) + self.grafana = Grafana.Grafana(self.config) + self.elastic = Elastic.Elastic(self.config, self.__class__.__name__.lower()) self.error_count = 0 self.pass_count = 0 self.test_count = 0 @@ -147,7 +147,7 @@ class Shaker(WorkloadBase): metadata = data['records'][record].pop('meta') samples = data['records'][record].pop('samples') # Ordered Dictionary to capture result types and metrics - outputs = OrderedDict() + outputs = collections.OrderedDict() for metric in metadata: outputs[metric[0]] = metric[1] # Iterate over each result type for each sample in record and diff --git a/lib/Tools.py b/lib/Tools.py index 94334edb1..69ad8caff 100644 --- a/lib/Tools.py +++ b/lib/Tools.py @@ -12,8 +12,7 @@ import logging import os -from subprocess import Popen -from subprocess import PIPE +import subprocess class Tools: @@ -26,7 +25,8 @@ class Tools: # Run command, return stdout as result def run_cmd(self, cmd): self.logger.debug("Running command : %s" % cmd) - process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) + process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) stdout, stderr = process.communicate() if len(stderr) > 0: return None diff --git a/lib/WorkloadBase.py b/lib/WorkloadBase.py index 327d0a57f..a6f468708 100644 --- a/lib/WorkloadBase.py +++ b/lib/WorkloadBase.py @@ -10,15 +10,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta -from abc import abstractmethod -import os +import abc import logging +import os import yaml class WorkloadBase: - __metaclass__ = ABCMeta + __metaclass__ = abc.ABCMeta logger = logging.getLogger('browbeat.WorkloadBase') success = 0 failure = 0 @@ -26,19 +25,19 @@ class WorkloadBase: total_scenarios = 0 browbeat = {} - @abstractmethod + @abc.abstractmethod def update_scenarios(self): pass - @abstractmethod + @abc.abstractmethod def update_tests(self): pass - @abstractmethod + @abc.abstractmethod def update_pass_tests(self): pass - @abstractmethod + @abc.abstractmethod def update_fail_tests(self): pass