1# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import re
7
8from pylib import constants
9
10
11_EXCLUSIONS = [
12    re.compile(r'.*OWNERS'),  # Should never be included.
13    re.compile(r'.*\.crx'),  # Chrome extension zip files.
14    re.compile(os.path.join('.*',
15                            r'\.git.*')),  # Any '.git*' directories/files.
16    re.compile(r'.*\.so'),  # Libraries packed into .apk.
17    re.compile(r'.*Mojo.*manifest\.json'),  # Some source_set()s pull these in.
18    re.compile(r'.*\.py'),  # Some test_support targets include python deps.
19    re.compile(r'.*\.apk'),  # Should be installed separately.
20    re.compile(r'.*lib.java/.*'),  # Never need java intermediates.
21
22    # Test filter files:
23    re.compile(r'.*/testing/buildbot/filters/.*'),
24
25    # Chrome external extensions config file.
26    re.compile(r'.*external_extensions\.json'),
27
28    # Exists just to test the compile, not to be run.
29    re.compile(r'.*jni_generator_tests'),
30
31    # v8's blobs and icu data get packaged into APKs.
32    re.compile(r'.*snapshot_blob.*\.bin'),
33    re.compile(r'.*icudtl.bin'),
34
35    # Scripts that are needed by swarming, but not on devices:
36    re.compile(r'.*llvm-symbolizer'),
37    re.compile(r'.*md5sum_bin'),
38    re.compile(os.path.join('.*', 'development', 'scripts', 'stack')),
39
40    # Required for java deobfuscation on the host:
41    re.compile(r'.*build/android/stacktrace/.*'),
42    re.compile(r'.*third_party/jdk/.*'),
43    re.compile(r'.*third_party/proguard/.*'),
44
45    # Build artifacts:
46    re.compile(r'.*\.stamp'),
47    re.compile(r'.*.pak\.info'),
48    re.compile(r'.*\.incremental\.json'),
49]
50
51
52def _FilterDataDeps(abs_host_files):
53  exclusions = _EXCLUSIONS + [
54      re.compile(os.path.join(constants.GetOutDirectory(), 'bin'))
55  ]
56  return [p for p in abs_host_files if not any(r.match(p) for r in exclusions)]
57
58
59def DevicePathComponentsFor(host_path, output_directory):
60  """Returns the device path components for a given host path.
61
62  This returns the device path as a list of joinable path components,
63  with None as the first element to indicate that the path should be
64  rooted at $EXTERNAL_STORAGE.
65
66  e.g., given
67
68    '$CHROMIUM_SRC/foo/bar/baz.txt'
69
70  this would return
71
72    [None, 'foo', 'bar', 'baz.txt']
73
74  This handles a couple classes of paths differently than it otherwise would:
75    - All .pak files get mapped to top-level paks/
76    - Anything in the output directory gets mapped relative to the output
77      directory rather than the source directory.
78
79  e.g. given
80
81    '$CHROMIUM_SRC/out/Release/icu_fake_dir/icudtl.dat'
82
83  this would return
84
85    [None, 'icu_fake_dir', 'icudtl.dat']
86
87  Args:
88    host_path: The absolute path to the host file.
89  Returns:
90    A list of device path components.
91  """
92  if host_path.startswith(output_directory):
93    if os.path.splitext(host_path)[1] == '.pak':
94      return [None, 'paks', os.path.basename(host_path)]
95    rel_host_path = os.path.relpath(host_path, output_directory)
96  else:
97    rel_host_path = os.path.relpath(host_path, constants.DIR_SOURCE_ROOT)
98
99  device_path_components = [None]
100  p = rel_host_path
101  while p:
102    p, d = os.path.split(p)
103    if d:
104      device_path_components.insert(1, d)
105  return device_path_components
106
107
108def GetDataDependencies(runtime_deps_path):
109  """Returns a list of device data dependencies.
110
111  Args:
112    runtime_deps_path: A str path to the .runtime_deps file.
113  Returns:
114    A list of (host_path, device_path) tuples.
115  """
116  if not runtime_deps_path:
117    return []
118
119  with open(runtime_deps_path, 'r') as runtime_deps_file:
120    rel_host_files = [l.strip() for l in runtime_deps_file if l]
121
122  output_directory = constants.GetOutDirectory()
123  abs_host_files = [
124      os.path.abspath(os.path.join(output_directory, r))
125      for r in rel_host_files]
126  filtered_abs_host_files = _FilterDataDeps(abs_host_files)
127  # TODO(crbug.com/752610): Filter out host executables, and investigate
128  # whether other files could be filtered as well.
129  return [(f, DevicePathComponentsFor(f, output_directory))
130          for f in filtered_abs_host_files]
131