Skip to content

Commit

Permalink
Add TARGETS argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sils committed Feb 10, 2015
1 parent 74640b5 commit 339c1e5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
15 changes: 9 additions & 6 deletions coala
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ if __name__ == "__main__":
# Set default section so we have a loger in case the SectionManager fails.
sections = {"default": Section("default")}
try:
sections, all_local_bears, all_global_bears = SectionManager().run()
sections, local_bears, global_bears, targets = SectionManager().run()
for section_name in sections:
section = sections[section_name]
if not bool(section.get("enabled", "true")):
if len(targets) == 0:
if not bool(section.get("enabled", "true")):
continue
elif section_name not in targets:
continue

global_bears = all_global_bears[section_name]
local_bears = all_local_bears[section_name]
sec_global_bears = global_bears[section_name]
sec_local_bears = local_bears[section_name]

SectionExecutor(section=section,
global_bear_list=global_bears,
local_bear_list=local_bears).run()
global_bear_list=sec_global_bears,
local_bear_list=sec_local_bears).run()
except KeyboardInterrupt:
print(_("Program terminated by user."))
except SystemExit:
Expand Down
3 changes: 3 additions & 0 deletions coalib/parsing/DefaultArgParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
description=__doc__
)

default_arg_parser.add_argument('TARGETS',
nargs='*',
help=_("Sections to be executed exclusively."))
default_arg_parser.add_argument('-f',
'--files',
nargs='+',
Expand Down
29 changes: 18 additions & 11 deletions coalib/settings/SectionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SectionManager:
def __init__(self):
self.cli_sections = None
self.default_section = None
self.conf_sections = None
self.sections = None

self.cli_parser = CliParser()
self.conf_parser = ConfParser()
Expand All @@ -41,12 +41,14 @@ def __init__(self):
self.local_bears = {}
self.global_bears = {}

self.targets = []

def run(self, arg_list=sys.argv[1:]):
self._load_configuration(arg_list)
self._fill_settings()
self._save_configuration()

return self.conf_sections, self.local_bears, self.global_bears
return self.sections, self.local_bears, self.global_bears, self.targets

def _load_configuration(self, arg_list):
self.cli_sections = self.cli_parser.reparse(arg_list=arg_list)
Expand All @@ -63,25 +65,30 @@ def _load_configuration(self, arg_list):
StringConstants.THIS_IS_A_BUG)
raise SystemExit

# We dont want to store targets argument back to file, thus remove it
for item in list(self.cli_sections["default"].contents.pop("targets",
"")):
self.targets.append(item.lower())

for section in self.cli_sections:
self.cli_sections[section].defaults = self.default_section

try:
config = os.path.abspath(
str(self.cli_sections["default"].get("config", "./coafile"))
)
self.conf_sections = self.conf_parser.reparse(config)
self.sections = self.conf_parser.reparse(config)

# We'll get the default section as default section for every
# section in this dict with this. Furthermore we will have the
# CLI Values take precedence over the conf values.
self._merge_section_dicts()
except self.conf_parser.FileNotFoundError:
self.conf_sections = self.cli_sections
self.sections = self.cli_sections

def _fill_settings(self):
for section_name in self.conf_sections:
section = self.conf_sections[section_name]
for section_name in self.sections:
section = self.sections[section_name]
section.retrieve_logging_objects()

bear_dirs = path_list(section["bear_dirs"])
Expand All @@ -103,22 +110,22 @@ def _fill_settings(self):

def _save_configuration(self):
self.conf_writer = None
default_section = self.conf_sections["default"]
default_section = self.sections["default"]
try:
if bool(default_section["save"]):
self.conf_writer = ConfWriter(str(default_section["config"]))
except ValueError:
self.conf_writer = ConfWriter(str(default_section["save"]))

if self.conf_writer is not None:
self.conf_writer.write_sections(self.conf_sections)
self.conf_writer.write_sections(self.sections)

