Doron Fediuck has uploaded a new change for review.

Change subject: Fixed pep8 issues
......................................................................

Fixed pep8 issues

Change-Id: Id58a37b6029ae5e86433e63bc32376ad0668bd7f
Signed-off-by: Doron Fediuck <dfedi...@redhat.com>
---
R src/ModuleAnalyzer.py
R src/PythonTaskRunner.py
R src/PythonTaskRunnerTest.py
M src/oschedproxyd.py
M src/request_handler.py
M src/utils.py
6 files changed, 79 insertions(+), 67 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-scheduler-proxy 
refs/changes/95/17395/1

diff --git a/src/loader.py b/src/ModuleAnalyzer.py
similarity index 94%
rename from src/loader.py
rename to src/ModuleAnalyzer.py
index 1301d42..ac9eaca 100644
--- a/src/loader.py
+++ b/src/ModuleAnalyzer.py
@@ -7,10 +7,12 @@
     Loads a module and checks if it has certain functions
     Will run as a process for safety, so it prints the result to stdout
 '''
+
+
 def analyze(path, name):
     os.chdir(path)
     mod = __import__(name)
-    retValue =(name,)
+    retValue = (name,)
     if hasattr(mod, _utils.FILTER):
         retValue += (_utils.FILTER,)
 
diff --git a/src/runner.py b/src/PythonTaskRunner.py
similarity index 92%
rename from src/runner.py
rename to src/PythonTaskRunner.py
index 7e37b78..4ce3e71 100755
--- a/src/runner.py
+++ b/src/PythonTaskRunner.py
@@ -23,16 +23,16 @@
 from ast import literal_eval
 from utils import utils
 
-class PythonMethodRunner(Thread):
+
+class PythonTaskRunner(Thread):
     def __init__(self, path, module, method, args):
-        super(PythonMethodRunner, self).__init__(group=None)
+        super(PythonTaskRunner, self).__init__(group=None)
         self._path = path
         self._result = None
         self._error = None
         self._process = None
         self._utils = utils()
         self._script = self.createScript(module, method, args)
-
 
     def run(self):
         try:
@@ -59,4 +59,4 @@
             module + "." +
             method + self._utils.createFunctionStringArgs(args)
             )
-        return  ["python", "-c", commandString]
+        return ["python", "-c", commandString]
diff --git a/src/runner_test.py b/src/PythonTaskRunnerTest.py
similarity index 90%
rename from src/runner_test.py
rename to src/PythonTaskRunnerTest.py
index bd43ab0..5b2b8b6 100644
--- a/src/runner_test.py
+++ b/src/PythonTaskRunnerTest.py
@@ -20,7 +20,7 @@
 
 import unittest
 
-from runner import ScriptRunner
+import PythonTaskRunner
 import os
 
 
@@ -30,10 +30,10 @@
                                   'plugins/filters/dummy.py')
         inputpath = os.path.join(os.getcwd(),
                                  'samples/singlehost.xml')
-        runner = ScriptRunner(scriptpath,
+        runner = PythonTaskRunner.PythonTaskRunner(scriptpath,
                               file(inputpath).read())
         runner.start()
         runner.join()
-        result = runner.get_result()
+        result = runner.getResults()
         assert result
         pass
diff --git a/src/oschedproxyd.py b/src/oschedproxyd.py
index 778c962..b0996c1 100644
--- a/src/oschedproxyd.py
+++ b/src/oschedproxyd.py
@@ -28,12 +28,14 @@
 import sys
 from time import strftime
 
+
 class SimpleThreadedXMLRPCServer(SocketServer.ThreadingMixIn,
                                  SimpleXMLRPCServer.SimpleXMLRPCServer):
     pass
 
 
-log_filename = '/var/log/ovirt/ovirt-scheduler-proxy.' + 
strftime("%Y%m%d_%H%M%S") + '.log'
+log_filename = ('/var/log/ovirt/ovirt-scheduler-proxy.' +
+                strftime("%Y%m%d_%H%M%S") + '.log')
 
 try:
     logging.basicConfig(level=logging.DEBUG,
@@ -43,14 +45,16 @@
                         filename=log_filename,
                         filemode='w')
 except:
-    log_filename = './ovirt-scheduler-proxy.' + strftime("%Y%m%d_%H%M%S") + 
'.log'
+    log_filename = ('./ovirt-scheduler-proxy.' +
+                    strftime("%Y%m%d_%H%M%S") + '.log')
     logging.basicConfig(level=logging.DEBUG,
                         name="ovirt-scheduler-proxy",
                         format='%(asctime)s %(levelname)-8s %(message)s',
                         datefmt='%a, %d %b %Y %H:%M:%S',
                         filename=log_filename,
                         filemode='w')
-    
+
+
 class ProxyServer():
     _server = ""
     _handler = ""
@@ -61,19 +65,19 @@
     def setup(self):
         logging.info("Setting up server")
         self._server = SimpleThreadedXMLRPCServer(
-                                         ("localhost", 18781),
-                                         allow_none=True
-                                         )
+            ("localhost", 18781),
+            allow_none=True
+            )
 
         # TODO make by config
         logging.info("Loading modules")
         self._handler = RequestHandler(
             os.path.join(os.getcwd() + '/../', "plugins"),
             os.getcwd()
-            )        
+            )
 
     def run(self):
-        logging.info("Loading modules")
+        logging.info("Running server")
         self._server.register_introspection_functions()
         self._server.register_instance(API(self._handler))
         self._server.serve_forever()
diff --git a/src/request_handler.py b/src/request_handler.py
index c7ef697..28069e0 100644
--- a/src/request_handler.py
+++ b/src/request_handler.py
@@ -21,14 +21,14 @@
 
 import os
 import time
-from runner import PythonMethodRunner
+from PythonTaskRunner import PythonTaskRunner
 from utils import utils
 
 
 class RequestHandler(object):
     '''
     RequestHandler runs all the plugins in parallel using instances of
