def cleanup(rel_info):
  """Delete test branch, switch back to experiment branch.
  """
  args = rel_info.args

  os.chdir(args.aosp_root)

  utils.repo_forall('git checkout %s' % (rel_info.experiment_branch), verbose=args.verbose)
  utils.repo_forall('git branch -D %s' % (rel_info.test_branch), verbose=args.verbose)
def merge_branches(rel_info):
    """Merge the experiment branch.

    First figure out the exact branch name, then merge the experiment into the
    test branch.

    Throws:
      Exception: if the experiment branch does not exist.
    """
    args = rel_info.args

    os.chdir(os.path.join(args.aosp_root, 'frameworks', 'base'))

    logger.debug("Parsing experiment branches.")
    lines = subprocess.check_output('git branch -a', shell=True)

    exp_branches = []
    logging_branches = []
    for line in sorted(lines.split('\n')):
        line = line.strip()
        b = '/'.join(line.split('/')[1:])
        if line.startswith('remotes/%s/%s/android-%s' %
                           (args.remote, LOGGING_BRANCH_PREFIX, args.aosp_base)):
            logger.info("Find logging branch %s" % (b))
            logging_branches.append(b)

        if line.startswith('remotes/%s/%s/android-%s' %
                           (args.remote,
                            EXPERIMENT_BRANCH_PREFIX, args.aosp_base)):
            if any([e in b for e in args.exp]):
                logger.info("Find experiment branch %s" % (b))
                exp_branches.append(b)

    if len(exp_branches) == 0:
        raise Exception(
            "No experiment branch found for experiment %s" % (args.exp))

    os.chdir(args.aosp_root)

    logger.info("Merging logging branches...")
    for b in logging_branches:
        utils.repo_forall('git merge %s -m "merge"' % (b), verbose=args.verbose)

    projs = utils.get_repo_projs(args.aosp_root)
    for exp in exp_branches:
        logger.info("Merging %s ..." % (exp))

        for proj in projs:
            os.chdir(os.path.join(args.aosp_root, proj))
            try:
                utils.call('git merge %s -m "test merge"' % (exp), verbose=args.verbose)
            except:
                logger.error("Failed to merge %s in repo %s" % (exp, proj))
                raise
            finally:
                os.chdir(os.path.join(args.aosp_root))
def setup_test_branch(rel_info):
  """Set up temporal test branch.

  The test branch is forked base on the latest PhoneLab develop branch.
  """
  args = rel_info.args

  os.chdir(args.aosp_root)

  logger.info("Fetching latest PhoneLab develop branch: %s" % (args.dev))
  utils.repo_forall('git fetch %s %s:%s' % (args.remote, args.dev, args.dev), verbose=args.verbose)

  branch = rand_string()
  logger.info("Creating temp branch %s" % (branch))
  utils.repo_forall('git checkout -B %s %s' % (branch,\
      args.dev), verbose=args.verbose)

  rel_info.test_branch = branch
def merge_branches(rel_info):
  """Merge the experiment branch.

  First figure out the exact branch name, then merge the experiment into the
  test branch.

  Throws:
    Exception: if the experiment branch does not exist.
  """
  args = rel_info.args

  os.chdir(os.path.join(args.aosp_root, 'frameworks', 'base'))

  logger.debug("Parsing experiment branches.")
  lines = subprocess.check_output('git branch -a', shell=True)

  experiment_branch = None
  for line in sorted(lines.split('\n')):
    line = line.strip()
    if line.startswith('remotes/%s/%s/android-%s' % (args.remote,\
        EXPERIMENT_BRANCH_PREFIX, args.aosp_base)):
      experiment_branch = '/'.join(line.split('/')[2:])
      if args.exp in experiment_branch:
        break

  if experiment_branch is None:
    raise Exception("No experiment branch found for experiment %s" % (args.exp))

  # refer to remote branch instead of local branch in case
  # the branch is not checked out yet for some projects
  experiment_branch = args.remote + '/' + experiment_branch

  # assign the experiment branch to rel_info right now.
  # otherwise, cleanup will not fail because of
  # undefined field.
  rel_info.experiment_branch = experiment_branch

  os.chdir(args.aosp_root)

  logger.info("Merging %s into %s" % (experiment_branch, rel_info.test_branch))
  utils.repo_forall('git merge %s -m "merge"' % (experiment_branch), verbose=args.verbose)