Skip to content

Commit

Permalink
SectionExecutor: Remove class
Browse files Browse the repository at this point in the history
This is part of a refactoring from SectionExecutor to a bunch of
functions.
  • Loading branch information
Udayan12167 committed May 31, 2015
1 parent 230dfd3 commit 7863e14
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 65 deletions.
6 changes: 3 additions & 3 deletions coalib/coala.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from coalib.output.ClosableObject import ClosableObject
from coalib.output.printers.ConsolePrinter import ConsolePrinter
from coalib.misc.StringConstants import StringConstants
from coalib.processes.SectionExecutor import SectionExecutor
from coalib.settings.ConfigurationGathering import gather_configuration
from coalib.processes.SectionExecutor import execute_section
from coalib.misc.i18n import _


Expand All @@ -40,12 +40,12 @@ def main():
continue

interactor.begin_section(section)
yielded_results = yielded_results or SectionExecutor(
yielded_results = yielded_results or execute_section(
section=section,
global_bear_list=global_bears[section_name],
local_bear_list=local_bears[section_name],
interactor=interactor,
log_printer=log_printer).run()[0]
log_printer=log_printer)[0]
did_nothing = False

if did_nothing:
Expand Down
98 changes: 47 additions & 51 deletions coalib/processes/SectionExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,66 +248,62 @@ def process_queues(interactor,
return retval


class SectionExecutor:
def execute_section(section,
global_bear_list,
local_bear_list,
interactor,
log_printer):
"""
The section executor does the following things:
Executes the section with the given bears.
The execute_section method does the following things:
1. Prepare a BearRunner
* Load files
* Create queues
2. Spawn up one or more BearRunner's
3. Output results from the BearRunner's
4. Join all processes
:param section: The section to execute.
:param global_bear_list: List of global bears belonging to the section.
:param local_bear_list: List of local bears belonging to the section.
:param interactor: The interactor the user interacts with.
:param log_printer: The log_printer to warn to.
:return: Tuple containing a bool (True if results were yielded, False
otherwise), a Manager.dict containing all local results
(filenames are key) and a Manager.dict containing all global
bear results (bear names are key).
"""
local_bear_list = Dependencies.resolve(local_bear_list)
global_bear_list = Dependencies.resolve(global_bear_list)

def __init__(self,
section,
local_bear_list,
global_bear_list,
interactor,
log_printer):
self.section = section
self.local_bear_list = Dependencies.resolve(local_bear_list)
self.global_bear_list = Dependencies.resolve(global_bear_list)

self.interactor = interactor
self.log_printer = log_printer

def run(self):
"""
Executes the section with the given bears.
:return: Tuple containing a bool (True if results were yielded, False
otherwise), a Manager.dict containing all local results
(filenames are key) and a Manager.dict containing all global
bear results (bear names are key).
"""
running_processes = get_cpu_count()
processes, arg_dict = instantiate_processes(self.section,
self.log_printer,
self.local_bear_list,
self.global_bear_list,
running_processes)

logger_thread = LogPrinterThread(arg_dict["message_queue"],
self.log_printer)
# Start and join the logger thread along with the BearRunner's
processes.append(logger_thread)
running_processes = get_cpu_count()
processes, arg_dict = instantiate_processes(section,
log_printer,
local_bear_list,
global_bear_list,
running_processes)

for runner in processes:
runner.start()
logger_thread = LogPrinterThread(arg_dict["message_queue"],
log_printer)
# Start and join the logger thread along with the BearRunner's
processes.append(logger_thread)

try:
return (process_queues(self.interactor,
processes,
arg_dict["control_queue"],
arg_dict["local_result_dict"],
arg_dict["global_result_dict"],
arg_dict["file_dict"]),
arg_dict["local_result_dict"],
arg_dict["global_result_dict"])
finally:
logger_thread.running = False

for runner in processes:
runner.join()
for runner in processes:
runner.start()

try:
return (process_queues(interactor,
processes,
arg_dict["control_queue"],
arg_dict["local_result_dict"],
arg_dict["global_result_dict"],
arg_dict["file_dict"]),
arg_dict["local_result_dict"],
arg_dict["global_result_dict"])
finally:
logger_thread.running = False

for runner in processes:
runner.join()
22 changes: 11 additions & 11 deletions coalib/tests/processes/SectionExecutorTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from coalib.settings.ConfigurationGathering import gather_configuration
from coalib.output.Interactor import Interactor
from coalib.output.printers.LogPrinter import LogPrinter
from coalib.processes.SectionExecutor import SectionExecutor
from coalib.processes.SectionExecutor import execute_section
from coalib.output.printers.ConsolePrinter import ConsolePrinter
from coalib.processes.CONTROL_ELEMENT import CONTROL_ELEMENT
from coalib.processes.SectionExecutor import process_queues
Expand Down Expand Up @@ -83,14 +83,12 @@ def setUp(self):
self.result_queue,
self.log_queue)

self.uut = SectionExecutor(self.sections["default"],
self.local_bears["default"],
self.global_bears["default"],
self.interactor,
self.interactor)

def test_run(self):
results = self.uut.run()
results = execute_section(self.sections["default"],
self.global_bears["default"],
self.local_bears["default"],
self.interactor,
self.interactor)
self.assertTrue(results[0])

local_results = self.result_queue.get(timeout=0)
Expand Down Expand Up @@ -121,9 +119,11 @@ def test_run(self):
# shouldn't make maintenance so hard for us here.

def test_empty_run(self):
self.uut.global_bear_list = []
self.uut.local_bear_list = []
results = self.uut.run()
results = execute_section(self.sections["default"],
[],
[],
self.interactor,
self.interactor)
# No results
self.assertFalse(results[0])
# One file
Expand Down

0 comments on commit 7863e14

Please sign in to comment.