Skip to content

Commit

Permalink
Collectors: Remove default log printers
Browse files Browse the repository at this point in the history
Default log printers are dangerous since they get instantiated on
importing the module and thus never get closed.
  • Loading branch information
sils committed Apr 15, 2015
1 parent 8acc2a4 commit a8273ea
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
18 changes: 9 additions & 9 deletions coalib/collecting/Collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def _import_bears(file_path, kinds):


@yield_once
def icollect(file_paths, files=True, dirs=True, log_printer=ConsolePrinter()):
def icollect(file_paths, log_printer, files=True, dirs=True):
"""
Evaluate globs in file paths and return all matching files.
:param file_paths: list of file paths that can include globs
:param log_printer: where to log things that go wrong
:param files: True if files are to be collected
:param dirs: True if dirs are to be collected
:param log_printer: where to log things that go wrong
:return: iterator that yields paths of all matching files
:raises SystemExit: when getting an invalid pattern
"""
Expand All @@ -56,26 +56,26 @@ def icollect(file_paths, files=True, dirs=True, log_printer=ConsolePrinter()):
raise SystemExit(-1)


def collect_files(file_paths):
def collect_files(file_paths, log_printer):
"""
Evaluate globs in file paths and return all matching files
:param file_paths: list of file paths that can include globs
:return: list of paths of all matching files
"""
return list(icollect(file_paths, dirs=False))
return list(icollect(file_paths, log_printer, dirs=False))


def collect_dirs(dir_paths):
def collect_dirs(dir_paths, log_printer):
"""
Evaluate globs in directory paths and return all matching directories
:param dir_paths: list of file paths that can include globs
:return: list of paths of all matching directories
"""
return list(icollect(dir_paths, files=False))
return list(icollect(dir_paths, log_printer, files=False))


@yield_once
def icollect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()):
def icollect_bears(bear_dirs, bear_names, kinds, log_printer):
"""
Collect all bears from bear directories that have a matching kind.
:param bear_dirs: directories that can contain bears
Expand All @@ -84,7 +84,7 @@ def icollect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()):
:param log_printer: log_printer to handle logging
:return: iterator that yields bear classes
"""
for bear_dir in icollect(bear_dirs, files=False):
for bear_dir in icollect(bear_dirs, log_printer, files=False):
for bear_name in bear_names:
for matching_file in iglob(
os.path.join(bear_dir, bear_name + '.py')):
Expand All @@ -99,7 +99,7 @@ def icollect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()):
.format(file=matching_file))


