File: jar_src.py

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rwxr-xr-x 4,739 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
#
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import sys
import zipfile

REPOSITORY_ROOT = os.path.abspath(
    os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))

# pylint: disable=wrong-import-position
# pylint: disable=import-error
sys.path.insert(0, os.path.join(REPOSITORY_ROOT, 'build/android/gyp'))
from util import build_utils
import action_helpers  # build_utils adds //build to sys.path.
import zip_helpers
# pylint: enable=import-error
# pylint: enable=wrong-import-position

JAVA_PACKAGE_PREFIX = 'org/chromium/'
JNI_ZERO_PACKAGE_PREFIX = 'org/jni_zero/'


def main():
  parser = argparse.ArgumentParser()
  action_helpers.add_depfile_arg(parser)
  parser.add_argument(
      '--excluded-classes',
      help='A list of .class file patterns to exclude from the jar.')
  parser.add_argument(
      '--src-search-dirs',
      action='append',
      help='A list of directories that should be searched'
      ' for the source files.')
  parser.add_argument(
      '--src-files', action='append', help='A list of source files to jar.')
  parser.add_argument(
      '--src-jars',
      action='append',
      help='A list of source jars to include in addition to source files.')
  parser.add_argument(
      '--src-list-files',
      action='append',
      help='A list of files that contain a list of sources,'
      ' e.g. a list of \'.sources\' files generated by GN.')
  parser.add_argument('--jar-path', help='Jar output path.', required=True)

  options = parser.parse_args()

  options.src_jars = action_helpers.parse_gn_list(options.src_jars)
  options.src_search_dirs = action_helpers.parse_gn_list(
      options.src_search_dirs)
  options.src_list_files = action_helpers.parse_gn_list(options.src_list_files)
  options.src_files = action_helpers.parse_gn_list(options.src_files)
  options.excluded_classes = action_helpers.parse_gn_list(
      options.excluded_classes)

  src_files = options.src_files

  # Add files from --source_list_files
  for src_list_file in options.src_list_files:
    with open(src_list_file, 'r') as f:
      src_files.extend(f.read().splitlines())

  # Preprocess source files by removing any prefix that comes before
  # the Java package name.
  for i, s in enumerate(src_files):
    prefix_position = s.find(JAVA_PACKAGE_PREFIX)
    if prefix_position == -1:
      prefix_position = s.find(JNI_ZERO_PACKAGE_PREFIX)
    if prefix_position != -1:
      src_files[i] = s[prefix_position:]

  excluded_classes = [
      f.replace('.class', '.java') for f in options.excluded_classes
  ]

  predicate = None
  if excluded_classes:
    predicate = lambda f: not build_utils.MatchesGlob(f, excluded_classes)

  # Create a dictionary that maps every source directory
  # to source files that it contains.
  dir_to_files_map = {}
  # Initialize the map.
  for src_search_dir in options.src_search_dirs:
    dir_to_files_map[src_search_dir] = []
  # Fill the map.
  for src_file in src_files:
    number_of_file_instances = 0
    for src_search_dir in options.src_search_dirs:
      target_path = os.path.join(src_search_dir, src_file)
      if os.path.isfile(target_path):
        number_of_file_instances += 1
        if not predicate or predicate(src_file):
          dir_to_files_map[src_search_dir].append(target_path)
    if (number_of_file_instances > 1):
      raise Exception('There is more than one instance of file %s in %s' %
                      (src_file, options.src_search_dirs))
    if (number_of_file_instances < 1):
      raise Exception('Unable to find file %s in %s' %
                      (src_file, options.src_search_dirs))

  # Jar the sources from every source search directory.
  with action_helpers.atomic_output(options.jar_path) as o, \
      zipfile.ZipFile(o, 'w', zipfile.ZIP_DEFLATED) as z:
    for src_search_dir in options.src_search_dirs:
      subpaths = dir_to_files_map[src_search_dir]
      if subpaths:
        zip_helpers.add_files_to_zip(subpaths, z, base_dir=src_search_dir)
      else:
        raise Exception(
            'Directory %s does not contain any files and can be'
            ' removed from the list of directories to search' % src_search_dir)

    # Jar additional src jars
    if options.src_jars:
      zip_helpers.merge_zips(z, options.src_jars, compress=True)

  if options.depfile:
    deps = []
    for sources in dir_to_files_map.values():
      deps.extend(sources)
    # Srcjar deps already captured in GN rules (no need to list them here).
    action_helpers.write_depfile(options.depfile, options.jar_path, deps)


if __name__ == '__main__':
  sys.exit(main())