Beispiel #1
0
def start_logging(dirname: str = "LOGS"):
    """Turns logging on an sets the log-directory to `dirname`.
    The log-directory, if it does not already exist, will be created
    lazily, i.e. only when logging actually starts."""
    access_presets()
    log_dir = os.path.abspath(dirname) if dirname else ''
    if log_dir != get_preset_value('log_dir'):
        set_preset_value('log_dir', log_dir)
        set_config_value('log_dir', log_dir)
    finalize_presets()
Beispiel #2
0
 def test_presets(self):
     """Checks whether changes to CONFIG_PRESET before spawning / forking
     new processes will be present in spawned or forked processes
     afterwards."""
     access_presets()
     set_preset_value('test',
                      'multiprocessing presets test',
                      allow_new_key=True)
     finalize_presets()
     access_presets()
     set_preset_value('test2',
                      'multiprocessing presets test2',
                      allow_new_key=True)
     finalize_presets()
     flag = multiprocessing.Value('b', 0)
     p = multiprocessing.Process(target=evaluate_presets, args=(flag, ))
     p.start()
     p.join()
     assert flag.value == 1
Beispiel #3
0
def main():
    """Creates a project (if a project name has been passed as command line
    parameter) or runs a quick self-test.
    """
    access_presets()
    set_preset_value('syntax_variant', 'heuristic')
    finalize_presets()
    if len(sys.argv) > 1:
        if sys.argv[1].lower() == "--selftest":
            if not selftest():
                print("Selftest FAILED :-(\n")
                sys.exit(1)
            print("Selftest SUCCEEDED :-)\n")
        else:
            if sys.argv[1].lower() in ('-ast', '--ast'):
                try:
                    with open(sys.argv[2], 'r', encoding='utf-8') as f:
                        ebnf = f.read()
                    syntax_tree = get_ebnf_grammar()(ebnf)
                    if is_error(syntax_tree.error_flag):
                        print('\n\n'.join(
                            str(err) for err in syntax_tree.errors_sorted))
                        sys.exit(1)
                    get_ebnf_transformer()(syntax_tree)
                    if is_error(syntax_tree.error_flag):
                        print('\n\n'.join(
                            str(err) for err in syntax_tree.errors_sorted))
                        sys.exit(1)
                    print(syntax_tree.as_sxpr(compact=True))
                except IndexError:
                    print("Usage:  dhparser.py -ast FILENAME.ebnf")
                    sys.exit(1)
                except FileNotFoundError:
                    print('File "%s" not found!' % sys.arg[2])
                    sys.exit(1)
                except IOError as e:
                    print('Could not read file "%s": %s' %
                          (sys.argv[2], str(e)))
            elif sys.argv[1].lower() in ('-cpu', '--cpu', '-mem', '--mem'):
                try:
                    func = partial(compile_on_disk, source_file=sys.argv[2])
                    if sys.argv[1].lower() in ('-cpu', '--cpu'):
                        _errors = cpu_profile(func, 1)
                    else:
                        _errors = mem_profile(func)
                    if _errors:
                        print('\n'.join(str(err) for err in _errors))
                        sys.exit(1)
                except IndexError:
                    print("Usage:  dhparser.py -cpu/mem FILENAME.ebnf")
                    sys.exit(1)
                except FileNotFoundError:
                    print('File "%s" not found!' % sys.arg[2])
                    sys.exit(1)
                except IOError as e:
                    print('Could not read file "%s": %s' %
                          (sys.argv[2], str(e)))
            elif os.path.exists(sys.argv[1]) and os.path.isfile(sys.argv[1]):
                _errors = compile_on_disk(sys.argv[1])
                if _errors:
                    print('\n'.join(str(err) for err in _errors))
                    sys.exit(1)
                else:
                    print('%s successfully compiled to %s' %
                          (sys.argv[1],
                           os.path.splitext(sys.argv[1])[0] + 'Parser.py'))
            else:
                create_project(sys.argv[1])
    else:
        print(
            'Usage: \n'
            '    dhparser.py PROJECTNAME  - to create a new project\n'
            '    dhparser.py FILENAME.ebnf  - to produce a python-parser from an EBNF-grammar\n'
            '    dhparser.py --selftest  - to run a self-test\n')
        choice = input('\nWould you now like to ...\n'
                       '  [1] create a new project\n'
                       '  [2] compile an ebnf-grammar\n'
                       '  [3] run a self-test\n'
                       '  [q] to quit\n'
                       'Please chose 1, 2 or 3> ')
        if choice.strip() == '1':
            project_name = input('Please project name or path > ')
            create_project(project_name)
        elif choice.strip() == '2':
            file_path = input('Please enter a file path for compilation > ')
            if os.path.exists(file_path) and os.path.isfile(file_path):
                _errors = compile_on_disk(file_path)
                if _errors:
                    print('\n\n'.join(str(err) for err in _errors))
                    sys.exit(1)
            else:
                print('File %s not found! Aborting.' % file_path)
                sys.exit(1)
        elif choice.strip() == '3':
            if LOGGING:
                start_logging(LOGGING)
            if not cpu_profile(selftest, 1):
                print("Selftest FAILED :-(\n")
                sys.exit(1)
            print("Selftest SUCCEEDED :-)\n")
        elif choice.strip().lower() not in {'q', 'quit', 'exit'}:
            print('No valid choice. Goodbye!')