def collect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()):
def collect_bears(bear_dirs, bear_names, kinds, log_printer):
"""
Collect all bears from bear directories that have a matching kind.
:param bear_dirs: directories that can contain bears
Expand Down
4 changes: 3 additions & 1 deletion coalib/processes/SectionExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def _instantiate_bears(self, file_dict, message_queue):
TIMEOUT=0.1)

def _instantiate_processes(self, job_count):
filename_list = collect_files(path_list(self.section.get('files', "")))
filename_list = collect_files(path_list(self.section.get('files',
"")),
self.log_printer)
file_dict = self._get_file_dict(filename_list)

manager = multiprocessing.Manager()
Expand Down
6 changes: 4 additions & 2 deletions coalib/settings/SectionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ def _fill_settings(self):
bears = list(section.get("bears", ""))
local_bears = collect_bears(bear_dirs,
bears,
[BEAR_KIND.LOCAL])
[BEAR_KIND.LOCAL],
self.log_printer)
global_bears = collect_bears(bear_dirs,
bears,
[BEAR_KIND.GLOBAL])
[BEAR_KIND.GLOBAL],
self.log_printer)
filler = SectionFiller(section, self.interactor, self.log_printer)
all_bears = copy.deepcopy(local_bears)
all_bears.extend(global_bears)
Expand Down
62 changes: 42 additions & 20 deletions coalib/tests/collecting/CollectorsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

sys.path.insert(0, ".")
from coalib.output.printers.ConsolePrinter import ConsolePrinter
from coalib.collecting.Collectors import collect_files, \
collect_dirs, \
collect_bears
Expand All @@ -14,21 +15,26 @@ def setUp(self):
current_dir = os.path.split(inspect.getfile(inspect.currentframe()))[0]
self.collectors_test_dir = os.path.join(current_dir,
"collectors_test_dir")
self.log_printer = ConsolePrinter()

def tearDown(self):
self.log_printer.close()

def test_file_empty(self):
self.assertRaises(TypeError, collect_files)

def test_file_invalid(self):
self.assertEqual(collect_files(["invalid_path"]), [])
self.assertEqual(collect_files(["invalid_path"], self.log_printer), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])
self.assertRaises(SystemExit, collect_files, ["**d"], self.log_printer)

def test_file_collection(self):
self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
"others",
"*",
"*2.py")]),
"*2.py")],
self.log_printer),
[os.path.join(self.collectors_test_dir,
"others",
"py_files",
Expand All @@ -41,19 +47,25 @@ def setUp(self):
self.collectors_test_dir = os.path.join(current_dir,
"collectors_test_dir")

self.log_printer = ConsolePrinter()

def tearDown(self):
self.log_printer.close()

def test_dir_empty(self):
self.assertRaises(TypeError, collect_dirs)

def test_dir_invalid(self):
self.assertEqual(collect_dirs(["invalid_path"]), [])
self.assertEqual(collect_dirs(["invalid_path"], self.log_printer), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])
self.assertRaises(SystemExit, collect_files, ["**d"], self.log_printer)

def test_dir_collection(self):
self.assertEqual(
sorted(collect_dirs([os.path.join(self.collectors_test_dir,
"**")])),
"**")],
self.log_printer)),
sorted([os.path.join(self.collectors_test_dir, "bears"),
os.path.join(self.collectors_test_dir, "bears", "__pycache__"),
os.path.join(self.collectors_test_dir, "others"),
Expand All @@ -68,40 +80,50 @@ def setUp(self):
self.collectors_test_dir = os.path.join(current_dir,
"collectors_test_dir")

self.log_printer = ConsolePrinter()

def tearDown(self):
self.log_printer.close()

def test_bear_empty(self):
self.assertRaises(TypeError, collect_bears)

def test_bear_invalid(self):
self.assertEqual(collect_bears(["invalid_paths"],
["invalid_name"],
["invalid kind"]), [])
["invalid kind"],
self.log_printer), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])
self.assertRaises(SystemExit, collect_files, ["**d"], self.log_printer)

def test_simple_single(self):
self.assertEqual(len(collect_bears([os.path.join(
self.collectors_test_dir, "bears")],
self.assertEqual(len(collect_bears(
[os.path.join(self.collectors_test_dir, "bears")],
["bear1"],
["kind"])), 1)
["kind"],
self.log_printer)), 1)

def test_reference_single(self):
self.assertEqual(len(collect_bears([os.path.join(
self.collectors_test_dir, "bears")],
self.assertEqual(len(collect_bears(
[os.path.join(self.collectors_test_dir, "bears")],
["metabear"],
["kind"])), 1)
["kind"],
self.log_printer)), 1)

def test_no_duplications(self):
self.assertEqual(len(collect_bears([os.path.join(
self.collectors_test_dir, "bears", "**")],
self.assertEqual(len(collect_bears(
[os.path.join(self.collectors_test_dir, "bears", "**")],
["*"],
["kind"])), 2)
["kind"],
self.log_printer)), 2)

def test_wrong_kind(self):
self.assertEqual(len(collect_bears([os.path.join(
self.collectors_test_dir, "bears", "**")],
self.assertEqual(len(collect_bears(
[os.path.join(self.collectors_test_dir, "bears", "**")],
["*"],
["other_kind"])), 0)
["other_kind"],
self.log_printer)), 0)


if __name__ == '__main__':
Expand Down

0 comments on commit a8273ea

Please sign in to comment.