Exemplo n.º 1
0
def main(args):
    patchset = GetRietveldPatchsetNumber()
    if patchset:
        args.extend([
            '--issue',
            GetRietveldIssueNumber(),
            '--patchset',
            patchset,
        ])
    else:
        rietveld_url = GetRietveldServerUrl()
        if rietveld_url:
            args.extend(['--rietveld_url', GetRietveldServerUrl()])
    try:
        cl = git_cl.Changelist()
        change = cl.GetChange(cl.GetUpstreamBranch(), None)
        # Hack around a limitation in logging.
        logging.getLogger().handlers = []
        sys.exit(
            trychange.TryChange(
                args,
                change,
                swallow_exception=False,
                prog='git try',
                extra_epilog='\n'
                'git try will diff against your tracked branch and will '
                'detect your rietveld\n'
                'code review if you are using git-cl\n'))
    except third_party.upload.ClientLoginError, e:
        print('Got an exception while trying to log in to Rietveld.')
        print(str(e))
Exemplo n.º 2
0
def main(args):
  """Runs cpplint on the current changelist."""
  """Adapted from git_cl.py CMDlint """
  parser = git_cl.OptionParser()
  parser.add_option('--filter', action='append', metavar='-x,+y',
                    help='Comma-separated list of cpplint\'s category-filters')
  parser.add_option('--project_root')
  parser.add_option('--base_branch')
  auth.add_auth_options(parser)
  options, args = parser.parse_args(args)
  auth_config = auth.extract_auth_config_from_options(options)

  # Access to a protected member _XX of a client class
  # pylint: disable=protected-access
  try:
    import cpplint
    import cpplint_chromium
  except ImportError:
    print('Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.')
    return 1

  # Change the current working directory before calling lint so that it
  # shows the correct base.
  settings = git_cl.settings
  previous_cwd = os.getcwd()
  os.chdir(settings.GetRoot())
  try:
    cl = git_cl.Changelist(auth_config=auth_config)
    change = cl.GetChange(git_common.get_or_create_merge_base(cl.GetBranch(), options.base_branch), None)
    files = [f.LocalPath() for f in change.AffectedFiles()]
    if not files:
      print('Cannot lint an empty CL')
      return 0

    # Process cpplints arguments if any.
    command = args + files
    if options.filter:
      command = ['--filter=' + ','.join(options.filter)] + command
    if options.project_root:
      command = ['--project_root=' + options.project_root] + command
    filenames = cpplint.ParseArguments(command)

    white_regex = re.compile(settings.GetLintRegex())
    black_regex = re.compile(settings.GetLintIgnoreRegex())
    extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace]
    for filename in filenames:
      if white_regex.match(filename):
        if black_regex.match(filename):
          print('Ignoring file %s' % filename)
        else:
          cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level,
                              extra_check_functions)
      else:
        print('Skipping file %s' % filename)
  finally:
    os.chdir(previous_cwd)
  print('Total errors found: %d\n' % cpplint._cpplint_state.error_count)
  if cpplint._cpplint_state.error_count != 0:
    return 1
  return 0
Exemplo n.º 3
0
def main(argv):
  chromium_src_root = chromiumSrcRoot()
  os.chdir(chromium_src_root)
  argv = argv[1:]
  addLayoutBotsIfNeeded(argv)

  with ScopedTemporaryFile() as diff_file:
    generateDiff(diff_file, chromium_src_root)
    args = [
        '--sub_rep', 'third_party/WebKit',
        '--root', 'src',
        '--rietveld_url', 'https://codereview.chromium.org',
        '--diff', diff_file,
    ]
    args.extend(argv)
    cl = git_cl.Changelist()
    change = cl.GetChange(cl.GetUpstreamBranch(), None)
    logging.getLogger().handlers = []
    return trychange.TryChange(args, change,
        swallow_exception=False, prog='git wktry')
