# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A utility script to prepare a binary release."""

import glob
import logging
import os
import re
import shutil
import subprocess
import sys
import urllib
import zipfile


_LOGGER = logging.getLogger(__name__)
_SRC_DIR = os.path.abspath(
    os.path.join(os.path.dirname(__file__), '../..'))
_VERSION_FILE = os.path.join(_SRC_DIR, 'syzygy/VERSION')
_BINARIES_DIR = os.path.join(_SRC_DIR, 'syzygy/binaries')


_GIT_VERSION_RE = re.compile('git-svn-id:[^@]+@([0-9]+)', re.M)


_SYZYGY_RELEASE_URL = ('http://syzygy-archive.commondatastorage.googleapis.com/'
    'builds/official/%(revision)d/benchmark.zip')


def _Shell(*cmd, **kw):
  """Runs cmd, returns the results from Popen(cmd).communicate()."""
  _LOGGER.info("Executing %s.", cmd)
  prog = subprocess.Popen(cmd, shell=True, **kw)

  stdout, stderr = prog.communicate()
  if prog.returncode != 0:
    raise RuntimeError("Command '%s' returned %d." % (cmd, prog.returncode))
  return (stdout, stderr)


def _GetFileVersion(file_path):
  # Get the most recent log message for the file, capture STDOUT.
  (stdout, ignore) = _Shell('git', 'log',
                            '-1', file_path,
                            stdout=subprocess.PIPE)
  match = _GIT_VERSION_RE.search(stdout)
  if not match:
    raise RuntimeError("Couldn't determine release version.")

  return int(match.group(1))


def main():
  # Enable info logging.
  logging.basicConfig(level=logging.INFO)

  # Retrieve the VERSION file's SVN revision number.
  revision = _GetFileVersion(_VERSION_FILE)

  # And build the corresponding archive URL.
  url = _SYZYGY_RELEASE_URL % { 'revision': revision }

  # Retrieve the corresponding archive to a temp file.
  _LOGGER.info("Retrieving release archive at '%s'.", url)
  (temp_file, response) = urllib.urlretrieve(url)

  # Create a new feature branch off the master branch for the release
  # before we start changing any files.
  _LOGGER.info("Creating a release-binaries feature branch.")
  _Shell('git', 'checkout', '-b', 'release-binaries', 'master')

  # Clean out the binaries directory.
  shutil.rmtree(_BINARIES_DIR)
  os.makedirs(_BINARIES_DIR)

  # Extract the contents of the archive to the binaries directory.
  _LOGGER.info("Unzipping release archive.")
  archive = zipfile.ZipFile(temp_file, 'r')
  archive.extractall(_BINARIES_DIR)

  # Now extract the executables from the Benchmark_Chrome egg to the
  # "exe" subdir of the binaries dir.
  egg_file = glob.glob(os.path.join(_BINARIES_DIR, 'Benchmark_Chrome*.egg'))[0]
  archive = zipfile.ZipFile(egg_file, 'r')
  exes = filter(lambda path: path.startswith('exe'), archive.namelist())
  print exes
  archive.extractall(_BINARIES_DIR, exes)

  # Add all the new files to the repo.
  _LOGGER.info("Committing release files.")
  _Shell('git', 'add', os.path.join(_BINARIES_DIR, '*'))

  # Now commit and upload the new binaries.
  message = 'Checking in version %d release binaries.' % revision
  _Shell('git', 'commit', '-m', message)
  _Shell('git', 'cl', 'upload', '-m', message)


if __name__ == '__main__':
  main()

@@ -1286,17 +1286,17 @@
 ER.info(
-%22
+'
 Executin
@@ -1300,17 +1300,17 @@
 ting %25s.
-%22
+'
 , cmd)%0A 
@@ -1450,17 +1450,17 @@
 ror(
-%22
+'
 Command 
 '%25s'
@@ -1455,20 +1455,20 @@
 Command 
-'%25s'
+%22%25s%22
  returne
@@ -1472,17 +1472,17 @@
 rned %25d.
-%22
+'
  %25 (cmd,
@@ -1849,16 +1849,17 @@
 ror(
-%22
+'
 Could
-n'
+ no
 t de
@@ -1882,17 +1882,17 @@
 version.
-%22
+'
 )%0A%0A  ret
@@ -2266,17 +2266,17 @@
 ER.info(
-%22
+'
 Retrievi
@@ -2301,14 +2301,14 @@
  at 
-'%25s'.%22
+%22%25s%22.'
 , ur
@@ -2486,17 +2486,17 @@
 ER.info(
-%22
+'
 Creating
@@ -2530,17 +2530,17 @@
  branch.
-%22
+'
 )%0A  _She
@@ -2779,17 +2779,17 @@
 ER.info(
-%22
+'
 Unzippin
@@ -2806,17 +2806,17 @@
 archive.
-%22
+'
 )%0A  arch
@@ -2967,13 +2967,13 @@
   # 
-%22exe%22
+'exe'
  sub
@@ -3303,17 +3303,17 @@
 ER.info(
-%22
+'
 Committi
@@ -3333,13 +3333,117 @@
 les.
-%22
+'
 )%0A  
+# Update any changed or deleted files.%0A  _Shell('git', 'add', '-u', _BINARIES_DIR)%0A  # Add new files.%0A  
 _She
@@ -3459,29 +3459,16 @@
  'add', 
-os.path.join(
 _BINARIE
@@ -3476,14 +3476,8 @@
 _DIR
-, '*')
 )%0A%0A 

Make sure deletes are committed as well as modify/adds. Standardize to single quotes, except for doc strings.