1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7Removes bundled libraries to make sure they are not used.
8
9See README for more details.
10"""
11
12
13import optparse
14import os.path
15import sys
16
17
18def DoMain(argv):
19  my_dirname = os.path.abspath(os.path.dirname(__file__))
20  source_tree_root = os.path.abspath(
21    os.path.join(my_dirname, '..', '..', '..'))
22
23  if os.path.join(source_tree_root, 'build', 'linux', 'unbundle') != my_dirname:
24    print ('Sanity check failed: please run this script from ' +
25           'build/linux/unbundle directory.')
26    return 1
27
28  parser = optparse.OptionParser()
29  parser.add_option('--do-remove', action='store_true')
30
31  options, args = parser.parse_args(argv)
32
33  exclusion_used = {}
34  for exclusion in args:
35    exclusion_used[exclusion] = False
36
37  for root, dirs, files in os.walk(source_tree_root, topdown=False):
38    # Only look at paths which contain a "third_party" component
39    # (note that e.g. third_party.png doesn't count).
40    root_relpath = os.path.relpath(root, source_tree_root)
41    if 'third_party' not in root_relpath.split(os.sep):
42      continue
43
44    for f in files:
45      path = os.path.join(root, f)
46      relpath = os.path.relpath(path, source_tree_root)
47
48      excluded = False
49      for exclusion in args:
50        # Require precise exclusions. Find the right-most third_party
51        # in the relative path, and if there is more than one ignore
52        # the exclusion if it's completely contained within the part
53        # before right-most third_party path component.
54        split = relpath.rsplit(os.sep + 'third_party' + os.sep, 1)
55        if len(split) > 1 and split[0].startswith(exclusion):
56          continue
57
58        if relpath.startswith(exclusion):
59          # Multiple exclusions can match the same path. Go through all of them
60          # and mark each one as used.
61          exclusion_used[exclusion] = True
62          excluded = True
63      if excluded:
64        continue
65
66      # Deleting gyp files almost always leads to gyp failures.
67      # These files come from Chromium project, and can be replaced if needed.
68      if f.endswith('.gyp') or f.endswith('.gypi'):
69        continue
70
71      # Deleting .isolate files leads to gyp failures. They are usually
72      # not used by a distro build anyway.
73      # See http://www.chromium.org/developers/testing/isolated-testing
74      # for more info.
75      if f.endswith('.isolate'):
76        continue
77
78      if options.do_remove:
79        # Delete the file - best way to ensure it's not used during build.
80        os.remove(path)
81      else:
82        # By default just print paths that would be removed.
83        print path
84
85  exit_code = 0
86
87  # Fail if exclusion list contains stale entries - this helps keep it
88  # up to date.
89  for exclusion, used in exclusion_used.iteritems():
90    if not used:
91      print '%s does not exist' % exclusion
92      exit_code = 1
93
94  if not options.do_remove:
95    print ('To actually remove files printed above, please pass ' +
96           '--do-remove flag.')
97
98  return exit_code
99
100
101if __name__ == '__main__':
102  sys.exit(DoMain(sys.argv[1:]))
103