forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportToFramework.py
More file actions
executable file
·352 lines (313 loc) · 16.5 KB
/
Copy pathexportToFramework.py
File metadata and controls
executable file
·352 lines (313 loc) · 16.5 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Exports AppSearch Androidx code to Framework
#
# NOTE: This will remove and replace all files in the
# packages/modules/AppSearch path.
#
# Example usage (from root dir of androidx workspace):
# $ ./frameworks/support/appsearch/exportToFramework.py "$HOME/android/master" "<jetpack git sha>"
# Special directives supported by this script:
#
# Causes the file where it appears to not be copied at all:
# @exportToFramework:skipFile()
#
# Causes the text appearing between startStrip() and endStrip() to be removed during export:
# // @exportToFramework:startStrip() ... // @exportToFramework:endStrip()
#
# Replaced with @hide:
# <!--@exportToFramework:hide-->
#
# Replaced with @CurrentTimeMillisLong:
# /*@exportToFramework:CurrentTimeMillisLong*/
#
# Removes the text appearing between ifJetpack() and else(), and causes the text appearing between
# else() and --> to become uncommented, to support framework-only Javadocs:
# <!--@exportToFramework:ifJetpack()-->
# Jetpack-only Javadoc
# <!--@exportToFramework:else()
# Framework-only Javadoc
# -->
# Note: Using the above pattern, you can hide a method in Jetpack but unhide it in Framework like
# this:
# <!--@exportToFramework:ifJetpack()-->@hide<!--@exportToFramework:else()-->
import os
import re
import subprocess
import sys
# Jetpack paths relative to frameworks/support/appsearch
JETPACK_API_ROOT = 'appsearch/src/main/java/androidx/appsearch'
JETPACK_API_TEST_ROOT = 'appsearch/src/androidTest/java/androidx/appsearch'
JETPACK_IMPL_ROOT = 'appsearch-local-storage/src/main/java/androidx/appsearch'
JETPACK_IMPL_TEST_ROOT = 'appsearch-local-storage/src/androidTest/java/androidx/appsearch'
JETPACK_TEST_UTIL_ROOT = 'appsearch-test-util/src/main/java/androidx/appsearch'
JETPACK_TEST_UTIL_TEST_ROOT = 'appsearch-test-util/src/androidTest/java/androidx/appsearch'
# Framework paths relative to packages/modules/AppSearch
FRAMEWORK_API_ROOT = 'framework/java/external/android/app/appsearch'
FRAMEWORK_API_TEST_ROOT = 'testing/coretests/src/android/app/appsearch/external'
FRAMEWORK_IMPL_ROOT = 'service/java/com/android/server/appsearch/external'
FRAMEWORK_IMPL_TEST_ROOT = 'testing/servicestests/src/com/android/server/appsearch/external'
FRAMEWORK_TEST_UTIL_ROOT = 'testing/testutils/src/android/app/appsearch/testutil/external'
FRAMEWORK_TEST_UTIL_TEST_ROOT = 'testing/servicestests/src/android/app/appsearch/testutil/external'
FRAMEWORK_CTS_TEST_ROOT = '../../../cts/tests/appsearch/src/com/android/cts/appsearch/external'
GOOGLE_JAVA_FORMAT = (
'../../../prebuilts/tools/common/google-java-format/google-java-format')
# Miscellaneous constants
CHANGEID_FILE_NAME = 'synced_jetpack_changeid.txt'
class ExportToFramework:
def __init__(self, jetpack_appsearch_root, framework_appsearch_root):
self._jetpack_appsearch_root = jetpack_appsearch_root
self._framework_appsearch_root = framework_appsearch_root
self._written_files = []
def _PruneDir(self, dir_to_prune):
for walk_path, walk_folders, walk_files in os.walk(dir_to_prune):
for walk_filename in walk_files:
abs_path = os.path.join(walk_path, walk_filename)
print('Prune: remove "%s"' % abs_path)
os.remove(abs_path)
def _TransformAndCopyFile(
self, source_path, dest_path, transform_func=None, ignore_skips=False):
with open(source_path, 'r') as fh:
contents = fh.read()
if not ignore_skips and '@exportToFramework:skipFile()' in contents:
print('Skipping: "%s" -> "%s"' % (source_path, dest_path), file=sys.stderr)
return
print('Copy: "%s" -> "%s"' % (source_path, dest_path), file=sys.stderr)
if transform_func:
contents = transform_func(contents)
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
with open(dest_path, 'w') as fh:
fh.write(contents)
# Save file for future formatting
self._written_files.append(dest_path)
def _TransformCommonCode(self, contents):
# Apply stripping
contents = re.sub(
r'\/\/ @exportToFramework:startStrip\(\).*?\/\/ @exportToFramework:endStrip\(\)',
'',
contents,
flags=re.DOTALL)
# Apply if/elses in javadocs
contents = re.sub(
r'<!--@exportToFramework:ifJetpack\(\)-->.*?<!--@exportToFramework:else\(\)(.*?)-->',
r'\1',
contents,
flags=re.DOTALL)
# Add additional imports if required
imports_to_add = []
if '@exportToFramework:CurrentTimeMillisLong' in contents:
imports_to_add.append('android.annotation.CurrentTimeMillisLong')
if '@exportToFramework:UnsupportedAppUsage' in contents:
imports_to_add.append('android.compat.annotation.UnsupportedAppUsage')
for import_to_add in imports_to_add:
contents = re.sub(
r'^(\s*package [^;]+;\s*)$', r'\1\nimport %s;\n' % import_to_add, contents,
flags=re.MULTILINE)
# Apply in-place replacements
return (contents
.replace('androidx.appsearch.app', 'android.app.appsearch')
.replace(
'androidx.appsearch.localstorage.',
'com.android.server.appsearch.external.localstorage.')
.replace('androidx.appsearch', 'android.app.appsearch')
.replace(
'androidx.annotation.GuardedBy',
'com.android.internal.annotations.GuardedBy')
.replace(
'androidx.annotation.VisibleForTesting',
'com.android.internal.annotations.VisibleForTesting')
.replace('androidx.annotation.', 'android.annotation.')
.replace('androidx.collection.ArrayMap', 'android.util.ArrayMap')
.replace('androidx.collection.ArraySet', 'android.util.ArraySet')
.replace(
'androidx.core.util.ObjectsCompat',
'java.util.Objects')
# Preconditions.checkNotNull is replaced with Objects.requireNonNull. We add both
# imports and let google-java-format sort out which one is unused.
.replace(
'import androidx.core.util.Preconditions;',
'import java.util.Objects; import com.android.internal.util.Preconditions;')
.replace('import androidx.annotation.RestrictTo;', '')
.replace('@RestrictTo(RestrictTo.Scope.LIBRARY)', '')
.replace('@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)', '')
.replace('Preconditions.checkNotNull(', 'Objects.requireNonNull(')
.replace('ObjectsCompat.', 'Objects.')
.replace('/*@exportToFramework:CurrentTimeMillisLong*/', '@CurrentTimeMillisLong')
.replace('/*@exportToFramework:UnsupportedAppUsage*/', '@UnsupportedAppUsage')
.replace('<!--@exportToFramework:hide-->', '@hide')
.replace('// @exportToFramework:skipFile()', '')
)
def _TransformTestCode(self, contents):
contents = (contents
.replace('androidx.appsearch.testutil.', 'android.app.appsearch.testutil.')
.replace(
'package androidx.appsearch.testutil;',
'package android.app.appsearch.testutil;')
.replace(
'androidx.appsearch.localstorage.LocalStorage',
'android.app.appsearch.AppSearchManager')
.replace('LocalStorage.', 'AppSearchManager.')
)
for shim in ['AppSearchSession', 'GlobalSearchSession', 'SearchResults']:
contents = re.sub(r"([^a-zA-Z])(%s)([^a-zA-Z0-9])" % shim, r'\1\2Shim\3', contents)
return self._TransformCommonCode(contents)
def _TransformAndCopyFolder(self, source_dir, dest_dir, transform_func=None):
for currentpath, folders, files in os.walk(source_dir):
dir_rel_to_root = os.path.relpath(currentpath, source_dir)
for filename in files:
source_abs_path = os.path.join(currentpath, filename)
dest_path = os.path.join(dest_dir, dir_rel_to_root, filename)
self._TransformAndCopyFile(source_abs_path, dest_path, transform_func)
def _ExportApiCode(self):
# Prod source
api_source_dir = os.path.join(self._jetpack_appsearch_root, JETPACK_API_ROOT)
api_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_API_ROOT)
# Unit tests
api_test_source_dir = os.path.join(self._jetpack_appsearch_root, JETPACK_API_TEST_ROOT)
api_test_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_API_TEST_ROOT)
# CTS tests
cts_test_source_dir = os.path.join(api_test_source_dir, 'cts')
cts_test_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_CTS_TEST_ROOT)
# Test utils
test_util_source_dir = os.path.join(self._jetpack_appsearch_root, JETPACK_TEST_UTIL_ROOT)
test_util_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_TEST_UTIL_ROOT)
# Prune existing files
self._PruneDir(api_dest_dir)
self._PruneDir(api_test_dest_dir)
self._PruneDir(cts_test_dest_dir)
self._PruneDir(test_util_dest_dir)
# Copy api classes. We can't use _TransformAndCopyFolder here because we
# need to specially handle the 'app' package.
print('~~~ Copying API classes ~~~')
def _TransformApiCode(contents):
contents = contents.replace(
'package androidx.appsearch.app;',
'package android.app.appsearch;')
return self._TransformCommonCode(contents)
for currentpath, folders, files in os.walk(api_source_dir):
dir_rel_to_root = os.path.relpath(currentpath, api_source_dir)
for filename in files:
# Figure out what folder to place them into
source_abs_path = os.path.join(currentpath, filename)
if dir_rel_to_root == 'app':
# Files in the 'app' folder live in the root of the platform tree
dest_path = os.path.join(api_dest_dir, filename)
else:
dest_path = os.path.join(api_dest_dir, dir_rel_to_root, filename)
self._TransformAndCopyFile(source_abs_path, dest_path, _TransformApiCode)
# Copy api unit tests. We can't use _TransformAndCopyFolder here because we need to skip the
# 'util' and 'cts' subfolders.
print('~~~ Copying API unit tests ~~~')
for currentpath, folders, files in os.walk(api_test_source_dir):
if (currentpath.startswith(cts_test_source_dir) or
currentpath.startswith(test_util_source_dir)):
continue
dir_rel_to_root = os.path.relpath(currentpath, api_test_source_dir)
for filename in files:
source_abs_path = os.path.join(currentpath, filename)
dest_path = os.path.join(api_test_dest_dir, dir_rel_to_root, filename)
self._TransformAndCopyFile(source_abs_path, dest_path, self._TransformTestCode)
# Copy CTS tests
print('~~~ Copying CTS tests ~~~')
self._TransformAndCopyFolder(
cts_test_source_dir, cts_test_dest_dir, transform_func=self._TransformTestCode)
# Copy test utils
print('~~~ Copying test utils ~~~')
self._TransformAndCopyFolder(
test_util_source_dir, test_util_dest_dir, transform_func=self._TransformTestCode)
for iface_file in (
'AppSearchSession.java', 'GlobalSearchSession.java', 'SearchResults.java'):
dest_file_name = os.path.splitext(iface_file)[0] + 'Shim.java'
self._TransformAndCopyFile(
os.path.join(api_source_dir, 'app/' + iface_file),
os.path.join(test_util_dest_dir, dest_file_name),
transform_func=self._TransformTestCode,
ignore_skips=True)
def _ExportImplCode(self):
impl_source_dir = os.path.join(self._jetpack_appsearch_root, JETPACK_IMPL_ROOT)
impl_test_source_dir = os.path.join(self._jetpack_appsearch_root, JETPACK_IMPL_TEST_ROOT)
impl_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_IMPL_ROOT)
impl_test_dest_dir = os.path.join(self._framework_appsearch_root, FRAMEWORK_IMPL_TEST_ROOT)
test_util_test_source_dir = os.path.join(
self._jetpack_appsearch_root, JETPACK_TEST_UTIL_TEST_ROOT)
test_util_test_dest_dir = os.path.join(
self._framework_appsearch_root, FRAMEWORK_TEST_UTIL_TEST_ROOT)
# Prune
self._PruneDir(impl_dest_dir)
self._PruneDir(impl_test_dest_dir)
self._PruneDir(test_util_test_dest_dir)
# Copy impl classes
def _TransformImplCode(contents):
contents = (contents
.replace('package androidx.appsearch',
'package com.android.server.appsearch.external')
.replace('com.google.android.icing.protobuf.', 'com.google.protobuf.')
)
return self._TransformCommonCode(contents)
self._TransformAndCopyFolder(
impl_source_dir, impl_dest_dir, transform_func=_TransformImplCode)
# Copy servicestests
def _TransformImplTestCode(contents):
contents = (contents
.replace('package androidx.appsearch',
'package com.android.server.appsearch.external')
.replace('com.google.android.icing.proto.',
'com.android.server.appsearch.icing.proto.')
.replace('com.google.android.icing.protobuf.',
'com.android.server.appsearch.protobuf.')
)
return self._TransformTestCode(contents)
self._TransformAndCopyFolder(
impl_test_source_dir, impl_test_dest_dir, transform_func=_TransformImplTestCode)
self._TransformAndCopyFolder(
test_util_test_source_dir,
test_util_test_dest_dir,
transform_func=self._TransformTestCode)
def _FormatWrittenFiles(self):
google_java_format_cmd = [GOOGLE_JAVA_FORMAT, '--aosp', '-i'] + self._written_files
print('$ ' + ' '.join(google_java_format_cmd))
subprocess.check_call(google_java_format_cmd, cwd=self._framework_appsearch_root)
def ExportCode(self):
self._ExportApiCode()
self._ExportImplCode()
self._FormatWrittenFiles()
def WriteChangeIdFile(self, changeid):
"""Copies the changeid of the most recent public CL into a file on the framework side.
This file is used for tracking, to determine what framework is synced to.
You must always provide a changeid of an exported, preferably even submitted CL. If you
abandon the CL pointed to by this changeid, the next person syncing framework will be unable
to find what CL it is synced to.
"""
file_path = os.path.join(self._framework_appsearch_root, CHANGEID_FILE_NAME)
with open(file_path, 'w') as fh:
print(changeid, file=fh)
print('Wrote "%s"' % file_path)
if __name__ == '__main__':
if len(sys.argv) != 3:
print('Usage: %s <path/to/framework/checkout> <git sha of head jetpack commit>' % (
sys.argv[0]),
file=sys.stderr)
sys.exit(1)
source_dir = os.path.normpath(os.path.dirname(sys.argv[0]))
dest_dir = os.path.normpath(sys.argv[1])
dest_dir = os.path.join(dest_dir, 'packages/modules/AppSearch')
if not os.path.isdir(dest_dir):
print('Destination path "%s" does not exist or is not a directory' % (
dest_dir),
file=sys.stderr)
sys.exit(1)
exporter = ExportToFramework(source_dir, dest_dir)
exporter.ExportCode()
exporter.WriteChangeIdFile(sys.argv[2])