#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A buildbot command for running and interpreting the build_archive.sh script.
"""

from buildbot.steps import shell
from buildbot.process import buildstep

class ScriptObserver(buildstep.LogLineObserver):
  """This class knows how to understand archive_build/coverage/
  layout_test_results.py test output."""

  def __init__(self):
    buildstep.LogLineObserver.__init__(self)
    self.last_change = ''
    self.build_number = ''
    self.build_name = ''

  def outLineReceived(self, line):
    """This is called once with each line of the test log."""
    if line.startswith('last change: '):
      self.last_change = line.split(' ')[2]
    elif line.startswith('build number: '):
      self.build_number = line.split(' ')[2]
    elif line.startswith('build name: '):
      self.build_name = line.split(' ',  2)[2]

class ArchiveCommand(shell.ShellCommand):
  """Buildbot command that knows how to display archive_build/coverage/
  layout_test_results.py test output."""

  def __init__(self, **kwargs):
    shell.ShellCommand.__init__(self, **kwargs)
    self.script_observer = ScriptObserver()
    self.base_url = kwargs['base_url']
    self.link_text = kwargs['link_text']
    self.addLogObserver('stdio', self.script_observer)
    self.index_suffix = kwargs.get('index_suffix', '')

  def createSummary(self, log):
    if (self.base_url and self.link_text):
      base_url = self.base_url % {'build_name': self.script_observer.build_name}
      if self.script_observer.build_number:
        url = ('%s/%s/%s%s' % (base_url,
                               self.script_observer.build_number,
                               self.script_observer.last_change,
                               self.index_suffix))
      else:
        url = ('%s/%s%s' % (base_url,
                            self.script_observer.last_change,
                            self.index_suffix))
      self.addURL(self.link_text, url)

#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A buildbot command for running and interpreting the build_archive.sh script.
"""

from buildbot.steps import shell
from buildbot.process import buildstep

class ScriptObserver(buildstep.LogLineObserver):
  """This class knows how to understand archive_build/coverage/
  layout_test_results.py test output."""

  def __init__(self):
    buildstep.LogLineObserver.__init__(self)
    self.last_change = ''
    self.build_number = ''
    self.build_name = ''

  def outLineReceived(self, line):
    """This is called once with each line of the test log."""
    if line.startswith('last change: '):
      self.last_change = line.split(' ')[2]
    elif line.startswith('build number: '):
      self.build_number = line.split(' ')[2]
    elif line.startswith('build name: '):
      self.build_name = line.split(' ',  2)[2]

class ArchiveCommand(shell.ShellCommand):
  """Buildbot command that knows how to display archive_build/coverage/
  layout_test_results.py test output."""

  # 'parms' is used in buildbot.steps.shell.ShellCommand.__init__ to separate
  # kwargs that should be passed to the BuildStep constructor from kwargs
  # that should be passed to the RemoteShellCommand constructor.  It's
  # harmless to omit 'parms' in buildbot7, but in buildbot8, extra kwargs
  # that aren't listed in 'parms' will cause the RemoteShellCommand
  # constructor to throw an exception.
  parms = ['base_url',
           'link_text',
           'more_link_url',
           'more_link_text',
           'name',
           'index_suffix']

  def __init__(self, **kwargs):
    shell.ShellCommand.__init__(self, **kwargs)
    self.script_observer = ScriptObserver()
    self.base_url = kwargs['base_url']
    self.link_text = kwargs['link_text']
    self.addLogObserver('stdio', self.script_observer)
    self.index_suffix = kwargs.get('index_suffix', '')

  def createSummary(self, log):
    if (self.base_url and self.link_text):
      base_url = self.base_url % {'build_name': self.script_observer.build_name}
      if self.script_observer.build_number:
        url = ('%s/%s/%s%s' % (base_url,
                               self.script_observer.build_number,
                               self.script_observer.last_change,
                               self.index_suffix))
      else:
        url = ('%s/%s%s' % (base_url,
                            self.script_observer.last_change,
                            self.index_suffix))
      self.addURL(self.link_text, url)

Fix archive_build command for buildbot-8 (backwards compatible for buildbot-7).