Exemplo n.º 4
0
def main(args):
  """Runs cpplint on the current changelist."""
  """Adapted from git_cl.py CMDlint """
  parser = git_cl.OptionParser()
  parser.add_option('--filter', action='append', metavar='-x,+y',
                    help='Comma-separated list of cpplint\'s category-filters')
  parser.add_option('--project_root')
  parser.add_option('--base_branch')
  options, args = parser.parse_args(args)

  # Access to a protected member _XX of a client class
  # pylint: disable=protected-access
  try:
    import cpplint
    import cpplint_chromium
  except ImportError:
    print('Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.')
    return 1

  # Change the current working directory before calling lint so that it
  # shows the correct base.
  settings = git_cl.settings
  previous_cwd = os.getcwd()
  os.chdir(settings.GetRoot())
  cl = git_cl.Changelist()
  base_branch = options.base_branch

  try:
    print('Running cpplint...')
    files = cl.GetAffectedFiles(git_common.get_or_create_merge_base(cl.GetBranch(), base_branch))
    if not files:
      print('Cannot lint an empty CL')
      return 0

    # Process cpplints arguments if any.
    command = args + files
    if options.filter:
      command = ['--filter=' + ','.join(options.filter)] + command
    if options.project_root:
      command = ['--project_root=' + options.project_root.replace('\\', '/')] + command
    filenames = cpplint.ParseArguments(command)

    white_regex = re.compile(settings.GetLintRegex())
    black_regex = re.compile(settings.GetLintIgnoreRegex())
    extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace]
    for filename in filenames:
      if white_regex.match(filename):
        if black_regex.match(filename):
          print('Ignoring file %s' % filename)
        else:
          cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level,
                              extra_check_functions)
      else:
        print('Skipping file %s' % filename)

    # Run format checks
    upstream_branch = cl.GetUpstreamBranch()
    format_output = None
    if base_branch and not (base_branch in upstream_branch):
        print('Skipping clang/gn format check because base_branch is %s instead of %s' % (base_branch, upstream_branch))
    else:
        format_output = RunFormatCheck(upstream_branch)
  finally:
    os.chdir(previous_cwd)

  if format_output:
      print(format_output)
      return 1
  if cpplint._cpplint_state.error_count != 0:
    print('cpplint errors found: %d\n' % cpplint._cpplint_state.error_count)
    return 1

  print('lint and format checks succeeded')
  return 0
