def Run(self, args):
    try:
      contexts = context_util.CalculateExtendedSourceContexts(
          args.source_directory)
    except context_util.GenerateSourceContextError as e:
      # This is a usage error. Wrap it with core_exceptions.Error to report
      # it properly (i.e., as an error instead of a crash).
      raise core_exceptions.Error(e)

    # Create the source-context.json file.
    output_file = context_util.CONTEXT_FILENAME

    output_directory = args.output_directory
    output_file = os.path.join(output_directory, output_file)

    if context_util.HasPendingChanges(args.source_directory):
      log.warning(
          'There are uncommitted changes in directory [{0}].\n'
          'The generated source context files will not reflect the current '
          'state of your source code.\n'
          'For best results, commit all changes and re-run this command.\n'
          .format(args.source_directory))
    best_context = context_util.BestSourceContext(contexts)
    files.MakeDir(output_directory)
    files.WriteFileContents(
        output_file, json.dumps(best_context, indent=2, sort_keys=True))
def _GetSourceContextsForUpload(source_dir):
  """Gets source context file information.

  Args:
    source_dir: str, path to the service's source directory
  Returns:
    A dict of filename to (str) source context file contents.
  """
  source_contexts = {}
  # Error message in case of failure.
  m = ('Could not generate [{name}]: {error}\n'
       'Stackdriver Debugger may not be configured or enabled on this '
       'application. See https://cloud.google.com/debugger/ for more '
       'information.')
  try:
    contexts = context_util.CalculateExtendedSourceContexts(source_dir)
  except context_util.GenerateSourceContextError as e:
    log.info(m.format(name=context_util.CONTEXT_FILENAME, error=e))
    return source_contexts

  try:
    context = context_util.BestSourceContext(contexts)
    source_contexts[context_util.CONTEXT_FILENAME] = json.dumps(context)
  except KeyError as e:
    log.info(m.format(name=context_util.CONTEXT_FILENAME, error=e))
  return source_contexts
    def Run(self, args):
        log.warning('This command is deprecated. Please use '
                    '`gcloud beta source debug gen-repo-info-file` instead.')
        contexts = context_util.CalculateExtendedSourceContexts(
            args.source_directory)

        # First create the old-style source-context.json file
        if args.output_file:
            log.warning(
                'The --output-file option is deprecated and will soon be removed.'
            )
            output_directory = os.path.dirname(args.output_file)
            output_file = args.output_file
        else:
            output_directory = ''
            output_file = context_util.CONTEXT_FILENAME

        if not output_directory:
            if args.output_directory:
                output_directory = args.output_directory
                output_file = os.path.join(output_directory, output_file)
            else:
                output_directory = '.'

        best_context = context_util.BestSourceContext(contexts)
        files.MakeDir(output_directory)
        files.WriteFileContents(
            output_file, json.dumps(best_context, indent=2, sort_keys=True))
    def Run(self, args):
        contexts = context_util.CalculateExtendedSourceContexts(
            args.source_directory)

        # First create the old-style source-context.json file
        output_file = context_util.CONTEXT_FILENAME

        output_directory = args.output_directory
        output_file = os.path.join(output_directory, output_file)

        if context_util.HasPendingChanges(args.source_directory):
            log.warn(
                'There are uncommitted changes in directory [{0}].\n'
                'The generated source context files will not reflect the current '
                'state of your source code.\n'
                'For best results, commit all changes and re-run this command.\n'
                .format(args.source_directory))
        best_context = context_util.BestSourceContext(contexts)
        files.MakeDir(output_directory)
        with open(output_file, 'w') as f:
            json.dump(best_context, f, indent=2, sort_keys=True)

        # Create the new source-contexts.json file.
        with open(
                os.path.join(output_directory,
                             context_util.EXT_CONTEXT_FILENAME), 'w') as f:
            json.dump(contexts, f, indent=2, sort_keys=True)
示例#5
0
    def _AddSourceDirToWorkspace(self, workspace, src_name, target_root):
        """Add files in the given directory to a workspace.

    Args:
      workspace: (source.Workspace) The workspace to add files to.
      src_name: (string) A directory to capture.
      target_root: (string) Root directory of the target tree in the capture.
    Returns:
      ([dict], int, int, int, int) A 4-tuple containing an array of source
      contexts, the number of files added to the workspace, the total size of
      the files added, and a count of files that were too big to upload.
    """

        src_path = os.path.abspath(src_name)
        source_contexts = []
        # Add context for any external repos.
        try:
            for s in contexts.CalculateExtendedSourceContexts(src_path):
                if s not in source_contexts:
                    source_contexts.append(s)
        except contexts.GenerateSourceContextError:
            # We don't care if there's no external source context. We can even
            # capture a bunch of local files if necessary.
            pass

        # TODO(b/36057057) Once "wsync capture" is available, use that instead of
        # explicitly modifying the workspace as we do here.

        paths = [
            os.path.relpath(f, src_path)
            for f in self._ignore_handler.GetFiles(src_path)
            if not os.path.islink(f)
        ]
        with _Uploader(workspace) as uploader:
            uploader.Start(len(paths))
            contents = None
            for path in paths:
                with open(os.path.join(src_path, path), 'r') as f:
                    contents = f.read()
                if contents:
                    uploader.UploadFile(os.path.join(target_root, path),
                                        contents)
            return (source_contexts, ) + uploader.GetStatistics()
