AIDEGen: Refactor is_target_module method from project_info.py to module_info.py
The method is_target_module is better to belong to module_info class than to
project_info class.
Bug: 135572339
Test: 1. aidegen tradefed -n
rename core.iml to _core.iml, dependencies.iml to
_dependencies.iml
2. m aidegen;aidegen-dev tradefed -n
contents of core.iml should be the same as that of _core.iml
and that of dependencies.iml is the same as _dependencies.iml
as well.
If you have installed kdiff3 you can try
kdiff3 tools/tradefederation/core/_core.iml tools/tradefederation/core/core.iml
The two files' contants are the same.
kdiff3 tools/tradefederation/core/_dependencies.iml tools/tradefederation/core/dependencies.iml
The two files' contants are the same.
Change-Id: Iedbb86550ec8732db26e9e85995964bcc732478d
diff --git a/aidegen/lib/common_util.py b/aidegen/lib/common_util.py
index 9173685..804ae3b 100644
--- a/aidegen/lib/common_util.py
+++ b/aidegen/lib/common_util.py
@@ -41,14 +41,6 @@
OUTSIDE_ROOT_ERROR = '{} is outside android root.'
PATH_NOT_EXISTS_ERROR = 'The path {} doesn\'t exist.'
NO_MODULE_DEFINED_ERROR = 'No modules defined at {}.'
-# Java related classes.
-JAVA_TARGET_CLASSES = ['APPS', 'JAVA_LIBRARIES', 'ROBOLECTRIC']
-# C, C++ related classes.
-NATIVE_TARGET_CLASSES = [
- 'HEADER_LIBRARIES', 'NATIVE_TESTS', 'STATIC_LIBRARIES', 'SHARED_LIBRARIES'
-]
-TARGET_CLASSES = JAVA_TARGET_CLASSES
-TARGET_CLASSES.extend(NATIVE_TARGET_CLASSES)
_REBUILD_MODULE_INFO = '%s We should rebuild module-info.json file for it.'
_ENVSETUP_NOT_RUN = ('Please run "source build/envsetup.sh" and "lunch" before '
'running aidegen.')
diff --git a/aidegen/lib/module_info.py b/aidegen/lib/module_info.py
index 244ddf4..7d86a05 100644
--- a/aidegen/lib/module_info.py
+++ b/aidegen/lib/module_info.py
@@ -23,8 +23,18 @@
from aidegen import constant
from aidegen.lib import common_util
from aidegen.lib import module_info_util
+from atest import constants
from atest import module_info
+# Java related classes.
+JAVA_TARGET_CLASSES = ['APPS', 'JAVA_LIBRARIES', 'ROBOLECTRIC']
+# C, C++ related classes.
+NATIVE_TARGET_CLASSES = [
+ 'HEADER_LIBRARIES', 'NATIVE_TESTS', 'STATIC_LIBRARIES', 'SHARED_LIBRARIES'
+]
+TARGET_CLASSES = JAVA_TARGET_CLASSES
+TARGET_CLASSES.extend(NATIVE_TARGET_CLASSES)
+
class AidegenModuleInfo(module_info.ModuleInfo):
"""Class that offers fast/easy lookup for Module related details.
@@ -114,3 +124,21 @@
module_file_rel_path = os.path.relpath(
merged_file_path, common_util.get_android_root_dir())
return module_file_rel_path, merged_file_path
+
+ @staticmethod
+ def is_target_module(mod_info):
+ """Determine if the module is a target module.
+
+ Determine if a module's class is in TARGET_CLASSES.
+
+ Args:
+ mod_info: A module's module-info dictionary to be checked.
+
+ Returns:
+ A boolean, true if it is a target module, otherwise false.
+ """
+ if mod_info:
+ return any(
+ x in mod_info.get(constants.MODULE_CLASS, [])
+ for x in TARGET_CLASSES)
+ return False
diff --git a/aidegen/lib/module_info_unittest.py b/aidegen/lib/module_info_unittest.py
new file mode 100644
index 0000000..d874191
--- /dev/null
+++ b/aidegen/lib/module_info_unittest.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+#
+# Copyright 2019, 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.
+
+"""Unittests for module_info."""
+
+import unittest
+
+from aidegen.lib.module_info import AidegenModuleInfo
+
+
+class AidegenModuleInfoUnittests(unittest.TestCase):
+ """Unit tests for module_info.py"""
+
+ def test_is_target_module(self):
+ """Test is_target_module with different conditions."""
+ self.assertFalse(AidegenModuleInfo.is_target_module({}))
+ self.assertFalse(AidegenModuleInfo.is_target_module({'path': ''}))
+ self.assertFalse(AidegenModuleInfo.is_target_module({'class': ''}))
+ self.assertTrue(AidegenModuleInfo.is_target_module({'class': ['APPS']}))
+ self.assertTrue(
+ AidegenModuleInfo.is_target_module({'class': ['JAVA_LIBRARIES']}))
+ self.assertTrue(
+ AidegenModuleInfo.is_target_module({'class': ['ROBOLECTRIC']}))
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/aidegen/lib/project_info.py b/aidegen/lib/project_info.py
index 4dd6591..3f50d2a 100644
--- a/aidegen/lib/project_info.py
+++ b/aidegen/lib/project_info.py
@@ -23,6 +23,7 @@
from aidegen import constant
from aidegen.lib import common_util
+from aidegen.lib import module_info
_ANDROID_MK = 'Android.mk'
_ANDROID_BP = 'Android.bp'
@@ -165,39 +166,23 @@
project path.
"""
logging.info('Find modules whose class is in %s under %s.',
- common_util.TARGET_CLASSES, self.project_relative_path)
+ module_info.TARGET_CLASSES, self.project_relative_path)
for name, data in self.modules_info.name_to_module_info.items():
if common_util.is_project_path_relative_module(
data, self.project_relative_path):
- if self._is_a_target_module(data):
+ if self.modules_info.is_target_module(data):
self.project_module_names.add(name)
if self.modules_info.is_robolectric_test(name):
self.project_module_names.add(_ROBOLECTRIC_MODULE)
else:
logging.debug(_NOT_TARGET, name, data['class'],
- common_util.TARGET_CLASSES)
+ module_info.TARGET_CLASSES)
def _filter_out_modules(self):
"""Filter out unnecessary modules."""
for module in _EXCLUDE_MODULES:
self.dep_modules.pop(module, None)
- @staticmethod
- def _is_a_target_module(data):
- """Determine if the module is a target module.
-
- A module's class is in {'APPS', 'JAVA_LIBRARIES', 'ROBOLECTRIC'}
-
- Args:
- data: the module-info dictionary of the checked module.
-
- Returns:
- A boolean, true if is a target module, otherwise false.
- """
- if not 'class' in data:
- return False
- return any(x in data['class'] for x in common_util.TARGET_CLASSES)
-
def get_dep_modules(self, module_names=None, depth=0):
"""Recursively find dependent modules of the project.
diff --git a/aidegen/lib/project_info_unittest.py b/aidegen/lib/project_info_unittest.py
index 96ea627..dce10eb 100644
--- a/aidegen/lib/project_info_unittest.py
+++ b/aidegen/lib/project_info_unittest.py
@@ -66,27 +66,6 @@
proj_info = project_info.ProjectInfo(self.args.module_name)
self.assertEqual(proj_info.dep_modules, _EXPECT_DEPENDENT_MODULES)
- def test_is_a_target_module(self):
- """Test _is_a_target_module with different conditions."""
- self.assertEqual(project_info.ProjectInfo._is_a_target_module({}),
- False)
- self.assertEqual(project_info.ProjectInfo._is_a_target_module(
- {'path': ''}), False)
- self.assertEqual(project_info.ProjectInfo._is_a_target_module(
- {'class': ''}), False)
- self.assertEqual(
- project_info.ProjectInfo._is_a_target_module({
- 'class': ['APPS']
- }), True)
- self.assertEqual(
- project_info.ProjectInfo._is_a_target_module({
- 'class': ['JAVA_LIBRARIES']
- }), True)
- self.assertEqual(
- project_info.ProjectInfo._is_a_target_module({
- 'class': ['ROBOLECTRIC']
- }), True)
-
@mock.patch.object(common_util, 'get_android_root_dir')
def test_get_target_name(self, mock_get_root):
"""Test _get_target_name with different conditions."""