diff --git a/aiida/__init__.py b/aiida/__init__.py index ccb54a26af..d6ecd3a4fb 100644 --- a/aiida/__init__.py +++ b/aiida/__init__.py @@ -27,6 +27,15 @@ LOG_LEVEL_REPORT = 23 logging.addLevelName(LOG_LEVEL_REPORT, 'REPORT') + +# A logging filter that can be used to disable logging +class NotInTestingFilter(logging.Filter): + + def filter(self, record): + from aiida import settings + return not settings.TESTING_MODE + + # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error when DEBUG=False. @@ -45,11 +54,17 @@ 'datefmt': '%m/%d/%Y %I:%M:%S %p', }, }, + 'filters': { + 'testing': { + '()': NotInTestingFilter + } + }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'halfverbose', + 'filters': ['testing'] }, 'dblogger': { # get_property takes the property from the config json file diff --git a/aiida/backends/djsite/db/subtests/djangomigrations.py b/aiida/backends/djsite/db/subtests/djangomigrations.py index b2541281c9..1db891ea3b 100644 --- a/aiida/backends/djsite/db/subtests/djangomigrations.py +++ b/aiida/backends/djsite/db/subtests/djangomigrations.py @@ -10,13 +10,11 @@ from aiida.backends.testbase import AiidaTestCase - class CalcStateChanges(AiidaTestCase): # Class to check if the migration code that deals with removing the # NOTFOUND and UNDETERMINED calc states works properly def test_unexpected_calc_states(self): import logging - from django.utils import timezone from aiida.orm.calculation import Calculation @@ -32,8 +30,7 @@ def test_unexpected_calc_states(self): calc_params = { 'computer': self.computer, - 'resources': {'num_machines': 1, - 'num_mpiprocs_per_machine': 1} + 'resources': {'num_machines': 1, 'num_mpiprocs_per_machine': 1} } for state in ['NOTFOUND', 'UNDETERMINED']: @@ -45,25 +42,9 @@ def test_unexpected_calc_states(self): time_before_fix = timezone.now() - # First of all, I re-enable logging in case it was disabled by - # mistake by a previous test (e.g. one that disables and reenables - # again, but that failed) - logging.disable(logging.NOTSET) - # Temporarily disable logging to the stream handler (i.e. screen) - # because otherwise fix_calc_states will print warnings - handler = next((h for h in logging.getLogger('aiida').handlers if - isinstance(h, logging.StreamHandler)), None) - - if handler: - original_level = handler.level - handler.setLevel(logging.ERROR) - # Call the code that deals with updating these states state_change.fix_calc_states(None, None) - if handler: - handler.setLevel(original_level) - current_state = job.get_state() self.assertNotEqual(current_state, state, "Migration code failed to change removed state {}". diff --git a/aiida/backends/djsite/utils.py b/aiida/backends/djsite/utils.py index 36ab0efc2d..6c4cf3c54f 100644 --- a/aiida/backends/djsite/utils.py +++ b/aiida/backends/djsite/utils.py @@ -8,7 +8,6 @@ # For further information please visit http://www.aiida.net # ########################################################################### -import logging import os import django from aiida.utils.logger import get_dblogger_extra diff --git a/aiida/backends/sqlalchemy/tests/migrations.py b/aiida/backends/sqlalchemy/tests/migrations.py index 03471ce000..d6ee8589a4 100644 --- a/aiida/backends/sqlalchemy/tests/migrations.py +++ b/aiida/backends/sqlalchemy/tests/migrations.py @@ -9,7 +9,6 @@ ########################################################################### import copy -import logging import unittest import os @@ -49,8 +48,6 @@ class TestMigrationApplicationSQLA(AiidaTestCase): # The path of the migration configuration (the actual configuration - not # the testing) alembic_dpath = None - # The initial alembic log level - to be restored after the testing - init_alemb_log_level = '' @classmethod def setUpClass(cls, *args, **kwargs): @@ -59,15 +56,10 @@ def setUpClass(cls, *args, **kwargs): os.path.realpath(utils.__file__)) def setUp(self): - self.init_alemb_log_level = get_property('logging.alembic_loglevel') - set_property('logging.alembic_loglevel', - logging.getLevelName(logging.ERROR)) self.migrate_db_with_non_testing_migrations("base") def tearDown(self): self.migrate_db_with_non_testing_migrations("head") - set_property('logging.alembic_loglevel', - logging.getLevelName(self.init_alemb_log_level)) def migrate_db_with_non_testing_migrations(self, destination): if destination not in ["head", "base"]: diff --git a/aiida/backends/tests/backup_script.py b/aiida/backends/tests/backup_script.py index b09c54b1d9..f1afd8f5e9 100644 --- a/aiida/backends/tests/backup_script.py +++ b/aiida/backends/tests/backup_script.py @@ -199,12 +199,8 @@ def test_loading_backup_time_params_from_file_4(self): In the parsed JSON string, the endDateOfBackup & daysToBackuplimit are set which should lead to an exception. """ - import logging from aiida.common.additions.backup_script.backup_base import BackupError - # Disable the logging messages - logging.disable(logging.ERROR) - backup_variables = json.loads(self._json_test_input_5) self._backup_setup_inst._ignore_backup_dir_existence_check = True # An exception should be raised because endDateOfBackup @@ -212,9 +208,6 @@ def test_loading_backup_time_params_from_file_4(self): with self.assertRaises(BackupError): self._backup_setup_inst._read_backup_info_from_dict(backup_variables) - # Enable the logging messages - logging.disable(logging.NOTSET) - def check_full_deserialization_serialization(self, input_string, backup_inst): input_variables = json.loads(input_string) backup_inst._ignore_backup_dir_existence_check = True @@ -274,10 +267,6 @@ def test_timezone_addition_and_dir_correction(self): that don't have a timezone. Moreover, it checks if the given directory paths are normalized as expected. """ - import logging - - # Disable the logging messages - logging.disable(logging.INFO) backup_variables = json.loads(self._json_test_input_6) self._backup_setup_inst._ignore_backup_dir_existence_check = True @@ -305,9 +294,6 @@ def test_timezone_addition_and_dir_correction(self): "_backup_setup_inst destination directory is " "not normalized as expected.") - # Enable the logging messages - logging.disable(logging.NOTSET) - class TestBackupScriptIntegration(AiidaTestCase): @@ -318,12 +304,8 @@ class TestBackupScriptIntegration(AiidaTestCase): _bs_instance = backup_setup.BackupSetup() def test_integration(self): - import logging from aiida.utils.capturing import Capturing - # Disable the logging messages - logging.disable(logging.INFO) - # Fill in the repository with data self.fill_repo() try: @@ -359,9 +341,6 @@ def test_integration(self): finally: shutil.rmtree(temp_folder, ignore_errors=True) - # Enable the logging messages - logging.disable(logging.NOTSET) - def fill_repo(self): from aiida.orm import JobCalculation, CalculationFactory, Data, DataFactory diff --git a/aiida/backends/tests/daemon.py b/aiida/backends/tests/daemon.py index e096c0fc53..6380109403 100644 --- a/aiida/backends/tests/daemon.py +++ b/aiida/backends/tests/daemon.py @@ -19,16 +19,6 @@ class TestDaemonBasic(AiidaTestCase): - def setUp(self): - super(TestDaemonBasic, self).setUp() - import logging - logging.disable(logging.CRITICAL) - - def tearDown(self): - super(TestDaemonBasic, self).tearDown() - import logging - logging.disable(logging.NOTSET) - def test_workflow_fast_kill(self): from aiida.cmdline.commands.workflow import Workflow as WfCmd @@ -51,7 +41,7 @@ def test_workflow_fast_kill(self): # Killing the head workflow wf_cmd = WfCmd() - wf_cmd.workflow_kill(*[str(head_wf.pk), "-f"]) + wf_cmd.workflow_kill(*[str(head_wf.pk), '-f', '-q']) # At this point no running workflow should be found running_no = 0 diff --git a/aiida/backends/tests/orm/log.py b/aiida/backends/tests/orm/log.py index 352d226914..f67da2801d 100644 --- a/aiida/backends/tests/orm/log.py +++ b/aiida/backends/tests/orm/log.py @@ -134,19 +134,6 @@ def test_db_log_handler(self): message = 'Testing logging of critical failure' calc = Calculation() - # Make sure that global logging is not accidentally disabled - logging.disable(logging.NOTSET) - - # # Temporarily disable logging to the stream handler (i.e. screen) - # # because otherwise fix_calc_states will print warnings - # handler = next((h for h in logging.getLogger('aiida').handlers if - # isinstance(h, logging.StreamHandler)), None) - - # # try: - # if handler: - # original_level = handler.level - # handler.setLevel(logging.CRITICAL + 1) - # Firing a log for an unstored should not end up in the database calc.logger.critical(message) @@ -160,8 +147,4 @@ def test_db_log_handler(self): logs = self._backend.log.find() self.assertEquals(len(logs), 1) - self.assertEquals(logs[0].message, message) - - # finally: - # if handler: - # handler.setLevel(original_level) + self.assertEquals(logs[0].message, message) \ No newline at end of file diff --git a/aiida/backends/tests/work/workChain.py b/aiida/backends/tests/work/workChain.py index 9ab8eb3539..109891c23f 100644 --- a/aiida/backends/tests/work/workChain.py +++ b/aiida/backends/tests/work/workChain.py @@ -387,15 +387,6 @@ def _run_with_checkpoints(self, wf_class, inputs=None): class TestWorkchainWithOldWorkflows(AiidaTestCase): - def setUp(self): - super(TestWorkchainWithOldWorkflows, self).setUp() - import logging - logging.disable(logging.CRITICAL) - - def tearDown(self): - super(TestWorkchainWithOldWorkflows, self).tearDown() - import logging - logging.disable(logging.NOTSET) def test_call_old_wf(self): wf = WorkflowDemo() diff --git a/aiida/backends/tests/workflows.py b/aiida/backends/tests/workflows.py index ce8b193893..7a30e70ad3 100644 --- a/aiida/backends/tests/workflows.py +++ b/aiida/backends/tests/workflows.py @@ -197,24 +197,11 @@ def test_failing_calc_in_wf(self): sub-workflows) that has an exception at one of its steps stops properly and it is not left as RUNNING. """ - import logging from aiida.daemon.workflowmanager import execute_steps from aiida.workflows.test import (FailingWFTestSimple, FailingWFTestSimpleWithSubWF) try: - # First of all, I re-enable logging in case it was disabled by - # mistake by a previous test (e.g. one that disables and reenables - # again, but that failed) - logging.disable(logging.NOTSET) - # Temporarily disable logging to the stream handler (i.e. screen) - # because otherwise fix_calc_states will print warnings - handler = next((h for h in logging.getLogger('aiida').handlers if - isinstance(h, logging.StreamHandler)), None) - if handler: - original_level = handler.level - handler.setLevel(logging.ERROR) - # Testing the error propagation of a simple workflow wf = FailingWFTestSimple() wf.store() @@ -238,8 +225,7 @@ def test_failing_calc_in_wf(self): self.assertLess(step_no, 5, "This workflow should have stopped " "since it is failing") finally: - if handler: - handler.setLevel(original_level) + pass def test_result_parameter_name_colision(self): """ diff --git a/aiida/cmdline/commands/devel.py b/aiida/cmdline/commands/devel.py index 158fa0aa36..ce24913d7e 100644 --- a/aiida/cmdline/commands/devel.py +++ b/aiida/cmdline/commands/devel.py @@ -493,6 +493,9 @@ def run_tests(self, *args): from aiida.backends import settings from aiida.backends.testbase import run_aiida_db_tests from aiida.backends.testbase import check_if_tests_can_run + from aiida import settings + + settings.TESTING_MODE = True # For final summary test_failures = [] diff --git a/aiida/cmdline/commands/workflow.py b/aiida/cmdline/commands/workflow.py index 44fe0023ce..8e7916d556 100644 --- a/aiida/cmdline/commands/workflow.py +++ b/aiida/cmdline/commands/workflow.py @@ -134,6 +134,7 @@ def workflow_kill(self, *args): Pass a list of workflow PKs to kill them. If you also pass the -f option, no confirmation will be asked. + If you pass the -q option, additional information will be suppressed """ from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): @@ -145,6 +146,7 @@ def workflow_kill(self, *args): from aiida.orm.workflow import WorkflowKillError, WorkflowUnkillable force = False + verbose = True wfs = [] args = list(args) @@ -153,6 +155,8 @@ def workflow_kill(self, *args): param = args.pop() if param == '-f': force = True + elif param == '-q': + verbose = False else: try: wfs.append(int(param)) @@ -177,7 +181,7 @@ def workflow_kill(self, *args): counter = 0 for wf_pk in wfs: try: - kill_from_pk(wf_pk, verbose=True) + kill_from_pk(wf_pk, verbose=verbose) counter += 1 except NotExistent: print >> sys.stderr, ("WARNING: workflow {} " @@ -192,8 +196,9 @@ def workflow_kill(self, *args): sys.stdout.write("{}: {}\n".format(e.__class__.__name__, e.message)) - print >> sys.stderr, "{} workflow{} killed.".format(counter, - "" if counter <= 1 else "s") + if verbose: + print >> sys.stderr, "{} workflow{} killed.".format(counter, + "" if counter <= 1 else "s") def print_logshow(self, *args): from aiida.backends.utils import load_dbenv, is_dbenv_loaded diff --git a/aiida/common/additions/backup_script/backup_base.py b/aiida/common/additions/backup_script/backup_base.py index 6eec8a8a24..0268aacd88 100644 --- a/aiida/common/additions/backup_script/backup_base.py +++ b/aiida/common/additions/backup_script/backup_base.py @@ -82,7 +82,7 @@ def __init__(self, backup_info_filepath, additional_back_time_mins): format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') # The logger of the backup script - self._logger = logging.getLogger("aiida_backup") + self._logger = logging.getLogger('aiida.aiida_backup') def _read_backup_info_from_file(self, backup_info_file_name): """ diff --git a/aiida/settings.py b/aiida/settings.py index 64cc1da345..21d692b063 100644 --- a/aiida/settings.py +++ b/aiida/settings.py @@ -7,20 +7,16 @@ # For further information on the license, see the LICENSE.txt file # # For further information please visit http://www.aiida.net # ########################################################################### - import os - - from aiida.backends import settings - from aiida.common.exceptions import ConfigurationError from aiida.common.setup import (get_config, get_secret_key, get_property, get_profile_config, get_default_profile, parse_repository_uri) - USE_TZ = True +TESTING_MODE = False try: confs = get_config()