-    PythonMethodRunner
+    PythonTaskRunner
     When all threads are joined, the results are aggregated and returned
 
     Keyword arguments:
@@ -43,7 +43,6 @@
         self._utils = utils()
         self.loadModules()
 
-
     '''
         Safely load the user modules in another process
         and find what they implement
@@ -54,7 +53,7 @@
             if not os.path.splitext(fileName)[1] == '.py':
                 continue
             module = os.path.splitext(fileName)[0]
-            runner = PythonMethodRunner(
+            runner = PythonTaskRunner(
                 self._analyzerDir,
                 self._utils.LOADER_MODULE,
                 self._utils.LOADER_FUNC,
@@ -89,26 +88,28 @@
             "balance": list(self._balancers)
             }
 
+    #Intersects the results from the filters
+    def aggregateFilterResults(self, filterRunners):
+        resultSet = None
+        for runner in filterRunners:
+            if runner.getResults() is None:
+                continue
+            hosts = runner.getResults()
+            if resultSet is None:
+                resultSet = set(hosts)
+                continue
+            resultSet = resultSet.intersection(hosts)
+
+        return list(resultSet)
+
     def run_filters(self, filters, hostsXml, vmXml, properties_map):
-        #Intersects the results from the filters
-        def aggregateResults(filterRunners):
-            resultSet = None
-            for runner in filterRunners:
-                if runner.getResults() is None:
-                    continue
-                hosts = runner.getResults()
-                if resultSet is None:
-                    resultSet = set(hosts)
-                    continue
-                resultSet = resultSet.intersection(hosts)
-            return list(resultSet)
         #run each filter in a process for robustness
         filterRunners = []
         for f in filters:
             if f not in self._filters:
                 #log?
                 continue
-            runner = PythonMethodRunner(
+            runner = PythonTaskRunner(
                 self._pluginDir,
                 f,
                 self._utils.FILTER,
@@ -122,23 +123,25 @@
         #TODO add timeout config
         self._utils.waitOnGroup(filterRunners)
 
-        return aggregateResults(filterRunners)
+        return self.aggregateFilterResults(filterRunners)
 
-    def run_cost_functions(self, cost_functions, hostsXml, vmXml, 
properties_map):
-        #accumalate the results
-        def aggregateResults(scoreRunners):
-            results = {}
-            for runner, weight in scoreRunners:
-                if runner.getResults() is None:
-                    continue
-                hostScores = runner.getResults()
-                for host, score in hostScores:
-                    if not host in results:
-                        results[host] = 0
-                    results[host] += weight * score
+    #accumulate the results
+    def aggregateWeightResults(self, scoresList):
+        results = {}
+        for runner, weight in scoresList:
+            if runner.getResults() is None:
+                continue
+            hostScores = runner.getResults()
+            for host, score in hostScores:
+                if not host in results:
+                    results[host] = 0
+                results[host] += weight * score
+    
+        return [(host, totalScore) for host,
+                totalScore in results.iteritems()]
 
-            return [(host, totalScore) for host, totalScore in 
results.iteritems()]
-
+    def run_cost_functions(self, cost_functions, hostsXml,
+                           vmXml, properties_map):
 
         #run each filter in a process for robustness
         scoreRunners = []
@@ -147,7 +150,7 @@
             if name not in self._scores:
                 #log?
                 continue
-            runner = PythonMethodRunner(
+            runner = PythonTaskRunner(
                 self._pluginDir,
                 name,
                 self._utils.SCORE,
@@ -161,20 +164,20 @@
 
         self._utils.waitOnGroup(scoreRunners)
 
-        return aggregateResults(zip(scoreRunners, weights))
+        return self.aggregateWeightResults(zip(scoreRunners, weights))
 
     def run_load_balancing(self, balance, hostsXml, properties_map):
         if balance not in self._balancers:
             #log?
             return
 
-        runner = PythonMethodRunner(
-                self._pluginDir,
-                balance,
-                self._utils.BALANCE,
-                (hostsXml, properties_map)
-                )
+        runner = PythonTaskRunner(
+            self._pluginDir,
+            balance,
+            self._utils.BALANCE,
+            (hostsXml, properties_map)
+            )
 
         runner.start()
         runner.join(30)
-        return runner.getResults()
\ No newline at end of file
+        return runner.getResults()
diff --git a/src/utils.py b/src/utils.py
index 6c63355..647e598 100644
--- a/src/utils.py
+++ b/src/utils.py
@@ -25,27 +25,30 @@
     def __init__(self):
         pass
 
-    FILTER='filterFunction'
-    SCORE='scoreFunction'
-    BALANCE='balanceFunction'
-    LOADER_MODULE='loader'
-    LOADER_FUNC='analyze'
+    FILTER = 'filterFunction'
+    SCORE = 'scoreFunction'
+    BALANCE = 'balanceFunction'
+    LOADER_MODULE = 'ModuleAnalyzer'
+    LOADER_FUNC = 'analyze'
 
     '''
         Creates a process from script
     '''
-    def createProcess(self, script, runLocation):
+    def createProcess(self, script, runLocation=None):
         #script should be a list and not a string
         if isinstance(script, basestring):
             script = [script]
-        process = subprocess.Popen(script, stdin=subprocess.PIPE, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=runLocation)
+        process = subprocess.Popen(script, stdin=subprocess.PIPE,
+                                   stdout=subprocess.PIPE,
+                                   stderr=subprocess.PIPE,
+                                   cwd=runLocation)
         return process
 
     '''
        Create a process and execute it
        Done synchronously
     '''
-    def execInProcess(self,script, script_input=None):
+    def execInProcess(self, script, script_input=None):
         process = self.createProcess(script)
         return process.communicate(script_input)
 
@@ -67,7 +70,7 @@
             if timeLeft < 0:
                 break
             runner.join(timeLeft)
-        #Make sure we dont have dangling processes
+        #Make sure we don't have dangling processes
         for runner in runners:
             runner.stop()
 
@@ -80,4 +83,4 @@
         if isinstance(args, basestring):
             return '(' + args + ')'
         # then it must be some kind of list, return as (a,b, ...)
-        return str(tuple(args))
\ No newline at end of file
+        return str(tuple(args))


-- 
To view, visit http://gerrit.ovirt.org/17395
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id58a37b6029ae5e86433e63bc32376ad0668bd7f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-scheduler-proxy
Gerrit-Branch: master
Gerrit-Owner: Doron Fediuck <dfedi...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to