Skip to content

Commit

Permalink
Resurrected RHELPLAN-53633 - Collections - script - parameterize the …
Browse files Browse the repository at this point in the history
…prefix of nested subroles name

Adding an option --subrole-prefix SUBROLE_PREFIX as well as an
env var COLLECTION_SUBROLE_PREFIX.
  --subrole-prefix SUBROLE_PREFIX
        If sub-role name does not start with the specified value,
        change the name to start with the value; default to an empty string.
  • Loading branch information
nhosoi committed Sep 24, 2020
1 parent 327801f commit ad968f5
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lsr_role2collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# --dest-path COLLECTION_DEST_PATH
# --role ROLE_NAME | --tox
# (if --tox is set, COLLECTION_SRC_PATH/template is expected)
# [--subrole-prefix STR]
# [--replace-dot STR]
# [-h]
# Or
Expand Down Expand Up @@ -141,9 +142,16 @@ def task_cb(self, a_task, ru_task, module_name, module_args, delegate_to):
ru_task[module_name]["name"] = prefix + self.rolename
elif rolename.startswith("{{ role_path }}"):
match = re.match(r"{{ role_path }}/roles/([\w\d\.]+)", rolename)
ru_task[module_name]["name"] = prefix + match.group(1).replace(
".", replace_dot
)
if match.group(1).startswith(subrole_prefix):
ru_task[module_name]["name"] = prefix + match.group(1).replace(
".", replace_dot
)
else:
ru_task[module_name]["name"] = (
prefix
+ subrole_prefix
+ match.group(1).replace(".", replace_dot)
)
elif module_name in role_modules:
logging.debug(f"\ttask role module {module_name}")
# assumes ru_task is an orderreddict
Expand Down Expand Up @@ -289,7 +297,7 @@ def file_replace(path, find, replace, file_patterns):
"--src-path",
type=Path,
default=os.environ.get("COLLECTION_SRC_PATH", HOME + "/linux-system-roles"),
help="Path to linux-system-role",
help="Path to linux-system-roles",
)
parser.add_argument(
"--role",
Expand All @@ -315,6 +323,15 @@ def file_replace(path, find, replace, file_patterns):
"default to '_'"
),
)
parser.add_argument(
"--subrole-prefix",
type=str,
default=os.environ.get("COLLECTION_SUBROLE_PREFIX", ""),
help=(
"If sub-role name does not start with the specified value, "
"change the name to start with the value; default to an empty string"
),
)
args, unknown = parser.parse_known_args()

role = args.role
Expand All @@ -328,6 +345,7 @@ def file_replace(path, find, replace, file_patterns):
prefix = namespace + "." + collection + "."
top_dest_path = args.dest_path.resolve()
replace_dot = args.replace_dot
subrole_prefix = args.subrole_prefix

dest_path = Path.joinpath(
top_dest_path, "ansible_collections/" + namespace + "/" + collection
Expand Down Expand Up @@ -576,6 +594,7 @@ def process_readme(src_path, filename, rolename, original=None, issubrole=False)


dest = dest_path / "docs" / role
no_readme_link = []
for doc in DOCS:
src = src_path / doc
if src.is_dir():
Expand Down Expand Up @@ -889,7 +908,11 @@ def add_rolename(filename, rolename):
if extra.name == "roles":
for sr in extra.iterdir():
# If a role name contains '.', replace it with replace_dot
dr = sr.name.replace(".", replace_dot)
# convert nested subroles to prefix name with subrole_prefix.
if sr.name.startswith(subrole_prefix):
dr = sr.name.replace(".", replace_dot)
else:
dr = subrole_prefix + sr.name.replace(".", replace_dot)
copy_tree_with_replace(sr, dest_path, prefix, dr, ROLE_DIRS)
# copy tests dir to dest_path/"tests"
copy_tree_with_replace(
Expand Down

0 comments on commit ad968f5

Please sign in to comment.