• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3 -B
2
3# Copyright 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""ojluni_modify_expectation is a command-line tool for modifying the EXPECTED_UPSTREAM file."""
17
18import argparse
19import sys
20
21# pylint: disable=g-importing-member
22# pylint: disable=g-multiple-import
23from typing import (
24    Sequence,
25    List,
26)
27
28from common_util import (
29    ExpectedUpstreamEntry,
30    ExpectedUpstreamFile,
31    LIBCORE_DIR,
32    OpenjdkFinder,
33    OjluniFinder,
34)
35
36# Import git only after common_util because common_util will
37# produce informative error
38from git import (Commit, Repo)
39from gitdb.exc import BadName
40
41LIBCORE_REPO = Repo(LIBCORE_DIR.as_posix())
42
43AUTOCOMPLETE_TAGS = [
44    'jdk7u/jdk7u40-b60',
45    'jdk8u/jdk8u121-b13',
46    'jdk8u/jdk8u60-b31',
47    'jdk9/jdk-9+181',
48    'jdk11u/jdk-11+28',
49    'jdk11u/jdk-11.0.13-ga',
50]
51
52
53def error_and_exit(msg: str) -> None:
54  print(f'Error: {msg}', file=sys.stderr)
55  sys.exit(1)
56
57
58def get_commit_or_exit(git_ref: str) -> Commit:
59  try:
60    return LIBCORE_REPO.commit(git_ref)
61  except BadName as e:
62    error_and_exit(f'{e}')
63
64
65def autocomplete_tag_or_commit(str_tag_or_commit: str) -> List[str]:
66  """Returns a list of tags / commits matching the given partial string."""
67  if str_tag_or_commit is None:
68    str_tag_or_commit = ''
69  return list(
70      filter(lambda tag: tag.startswith(str_tag_or_commit), AUTOCOMPLETE_TAGS))
71
72
73COMMAND_ACTIONS = ['add', 'modify', 'sort']
74
75
76def autocomplete_action(partial_str: str) -> None:
77  result_list = list(
78      filter(lambda action: action.startswith(partial_str), COMMAND_ACTIONS))
79  print('\n'.join(result_list))
80  exit(0)
81
82
83def main(argv: Sequence[str]) -> None:
84  is_auto_complete = len(argv) >= 2 and argv[0] == '--autocomplete'
85  # argparse can't help autocomplete subcommand. We implement this without
86  # argparse here.
87  if is_auto_complete and argv[1] == '1':
88    action = argv[2] if len(argv) >= 3 else ''
89    autocomplete_action(action)
90
91  # If it's for autocompletion, then all arguments are optional.
92  parser_nargs = '?' if is_auto_complete else 1
93
94  main_parser = argparse.ArgumentParser(
95      description='A command line tool modifying the EXPECTED_UPSTREAM file.')
96  # --autocomplete <int> is an 'int' argument because the value represents
97  # the raw index of the argument to be autocompleted received in the Shell,
98  # and this number is not always the same as the number of arguments
99  # received here, i.e. len(argv), for examples of empty value in the
100  # argument or autocompleting the middle argument, not last argument.
101  main_parser.add_argument(
102      '--autocomplete', type=int, help='flag when tabbing in command line')
103  subparsers = main_parser.add_subparsers(
104      dest='command', help='sub-command help')
105
106  add_parser = subparsers.add_parser(
107      'add', help='Add a new entry into the EXPECTED_UPSTREAM '
108      'file')
109  add_parser.add_argument(
110      'tag_or_commit',
111      nargs=parser_nargs,
112      help='A git tag or commit in the upstream-openjdkXXX branch')
113  add_parser.add_argument(
114      'class_or_source_file',
115      nargs=parser_nargs,
116      help='Fully qualified class name or upstream source path')
117  add_parser.add_argument(
118      'ojluni_path', nargs='?', help='Destination path in ojluni/')
119
120  modify_parser = subparsers.add_parser(
121      'modify', help='Modify an entry in the EXPECTED_UPSTREAM file')
122  modify_parser.add_argument(
123      'class_or_ojluni_path', nargs=parser_nargs, help='File path in ojluni/')
124  modify_parser.add_argument(
125      'tag_or_commit',
126      nargs=parser_nargs,
127      help='A git tag or commit in the upstream-openjdkXXX branch')
128  modify_parser.add_argument(
129      'source_file', nargs='?', help='A upstream source path')
130
131  subparsers.add_parser(
132      'sort', help='Sort the entries in the EXPECTED_UPSTREAM file')
133
134  args = main_parser.parse_args(argv)
135
136  expected_upstream_file = ExpectedUpstreamFile()
137  expected_entries = expected_upstream_file.read_all_entries()
138
139  if is_auto_complete:
140    no_args = args.autocomplete
141
142    autocomp_result = []
143    if args.command == 'modify':
144      if no_args == 2:
145        input_class_or_ojluni_path = args.class_or_ojluni_path
146        if input_class_or_ojluni_path is None:
147          input_class_or_ojluni_path = ''
148
149        existing_dst_paths = list(
150            map(lambda entry: entry.dst_path, expected_entries))
151        ojluni_finder: OjluniFinder = OjluniFinder(existing_dst_paths)
152        # Case 1: Treat the input as file path
153        autocomp_result += ojluni_finder.match_path_prefix(
154            input_class_or_ojluni_path)
155
156        # Case 2: Treat the input as java package / class name
157        autocomp_result += ojluni_finder.match_classname_prefix(
158            input_class_or_ojluni_path)
159      elif no_args == 3:
160        autocomp_result += autocomplete_tag_or_commit(args.tag_or_commit)
161    elif args.command == 'add':
162      if no_args == 2:
163        autocomp_result += autocomplete_tag_or_commit(args.tag_or_commit)
164      elif no_args == 3:
165        commit = get_commit_or_exit(args.tag_or_commit)
166        class_or_src_path = args.class_or_source_file
167        if class_or_src_path is None:
168          class_or_src_path = ''
169
170        openjdk_finder: OpenjdkFinder = OpenjdkFinder(commit)
171
172        matches = openjdk_finder.match_path_prefix(
173            class_or_src_path)
174
175        matches += openjdk_finder.match_classname_prefix(
176            class_or_src_path)
177
178        existing_dst_paths = set(map(lambda e: e.dst_path, expected_entries))
179
180        # Translate the class names or source paths to dst paths and exclude
181        # such matches from the auto-completion result
182        def source_not_exists(src_path_or_class: str) -> bool:
183          nonlocal existing_dst_paths, openjdk_finder
184          t_src_path = openjdk_finder.find_src_path_from_classname(
185              src_path_or_class)
186          if t_src_path is None:
187            # t_src_path is a java package. It must not in existing_dst_paths.
188            return True
189          t_dst_path = OpenjdkFinder.translate_src_path_to_ojluni_path(
190              t_src_path)
191          return t_dst_path not in existing_dst_paths
192
193        autocomp_result += list(filter(source_not_exists, matches))
194
195    print('\n'.join(autocomp_result))
196    exit(0)
197
198  if args.command == 'modify':
199    dst_class_or_file = args.class_or_ojluni_path[0]
200    dst_path = OjluniFinder.translate_from_class_name_to_ojluni_path(
201        dst_class_or_file)
202    matches = list(filter(lambda e: dst_path == e.dst_path, expected_entries))
203    if not matches:
204      error_and_exit(f'{dst_path} is not found in the EXPECTED_UPSTREAM.')
205    entry: ExpectedUpstreamEntry = matches[0]
206    str_tag_or_commit = args.tag_or_commit[0]
207    is_src_given = args.source_file is not None
208    src_path = args.source_file if is_src_given else entry.src_path
209    commit = get_commit_or_exit(str_tag_or_commit)
210    openjdk_finder: OpenjdkFinder = OpenjdkFinder(commit)
211    if openjdk_finder.has_file(src_path):
212      pass
213    elif not is_src_given:
214      guessed_src_path = openjdk_finder.find_src_path_from_ojluni_path(dst_path)
215      if guessed_src_path is None:
216        error_and_exit('[source_file] argument is required.')
217      src_path = guessed_src_path
218    else:
219      error_and_exit(f'{src_path} is not found in the {str_tag_or_commit}')
220    entry.git_ref = str_tag_or_commit
221    entry.src_path = src_path
222    expected_upstream_file.write_all_entries(expected_entries)
223    print(f'Modified the entry {entry}')
224  elif args.command == 'add':
225    class_or_src_path = args.class_or_source_file[0]
226    str_tag_or_commit = args.tag_or_commit[0]
227    commit = get_commit_or_exit(str_tag_or_commit)
228    openjdk_finder = OpenjdkFinder(commit)
229    src_path = openjdk_finder.find_src_path_from_classname(class_or_src_path)
230    if src_path is None:
231      search_paths = openjdk_finder.get_search_paths()
232      error_and_exit(f'{class_or_src_path} is not found in {commit}. '
233                     f'The search paths are:\n{search_paths}')
234    ojluni_path = args.ojluni_path
235    # Guess the source path if it's not given in the argument
236    if ojluni_path is None:
237      ojluni_path = OpenjdkFinder.translate_src_path_to_ojluni_path(src_path)
238    if ojluni_path is None:
239      error_and_exit('The ojluni destination path is not given.')
240
241    matches = list(
242        filter(lambda e: ojluni_path == e.dst_path, expected_entries))
243    if matches:
244      error_and_exit(f"Can't add the file {ojluni_path} because "
245                     f'{class_or_src_path} exists in the EXPECTED_UPSTREAM')
246
247    new_entry = ExpectedUpstreamEntry(ojluni_path, str_tag_or_commit, src_path)
248    expected_upstream_file.write_new_entry(new_entry, expected_entries)
249  elif args.command == 'sort':
250    expected_upstream_file.sort_and_write_all_entries(expected_entries)
251  else:
252    error_and_exit(f'Unknown subcommand: {args.command}')
253
254
255if __name__ == '__main__':
256  main(sys.argv[1:])
257