示例#6
0
def _GetSourceContextsForUpload(source_dir):
    """Gets source context file information.

  Args:
    source_dir: str, path to the service's source directory
  Returns:
    A dict of filename to (str) source context file contents.
  """
    source_contexts = {}
    try:
        contexts = context_util.CalculateExtendedSourceContexts(source_dir)
        source_contexts[context_util.EXT_CONTEXT_FILENAME] = json.dumps(
            contexts)
        context = context_util.BestSourceContext(contexts)
        source_contexts[context_util.CONTEXT_FILENAME] = json.dumps(context)
    # This error could either be raised by context_util.BestSourceContext or by
    # context_util.CalculateExtendedSourceContexts (in which case stop looking)
    except context_util.GenerateSourceContextError as e:
        log.warn('Could not generate [{name}]: {error}'.format(
            name=context_util.CONTEXT_FILENAME, error=e))
    return source_contexts
示例#7
0
  def Run(self, args):
    contexts = context_util.CalculateExtendedSourceContexts(
        args.source_directory)

    # First create the old-style source-context.json file
    output_file = context_util.CONTEXT_FILENAME

    output_directory = args.output_directory
    output_file = os.path.join(output_directory, output_file)

    best_context = context_util.BestSourceContext(contexts,
                                                  args.source_directory)
    files.MakeDir(output_directory)
    with open(output_file, 'w') as f:
      json.dump(best_context, f, indent=2, sort_keys=True)

    # Create the new source-contexts.json file.
    with open(
        os.path.join(output_directory,
                     context_util.EXT_CONTEXT_FILENAME), 'w') as f:
      json.dump(contexts, f, indent=2, sort_keys=True)
示例#8
0
    def Run(self, args):
        contexts = context_util.CalculateExtendedSourceContexts(
            args.source_directory)

        # First create the old-style source-context.json file
        if args.output_file:
            log.warn(
                'The --output-file option is deprecated and will soon be removed.'
            )
            output_directory = os.path.dirname(args.output_file)
            output_file = args.output_file
        else:
            output_directory = ''
            output_file = context_util.CONTEXT_FILENAME

        if not output_directory:
            if args.output_directory:
                output_directory = args.output_directory
                output_file = os.path.join(output_directory, output_file)
            else:
                output_directory = '.'

        best_context = context_util.BestSourceContext(contexts,
                                                      args.source_directory)
        files.MakeDir(output_directory)
        with open(output_file, 'w') as f:
            json.dump(best_context, f, indent=2, sort_keys=True)

        # Create the new source-contexts.json file.
        if args.output_directory and args.output_directory != output_directory:
            output_directory = args.output_directory
            files.MakeDir(output_directory)
        with open(
                os.path.join(output_directory,
                             context_util.EXT_CONTEXT_FILENAME), 'w') as f:
            json.dump(contexts, f, indent=2, sort_keys=True)
示例#9
0
    def _AddSourceDirToWorkspace(self, workspace, src_name, target_root):
        """Add files in the given directory to a workspace.

    Args:
      workspace: (source.Workspace) The workspace to add files to.
      src_name: (string) A directory to capture.
      target_root: (string) Root directory of the target tree in the capture.
    Returns:
      ([dict], int, int) A 3-tuple containing an array of source contexts,
      the number of files added to the workspace, and the total size of the
      files added.
    """

        src_path = os.path.abspath(src_name)
        source_contexts = []
        # Add context for any external repos.
        try:
            for s in contexts.CalculateExtendedSourceContexts(src_path):
                if s not in source_contexts:
                    source_contexts.append(s)
        except contexts.GenerateSourceContextError:
            # We don't care if there's no external source context. We can even
            # capture a bunch of local files if necessary.
            pass

        # TODO(user) Once "wsync capture" is available, use that instead of
        # explicitly modifying the workspace as we do here.

        paths = [
            os.path.relpath(f, src_path)
            for f in self._ignore_handler.GetFiles(src_path)
            if not os.path.islink(f)
        ]
        total_files = len(paths)
        progress_bar = console_io.ProgressBar(
            'Uploading {0} files'.format(total_files))
        (read_progress,
         write_progress) = console_io.ProgressBar.SplitProgressBar(
             progress_bar.SetProgress, [1, 6])

        def UpdateProgress(action_count):
            write_progress((1.0 * action_count) / total_files)

        workspace.SetPostCallback(UpdateProgress)
        progress_bar.Start()

        total_size = 0
        file_count = 0
        contents = None
        for path in paths:
            with open(os.path.join(src_path, path), 'r') as f:
                contents = f.read()
            if contents:
                total_size += len(contents)
                file_count += 1
                read_progress((1.0 * file_count) / total_files)
                try:
                    workspace.WriteFile(os.path.join(target_root, path),
                                        contents)
                except source.FileTooBigException as e:
                    log.warning(e)
        return (source_contexts, total_files, total_size)