From 3bcea86de6bbab9ae23146b2aed08028ae800ecb Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Tue, 12 Jan 2010 06:36:38 +0000 Subject: [PATCH] Make export_tarball.py more reliable and simplify it. TEST=none BUG=none Review URL: http://codereview.chromium.org/545010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35996 0039d316-1c4b-4281-b951-d872f2087c98 --- tools/export_tarball/export_tarball.py | 82 ++++++++++++-------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/tools/export_tarball/export_tarball.py b/tools/export_tarball/export_tarball.py index 79cc4face04b19..607f478f8810b4 100644 --- a/tools/export_tarball/export_tarball.py +++ b/tools/export_tarball/export_tarball.py @@ -9,7 +9,7 @@ It can also remove files which are not strictly required for build, so that the resulting tarball can be reasonably small (last time it was ~110 MB). -Example usage (make sure gclient is in your PATH): +Example usage: export_tarball.py /foo/bar @@ -21,20 +21,25 @@ import contextlib import optparse import os -import shutil -import subprocess import sys import tarfile -import tempfile -def RunCommand(argv): - """Runs the command with given argv and returns exit code.""" - try: - proc = subprocess.Popen(argv, stdout=None) - except OSError: - return 1 - output = proc.communicate()[0] - return proc.returncode +NONESSENTIAL_DIRS = ( + 'chrome/test/data', + 'chrome/tools/test/reference_build', + 'gears/binaries', + 'net/data/cache_tests', + 'o3d/documentation', + 'o3d/samples', + 'third_party/lighttpd', + 'third_party/WebKit/LayoutTests', + 'webkit/data/layout_tests', + 'webkit/tools/test/reference_build', +) + +def GetSourceDirectory(): + return os.path.realpath( + os.path.join(os.path.dirname(__file__), '..', '..', '..', 'src')) def main(argv): parser = optparse.OptionParser() @@ -49,40 +54,29 @@ def main(argv): print '(without .tar.bz2 extension).' return 1 + if not os.path.exists(GetSourceDirectory()): + print 'Cannot find the src directory.' + return 1 + output_fullname = args[0] + '.tar.bz2' output_basename = os.path.basename(args[0]) - target_dir = tempfile.mkdtemp() - - try: - if RunCommand(['gclient', 'export', target_dir]) != 0: - print 'gclient failed' - return 1 - - if options.remove_nonessential_files: - nonessential_dirs = ( - 'src/chrome/test/data', - 'src/chrome/tools/test/reference_build', - 'src/gears/binaries', - 'src/net/data/cache_tests', - 'src/o3d/documentation', - 'src/o3d/samples', - 'src/third_party/lighttpd', - 'src/third_party/WebKit/LayoutTests', - 'src/webkit/data/layout_tests', - 'src/webkit/tools/test/reference_build', - ) - for dir in nonessential_dirs: - path = os.path.join(target_dir, dir) - try: - print 'removing %s...' % dir - shutil.rmtree(path) - except OSError, e: - print 'error while trying to remove %s, skipping' % dir - - with contextlib.closing(tarfile.open(output_fullname, 'w:bz2')) as archive: - archive.add(os.path.join(target_dir, 'src'), arcname=output_basename) - finally: - shutil.rmtree(target_dir) + + def ShouldExcludePath(path): + head, tail = os.path.split(path) + if tail in ('.svn', '.git'): + return True + + if not options.remove_nonessential_files: + return False + for nonessential_dir in NONESSENTIAL_DIRS: + if path.startswith(os.path.join(GetSourceDirectory(), nonessential_dir)): + return True + + return False + + with contextlib.closing(tarfile.open(output_fullname, 'w:bz2')) as archive: + archive.add(GetSourceDirectory(), arcname=output_basename, + exclude=ShouldExcludePath) return 0