<commit_before>#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Commit all files within a directory to an SVN repository.

To test:
  cd .../buildbot/slave/skia_slave_scripts/utils
  echo "SvnUsername" >../../../site_config/.svnusername
  echo "SvnPassword" >../../../site_config/.svnpassword
  rm -rf /tmp/svn-source-dir
  mkdir -p /tmp/svn-source-dir
  date >/tmp/svn-source-dir/date.png
  date >/tmp/svn-source-dir/date.txt
  CR_BUILDBOT_PATH=../../../third_party/chromium_buildbot
  PYTHONPATH=$CR_BUILDBOT_PATH/scripts:$CR_BUILDBOT_PATH/site_config \
   python merge_into_svn.py \
   --source_dir_path=/tmp/svn-source-dir \
   --merge_dir_path=/tmp/svn-merge-dir \
   --dest_svn_url=https://skia-autogen.googlecode.com/svn/gm-actual/test \
   --svn_username_file=.svnusername --svn_password_file=.svnpassword
  # and check
  #  http://code.google.com/p/skia-autogen/source/browse/#svn%2Fgm-actual%2Ftest

"""

import misc
import optparse
import os
import shutil
import sys
import tempfile

from slave import svn


def ReadFirstLineOfFileAsString(filename):
  """Safely return the first line of a file as a string.
  If there is an exception, it will be raised to the caller, but we will
  close the file handle first.

  @param filename path to the file to read
  """
  f = open(filename, 'r')
  try:
    contents = f.readline().splitlines()[0]
  finally:
    f.close()
  return contents

def _CopyAllFiles(source_dir, dest_dir):
  """Copy all files from source_dir into dest_dir.

  Recursively copies files from directories as well.
  Skips .svn directories.

  @param source_dir
  @param dest_dir
  """
  basenames = os.listdir(source_dir)
  for basename in basenames:
    source_path = os.path.join(source_dir, basename)
    dest_path = os.path.join(dest_dir, basename)
    if basename == '.svn':
      continue
    if os.path.isdir(source_path):
      _CopyAllFiles(source_path, dest_path)
    else:
      shutil.copyfile(source_path, dest_path)

def _DeleteDirectoryContents(dir):
  """Delete all contents (recursively) within dir, but don't delete the
  directory itself.

  @param dir directory whose contents to delete
  """
  basenames = os.listdir(dir)
  for basename in basenames:
    path = os.path.join(dir, basename)
    if os.path.isdir(path):
      shutil.rmtree(path)
    else:
      os.unlink(path)

# TODO: We should either add an Update() command to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnUpdate(repo, additional_svn_flags=[]):
  return repo._RunSvnCommand(['update'] + additional_svn_flags)

# TODO: We should either add an Import() command to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnImport(repo, path, url, message, additional_svn_flags=[]):
  return repo._RunSvnCommand(['import', path, url, '--message', message]
                             + additional_svn_flags)

# TODO: We should either add a command like this to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnDoesUrlExist(repo, url):
  try:
    repo._RunSvnCommand(['ls', url])
    return True
  except Exception:
    # TODO: this will treat *any* exception in "svn ls" as signalling that
    # the URL does not exist.  Should we look for something more specific?
    return False

def MergeIntoSvn(options):
  """Update an SVN repository with any new/modified files from a directory.

  @param options struct of command-line option values from optparse
  """
  # Get path to SVN username and password files.
  # (Patterned after slave_utils.py's logic to find the .boto file.)
  site_config_path = os.path.join(
      os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, 'site_config')
  svn_username_path = os.path.join(site_config_path, options.svn_username_file)
  svn_password_path = os.path.join(site_config_path, options.svn_password_file)
  svn_username = ReadFirstLineOfFileAsString(svn_username_path).rstrip()
  svn_password = ReadFirstLineOfFileAsString(svn_password_path).rstrip()

  # Check out the SVN repository into the merge dir.
  # (If no merge dir was specified, use a temporary dir.)
  if options.merge_dir_path:
    mergedir = options.merge_dir_path
  else:
    mergedir = tempfile.mkdtemp()
  if not os.path.isdir(mergedir):
    os.makedirs(mergedir)
  repo = svn.Svn(directory=mergedir,<commit_msg>Re-disable skia-autogen upload until UUID issue is resolved Review URL: https://codereview.appspot.com/6464075<commit_after>#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Commit all files within a directory to an SVN repository.

To test:
  cd .../buildbot/slave/skia_slave_scripts/utils
  echo "SvnUsername" >../../../site_config/.svnusername
  echo "SvnPassword" >../../../site_config/.svnpassword
  rm -rf /tmp/svn-source-dir
  mkdir -p /tmp/svn-source-dir
  date >/tmp/svn-source-dir/date.png
  date >/tmp/svn-source-dir/date.txt
  CR_BUILDBOT_PATH=../../../third_party/chromium_buildbot
  PYTHONPATH=$CR_BUILDBOT_PATH/scripts:$CR_BUILDBOT_PATH/site_config \
   python merge_into_svn.py \
   --source_dir_path=/tmp/svn-source-dir \
   --merge_dir_path=/tmp/svn-merge-dir \
   --dest_svn_url=https://skia-autogen.googlecode.com/svn/gm-actual/test \
   --svn_username_file=.svnusername --svn_password_file=.svnpassword
  # and check
  #  http://code.google.com/p/skia-autogen/source/browse/#svn%2Fgm-actual%2Ftest

"""

