xref: /aosp_15_r20/external/libchrome/build/android/gyp/util/jar_info_utils.py (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1# Copyright 2018 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
6
7# Utilities to read and write .jar.info files.
8#
9# A .jar.info file contains a simple mapping from fully-qualified Java class
10# names to the source file that actually defines it.
11#
12# For APKs, the .jar.info maps the class names to the .jar file that which
13# contains its .class definition instead.
14
15
16def ParseJarInfoFile(info_path):
17  """Parse a given .jar.info file as a dictionary.
18
19  Args:
20    info_path: input .jar.info file path.
21  Returns:
22    A new dictionary mapping fully-qualified Java class names to file paths.
23  """
24  info_data = dict()
25  if os.path.exists(info_path):
26    with open(info_path, 'r') as info_file:
27      for line in info_file:
28        line = line.strip()
29        if line:
30          fully_qualified_name, path = line.split(',', 1)
31          info_data[fully_qualified_name] = path
32  return info_data
33
34
35def WriteJarInfoFile(info_path, info_data, source_file_map=None):
36  """Generate a .jar.info file from a given dictionary.
37
38  Args:
39    info_path: output file path.
40    info_data: a mapping of fully qualified Java class names to filepaths.
41    source_file_map: an optional mapping from java source file paths to the
42      corresponding source .srcjar. This is because info_data may contain the
43      path of Java source files that where extracted from an .srcjar into a
44      temporary location.
45  """
46  with open(info_path, 'w') as info_file:
47    for fully_qualified_name, path in info_data.items():
48      if source_file_map and path in source_file_map:
49        path = source_file_map[path]
50        assert not path.startswith('/tmp'), (
51            'Java file path should not be in temp dir: {}'.format(path))
52      info_file.write('{},{}\n'.format(fully_qualified_name, path))
53