def CheckChangedLUCIConfigs(input_api, output_api):
  import collections
  import base64
  import json
  import urllib2

  import auth
  import git_cl

  LUCI_CONFIG_HOST_NAME = 'luci-config.appspot.com'

  cl = git_cl.Changelist()
  remote, remote_branch = cl.GetRemoteBranch()
  if remote_branch.startswith('refs/remotes/%s/' % remote):
    remote_branch = remote_branch.replace(
        'refs/remotes/%s/' % remote, 'refs/heads/', 1)
  if remote_branch.startswith('refs/remotes/branch-heads/'):
    remote_branch = remote_branch.replace(
        'refs/remotes/branch-heads/', 'refs/branch-heads/', 1)

  remote_host_url = cl.GetRemoteUrl()
  if not remote_host_url:
    return [output_api.PresubmitError(
        'Remote host url for git has not been defined')]
  remote_host_url = remote_host_url.rstrip('/')
  if remote_host_url.endswith('.git'):
    remote_host_url = remote_host_url[:-len('.git')]

  # authentication
  try:
    authenticator = auth.get_authenticator_for_host(
        LUCI_CONFIG_HOST_NAME, auth.make_auth_config())
    acc_tkn = authenticator.get_access_token()
  except auth.AuthenticationError as e:
    return [output_api.PresubmitError(
        'Error in authenticating user.', long_text=str(e))]

  def request(endpoint, body=None):
    api_url = ('https://%s/_ah/api/config/v1/%s'
               % (LUCI_CONFIG_HOST_NAME, endpoint))
    req = urllib2.Request(api_url)
    req.add_header('Authorization', 'Bearer %s' % acc_tkn.token)
    if body is not None:
      req.add_header('Content-Type', 'application/json')
      req.add_data(json.dumps(body))
    return json.load(urllib2.urlopen(req))

  try:
    config_sets = request('config-sets').get('config_sets')
  except urllib2.HTTPError as e:
    return [output_api.PresubmitError(
        'Config set request to luci-config failed', long_text=str(e))]
  if not config_sets:
    return [output_api.PresubmitWarning('No config_sets were returned')]
  loc_pref = '%s/+/%s/' % (remote_host_url, remote_branch)
  dir_to_config_set = {
    '%s/' % cs['location'][len(loc_pref):].rstrip('/'): cs['config_set']
    for cs in config_sets
    if cs['location'].startswith(loc_pref) or
    ('%s/' % cs['location']) == loc_pref
  }
  cs_to_files = collections.defaultdict(list)
  for f in input_api.AffectedFiles():
    # windows
    file_path = f.LocalPath().replace(_os.sep, '/')
    for dr, cs in dir_to_config_set.iteritems():
      if dr == '/' or file_path.startswith(dr):
        cs_to_files[cs].append({
          'path': file_path[len(dr):] if dr != '/' else file_path,
          'content': base64.b64encode(
              '\n'.join(f.NewContents()).encode('utf-8'))
        })
  outputs = []
  for cs, f in cs_to_files.iteritems():
    try:
      # TODO(myjang): parallelize
      res = request(
          'validate-config', body={'config_set': cs, 'files': f})
    except urllib2.HTTPError as e:
      return [output_api.PresubmitError(
          'Validation request to luci-config failed', long_text=str(e))]
    for msg in res.get('messages', []):
      sev = msg['severity']
      if sev == 'WARNING':
        out_f = output_api.PresubmitPromptWarning
      elif sev == 'ERROR' or sev == 'CRITICAL':
        out_f = output_api.PresubmitError
      else:
        out_f = output_api.PresubmitNotifyResult
      outputs.append(out_f('Config validation: %s' % msg['text']))
  return outputs
Exemplo n.º 6
0
def main(args):
    """Runs cpplint on the current changelist."""
    """Adapted from git_cl.py CMDlint """
    parser = git_cl.OptionParser()
    parser.add_option(
        '--filter',
        action='append',
        metavar='-x,+y',
        help='Comma-separated list of cpplint\'s category-filters')
    parser.add_option('--project_root')
    parser.add_option('--base_branch')
    options, args = parser.parse_args(args)

    # Access to a protected member _XX of a client class
    # pylint: disable=protected-access
    try:
        import cpplint
        import cpplint_chromium
    except ImportError:
        print(
            'Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.'
        )
        return 1

    # Change the current working directory before calling lint so that it
    # shows the correct base.
    settings = git_cl.settings
    previous_cwd = os.getcwd()
    os.chdir(settings.GetRoot())

    exit_code = 0

    # Check for clang/gn format errors.
    cl = git_cl.Changelist()
    upstream_branch = cl.GetUpstreamBranch()
    upstream_commit = git_cl.RunGit(['merge-base', 'HEAD', upstream_branch])
    try:
        if HasFormatErrors(upstream_commit):
            print('Format check failed. Run npm format to fix.')
            exit_code = 1
    except:
        e = sys.exc_info()[1]
        print('Error running format check: %s' % e.info)
        exit_code = 1

    if exit_code == 0:
        print('Format check succeeded.')

    print('Running cpplint...')
    try:
        files = cl.GetAffectedFiles(
            git_common.get_or_create_merge_base(cl.GetBranch(),
                                                options.base_branch))
        if not files:
            print('Cannot lint an empty CL')
            return 0

        # Process cpplints arguments if any.
        command = args + files
        if options.filter:
            command = ['--filter=' + ','.join(options.filter)] + command
        if options.project_root:
            command = ['--project_root=' + options.project_root] + command
        filenames = cpplint.ParseArguments(command)

        white_regex = re.compile(settings.GetLintRegex())
        black_regex = re.compile(settings.GetLintIgnoreRegex())
        extra_check_functions = [
            cpplint_chromium.CheckPointerDeclarationWhitespace
        ]
        for filename in filenames:
            if white_regex.match(filename):
                if black_regex.match(filename):
                    print('Ignoring file %s' % filename)
                else:
                    cpplint.ProcessFile(filename,
                                        cpplint._cpplint_state.verbose_level,
                                        extra_check_functions)
            else:
                print('Skipping file %s' % filename)
    finally:
        os.chdir(previous_cwd)
    print('cpplint errors found: %d\n' % cpplint._cpplint_state.error_count)
    if cpplint._cpplint_state.error_count != 0:
        return 1
    return exit_code
