Exemplo n.º 1
0
def run_linter(args, files, validate_only):
    '''Runs the Google Closure Compiler linter+prettier against |files|.'''
    root = git_tools.root_dir()
    file_violations = set()
    for filename in files:
        contents = git_tools.file_contents(args, root, filename)
        try:
            new_contents = lint_tools.lint_javascript(filename, contents)
        except subprocess.CalledProcessError as e:
            print(
                'File %s%s%s lint failed:\n%s' %
                (COLORS.FAIL, filename, COLORS.NORMAL,
                 str(b'\n'.join(e.output.split(b'\n')[1:]), encoding='utf-8')),
                file=sys.stderr)
            validation_passed = False
            continue
        if contents != new_contents:
            validation_passed = False
            if validate_only:
                print('File %s%s%s lint failed' %
                      (COLORS.HEADER, filename, COLORS.NORMAL),
                      file=sys.stderr)
            else:
                print('Fixing %s%s%s' %
                      (COLORS.HEADER, filename, COLORS.NORMAL),
                      file=sys.stderr)
                with open(os.path.join(root, filename), 'wb') as o:
                    o.write(new_contents)
    return file_violations
Exemplo n.º 2
0
def _run_linter_one(args, linter, root, filename, validate_only):
    '''Runs the linter against one file.'''
    contents = git_tools.file_contents(args, root, filename)
    try:
        new_contents, violations = linter.run_one(filename, contents)
    except linters.LinterException as lex:
        print('File %s%s%s lint failed:\n%s' %
              (git_tools.COLORS.FAIL, filename, git_tools.COLORS.NORMAL,
               lex.message),
              file=sys.stderr)
        return filename, lex.fixable
    if contents != new_contents:
        violations_message = ', '.join(
            '%s%s%s' %
            (git_tools.COLORS.FAIL, violation, git_tools.COLORS.NORMAL)
            for violation in violations)
        if validate_only:
            print('File %s%s%s lint failed: %s' %
                  (git_tools.COLORS.HEADER, filename, git_tools.COLORS.NORMAL,
                   violations_message),
                  file=sys.stderr)
        else:
            print('Fixing %s%s%s' %
                  (git_tools.COLORS.HEADER, filename, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
            with open(os.path.join(root, filename), 'wb') as outfile:
                outfile.write(new_contents)
        return filename, True
    return None, False
Exemplo n.º 3
0
def run_linter(args, files, validate_only):
    '''Runs the Google Closure Compiler linter+prettier against |files|.'''
    root = git_tools.root_dir()
    file_violations = set()
    for filename in files:
        contents = git_tools.file_contents(args, root, filename)
        parser = VueHTMLParser()
        try:
            sections = parser.parse(contents.decode('utf-8'))
        except AssertionError:
            print('File %s%s%s lint failed' %
                  (COLORS.FAIL, filename, COLORS.NORMAL),
                  file=sys.stderr)
            validation_passed = False
            continue

        new_sections = []
        for tag, starttag, section_contents in sections:
            try:
                if tag == 'script':
                    new_section_contents = lint_tools.lint_javascript(
                        filename + '.js', section_contents.encode('utf-8'))
                    new_sections.append(
                        '%s\n%s\n</%s>' %
                        (starttag, new_section_contents.decode('utf-8'), tag))
                elif tag == 'template':
                    new_section_contents = lint_tools.lint_html(
                        section_contents.encode('utf-8'))
                    new_sections.append(
                        '%s\n%s\n</%s>' %
                        (starttag, new_section_contents.decode('utf-8'), tag))
                else:
                    new_sections.append('%s\n%s\n</%s>' %
                                        (starttag, section_contents, tag))
            except subprocess.CalledProcessError as e:
                print('File %s%s%s lint failed:\n%s' %
                      (COLORS.FAIL, filename, COLORS.NORMAL,
                       str(b'\n'.join(e.output.split(b'\n')[1:]),
                           encoding='utf-8')),
                      file=sys.stderr)
                validation_passed = False
                break

        if len(new_sections) != len(sections):
            continue

        new_contents = ('\n\n'.join(new_sections)).encode('utf-8') + b'\n'
        if contents != new_contents:
            validation_passed = False
            if validate_only:
                print('File %s%s%s lint failed' %
                      (COLORS.HEADER, filename, COLORS.NORMAL),
                      file=sys.stderr)
            else:
                print('Fixing %s%s%s' %
                      (COLORS.HEADER, filename, COLORS.NORMAL),
                      file=sys.stderr)
                with open(os.path.join(root, filename), 'wb') as o:
                    o.write(new_contents)
    return file_violations
Exemplo n.º 4
0
def run_validations(args, files, validate_only):
  '''Runs all validations against |files|.

  A validation consists of performing regex substitution against the contents
  of each file in |files|.  Validation fails if the resulting content is not
  identical to the original.  The contents of the files will be presented as a
  single string, allowing for multi-line matches.
  '''
  root = git_tools.root_dir()
  validation_passed = True
  for filename in files:
    contents = git_tools.file_contents(args, root, filename)
    violations = []

    # Run all validations sequentially, so all violations can be fixed
    # together.
    for error_string, search, replace in VALIDATIONS:
      replaced = search.sub(replace, contents)
      if replaced != contents:
        violations.append(error_string)
        contents = replaced

    if violations:
      validation_passed = False
      violations_message = ', '.join('%s%s%s' % (COLORS.FAIL, violation,
        COLORS.NORMAL) for violation in violations)
      if validate_only:
        print('File %s%s%s has %s.' % (COLORS.HEADER, filename, COLORS.NORMAL,
          violations_message), file=sys.stderr)
      else:
        print('Fixing %s%s%s for %s.' % (COLORS.HEADER, filename, COLORS.NORMAL,
          violations_message), file=sys.stderr)
        with open(os.path.join(root, filename), 'wb') as f:
          f.write(replaced)
  return validation_passed
Exemplo 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
Exemplo 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
Exemplo n.º 7
0
def _run_linter(args, linter, filenames, validate_only):
    '''Runs the linter against all files.'''
    logging.debug('%s: Files to consider: %s', linter.name,
                  ' '.join(filenames))
    logging.debug('%s: Running with %d threads', linter.name, args.jobs)
    files = dict((filename, git_tools.file_contents(args, _ROOT, filename))
                 for filename in filenames)
    results = multiprocessing.Pool(args.jobs).starmap(
        _run_linter_one, [(linter, filename, contents, validate_only)
                          for filename, contents in files.items()])
    results.extend(_run_linter_all(args, linter, filenames, validate_only))
    return (set(violation for violation, _ in results
                if violation is not None),
            any(fixable for _, fixable in results))
Exemplo n.º 8
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
Exemplo n.º 9
0
def run_linter(args, files, validate_only):
    '''Runs the Google Closure Compiler linter against |files|.'''
    root = git_tools.root_dir()
    validation_passed = True
    for filename in files:
        contents = git_tools.file_contents(args, root, filename)

        with tempfile.NamedTemporaryFile(suffix='.js') as f:
            f.write(contents)
            f.flush()
            f.seek(0, 0)

            if validate_only:
                try:
                    output = subprocess.check_output(
                        [GJSLINT_PATH, '--nojsdoc', '--quiet', f.name])
                except subprocess.CalledProcessError as e:
                    print('File %s%s%s:\n%s' %
                          (COLORS.HEADER, filename, COLORS.NORMAL,
                           str(b'\n'.join(e.output.split(b'\n')[1:]),
                               encoding='utf-8')),
                          file=sys.stderr)
                    validation_passed = False
            else:
                previous_outputs = set("")
                while True:
                    output = subprocess.check_output(
                        [FIXJSSTYLE_PATH, '--strict', f.name])
                    if output in previous_outputs:
                        break
                    previous_outputs.add(output)
                subprocess.check_call([
                    CLANG_FORMAT_PATH, '-style=Google',
                    '-assume-filename=%s' % filename, '-i', f.name
                ])
                with open(f.name, 'rb') as f2:
                    new_contents = f2.read()
                if contents != new_contents:
                    print('Fixing %s%s%s' %
                          (COLORS.HEADER, filename, COLORS.NORMAL),
                          file=sys.stderr)
                    with open(os.path.join(root, filename), 'wb') as o:
                        o.write(new_contents)
                    validation_passed = False
    return validation_passed
Exemplo n.º 10
0
def run_linter(args, files, validate_only):
  '''Runs the Google Closure Compiler linter against |files|.'''
  root = git_tools.root_dir()
  file_violations = set()
  for filename in files:
    contents = git_tools.file_contents(args, root, filename)

    with tempfile.NamedTemporaryFile(suffix='.js') as f:
      f.write(contents)
      f.flush()
      f.seek(0, 0)

      try:
        subprocess.check_output(['yarn', 'run', 'refactor', '--', f.name,
                                 '--assume-filename=%s' % filename],
                                stderr=subprocess.STDOUT)
        subprocess.check_output([FIXJSSTYLE_PATH, '--strict', f.name],
                                stderr=subprocess.STDOUT)
        subprocess.check_output([CLANG_FORMAT_PATH, '-style=Google',
                                 '-assume-filename=%s' % filename,
                                 '-i', f.name], stderr=subprocess.STDOUT)
      except subprocess.CalledProcessError as e:
        print('File %s%s%s lint failed:\n%s' %
              (COLORS.HEADER, filename, COLORS.NORMAL,
               str(b'\n'.join(e.output.split(b'\n')[1:]), encoding='utf-8')),
              file=sys.stderr)
        file_violations.add(filename)
      with open(f.name, 'rb') as f2:
        new_contents = f2.read()
      if contents != new_contents:
        file_violations.add(filename)
        if validate_only:
          print('File %s%s%s lint failed.' %
                (COLORS.HEADER, filename, COLORS.NORMAL),
                file=sys.stderr)
        else:
          print('Fixing %s%s%s' % (COLORS.HEADER, filename,
            COLORS.NORMAL), file=sys.stderr)
          with open(os.path.join(root, filename), 'wb') as o:
            o.write(new_contents)
  return file_violations
Exemplo n.º 11
0
def run_linter(args, files, validate_only):
  '''Runs the Google Closure Compiler linter against |files|.'''
  root = git_tools.root_dir()
  validation_passed = True
  for filename in files:
    contents = git_tools.file_contents(args, root, filename)

    with tempfile.NamedTemporaryFile(suffix='.js') as f:
      f.write(contents)
      f.flush()
      f.seek(0, 0)

      if validate_only:
        try:
          output = subprocess.check_output([
            GJSLINT_PATH, '--nojsdoc', '--quiet',
            f.name])
        except subprocess.CalledProcessError as e:
          print('File %s%s%s:\n%s' % (COLORS.HEADER, filename, COLORS.NORMAL,
            str(b'\n'.join(e.output.split(b'\n')[1:]),
              encoding='utf-8')), file=sys.stderr)
          validation_passed = False
      else:
        previous_outputs = set("")
        while True:
          output = subprocess.check_output([FIXJSSTYLE_PATH, '--strict', f.name])
          if output in previous_outputs:
            break
          previous_outputs.add(output)
        subprocess.check_call([CLANG_FORMAT_PATH, '-style=Google',
                               '-assume-filename=%s' % filename,
                               '-i', f.name])
        with open(f.name, 'rb') as f2:
          new_contents = f2.read()
        if contents != new_contents:
          print('Fixing %s%s%s' % (COLORS.HEADER, filename,
            COLORS.NORMAL), file=sys.stderr)
          with open(os.path.join(root, filename), 'wb') as o:
            o.write(new_contents)
          validation_passed = False
  return validation_passed
Exemplo n.º 12
0
def _run_linter_all(args, linter, files, validate_only):
    try:
        new_file_contents, original_contents, violations = linter.run_all(
            files,
            lambda filename: git_tools.file_contents(args, _ROOT, filename))
    except linters.LinterException as lex:
        print(
            'Files %s%s%s lint failed:\n%s' %
            (git_tools.COLORS.FAIL, ', '.join([filename
                                               for filename in files]),
             git_tools.COLORS.NORMAL, lex.message),
            file=sys.stderr)
        return [(filename, lex.fixable) for filename in files]

    result = []
    for filename in new_file_contents:
        if original_contents[filename] == new_file_contents[filename]:
            result.append([None, False])
        else:
            result.append(
                _report_linter_results(filename, new_file_contents[filename],
                                       validate_only, violations, True))
    return result