def _merge_section_dicts(self):
for name in self.cli_sections:
if name in self.conf_sections:
self.conf_sections[name].update(
if name in self.sections:
self.sections[name].update(
self.cli_sections[name],
ignore_defaults=(name != "default"))
else:
# no deep copy needed
self.conf_sections[name] = self.cli_sections[name]
self.sections[name] = self.cli_sections[name]
3 changes: 2 additions & 1 deletion coalib/tests/processes/SectionExecutorTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def setUp(self):
".coafile"))
self.testcode_c_path = os.path.join(os.path.dirname(config_path), "testcode.c")

self.sections, self.local_bears, self.global_bears = SectionManager().run(["--config", config_path])
self.sections, self.local_bears, self.global_bears, targets =\
SectionManager().run(["--config", config_path])
self.assertEqual(len(self.local_bears["default"]), 1)
self.assertEqual(len(self.global_bears["default"]), 1)

Expand Down
38 changes: 26 additions & 12 deletions coalib/tests/settings/SectionManagerTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ def test_run(self):
defaults = ConfParser().parse(os.path.abspath(os.path.join(StringConstants.coalib_root, "default_coafile")))

uut = SectionManager()
# We need to use a bad filename or this will parse the .coafile we use for coala
conf_sections = uut.run(arg_list=['-S', "test=5", "-c", "some_bad_filename"])[0]
# We need to use a bad filename or this will parse coalas .coafile
conf_sections = uut.run(
arg_list=['-S', "test=5", "-c", "some_bad_filename"])[0]

self.assertEqual(str(conf_sections["default"]), "Default {config : some_bad_filename, test : 5}")
self.assertEqual(str(conf_sections["default"].defaults), str(defaults["default"]))
self.assertEqual(str(conf_sections["default"]),
"Default {config : some_bad_filename, test : 5}")
self.assertEqual(str(conf_sections["default"].defaults),
str(defaults["default"]))

local_bears = uut.run(arg_list=['-S test=5',
'-c bad_filename',
Expand All @@ -28,24 +31,29 @@ def test_run(self):

def test_nonexistent_file(self):
filename = "bad.one/test\neven with bad chars in it"
SectionManager().run(arg_list=['-S', "config=" + filename]) # Shouldn't throw an exception
# Shouldn't throw an exception
SectionManager().run(arg_list=['-S', "config=" + filename])

tmp = StringConstants.coalib_root
StringConstants.coalib_root = filename
self.assertRaises(SystemExit, SectionManager().run)
StringConstants.coalib_root = tmp

def test_back_saving(self):
filename = os.path.join(tempfile.gettempdir(), "SectionManagerTestFile")
filename = os.path.join(tempfile.gettempdir(),
"SectionManagerTestFile")

# We need to use a bad filename or this will parse the .coafile we use for coala
SectionManager().run(arg_list=['-S', "save=" + filename, "-c", "some_bad_filename"])
# We need to use a bad filename or this will parse coalas .coafile
SectionManager().run(
arg_list=['-S', "save=" + filename, "-c", "some_bad_filename"])

with open(filename, "r") as f:
lines = f.readlines()
self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines)
self.assertEqual(["[Default]\n", "config = some_bad_filename\n"],
lines)

SectionManager().run(arg_list=['-S', "save=true", "config=" + filename, "test.value=5"])
SectionManager().run(
arg_list=['-S', "save=true", "config=" + filename, "test.value=5"])

with open(filename, "r") as f:
lines = f.readlines()
Expand All @@ -56,8 +64,14 @@ def test_back_saving(self):
"value = 5\n"], lines)

def test_logging_objects(self):
conf_sections, n, m = SectionManager().run(arg_list=['-S', "log_type=none"])
self.assertIsInstance(conf_sections["default"].log_printer, NullPrinter)
conf_sections = SectionManager().run(arg_list=['-S',
"log_type=none"])[0]
self.assertIsInstance(conf_sections["default"].log_printer,
NullPrinter)

def test_targets(self):
targets = SectionManager().run(arg_list=["test1", "test2"])[3]
self.assertEqual(targets, ["test1", "test2"])


if __name__ == '__main__':
Expand Down

0 comments on commit 339c1e5

Please sign in to comment.