Exemplo n.º 7
0
if __name__ == '__main__':
    args = sys.argv[1:]
    patchset = GetRietveldPatchsetNumber()
    if patchset:
        args.extend([
            '--issue',
            GetRietveldIssueNumber(),
            '--patchset',
            patchset,
        ])
    else:
        rietveld_url = GetRietveldServerUrl()
        if rietveld_url:
            args.extend(['--rietveld_url', GetRietveldServerUrl()])
    try:
        cl = git_cl.Changelist()
        change = cl.GetChange(cl.GetUpstreamBranch(), None)
        # Hack around a limitation in logging.
        logging.getLogger().handlers = []
        sys.exit(
            trychange.TryChange(
                args,
                change,
                swallow_exception=False,
                prog='git try',
                extra_epilog='\n'
                'git try will diff against your tracked branch and will '
                'detect your rietveld\n'
                'code review if you are using git-cl\n'))
    except third_party.upload.ClientLoginError, e:
        print('Got an exception while trying to log in to Rietveld.')
Exemplo n.º 8
0
    def __append_branch(self, branch, depth=0):
        """Recurses through the tree structure and appends an OutputLine to the
    OutputManager for each branch."""
        branch_info = self.__branches_info[branch]
        if branch_info:
            branch_hash = branch_info.hash
        else:
            try:
                branch_hash = hash_one(branch, short=True)
            except subprocess2.CalledProcessError:
                branch_hash = None

        line = OutputLine()

        # The branch name with appropriate indentation.
        suffix = ''
        if branch == self.__current_branch or (self.__current_branch == 'HEAD'
                                               and branch
                                               == self.__current_hash):
            suffix = ' *'
        branch_string = branch
        if branch in self.__gone_branches:
            branch_string = '{%s:GONE}' % branch
        if not branch:
            branch_string = '{NO_UPSTREAM}'
        main_string = '  ' * depth + branch_string + suffix
        line.append(main_string,
                    color=self.__color_for_branch(branch, branch_hash))

        # The branch hash.
        if self.verbosity >= 2:
            line.append(branch_hash or '', separator=' ', color=Fore.RED)

        # The branch tracking status.
        if self.verbosity >= 1:
            ahead_string = ''
            behind_string = ''
            front_separator = ''
            center_separator = ''
            back_separator = ''
            if branch_info and not self.__is_invalid_parent(
                    branch_info.upstream):
                ahead = branch_info.ahead
                behind = branch_info.behind

                if ahead:
                    ahead_string = 'ahead %d' % ahead
                if behind:
                    behind_string = 'behind %d' % behind

                if ahead or behind:
                    front_separator = '['
                    back_separator = ']'

                if ahead and behind:
                    center_separator = '|'

            line.append(front_separator, separator=' ')
            line.append(ahead_string, separator=' ', color=Fore.MAGENTA)
            line.append(center_separator, separator=' ')
            line.append(behind_string, separator=' ', color=Fore.MAGENTA)
            line.append(back_separator)

        # The Rietveld issue associated with the branch.
        if self.verbosity >= 2:
            import git_cl  # avoid heavy import cost unless we need it
            none_text = '' if self.__is_invalid_parent(branch) else 'None'
            url = git_cl.Changelist(
                branchref=branch).GetIssueURL() if branch_hash else None
            line.append(url or none_text,
                        color=Fore.BLUE if url else Fore.WHITE)

        self.output.append(line)

        for child in sorted(self.__parent_map.pop(branch, ())):
            self.__append_branch(child, depth=depth + 1)