Ejemplo n.º 1
0
def main():
  if not git_tools.verify_toolchain({
    PIP_PATH: 'sudo apt-get install python-pip',
    CLANG_FORMAT_PATH: 'sudo apt-get install clang-format-3.7',
    FIXJSSTYLE_PATH: 'pip install --user https://github.com/google/closure-linter/zipball/master'
  }):
    sys.exit(1)
  args = git_tools.parse_arguments(tool_description='lints javascript',
        file_whitelist=[br'^frontend/www/(js|ux)/.*\.js$'],
        file_blacklist=[br'.*third_party.*', br'.*js/omegaup/lang\..*'])

  if not args.files:
    return 0

  validate_only = args.tool == 'validate'

  file_violations = run_linter(args, args.files, validate_only)
  if file_violations:
    if validate_only:
      if git_tools.attempt_automatic_fixes(sys.argv[0], args, file_violations):
        return 1
      print('%sValidation errors.%s '
            'Please run `%s` to fix them.' % (COLORS.FAIL,
            COLORS.NORMAL,
            git_tools.get_fix_commandline(sys.argv[0], args, file_violations)),
            file=sys.stderr)
    else:
      print('Files written to working directory. '
            '%sPlease commit them before pushing.%s' %
            (COLORS.HEADER, COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 2
0
def main():
    args = git_tools.parse_arguments(
        tool_description='lints javascript',
        file_whitelist=[br'^frontend/www/(js|ux)/.*\.js$'],
        file_blacklist=[br'.*third_party.*', br'.*js/omegaup/lang\..*'])

    if not args.files:
        return 0

    validate_only = args.tool == 'validate'

    file_violations = run_linter(args, args.files, validate_only)
    if file_violations:
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 file_violations):
                return 1
            print('%sValidation errors.%s '
                  'Please run `%s` to fix them.' %
                  (COLORS.FAIL, COLORS.NORMAL,
                   git_tools.get_fix_commandline(sys.argv[0], args,
                                                 file_violations)),
                  file=sys.stderr)
        else:
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (COLORS.HEADER, COLORS.NORMAL),
                  file=sys.stderr)
        return 1
    return 0
Ejemplo n.º 3
0
def main():
    args = git_tools.parse_arguments(
        tool_description='purges whitespace',
        file_whitelist=[br'^frontend.*\.(php|css|js|sql|tpl|py|vue)$'],
        file_blacklist=[br'.*third_party.*'])
    if not args.files:
        return 0

    validate_only = args.tool == 'validate'

    file_violations = run_validations(args, args.files, validate_only)
    if file_violations:
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 file_violations):
                return 1
            print('%sWhitespace validation errors.%s '
                  'Please run `%s` to fix them.' %
                  (COLORS.FAIL, COLORS.NORMAL,
                   git_tools.get_fix_commandline(sys.argv[0], args,
                                                 file_violations)),
                  file=sys.stderr)
        else:
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (COLORS.HEADER, COLORS.NORMAL),
                  file=sys.stderr)
        return 1
    return 0
