From 6442eafcd69c91808ef6e11daf09d5c53d541f8d Mon Sep 17 00:00:00 2001 From: Noriko Hosoi Date: Tue, 17 Nov 2020 08:30:34 -0800 Subject: [PATCH] RHELPLAN-59755 - Collection - script - not include symlinks in the collection converted format lsr_role2collection.py - In copying the roles tests dir to the collections tests, by setting symlinks=False, it follows the links and copies the real files and directories. Plus, when calling copy_tree_with_replace, set "linux-system-roles.ROLENAME" to "ignoreme", which avoids copying the dir. Removing unnecessary test case from test_lsr_role2collection.py --- lsr_role2collection.py | 41 ++++++++------------ tests/unit/test_lsr_role2collection.py | 52 -------------------------- 2 files changed, 15 insertions(+), 78 deletions(-) diff --git a/lsr_role2collection.py b/lsr_role2collection.py index b233b9f0..862c1cef 100644 --- a/lsr_role2collection.py +++ b/lsr_role2collection.py @@ -591,7 +591,7 @@ def copy_tree_with_replace( lsr_copytree( src, dest, - ignore=ignore_patterns(ignoreme), + ignore=ignore_patterns(*ignoreme), symlinks=symlinks, dirs_exist_ok=True, ) @@ -603,23 +603,11 @@ def copy_tree_with_replace( lsrxfrm.run() -def cleanup_symlinks(path, role): +def cleanup_roles_dir(path): """ Clean up symlinks in tests/roles - - Remove symlinks. - - If linux-system-roles.rolename is an empty dir, rmdir it. """ if path.exists(): - nodes = sorted(list(path.rglob("*")), reverse=True) - for node in nodes: - if node.is_symlink() and r"linux-system-roles." + role == node.name: - node.unlink() - elif ( - node.is_dir() - and r"linux-system-roles." + role == node.name - and not any(node.iterdir()) - ): - node.rmdir() roles_dir = path / "roles" if roles_dir.exists() and not any(roles_dir.iterdir()): roles_dir.rmdir() @@ -1022,12 +1010,12 @@ def role2collection(): TESTS, transformer_args, isrole=False, - ignoreme="artifacts", - symlinks=True, + ignoreme=["artifacts", "linux-system-roles.*", "__pycache__"], + symlinks=False, ) - # remove symlinks in the tests/role, then updating the rolename to the collection format - cleanup_symlinks(tests_dir / role, role) + # remove "roles" directory if empty. + cleanup_roles_dir(tests_dir / role) # ============================================================================== @@ -1107,6 +1095,7 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False) comment = "## Supported Linux System Roles" update_readme(src_path, filename, rolename, comment, issubrole) + ignoreme = ["linux-system-roles.*"] dest = docs_dir / role for doc in DOCS: src = src_path / doc @@ -1116,7 +1105,7 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False) src, dest, symlinks=False, - ignore=ignore_patterns("roles"), + ignore=ignore_patterns(*ignoreme), dirs_exist_ok=True, ) if doc == "examples": @@ -1131,9 +1120,8 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False) elif src.is_file(): process_readme(src_path, doc, role) - # Remove symlinks in the docs/role (e.g., in the examples). - # Update the rolename to the collection format as done in the tests. - cleanup_symlinks(dest, role) + # remove "roles" directory if empty. + cleanup_roles_dir(dest) # ============================================================================== @@ -1226,10 +1214,11 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False) TESTS, transformer_args, isrole=False, - ignoreme="artifacts", + ignoreme=["artifacts", "linux-system-roles.*", "__pycache__"], + symlinks=False, ) - # remove symlinks in the tests/role, then updating the rolename to the collection format - cleanup_symlinks(tests_dir / dr, dr) + # remove "roles" directory if empty. + cleanup_roles_dir(tests_dir / dr) # copy README.md to dest_path/roles/sr.name readme = sr / "README.md" if readme.is_file(): @@ -1252,7 +1241,7 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False) else: dest = dest_path / extra.name logging.info(f"Copying extra {extra} to {dest}") - copytree(extra, dest) + lsr_copytree(extra, dest) # Other extra files. else: if extra.name.endswith(".yml") and "playbook" in extra.name: diff --git a/tests/unit/test_lsr_role2collection.py b/tests/unit/test_lsr_role2collection.py index fdbff5d6..b2135e0d 100644 --- a/tests/unit/test_lsr_role2collection.py +++ b/tests/unit/test_lsr_role2collection.py @@ -12,7 +12,6 @@ from lsr_role2collection import ( file_replace, copy_tree_with_replace, - cleanup_symlinks, import_replace, from_replace, gather_module_utils_parts, @@ -277,57 +276,6 @@ def test_copy_tree_with_replace(self): ) shutil.rmtree(coll_path) - def test_cleanup_symlinks(self): - """test cleanup_symlinks""" - owner = "linux-system-roles" - params = [ - { - "key": "roles", - "subkey": "-", - "value": owner, - "delim": ".", - "subvalue": rolename, - }, - ] - tmpdir = tempfile.TemporaryDirectory() - coll_path = ( - Path(tmpdir.name) / "ansible_collections" / namespace / collection_name - ) - role_path = coll_path / "roles" / rolename - self.create_test_tree( - role_path / "tasks", test_yaml_str, params, ".yml", is_vertical=False - ) - test_path = coll_path / "tests" / rolename - self.create_test_tree( - test_path, test_yaml_str, params, ".yml", is_vertical=False - ) - link = owner + "." + rolename - test_role_path = test_path / "roles" - # case 1. tests/roles/owner.systemrole -> roles/systemrole - self.create_test_link(test_role_path, link, role_path, True) - cleanup_symlinks(test_path, rolename) - self.check_test_link(test_role_path, False) - # case 2. tests/roles/owner.systemrole -> roles/systemrole - # tests/roles/some_file - self.create_test_link(test_role_path, link, role_path, True) - self.create_test_tree( - test_role_path, test_yaml_str, params, ".yml", is_vertical=False - ) - cleanup_symlinks(test_path, rolename) - self.check_test_link(test_role_path, True) - self.check_test_link(test_role_path / link, False) - shutil.rmtree(test_role_path) - # case 3. tests/roles/owner.systemrole -> roles/systemrole - # tests/roles/some_symlinks - self.create_test_link(test_role_path, link, role_path, True) - extralink = "extralink" - self.create_test_link(test_role_path, extralink, role_path, True) - cleanup_symlinks(test_path, rolename) - self.check_test_link(test_role_path, True) - self.check_test_link(test_role_path / link, False) - self.check_test_link(test_role_path / extralink, True) - shutil.rmtree(test_role_path) - def test_import_replace(self): module_names = ["util0", "util1"] src_module_utils = []