Patch usage of old in apps 'yunohost tools diagnosis'

This commit is contained in:
Alexandre Aubin 2020-04-23 18:59:12 +02:00
parent f68ae4561f
commit cb0a87de25

View file

@ -2843,29 +2843,46 @@ def _patch_legacy_helpers(app_folder):
# sudo yunohost app initdb $db_user -p $db_pwd # sudo yunohost app initdb $db_user -p $db_pwd
# by # by
# ynh_mysql_setup_db --db_user=$db_user --db_name=$db_user --db_pwd=$db_pwd # ynh_mysql_setup_db --db_user=$db_user --db_name=$db_user --db_pwd=$db_pwd
"yunohost app initdb": ( "yunohost app initdb": {
r"(sudo )?yunohost app initdb \"?(\$\{?\w+\}?)\"?\s+-p\s\"?(\$\{?\w+\}?)\"?", "pattern": r"(sudo )?yunohost app initdb \"?(\$\{?\w+\}?)\"?\s+-p\s\"?(\$\{?\w+\}?)\"?",
r"ynh_mysql_setup_db --db_user=\2 --db_name=\2 --db_pwd=\3"), "replace": r"ynh_mysql_setup_db --db_user=\2 --db_name=\2 --db_pwd=\3",
"important": True
},
# Replace # Replace
# sudo yunohost app checkport whaterver # sudo yunohost app checkport whaterver
# by # by
# ynh_port_available whatever # ynh_port_available whatever
"yunohost app checkport": ( "yunohost app checkport": {
r"(sudo )?yunohost app checkport", "pattern": r"(sudo )?yunohost app checkport",
r"ynh_port_available"), "replace": r"ynh_port_available",
"important": True
},
# We can't migrate easily port-available # We can't migrate easily port-available
# .. but at the time of writing this code, only two non-working apps are using it. # .. but at the time of writing this code, only two non-working apps are using it.
"yunohost tools port-available": (None, None), "yunohost tools port-available": {"important":True},
# Replace # Replace
# yunohost app checkurl "${domain}${path_url}" -a "${app}" # yunohost app checkurl "${domain}${path_url}" -a "${app}"
# by # by
# ynh_webpath_register --app=${app} --domain=${domain} --path_url=${path_url} # ynh_webpath_register --app=${app} --domain=${domain} --path_url=${path_url}
"yunohost app checkurl": ( "yunohost app checkurl": {
r"(sudo )?yunohost app checkurl \"?(\$\{?\w+\}?)\/?(\$\{?\w+\}?)\"?\s+-a\s\"?(\$\{?\w+\}?)\"?", "pattern": r"(sudo )?yunohost app checkurl \"?(\$\{?\w+\}?)\/?(\$\{?\w+\}?)\"?\s+-a\s\"?(\$\{?\w+\}?)\"?",
r"ynh_webpath_register --app=\4 --domain=\2 --path_url=\3"), "replace": r"ynh_webpath_register --app=\4 --domain=\2 --path_url=\3",
"important": True
},
# Remove
# Automatic diagnosis data from YunoHost
# __PRE_TAG1__$(yunohost tools diagnosis | ...)__PRE_TAG2__"
#
"yunohost tools diagnosis": {
"pattern": r"(Automatic diagnosis data from YunoHost( *\n)*)? *(__\w+__)? *\$\(yunohost tools diagnosis.*\)(__\w+__)?",
"replace": r"",
"important": False
}
} }
stuff_to_replace_compiled = {h: (re.compile(r[0]), r[1]) if r[0] else (None,None) for h, r in stuff_to_replace.items()} for helper, infos in stuff_to_replace.items():
infos["pattern"] = re.compile(infos["pattern"]) if infos.get("pattern") else None
infos["replace"] = infos.get("replace")
for filename in files_to_patch: for filename in files_to_patch:
@ -2875,18 +2892,20 @@ def _patch_legacy_helpers(app_folder):
content = read_file(filename) content = read_file(filename)
replaced_stuff = False replaced_stuff = False
show_warning = False
for helper, regexes in stuff_to_replace_compiled.items(): for helper, infos in stuff_to_replace.items():
pattern, replace = regexes
# If helper is used, attempt to patch the file # If helper is used, attempt to patch the file
if helper in content and pattern != "": if helper in content and infos["pattern"]:
content = pattern.sub(replace, content) content = infos["pattern"].sub(infos["replace"], content)
replaced_stuff = True replaced_stuff = True
if infos["important"]:
show_warning = True
# If the helpert is *still* in the content, it means that we # If the helpert is *still* in the content, it means that we
# couldn't patch the deprecated helper in the previous lines. In # couldn't patch the deprecated helper in the previous lines. In
# that case, abort the install or whichever step is performed # that case, abort the install or whichever step is performed
if helper in content: if helper in content and infos["important"]:
raise YunohostError("This app is likely pretty old and uses deprecated / outdated helpers that can't be migrated easily. It can't be installed anymore.") raise YunohostError("This app is likely pretty old and uses deprecated / outdated helpers that can't be migrated easily. It can't be installed anymore.")
if replaced_stuff: if replaced_stuff:
@ -2902,5 +2921,7 @@ def _patch_legacy_helpers(app_folder):
# Actually write the new content in the file # Actually write the new content in the file
write_to_file(filename, content) write_to_file(filename, content)
if show_warning:
# And complain about those damn deprecated helpers # And complain about those damn deprecated helpers
logger.error("/!\ Packagers ! This app uses a very old deprecated helpers ... Yunohost automatically patched the helpers to use the new recommended practice, but please do consider fixing the upstream code right now ...") logger.error("/!\ Packagers ! This app uses a very old deprecated helpers ... Yunohost automatically patched the helpers to use the new recommended practice, but please do consider fixing the upstream code right now ...")