コード例 #1
0
ファイル: database_schema.py プロジェクト: lhchavez/omegaup
def main():
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument(
                '--mysql-config-file',
                default=database_utils.default_config_file(),
                help='.my.cnf file that stores credentials'),
            git_tools.Argument(
                '--database', default='omegaup', help='MySQL database'),
            git_tools.Argument(
                '--username', default='root', help='MySQL root username'),
            git_tools.Argument(
                '--password', default='omegaup', help='MySQL password')])

    # If running in an automated environment, we can close stdin.
    # This will disable all prompts.
    if (args.continuous_integration
            or os.environ.get('CONTINUOUS_INTEGRATION') == 'true'):
        sys.stdin.close()

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files if
                          filename.endswith('.sql'))
    if not filtered_files:
        return

    root = git_tools.root_dir()
    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(
        args, root, 'frontend/database/schema.sql')

    if (strip_mysql_extensions(expected.strip()) != strip_mysql_extensions(
            actual.strip())):
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' % (
                      git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                      git_tools.get_fix_commandline(sys.argv[0], args,
                                                    filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root,
                                   'frontend/database/schema.sql'), 'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' % (
                      git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
コード例 #2
0
ファイル: database_schema.py プロジェクト: yoshoo90/omegaup
def main():
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument('--mysql-config-file',
                               default=database_utils.default_config_file(),
                               help='.my.cnf file that stores credentials'),
            git_tools.Argument('--database',
                               default='omegaup',
                               help='MySQL database'),
            git_tools.Argument('--username',
                               default='root',
                               help='MySQL root username'),
            git_tools.Argument('--password',
                               default='omegaup',
                               help='MySQL password')
        ])

    # If running in an automated environment, we can close stdin.
    # This will disable all prompts.
    if (args.continuous_integration
            or os.environ.get('CONTINUOUS_INTEGRATION') == 'true'):
        sys.stdin.close()

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files
                          if filename.endswith('.sql'))
    if not filtered_files:
        return

    root = git_tools.root_dir()
    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(args, root,
                                     'frontend/database/schema.sql')

    if (strip_mysql_extensions(expected.strip()) != strip_mysql_extensions(
            actual.strip())):
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' %
                  (git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                   git_tools.get_fix_commandline(sys.argv[0], args,
                                                 filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root, 'frontend/database/schema.sql'),
                      'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
コード例 #3
0
ファイル: lint.py プロジェクト: frcepeda/hook_tools
def main() -> None:
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='lints a project',
        extra_arguments=[
            git_tools.Argument(
                '--pre-upload',
                action='store_true',
                help='Mark this as being run from within a pre-upload hook'),
            git_tools.Argument(
                '--command-name',
                default=None,
                type=str,
                help='Override the command name to execute this'),
            git_tools.Argument(
                '--linters', help='Comma-separated subset of linters to run'),
            git_tools.Argument(
                '--diagnostics-output',
                default=DiagnosticsOutput.STDERR,
                type=DiagnosticsOutput,
                choices=DiagnosticsOutput.__members__.values(),
                help='How to display diagnostics provided by the linters.'),
        ])
    if not args.files:
        return

    # If running in an automated environment, we can close stdin.
    # This will disable all prompts.
    if (args.continuous_integration
            or os.environ.get('CONTINUOUS_INTEGRATION') == 'true'):
        sys.stdin.close()

    validate_only = args.tool == 'validate'

    with open(args.config_file, 'r') as config_file:
        config = json.load(config_file)

    file_violations: Set[Text] = set()
    fixable = False

    for linter, options in _get_enabled_linters(config, args.config_file,
                                                args.linters):
        filtered_files = args.files

        # Filter only the files in the allowlist.
        allowlist = [re.compile(r) for r in options.get('allowlist', [])]
        filtered_files = [
            filename for filename in filtered_files
            if any(r.match(filename) for r in allowlist)]

        # And not in the denylist.
        denylist = [re.compile(r) for r in options.get('denylist', [])]
        filtered_files = [
            filename for filename in filtered_files
            if all(not r.match(filename) for r in denylist)]
        local_violations, local_fixable = _run_linter(
            args, linter(options), filtered_files,
            validate_only, args.diagnostics_output)
        file_violations |= local_violations
        fixable |= local_fixable

    if file_violations:
        if not fixable:
            _report_error('Errors cannot be automatically fixed.',
                          args.diagnostics_output)
        elif validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0],
                                                 args,
                                                 file_violations,
                                                 pre_upload=args.pre_upload):
                sys.exit(1)
            _report_error(('Linter validation errors. '
                           'Please run\n\n    %s\n\nto fix them.') %
                          git_tools.get_fix_commandline(
                              _get_command_name(args.command_name), args,
                              file_violations), args.diagnostics_output)
        else:
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' % (
                      git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
コード例 #4
0
ファイル: database_schema.py プロジェクト: heduenas/omegaup
def main() -> None:
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument('--mysql-config-file',
                               default=database_utils.default_config_file(),
                               help='.my.cnf file that stores credentials'),
            git_tools.Argument('--database',
                               default='omegaup',
                               help='MySQL database'),
            git_tools.Argument('--hostname',
                               default=None,
                               type=str,
                               help='Hostname of the MySQL server'),
            git_tools.Argument('--username',
                               default='root',
                               help='MySQL root username'),
            git_tools.Argument('--password',
                               default='omegaup',
                               help='MySQL password')
        ])

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files
                          if filename.endswith('.sql'))

    root = git_tools.root_dir()
    if not _check_mutually_exclusive_schema_modifications(
            args=args,
            root=root,
    ):
        sys.exit(1)
    if 'frontend/database/dao_schema.sql' in filtered_files:
        filtered_files.remove('frontend/database/dao_schema.sql')
    if not filtered_files:
        return

    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         hostname=args.hostname,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(args, root, _SCHEMA_FILENAME)

    expected_contents = strip_mysql_extensions(expected.strip())
    actual_contents = strip_mysql_extensions(actual.strip())

    if expected_contents != actual_contents:
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            sys.stderr.writelines(
                difflib.unified_diff(
                    actual_contents.decode('utf-8').splitlines(keepends=True),
                    expected_contents.decode('utf-8').splitlines(
                        keepends=True),
                    fromfile=_SCHEMA_FILENAME,
                    tofile=_SCHEMA_FILENAME))
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' %
                  (git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                   git_tools.get_fix_commandline(args, filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root, 'frontend/database/schema.sql'),
                      'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)