summaryrefslogtreecommitdiffstats
path: root/tools/export_tarball/export_v8_tarball.py
blob: b232c0aee7396c18064b2a601f199dc3bd064bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env 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.

"""Creates a tarball with V8 sources, but without .svn directories.

This allows easy packaging of V8, synchronized with browser releases.

Example usage:

export_v8_tarball.py /foo/bar

The above will create file /foo/bar/v8-VERSION.tar.bz2 if it doesn't exist.
"""

import optparse
import os
import re
import subprocess
import sys
import tarfile

_V8_MAJOR_VERSION_PATTERN = re.compile(r'#define\s+MAJOR_VERSION\s+(.*)')
_V8_MINOR_VERSION_PATTERN = re.compile(r'#define\s+MINOR_VERSION\s+(.*)')
_V8_BUILD_NUMBER_PATTERN = re.compile(r'#define\s+BUILD_NUMBER\s+(.*)')
_V8_PATCH_LEVEL_PATTERN = re.compile(r'#define\s+PATCH_LEVEL\s+(.*)')

_V8_PATTERNS = [
  _V8_MAJOR_VERSION_PATTERN,
  _V8_MINOR_VERSION_PATTERN,
  _V8_BUILD_NUMBER_PATTERN,
  _V8_PATCH_LEVEL_PATTERN]

_NONESSENTIAL_DIRS = (
    'third_party/icu',
)


def GetV8Version(v8_directory):
  """
  Returns version number as string based on the string
  contents of version.cc file.
  """
  with open(os.path.join(v8_directory, 'src', 'version.cc')) as version_file:
    version_contents = version_file.read()

  version_components = []
  for pattern in _V8_PATTERNS:
    version_components.append(pattern.search(version_contents).group(1).strip())

  if version_components[len(version_components) - 1] == '0':
    version_components.pop()

  return '.'.join(version_components)


def GetSourceDirectory():
  return os.path.realpath(
    os.path.join(os.path.dirname(__file__), '..', '..', '..', 'src'))


def GetV8Directory():
  return os.path.join(GetSourceDirectory(), 'v8')


# Workaround lack of the exclude parameter in add method in python-2.4.
# TODO(phajdan.jr): remove the workaround when it's not needed on the bot.
class MyTarFile(tarfile.TarFile):
  def set_remove_nonessential_files(self, remove):
    self.__remove_nonessential_files = remove

  def add(self, name, arcname=None, recursive=True, exclude=None, filter=None):
    head, tail = os.path.split(name)
    if tail in ('.svn', '.git'):
      return

    if self.__remove_nonessential_files:
      # Remove contents of non-essential directories, but preserve gyp files,
      # so that build/gyp_chromium can work.
      for nonessential_dir in _NONESSENTIAL_DIRS:
        dir_path = os.path.join(GetV8Directory(), nonessential_dir)
        if (name.startswith(dir_path) and
            os.path.isfile(name) and
            'gyp' not in name):
          return

    tarfile.TarFile.add(self, name, arcname=arcname, recursive=recursive)


def main(argv):
  parser = optparse.OptionParser()
  options, args = parser.parse_args(argv)

  if len(args) != 1:
    print 'You must provide only one argument: output file directory'
    return 1

  v8_directory = GetV8Directory()
  if not os.path.exists(v8_directory):
    print 'Cannot find the v8 directory.'
    return 1

  v8_version = GetV8Version(v8_directory)
  print 'Packaging V8 version %s...' % v8_version

  subprocess.check_call(["make", "dependencies"], cwd=v8_directory)

  output_basename = 'v8-%s' % v8_version

  # Package full tarball.
  output_fullname = os.path.join(args[0], output_basename + '.tar.bz2')
  if not os.path.exists(output_fullname):
    archive = MyTarFile.open(output_fullname, 'w:bz2')
    archive.set_remove_nonessential_files(False)
    try:
      archive.add(v8_directory, arcname=output_basename)
    finally:
      archive.close()

  # Package lite tarball.
  output_fullname = os.path.join(args[0], output_basename + '-lite.tar.bz2')
  if not os.path.exists(output_fullname):
    archive = MyTarFile.open(output_fullname, 'w:bz2')
    archive.set_remove_nonessential_files(True)
    try:
      archive.add(v8_directory, arcname=output_basename)
    finally:
      archive.close()

  return 0


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))