import misc
import optparse
import os
import shutil
import sys
import tempfile

from slave import svn


def ReadFirstLineOfFileAsString(filename):
  """Safely return the first line of a file as a string.
  If there is an exception, it will be raised to the caller, but we will
  close the file handle first.

  @param filename path to the file to read
  """
  f = open(filename, 'r')
  try:
    contents = f.readline().splitlines()[0]
  finally:
    f.close()
  return contents

def _CopyAllFiles(source_dir, dest_dir):
  """Copy all files from source_dir into dest_dir.

  Recursively copies files from directories as well.
  Skips .svn directories.

  @param source_dir
  @param dest_dir
  """
  basenames = os.listdir(source_dir)
  for basename in basenames:
    source_path = os.path.join(source_dir, basename)
    dest_path = os.path.join(dest_dir, basename)
    if basename == '.svn':
      continue
    if os.path.isdir(source_path):
      _CopyAllFiles(source_path, dest_path)
    else:
      shutil.copyfile(source_path, dest_path)

def _DeleteDirectoryContents(dir):
  """Delete all contents (recursively) within dir, but don't delete the
  directory itself.

  @param dir directory whose contents to delete
  """
  basenames = os.listdir(dir)
  for basename in basenames:
    path = os.path.join(dir, basename)
    if os.path.isdir(path):
      shutil.rmtree(path)
    else:
      os.unlink(path)

# TODO: We should either add an Update() command to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnUpdate(repo, additional_svn_flags=[]):
  return repo._RunSvnCommand(['update'] + additional_svn_flags)

# TODO: We should either add an Import() command to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnImport(repo, path, url, message, additional_svn_flags=[]):
  return repo._RunSvnCommand(['import', path, url, '--message', message]
                             + additional_svn_flags)

# TODO: We should either add a command like this to svn.py, or make its
# _RunSvnCommand() method public; in the meanwhile, abuse its private
# _RunSvnCommand() method.
# See https://code.google.com/p/skia/issues/detail?id=713
def _SvnDoesUrlExist(repo, url):
  try:
    repo._RunSvnCommand(['ls', url])
    return True
  except Exception:
    # TODO: this will treat *any* exception in "svn ls" as signalling that
    # the URL does not exist.  Should we look for something more specific?
    return False

def MergeIntoSvn(options):
  return 0
  """Update an SVN repository with any new/modified files from a directory.

  @param options struct of command-line option values from optparse
  """
  # Get path to SVN username and password files.
  # (Patterned after slave_utils.py's logic to find the .boto file.)
  site_config_path = os.path.join(
      os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, 'site_config')
  svn_username_path = os.path.join(site_config_path, options.svn_username_file)
  svn_password_path = os.path.join(site_config_path, options.svn_password_file)
  svn_username = ReadFirstLineOfFileAsString(svn_username_path).rstrip()
  svn_password = ReadFirstLineOfFileAsString(svn_password_path).rstrip()

  # Check out the SVN repository into the merge dir.
  # (If no merge dir was specified, use a temporary dir.)
  if options.merge_dir_path:
    mergedir = options.merge_dir_path
  else:
    mergedir = tempfile.mkdtemp()
  if not os.path.isdir(mergedir):
    os.makedirs(mergedir)
  repo = svn.Svn(directory=mergedir,
{'fraction_non_alphanumeric': 0.1086641852770885, 'fraction_numerical': 0.0041356492969396195, 'mean_word_length': 3.8608040201005025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4885094', 'n_tokens_mistral': 3325, 'n_tokens_neox': 3228, 'n_words': 897}