Beispiel #4
0
def run_grammar_tests(glob_pattern, get_grammar, get_transformer):
    DHParser.log.start_logging(LOGGING)
    error_report = testing.grammar_suite(
        os.path.join(scriptpath, 'test_grammar'),
        get_grammar, get_transformer,
        fn_patterns=[glob_pattern], report='REPORT', verbose=True)
    return error_report


if __name__ == '__main__':
    argv = sys.argv[:]
    if len(argv) > 1 and sys.argv[1] == "--debug":
        LOGGING = 'LOGS'
        del argv[1]
        access_presets()
        set_preset_value('history_tracking', True)
        finalize_presets()
        DHParser.log.start_logging(LOGGING)
    if (len(argv) >= 2 and (argv[1].endswith('.ebnf') or
        os.path.splitext(argv[1])[1].lower() in testing.TEST_READERS.keys())):
        # if called with a single filename that is either an EBNF file or a known
        # test file type then use the given argument
        arg = argv[1]
    else:
        # otherwise run all tests in the test directory
        arg = '*_test_*.ini'
    if arg.endswith('.ebnf'):
        recompile_grammar(arg, force=True)
    else:
        recompile_grammar(os.path.join(scriptpath, 'json.ebnf'),
                          force=False)
Beispiel #5
0
    fullpath = os.path.abspath(os.path.join(scriptpath, path))
    if fullpath not in sys.path:
        sys.path.append(fullpath)

try:
    from DHParser import configuration
    import DHParser.dsl
    from DHParser import testing
except ModuleNotFoundError:
    print('Could not import DHParser. Please adjust sys.path in file '
          '"%s" manually' % __file__)
    sys.exit(1)

if __name__ == "__main__":
    configuration.access_presets()
    configuration.set_preset_value('test_parallelization', True)
    configuration.finalize_presets()
    if not DHParser.dsl.recompile_grammar(
            'BibTeX.ebnf',
            force=False):  # recompiles Grammar only if it has changed
        print(
            '\nErrors while recompiling "BibTeX.ebnf":\n--------------------------------------\n\n'
        )
        with open('BibTeX_ebnf_ERRORS.txt') as f:
            print(f.read())
        sys.exit(1)

    sys.path.append('.')
    # must be appended after module creation, because otherwise an ImportError is raised under Windows

    from BibTeXParser import get_grammar, get_transformer
                                         get_transformer,
                                         fn_patterns=[glob_pattern],
                                         report='REPORT',
                                         verbose=True)
    return error_report


if __name__ == '__main__':
    argv = sys.argv[:]
    if len(argv) > 1 and sys.argv[1] == "--debug":
        DEBUG = True
        del argv[1]

    access_presets()
    # set_preset_value('test_parallelization', True)
    if DEBUG: set_preset_value('history_tracking', True)
    finalize_presets()

    if (len(argv) >= 2 and (argv[1].endswith('.ebnf') or os.path.splitext(
            argv[1])[1].lower() in testing.TEST_READERS.keys())):
        # if called with a single filename that is either an EBNF file or a known
        # test file type then use the given argument
        arg = argv[1]
    else:
        # otherwise run all tests in the test directory
        arg = '*_test_*.ini'
    if arg.endswith('.ebnf'):
        recompile_grammar(arg, force=True)
    else:
        recompile_grammar(os.path.join(scriptpath, 'ts2dataclass.ebnf'),
                          force=False)