Ejemplo n.º 4
0
def main():
  if not git_tools.verify_toolchain({
    PIP_PATH: 'sudo apt-get install python-pip',
    CLANG_FORMAT_PATH: 'sudo apt-get install clang-format-3.7',
    GJSLINT_PATH: 'pip install --user https://github.com/google/closure-linter/zipball/master'
  }):
    sys.exit(1)
  args = git_tools.parse_arguments(tool_description='lints javascript',
        file_whitelist=[br'^frontend/www/(js|ux)/.*\.js$'],
        file_blacklist=[br'.*third_party.*', br'.*js/omegaup/lang\..*'])

  if not args.files:
    return 0

  validate_only = args.tool == 'validate'

  if not run_linter(args, args.files, validate_only):
    if validate_only:
      print('%sValidation errors.%s '
            'Please run `%s` to fix them.' % (COLORS.FAIL,
            COLORS.NORMAL, git_tools.get_fix_commandline(sys.argv[0], args)),
            file=sys.stderr)
    else:
      print('Files written to working directory. '
          '%sPlease commit them before pushing.%s' % (COLORS.HEADER,
          COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 5
0
def main():
  args = git_tools.parse_arguments(tool_description='PHP linter',
        file_whitelist=[br'^frontend.*\.php$'],
        file_blacklist=[br'.*third_party.*'])
  if not args.files:
    return 0

  root = git_tools.root_dir()
  phpcs_args = [which('phpcbf'), '--encoding=utf-8',
      '--standard=%s' % os.path.join(root, 'stuff/phpcbf/Standards/OmegaUp/ruleset.xml')]

  validate_only = args.tool == 'validate'
  file_violations = set()

  for filename in args.files:
    contents = git_tools.file_contents(args, root, filename)
    cmd = phpcs_args + ['--stdin-path=%s' % filename]
    if args.verbose:
      print('Executing "%s".' % (
            ' '.join(pipes.quote(arg) for arg in cmd)), file=sys.stderr)
    with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
        cwd=root) as p:
      replaced = p.communicate(contents)[0]
      if p.returncode != 0 and not replaced:
        # phpcbf returns 1 if there was no change to the file. If there was an
        # actual error, there won't be anything in stdout.
        file_violations.add(filename)
        print('Execution of "%s" %sfailed with return code %d%s.' % (
              ' '.join(cmd), COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
    if contents != replaced:
      file_violations.add(filename)
      if validate_only:
        print('File %s%s%s has %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
      else:
        print('Fixing %s%s%s for %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
        with open(os.path.join(root, filename), 'wb') as f:
          f.write(replaced)

  if file_violations:
    if validate_only:
      if git_tools.attempt_automatic_fixes(sys.argv[0], args, file_violations):
        return 1
      print('%sPHP 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, file_violations)),
              file=sys.stderr)
    else:
      print('Files written to working directory. '
          '%sPlease commit them before pushing.%s' % (COLORS.HEADER,
          COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 6
0
def main():
  args = git_tools.parse_arguments(tool_description='PHP linter',
        file_whitelist=[br'^frontend.*\.php$'],
        file_blacklist=[br'.*third_party.*', br'.*dao/base.*',
                        br'frontend/server/libs/dao/Estructura.php',
                        br'frontend/server/libs/dao/model.inc.php'])
  if not args.files:
    return 0

  root = git_tools.root_dir()
  phpcs_args = [which('phpcbf'), '--encoding=utf-8',
      '--standard=%s' % os.path.join(root, 'stuff/omegaup-standard.xml')]

  validate_only = args.tool == 'validate'
  validation_passed = True

  for filename in args.files:
    contents = git_tools.file_contents(args, root, filename)
    cmd = phpcs_args + ['--stdin-path=%s' % filename]
    with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
        cwd=root) as p:
      replaced = p.communicate(contents)[0]
      if p.returncode != 0 and not replaced:
        # phpcbf returns 1 if there was no change to the file. If there was an
        # actual error, there won't be anything in stdout.
        validation_passed = False
        print('Execution of "%s" %sfailed with return code %d%s.' % (
              ' '.join(cmd), COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
    if contents != replaced:
      validation_passed = False
      if validate_only:
        print('File %s%s%s has %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
      else:
        print('Fixing %s%s%s for %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
        with open(os.path.join(root, filename), 'wb') as f:
          f.write(replaced)

  if not validation_passed:
    if validate_only:
      print('%sPHP 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)),
              file=sys.stderr)
    else:
      print('Files written to working directory. '
          '%sPlease commit them before pushing.%s' % (COLORS.HEADER,
          COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 7
0
def main():
  args = git_tools.parse_arguments(tool_description='PHP linter',
        file_whitelist=[br'^frontend.*\.php$'],
        file_blacklist=[br'.*third_party.*', br'.*dao/base.*',
                        br'frontend/server/libs/dao/Estructura.php'])
  if not args.files:
    return 0

  root = git_tools.root_dir()
  phpcs_args = [which('phpcbf'), '--encoding=utf-8',
      '--standard=%s' % os.path.join(root, 'stuff/omegaup-standard.xml')]

  validate_only = args.tool == 'validate'
  validation_passed = True

  for filename in args.files:
    contents = git_tools.file_contents(args, root, filename)
    cmd = phpcs_args + ['--stdin-path=%s' % filename]
    with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
        cwd=root) as p:
      replaced = p.communicate(contents)[0]
      if p.returncode != 0 and not replaced:
        # phpcbf returns 1 if there was no change to the file. If there was an
        # actual error, there won't be anything in stdout.
        validation_passed = False
        print('Execution of "%s" %sfailed with return code %d%s.' % (
              ' '.join(cmd), COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
    if contents != replaced:
      validation_passed = False
      if validate_only:
        print('File %s%s%s has %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
      else:
        print('Fixing %s%s%s for %slint errors%s.' % (COLORS.HEADER, filename,
          COLORS.NORMAL, COLORS.FAIL, COLORS.NORMAL), file=sys.stderr)
        with open(os.path.join(root, filename), 'wb') as f:
          f.write(replaced)

  if not validation_passed:
    if validate_only:
      print('%sPHP 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)),
              file=sys.stderr)
    else:
      print('Files written to working directory. '
          '%sPlease commit them before pushing.%s' % (COLORS.HEADER,
          COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 8
0
def main():
  args = git_tools.parse_arguments(tool_description='purges whitespace',
        file_whitelist=[br'^frontend.*\.(php|css|js|sql|tpl|py)$'],
        file_blacklist=[br'.*third_party.*', br'.*dao/base.*'])
  if not args.files:
    return 0

  validate_only = args.tool == 'validate'

  if not run_validations(args, args.files, validate_only):
    if validate_only:
      print('%sWhitespace validation errors.%s '
            'Please run `%s` to fix them.' % (COLORS.FAIL,
            COLORS.NORMAL, git_tools.get_fix_commandline(sys.argv[0], args)),
            file=sys.stderr)
    else:
      print('Files written to working directory. '
          '%sPlease commit them before pushing.%s' % (COLORS.HEADER,
          COLORS.NORMAL), file=sys.stderr)
    return 1
  return 0
Ejemplo n.º 9
0
def main():
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(tool_description='lints a project')
    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()
    fixable = False

    for linter, options in config['lint'].items():
        if linter not in _LINTER_MAPPING:
            print('Unknown linter %s%s%s.' %
                  (git_tools.COLORS.FAIL, linter, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
            sys.exit(1)

        filtered_files = args.files

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

        # And not in the blacklist.
        blacklist = [re.compile(r) for r in options.get('blacklist', [])]
        filtered_files = [
            filename for filename in filtered_files
            if all(not r.match(filename) for r in blacklist)
        ]

        local_violations, local_fixable = _run_linter(
            args, _LINTER_MAPPING[linter](options), filtered_files,
            validate_only)
        file_violations |= local_violations
        fixable |= local_fixable

    if file_violations:
        if not fixable:
            print('%sErrors cannot be automatically fixed.%s' %
                  (git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        elif validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 file_violations):
                sys.exit(1)
            print('%sLinter 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,
                                                 file_violations)),
                  file=sys.stderr)
        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)