2016-01-25 12:52:18 +01:00
#!/usr/bin/env python3
2017-01-31 09:15:18 +01:00
# -*- coding: utf8 -*-
2016-01-25 12:52:18 +01:00
2023-07-25 18:00:42 +02:00
import copy
2016-01-25 12:52:18 +01:00
import sys
import os
2016-12-23 18:06:44 +01:00
import re
2016-01-25 12:52:18 +01:00
import json
2017-08-31 02:01:29 +02:00
import shlex
import urllib . request
2020-03-31 19:17:54 +02:00
import subprocess
2020-04-08 02:35:03 +02:00
import time
2020-11-04 15:51:24 +01:00
import statistics
2020-11-17 01:13:05 +01:00
from datetime import datetime
2017-08-31 02:01:29 +02:00
2023-01-20 17:56:57 +01:00
try :
import toml
2023-09-07 23:11:03 +02:00
except Exception :
2023-01-20 17:56:57 +01:00
os . system ( ' pip3 install toml ' )
import toml
2023-07-25 18:00:42 +02:00
try :
import jsonschema
2023-09-07 23:11:03 +02:00
except Exception :
2023-07-25 20:23:41 +02:00
os . system ( ' pip3 install jsonschema ' )
2023-07-25 18:00:42 +02:00
import jsonschema
2020-03-31 21:15:08 +02:00
# ############################################################################
# Helper list
# ############################################################################
2021-04-11 17:13:59 +02:00
# Generated April 11th using:
2020-03-31 21:15:08 +02:00
# cat /path/to/yunohost/data/helpers.d/* | grep "^ynh_" | tr -d '(){ ' > helperlist
2021-11-13 19:05:35 +01:00
# for HELPER in $(cat helperlist); do REQUIRE=$(grep -whB5 "^$HELPER" /path/to/yunohost/data/helpers.d/* | grep "Requires .* or higher\." | grep -o -E "[0-9].[0-9].[0-9]"); echo "'$HELPER': '$REQUIRE'",; done | tr "'" '"'
2020-03-31 21:15:08 +02:00
official_helpers = {
2022-10-21 22:30:42 +02:00
" ynh_install_apps " : " " ,
" ynh_remove_apps " : " " ,
2021-10-24 14:33:41 +02:00
" ynh_wait_dpkg_free " : " 3.3.1 " ,
" ynh_package_is_installed " : " 2.2.4 " ,
" ynh_package_version " : " 2.2.4 " ,
" ynh_apt " : " 2.4.0 " ,
" ynh_package_update " : " 2.2.4 " ,
" ynh_package_install " : " 2.2.4 " ,
" ynh_package_remove " : " 2.2.4 " ,
" ynh_package_autoremove " : " 2.2.4 " ,
" ynh_package_autopurge " : " 2.7.2 " ,
" ynh_package_install_from_equivs " : " 2.2.4 " ,
" ynh_install_app_dependencies " : " 2.6.4 " ,
" ynh_add_app_dependencies " : " 3.8.1 " ,
" ynh_remove_app_dependencies " : " 2.6.4 " ,
" ynh_install_extra_app_dependencies " : " 3.8.1 " ,
" ynh_install_extra_repo " : " 3.8.1 " ,
" ynh_remove_extra_repo " : " 3.8.1 " ,
" ynh_add_repo " : " 3.8.1 " ,
" ynh_pin_repo " : " 3.8.1 " ,
" ynh_backup " : " 2.4.0 " ,
" ynh_restore " : " 2.6.4 " ,
" ynh_restore_file " : " 2.6.4 " ,
" ynh_store_file_checksum " : " 2.6.4 " ,
" ynh_backup_if_checksum_is_different " : " 2.6.4 " ,
" ynh_delete_file_checksum " : " 3.3.1 " ,
" ynh_backup_archive_exists " : " " ,
" ynh_backup_before_upgrade " : " 2.7.2 " ,
" ynh_restore_upgradebackup " : " 2.7.2 " ,
2021-11-13 19:05:35 +01:00
" ynh_app_config_get_one " : " " ,
2022-10-21 22:30:42 +02:00
# Commenting out config panel helpers
# that may legitimately be overwritten from config script
#"ynh_app_config_get": "",
#"ynh_app_config_show": "",
#"ynh_app_config_validate": "",
2021-11-13 19:05:35 +01:00
" ynh_app_config_apply_one " : " " ,
2022-10-21 22:30:42 +02:00
#"ynh_app_config_apply": "",
#"ynh_app_action_run": "",
#"ynh_app_config_run": "",
" ynh_add_fail2ban_config " : " 4.1.0 " ,
2021-10-24 14:33:41 +02:00
" ynh_remove_fail2ban_config " : " 3.5.0 " ,
" ynh_handle_getopts_args " : " 3.2.2 " ,
" ynh_get_ram " : " 3.8.1 " ,
" ynh_require_ram " : " 3.8.1 " ,
" ynh_die " : " 2.4.0 " ,
" ynh_print_info " : " 3.2.0 " ,
" ynh_print_log " : " 3.2.0 " ,
" ynh_print_warn " : " 3.2.0 " ,
" ynh_print_err " : " 3.2.0 " ,
" ynh_exec_err " : " 3.2.0 " ,
" ynh_exec_warn " : " 3.2.0 " ,
" ynh_exec_warn_less " : " 3.2.0 " ,
" ynh_exec_quiet " : " 3.2.0 " ,
" ynh_exec_fully_quiet " : " 3.2.0 " ,
" ynh_print_OFF " : " 3.2.0 " ,
" ynh_print_ON " : " 3.2.0 " ,
" ynh_script_progression " : " 3.5.0 " ,
" ynh_return " : " 3.6.0 " ,
" ynh_use_logrotate " : " 2.6.4 " ,
" ynh_remove_logrotate " : " 2.6.4 " ,
2022-10-21 22:30:42 +02:00
" ynh_multimedia_build_main_dir " : " " ,
" ynh_multimedia_addfolder " : " " ,
" ynh_multimedia_addaccess " : " " ,
2021-10-24 14:33:41 +02:00
" ynh_mysql_connect_as " : " 2.2.4 " ,
" ynh_mysql_execute_as_root " : " 2.2.4 " ,
" ynh_mysql_execute_file_as_root " : " 2.2.4 " ,
" ynh_mysql_create_db " : " 2.2.4 " ,
" ynh_mysql_drop_db " : " 2.2.4 " ,
" ynh_mysql_dump_db " : " 2.2.4 " ,
" ynh_mysql_create_user " : " 2.2.4 " ,
" ynh_mysql_user_exists " : " 2.2.4 " ,
" ynh_mysql_drop_user " : " 2.2.4 " ,
" ynh_mysql_setup_db " : " 2.6.4 " ,
" ynh_mysql_remove_db " : " 2.6.4 " ,
" ynh_find_port " : " 2.6.4 " ,
" ynh_port_available " : " 3.8.0 " ,
" ynh_validate_ip " : " 2.2.4 " ,
" ynh_validate_ip4 " : " 2.2.4 " ,
" ynh_validate_ip6 " : " 2.2.4 " ,
2022-10-21 22:30:42 +02:00
" ynh_add_nginx_config " : " 4.1.0 " ,
2021-10-24 14:33:41 +02:00
" ynh_remove_nginx_config " : " 2.7.2 " ,
" ynh_install_n " : " 2.7.1 " ,
" ynh_use_nodejs " : " 2.7.1 " ,
" ynh_install_nodejs " : " 2.7.1 " ,
" ynh_remove_nodejs " : " 2.7.1 " ,
" ynh_cron_upgrade_node " : " 2.7.1 " ,
" ynh_permission_create " : " 3.7.0 " ,
" ynh_permission_delete " : " 3.7.0 " ,
" ynh_permission_exists " : " 3.7.0 " ,
" ynh_permission_url " : " 3.7.0 " ,
" ynh_permission_update " : " 3.7.0 " ,
" ynh_permission_has_user " : " 3.7.1 " ,
2022-10-21 22:30:42 +02:00
" ynh_legacy_permissions_exists " : " 4.1.2 " ,
" ynh_legacy_permissions_delete_all " : " 4.1.2 " ,
" ynh_add_fpm_config " : " 4.1.0 " ,
2021-10-24 14:33:41 +02:00
" ynh_remove_fpm_config " : " 2.7.2 " ,
" ynh_install_php " : " 3.8.1 " ,
" ynh_remove_php " : " 3.8.1 " ,
" ynh_get_scalable_phpfpm " : " " ,
2022-10-21 22:30:42 +02:00
" ynh_composer_exec " : " " ,
" ynh_install_composer " : " " ,
2021-10-24 14:33:41 +02:00
" ynh_psql_connect_as " : " 3.5.0 " ,
" ynh_psql_execute_as_root " : " 3.5.0 " ,
" ynh_psql_execute_file_as_root " : " 3.5.0 " ,
" ynh_psql_create_db " : " 3.5.0 " ,
" ynh_psql_drop_db " : " 3.5.0 " ,
" ynh_psql_dump_db " : " 3.5.0 " ,
" ynh_psql_create_user " : " 3.5.0 " ,
" ynh_psql_user_exists " : " 3.5.0 " ,
" ynh_psql_database_exists " : " 3.5.0 " ,
" ynh_psql_drop_user " : " 3.5.0 " ,
" ynh_psql_setup_db " : " 2.7.1 " ,
" ynh_psql_remove_db " : " 2.7.1 " ,
" ynh_psql_test_if_first_run " : " 2.7.1 " ,
" ynh_app_setting_get " : " 2.2.4 " ,
" ynh_app_setting_set " : " 2.2.4 " ,
" ynh_app_setting_delete " : " 2.2.4 " ,
" ynh_app_setting " : " " ,
" ynh_webpath_available " : " 2.6.4 " ,
" ynh_webpath_register " : " 2.6.4 " ,
" ynh_string_random " : " 2.2.4 " ,
" ynh_replace_string " : " 2.6.4 " ,
" ynh_replace_special_string " : " 2.7.7 " ,
" ynh_sanitize_dbid " : " 2.2.4 " ,
" ynh_normalize_url_path " : " 2.6.4 " ,
2022-10-21 22:30:42 +02:00
" ynh_add_systemd_config " : " 4.1.0 " ,
2021-10-24 14:33:41 +02:00
" ynh_remove_systemd_config " : " 2.7.2 " ,
" ynh_systemd_action " : " 3.5.0 " ,
" ynh_clean_check_starting " : " 3.5.0 " ,
" ynh_user_exists " : " 2.2.4 " ,
" ynh_user_get_info " : " 2.2.4 " ,
" ynh_user_list " : " 2.4.0 " ,
" ynh_system_user_exists " : " 2.2.4 " ,
" ynh_system_group_exists " : " 3.5.0 " ,
" ynh_system_user_create " : " 2.6.4 " ,
" ynh_system_user_delete " : " 2.6.4 " ,
" ynh_exec_as " : " 4.1.7 " ,
" ynh_exit_properly " : " 2.6.4 " ,
" ynh_abort_if_errors " : " 2.6.4 " ,
" ynh_setup_source " : " 2.6.4 " ,
" ynh_local_curl " : " 2.6.4 " ,
" ynh_add_config " : " 4.1.0 " ,
" ynh_replace_vars " : " 4.1.0 " ,
2021-11-13 19:05:35 +01:00
" ynh_read_var_in_file " : " " ,
" ynh_write_var_in_file " : " " ,
2021-10-24 14:33:41 +02:00
" ynh_render_template " : " " ,
" ynh_get_debian_release " : " 2.7.1 " ,
" ynh_secure_remove " : " 2.6.4 " ,
" ynh_read_manifest " : " 3.5.0 " ,
" ynh_app_upstream_version " : " 3.5.0 " ,
" ynh_app_package_version " : " 3.5.0 " ,
" ynh_check_app_version_changed " : " 3.5.0 " ,
" ynh_compare_current_package_version " : " 3.8.0 " ,
2020-03-31 21:15:08 +02:00
}
2022-11-27 18:16:33 +01:00
deprecated_helpers_in_v2 = [
( " ynh_clean_setup " , " (?) " ) ,
( " ynh_abort_if_errors " , " nothing, handled by the core, just get rid of it " ) ,
( " ynh_backup_before_upgrade " , " nothing, handled by the core, just get rid of it " ) ,
( " ynh_restore_upgradebackup " , " nothing, handled by the core, just get rid of it " ) ,
( " ynh_system_user_create " , " the system_user resource " ) ,
( " ynh_system_user_delete " , " the system_user resource " ) ,
( " ynh_webpath_register " , " the permission resource " ) ,
( " ynh_webpath_available " , " the permission resource " ) ,
( " ynh_permission_update " , " the permission resource " ) ,
( " ynh_permission_create " , " the permission resource " ) ,
( " ynh_permission_exists " , " the permission resource " ) ,
( " ynh_legacy_permissions_exists " , " the permission resource " ) ,
( " ynh_legacy_permissions_delete_all " , " the permission resource " ) ,
( " ynh_install_app_dependencies " , " the apt resource " ) ,
( " ynh_install_extra_app_dependencies " , " the apt source " ) ,
( " ynh_remove_app_dependencies " , " the apt resource " ) ,
( " ynh_psql_test_if_first_run " , " the database resource " ) ,
( " ynh_mysql_setup_db " , " the database resource " ) ,
( " ynh_psql_setup_db " , " the database resource " ) ,
( " ynh_mysql_remove_db " , " the database resource " ) ,
( " ynh_psql_remove_db " , " the database resource " ) ,
( " ynh_find_port " , " the port resource " ) ,
2023-02-21 23:08:35 +01:00
( " ynh_send_readme_to_admin " , " the doc/POST_INSTALL.md or POST_UPGRADE.md mechanism " )
2022-11-27 18:16:33 +01:00
]
# Default to 1, set to 2 automatically if a toml manifest is found
app_packaging_format = None
2016-11-03 19:09:07 +01:00
2019-01-28 23:33:27 +01:00
# ############################################################################
# Utilities
# ############################################################################
2021-10-24 14:33:41 +02:00
2016-01-25 12:52:18 +01:00
class c :
2021-10-24 14:33:41 +02:00
HEADER = " \033 [94m "
OKBLUE = " \033 [94m "
OKGREEN = " \033 [92m "
WARNING = " \033 [93m "
MAYBE_FAIL = " \033 [96m "
FAIL = " \033 [91m "
END = " \033 [0m "
BOLD = " \033 [1m "
UNDERLINE = " \033 [4m "
2016-11-03 19:09:07 +01:00
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
class TestReport :
2020-10-08 14:25:11 +02:00
def __init__ ( self , message ) :
self . message = message
2021-02-03 18:30:26 +01:00
def display ( self , prefix = " " ) :
_print ( prefix + self . style % self . message )
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
class Warning ( TestReport ) :
2020-11-18 23:56:58 +01:00
style = c . WARNING + " ! %s " + c . END
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
class Error ( TestReport ) :
2020-11-18 23:56:58 +01:00
style = c . FAIL + " ✘ %s " + c . END
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
class Info ( TestReport ) :
2021-02-03 18:30:26 +01:00
style = " - %s " + c . END
2020-11-17 01:13:05 +01:00
2021-10-24 14:33:41 +02:00
2020-11-17 01:13:05 +01:00
class Success ( TestReport ) :
2020-11-18 23:56:58 +01:00
style = c . OKGREEN + " ☺ %s ♥ " + c . END
2020-11-17 01:13:05 +01:00
2021-10-24 14:33:41 +02:00
2020-11-21 00:33:09 +01:00
class Critical ( TestReport ) :
style = c . FAIL + " ✘✘✘ %s " + c . END
2016-01-25 12:52:18 +01:00
2019-01-28 23:33:27 +01:00
2020-06-10 17:34:34 +02:00
output = " plain "
def _print ( * args , * * kwargs ) :
if output == " plain " :
print ( * args , * * kwargs )
2020-10-08 14:25:11 +02:00
def report_warning_not_reliable ( str ) :
2020-06-10 17:34:34 +02:00
_print ( c . MAYBE_FAIL + " ? " , str , c . END )
2020-03-31 04:40:00 +02:00
def print_happy ( str ) :
2020-11-18 23:56:58 +01:00
_print ( c . OKGREEN + " ☺ " , str , " ♥ " )
2020-03-31 04:40:00 +02:00
2017-08-31 02:01:29 +02:00
def urlopen ( url ) :
try :
conn = urllib . request . urlopen ( url )
except urllib . error . HTTPError as e :
2021-10-24 14:33:41 +02:00
return { " content " : " " , " code " : e . code }
2017-08-31 02:01:29 +02:00
except urllib . error . URLError as e :
2021-10-24 14:33:41 +02:00
_print ( " Could not fetch %s : %s " % ( url , e ) )
return { " content " : " " , " code " : 0 }
return { " content " : conn . read ( ) . decode ( " UTF8 " ) , " code " : 200 }
2016-11-03 19:09:07 +01:00
2016-01-25 12:52:18 +01:00
2019-01-28 23:33:27 +01:00
def file_exists ( file_path ) :
return os . path . isfile ( file_path ) and os . stat ( file_path ) . st_size > 0
2016-11-03 19:09:07 +01:00
2020-06-10 17:34:34 +02:00
2020-03-31 04:27:41 +02:00
def spdx_licenses ( ) :
cachefile = " .spdx_licenses "
2020-04-08 02:35:03 +02:00
if os . path . exists ( cachefile ) and time . time ( ) - os . path . getmtime ( cachefile ) < 3600 :
2020-03-31 04:27:41 +02:00
return open ( cachefile ) . read ( )
2020-10-08 14:25:11 +02:00
url = " https://spdx.org/licenses/ "
2021-10-24 14:33:41 +02:00
content = urlopen ( url ) [ " content " ]
2020-03-31 04:27:41 +02:00
open ( cachefile , " w " ) . write ( content )
return content
2021-10-24 14:33:41 +02:00
2023-07-25 18:00:42 +02:00
def manifest_v2_schema ( ) :
cachefile = " .manifest.v2.schema.json "
if os . path . exists ( cachefile ) and time . time ( ) - os . path . getmtime ( cachefile ) < 3600 :
return json . loads ( open ( cachefile ) . read ( ) )
url = " https://raw.githubusercontent.com/YunoHost/apps/master/schemas/manifest.v2.schema.json "
content = urlopen ( url ) [ " content " ]
open ( cachefile , " w " ) . write ( content )
return json . loads ( content )
2020-10-08 14:25:11 +02:00
tests = { }
2020-11-18 23:56:52 +01:00
tests_reports = {
2020-11-21 00:33:09 +01:00
" success " : [ ] ,
" info " : [ ] ,
2020-11-18 23:56:52 +01:00
" warning " : [ ] ,
" error " : [ ] ,
2020-11-21 00:33:09 +01:00
" critical " : [ ] ,
2020-11-18 23:56:52 +01:00
}
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
def test ( * * kwargs ) :
def decorator ( f ) :
clsname = f . __qualname__ . split ( " . " ) [ 0 ]
if clsname not in tests :
tests [ clsname ] = [ ]
2021-10-24 14:33:41 +02:00
tests [ clsname ] . append ( ( f , kwargs ) )
2020-10-08 14:25:11 +02:00
return f
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
return decorator
2021-10-24 14:33:41 +02:00
class TestSuite :
2020-10-08 14:25:11 +02:00
def run_tests ( self ) :
2021-02-03 18:30:26 +01:00
reports = [ ]
2020-10-08 14:25:11 +02:00
for test , options in tests [ self . __class__ . __name__ ] :
if " only " in options and self . name not in options [ " only " ] :
continue
if " ignore " in options and self . name in options [ " ignore " ] :
continue
2021-02-03 18:30:26 +01:00
2021-02-22 18:42:07 +01:00
this_test_reports = list ( test ( self ) )
for report in this_test_reports :
report . test_name = test . __qualname__
reports + = this_test_reports
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
# Display part
2021-02-03 18:30:26 +01:00
def report_type ( report ) :
return report . __class__ . __name__ . lower ( )
2021-02-03 18:42:50 +01:00
if any ( report_type ( r ) in [ " warning " , " error " , " critical " ] for r in reports ) :
2021-10-24 14:33:41 +02:00
prefix = c . WARNING + " ! "
2021-02-03 18:42:50 +01:00
elif any ( report_type ( r ) in [ " info " ] for r in reports ) :
2021-10-24 14:33:41 +02:00
prefix = " ⓘ "
2021-02-03 18:42:50 +01:00
else :
2021-10-24 14:33:41 +02:00
prefix = c . OKGREEN + " ✔ "
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
_print ( " " + c . BOLD + prefix + c . OKBLUE + self . test_suite_name + c . END )
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
if len ( reports ) :
_print ( " " )
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
for report in reports :
report . display ( prefix = " " )
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
if len ( reports ) :
_print ( " " )
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
for report in reports :
2021-02-22 18:42:07 +01:00
tests_reports [ report_type ( report ) ] . append ( ( report . test_name , report ) )
2021-02-03 18:30:26 +01:00
2020-11-17 01:13:05 +01:00
def run_single_test ( self , test ) :
reports = list ( test ( self ) )
2021-02-03 18:30:26 +01:00
2021-02-03 18:42:50 +01:00
def report_type ( report ) :
return report . __class__ . __name__ . lower ( )
for report in reports :
report . display ( )
test_name = test . __qualname__
tests_reports [ report_type ( report ) ] . append ( ( test_name , report ) )
2021-02-03 18:30:26 +01:00
2023-09-07 23:11:03 +02:00
2021-10-28 15:24:32 +02:00
# Defined in packaging module
# See https://github.com/pypa/packaging/blob/20cd09e00917adbc4afeaa753be831a6bc2740f7/packaging/version.py#L225
VERSION_PATTERN = r """
v ?
( ? :
( ? : ( ? P < epoch > [ 0 - 9 ] + ) ! ) ? # epoch
( ? P < release > [ 0 - 9 ] + ( ? : \. [ 0 - 9 ] + ) * ) # release segment
( ? P < pre > # pre-release
[ - _ \. ] ?
( ? P < pre_l > ( a | b | c | rc | alpha | beta | pre | preview ) )
[ - _ \. ] ?
( ? P < pre_n > [ 0 - 9 ] + ) ?
) ?
( ? P < post > # post release
( ? : - ( ? P < post_n1 > [ 0 - 9 ] + ) )
|
( ? :
[ - _ \. ] ?
( ? P < post_l > post | rev | r )
[ - _ \. ] ?
( ? P < post_n2 > [ 0 - 9 ] + ) ?
)
) ?
( ? P < dev > # dev release
[ - _ \. ] ?
( ? P < dev_l > dev )
[ - _ \. ] ?
( ? P < dev_n > [ 0 - 9 ] + ) ?
) ?
)
( ? : \+ ( ? P < local > [ a - z0 - 9 ] + ( ? : [ - _ \. ] [ a - z0 - 9 ] + ) * ) ) ? # local version
"""
2021-02-03 18:30:26 +01:00
2019-01-28 23:33:27 +01:00
# ############################################################################
# Actual high-level checks
# ############################################################################
2020-03-31 05:45:51 +02:00
scriptnames = [ " _common.sh " , " install " , " remove " , " upgrade " , " backup " , " restore " ]
2020-06-10 17:34:34 +02:00
2020-10-08 14:25:11 +02:00
class App ( TestSuite ) :
2019-03-02 01:19:40 +01:00
def __init__ ( self , path ) :
2021-02-03 18:30:26 +01:00
_print ( " Analyzing app %s ... " % path )
2019-03-02 01:19:40 +01:00
self . path = path
2020-10-08 14:25:11 +02:00
self . manifest_ = Manifest ( self . path )
self . manifest = self . manifest_ . manifest
2020-03-31 05:45:51 +02:00
self . scripts = { f : Script ( self . path , f ) for f in scriptnames }
2020-11-11 17:08:49 +01:00
self . configurations = Configurations ( self )
2020-11-17 01:13:05 +01:00
self . app_catalog = AppCatalog ( self . manifest [ " id " ] )
2020-10-08 14:25:11 +02:00
2021-02-03 18:30:26 +01:00
self . test_suite_name = " General stuff, misc helper usage "
_print ( )
2019-03-02 01:43:29 +01:00
def analyze ( self ) :
2016-12-18 02:37:07 +01:00
2020-10-08 14:25:11 +02:00
self . manifest_ . run_tests ( )
2019-03-02 01:43:29 +01:00
2020-03-31 05:45:51 +02:00
for script in [ self . scripts [ s ] for s in scriptnames if self . scripts [ s ] . exists ] :
2020-10-08 14:25:11 +02:00
script . run_tests ( )
self . run_tests ( )
2020-11-11 17:08:49 +01:00
self . configurations . run_tests ( )
2020-11-17 01:13:05 +01:00
self . app_catalog . run_tests ( )
self . report ( )
def report ( self ) :
2021-02-03 18:30:26 +01:00
_print ( " ======= " )
2020-11-18 23:56:52 +01:00
# These are meant to be the last stuff running, they are based on
# previously computed errors/warning/successes
self . run_single_test ( App . qualify_for_level_7 )
self . run_single_test ( App . qualify_for_level_8 )
2020-12-17 22:47:24 +01:00
self . run_single_test ( App . qualify_for_level_9 )
2020-11-17 01:13:05 +01:00
if output == " json " :
2021-10-24 14:33:41 +02:00
print (
json . dumps (
{
" success " : [ test for test , _ in tests_reports [ " success " ] ] ,
" info " : [ test for test , _ in tests_reports [ " info " ] ] ,
" warning " : [ test for test , _ in tests_reports [ " warning " ] ] ,
" error " : [ test for test , _ in tests_reports [ " error " ] ] ,
" critical " : [ test for test , _ in tests_reports [ " critical " ] ] ,
} ,
indent = 4 ,
)
)
2020-11-17 01:13:05 +01:00
return
2020-11-21 00:33:09 +01:00
if tests_reports [ " error " ] or tests_reports [ " critical " ] :
2020-11-17 01:13:05 +01:00
sys . exit ( 1 )
2020-11-18 23:56:52 +01:00
def qualify_for_level_7 ( self ) :
2020-11-21 00:33:09 +01:00
if tests_reports [ " critical " ] :
_print ( " There are some critical issues in this app :( " )
elif tests_reports [ " error " ] :
2020-11-19 00:07:36 +01:00
_print ( " Uhoh there are some errors to be fixed :( " )
2021-10-01 04:08:15 +02:00
elif len ( tests_reports [ " warning " ] ) > = 3 :
2020-11-19 00:07:36 +01:00
_print ( " Still some warnings to be fixed :s " )
2021-09-23 22:28:00 +02:00
elif len ( tests_reports [ " warning " ] ) == 2 :
_print ( " Only 2 warnings remaining! You can do it! " )
elif len ( tests_reports [ " warning " ] ) == 1 :
_print ( " Only 1 warning remaining! You can do it! " )
2020-11-17 01:13:05 +01:00
else :
2021-10-24 14:33:41 +02:00
yield Success (
" Not even a warning! Congratz and thank you for keeping this package up to date with good practices! This app qualifies for level 7! "
)
2020-11-17 01:13:05 +01:00
2020-11-18 23:56:52 +01:00
def qualify_for_level_8 ( self ) :
2020-11-17 01:13:05 +01:00
2020-11-18 23:56:52 +01:00
successes = [ test . split ( " . " ) [ 1 ] for test , _ in tests_reports [ " success " ] ]
2020-11-17 01:13:05 +01:00
2020-11-18 23:56:52 +01:00
# Level 8 = qualifies for level 7 + maintained + long term good quality
catalog_infos = self . app_catalog . catalog_infos
2023-01-08 15:26:32 +01:00
antifeatures = catalog_infos and catalog_infos . get ( " antifeatures " , [ ] )
if any ( af in antifeatures for af in [ ' package-not-maintained ' , ' deprecated-software ' , ' alpha-software ' , ' replaced-by-another-app ' ] ) :
_print ( " In the catalog, the app is flagged as not maintained / deprecated / alpha or replaced by another app " )
2021-10-24 14:33:41 +02:00
elif (
" qualify_for_level_7 " in successes
and " is_long_term_good_quality " in successes
) :
yield Success (
" The app is maintained and long-term good quality, and therefore qualifies for level 8! "
)
2020-11-17 01:13:05 +01:00
2020-12-17 22:47:24 +01:00
def qualify_for_level_9 ( self ) :
if self . app_catalog . catalog_infos . get ( " high_quality " , False ) :
yield Success ( " The app is flagged as high-quality in the app catalog " )
2020-11-11 17:08:49 +01:00
#########################################
# _____ _ #
# | __ \ | | #
# | | \/ ___ _ __ ___ _ __ __ _| | #
# | | __ / _ \ '_ \ / _ \ '__/ _` | | #
# | |_\ \ __/ | | | __/ | | (_| | | #
# \____/\___|_| |_|\___|_| \__,_|_| #
# #
#########################################
@test ( )
def mandatory_scripts ( app ) :
2021-10-24 14:33:41 +02:00
filenames = (
" LICENSE " ,
" README.md " ,
" scripts/install " ,
" scripts/remove " ,
" scripts/upgrade " ,
" scripts/backup " ,
" scripts/restore " ,
)
2020-11-11 17:08:49 +01:00
for filename in filenames :
if not file_exists ( app . path + " / " + filename ) :
yield Error ( " Providing %s is mandatory " % filename )
2021-04-23 18:34:39 +02:00
if file_exists ( app . path + " /LICENSE " ) :
license_content = open ( app . path + " /LICENSE " ) . read ( )
if " File containing the license of your package " in license_content :
2023-11-24 18:10:07 +01:00
yield Error ( " You should put an actual license in LICENSE... " )
2021-04-23 18:34:39 +02:00
2021-05-10 20:17:58 +02:00
@test ( )
def doc_dir ( app ) :
2022-11-27 18:16:33 +01:00
2021-05-13 19:49:16 +02:00
if not os . path . exists ( app . path + " /doc " ) :
2023-01-11 09:45:55 +01:00
if app_packaging_format < = 1 :
2023-11-24 18:10:07 +01:00
yield Warning (
2023-01-11 09:45:55 +01:00
""" READMEs are to be automatically generated using https://github.com/YunoHost/apps/tree/master/tools/README-generator.
- You are encouraged to create a doc / DISCLAIMER . md file , which should contain any important information to be presented to the admin before installation . Check https : / / github . com / YunoHost / example_ynh / blob / master / doc / DISCLAIMER . md for more details ( it should be somewhat equivalent to the old ' Known limitations ' and ' Specific features ' section ) . ( It ' s not mandatory to create this file if you ' re absolutely sure there ' s no relevant info to show to the user)
- If relevant for this app , screenshots can be added in a doc / screenshots / folder . """
)
elif app_packaging_format > = 2 :
yield Error (
""" Having a doc/ folder is now mandatory in packaging v2 and is expected to contain :
- ( recommended ) doc / DESCRIPTION . md : a long description of the app , typically around 5 ~ 20 lines , for example to list features
- ( recommended ) doc / screenshots / : a folder containing at least one . png ( or . jpg ) screenshot of the app
- ( if relevant ) doc / ADMIN . md : an admin doc page meant to provide general info about adminstrating this app , will be available in yunohost ' s webadmin
- ( if relevant ) doc / SOME_OTHER_PAGE . md : an arbitrarily named admin doc page meant to provide info on a specific topic , will be available in yunohost ' s webadmin
- ( if relevant ) doc / PRE_INSTALL . md , POST_INSTALL . md : important informations to display to the user before / after the install ( similar mechanism exists for upgrade )
"""
)
2021-05-10 20:17:58 +02:00
2022-05-06 21:08:58 +02:00
if os . path . exists ( app . path + " /doc/screenshots " ) :
du_output = subprocess . check_output ( [ " du " , " -sb " , app . path + " /doc/screenshots " ] , shell = False )
screenshots_size = int ( du_output . split ( ) [ 0 ] )
if screenshots_size > 512000 :
2022-12-23 23:31:16 +01:00
yield Info ( " Consider keeping the content of doc/screenshots under ~512Kb for better UI/UX once the screenshots will be integrated in the webadmin app ' s catalog (to be discussed with the team) " )
2022-12-03 18:12:01 +01:00
2022-12-03 20:13:41 +01:00
for path in os . listdir ( app . path + " /doc/screenshots " ) :
2022-12-04 03:24:45 +01:00
if path == " .gitkeep " :
continue
2022-12-03 20:13:41 +01:00
if all ( not path . lower ( ) . endswith ( ext ) for ext in [ " .jpg " , " .jpeg " , " .png " , " .gif " , " .webp " ] ) :
yield Warning ( " In the doc/screenshots folder, only .jpg, .jpeg, .png, .webp and .gif are accepted " )
break
2022-05-06 21:08:58 +02:00
2022-11-27 18:16:33 +01:00
@test ( )
def doc_dir_v2 ( app ) :
if app_packaging_format < = 1 :
return
2023-01-11 09:45:55 +01:00
if os . path . exists ( app . path + " /doc " ) and not os . path . exists ( app . path + " /doc/DESCRIPTION.md " ) :
2022-11-27 18:16:33 +01:00
yield Error ( " A DESCRIPTION.md is now mandatory in packaging v2 and is meant to contains an extensive description of what the app is and does. Consider also adding a ' /doc/screenshots/ ' folder with a few screenshots of what the app looks like. " )
elif os . system ( fr ' grep -inrq " Some long and extensive description \ |lorem ipsum dolor sit amet \ |Ut enim ad minim veniam " { app . path } /doc/DESCRIPTION.md ' ) == 0 :
yield Error ( " It looks like DESCRIPTION.md just contains placeholder texts " )
if os . path . exists ( app . path + " /doc/DISCLAIMER.md " ) :
yield Warning ( """ The whole thing about doc/DISCLAIMER.md is refactored again in v2 (sorry about that :/) to improve the UX - basically people shouldnt have to actively go read the READMEs to get those infos
You are encouraged to split its infos into :
- Integration - related infos ( eg . LDAP / SSO support , arch support , resource usage , . . . )
- > neant to go in the ' integration ' section of the manifest . toml
- Antifeatures - related infos ( eg . alpha / deprecated software , arbitrary limiations , . . . )
- > these are now formalized using the ' antifeatures ' mechanism in the app catalog directly : cf https : / / github . com / YunoHost / apps / blob / master / antifeatures . yml and the ' antifeatures ' key in apps . json
- Important infos that the admin should be made aware of * before * or * after * the install
2023-02-21 23:08:35 +01:00
- > infos * before * the install are meant to go in ' doc/PRE_INSTALL.md '
- > infos * after * the install are meant to go in ' doc/POST_INSTALL.md ' ( mostly meant to replace ynh_send_readme_to_admin , typically tips about how to login for the first time on the app / finish the install , . . . ) .
2022-11-27 18:16:33 +01:00
- > these will be shown to the admin before / after the install ( and the post_install notif will also be available in the app info page )
- > note that in these files , the __FOOBAR__ syntax is supported and replaced with the corresponding ' foobar ' setting .
- General admin - related infos ( eg . how to access the admin interface of the app , how to install plugin , etc )
- > meant to go in ' doc/ADMIN.md ' which shall be made available in the app info page in the webadmin after installation .
- > if relevant , you can also create custom doc page , just create ' doc/WHATEVER.MD ' and this will correspond to a specific documentation tab in the webadmin .
- > note that in these files , the __FOOBAR__ syntax is supported and replaced with the corresponding ' foobar ' setting .
""" )
2021-10-23 17:23:08 +02:00
@test ( )
2023-02-20 19:34:13 +01:00
def disclaimer_wording_or_placeholder ( app ) :
2021-10-24 14:32:15 +02:00
if os . path . exists ( app . path + " /doc " ) :
2021-10-24 14:33:41 +02:00
if (
os . system (
r " grep -nr -q ' Any known limitations, constrains or stuff not working, such as \ |Other infos that people should be ' %s /doc/ "
% app . path
)
== 0
) :
2023-11-24 18:10:07 +01:00
yield Warning (
2021-10-24 14:33:41 +02:00
" In DISCLAIMER.md: ' Any known limitations [...] such as ' and ' Other infos [...] such as ' are supposed to be placeholder sentences meant to explain to packagers what is the expected content, but is not an appropriate wording for end users :/ "
)
2023-02-20 19:34:13 +01:00
if (
os . system (
r " grep -nr -q ' This is a dummy \ |Ceci est une fausse ' %s /doc/ "
% app . path
)
== 0
) :
yield Warning (
" The doc/ folder seems to still contain some dummy, placeholder messages in the .md markdown files. If those files are not useful in the context of your app, simply remove them. "
)
2020-11-11 17:08:49 +01:00
@test ( )
def change_url_script ( app ) :
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
args = app . manifest [ " arguments " ] . get ( " install " , [ ] )
else :
2023-07-25 18:00:42 +02:00
keyandargs = copy . deepcopy ( app . manifest [ " install " ] )
2022-11-27 18:16:33 +01:00
for key , infos in keyandargs . items ( ) :
infos [ " name " ] = key
args = keyandargs . values ( )
has_domain_arg = any ( a [ " name " ] == " domain " for a in args )
2020-11-11 17:08:49 +01:00
if has_domain_arg and not file_exists ( app . path + " /scripts/change_url " ) :
2021-10-24 14:33:41 +02:00
yield Info (
" Consider adding a change_url script to support changing where the app can be reached "
)
2020-10-15 21:29:17 +02:00
2021-11-14 21:36:38 +01:00
@test ( )
def config_panel ( app ) :
if file_exists ( app . path + " config_panel.json " ) :
2022-12-03 20:13:41 +01:00
yield Error (
2021-11-14 21:36:38 +01:00
" JSON config panels are not supported anymore, should be replaced by a toml version "
)
2022-01-25 00:12:14 +01:00
if file_exists ( app . path + " config_panel.toml " ) :
if os . system ( " grep -q ' version = \" 0.1 \" ' ' %s ' " % ( app . path + " config_panel.toml " ) ) == 0 :
2022-12-03 20:13:41 +01:00
yield Error (
2022-01-25 00:12:14 +01:00
" Config panels version 0.1 are not supported anymore, should be adapted for version 1.0 "
)
elif os . path . exists ( app . path + " /scripts/config " ) and os . system ( " grep -q ' YNH_CONFIG_ \\ |yunohost app action ' ' %s ' " % ( app . path + " /scripts/config " ) ) == 0 :
yield Error (
" The config panel is set to version 1.x, but the config script is apparently still using some old code from 0.1 such as ' $YNH_CONFIG_STUFF ' or ' yunohost app action ' "
)
2021-11-14 21:36:38 +01:00
2020-11-28 19:55:26 +01:00
@test ( )
def badges_in_readme ( app ) :
id_ = app . manifest [ " id " ]
if not file_exists ( app . path + " /README.md " ) :
return
content = open ( app . path + " /README.md " ) . read ( )
2023-09-07 23:11:03 +02:00
if " This README was automatically generated " not in content or not " dash.yunohost.org/integration/ %s .svg " % id_ in content :
2023-04-01 14:07:40 +02:00
yield Warning (
2023-01-11 09:45:55 +01:00
" It looks like the README was not generated automatically by https://github.com/YunoHost/apps/tree/master/tools/README-generator. "
" Note that nowadays you are not suppose to edit README.md, the yunohost bot will usually automatically update it if your app is hosted in the YunoHost-Apps org ... or you can also generate it by running the README-generator yourself. "
2020-11-28 19:55:26 +01:00
)
2022-12-03 20:13:41 +01:00
superoldstuff = [ " %20% 28Apps % 29 " , " %20% 28Community % 29 " , " /jenkins/job " , " ci-buster " , " ci-stretch " , " ci-apps-arm " ]
2022-01-25 00:56:06 +01:00
if any ( oldstuff in content for oldstuff in superoldstuff ) :
2022-12-03 20:13:41 +01:00
yield Error (
2022-01-25 00:56:06 +01:00
" The README contains references to super-old build status (such as old jenkins job or ci-apps-arm or ci-stretch...) which are not relevant anymore. Please consider switching to the new auto-generated README format which contains the standard CI badge at the top. "
)
2021-01-10 18:51:58 +01:00
@test ( )
def placeholder_help_string ( self ) :
2021-10-24 14:33:41 +02:00
if (
os . system (
" grep -q ' Use the help field ' %s /manifest.json 2>/dev/null " % self . path
)
== 0
) :
yield Warning (
" Sounds like your manifest.json contains some default placeholder help string ( ' Use the help field to... ' ) ... either replace it with an actually helpful explanation, or remove the help string entirely if you don ' t use it. "
)
2021-01-10 18:51:58 +01:00
2022-08-09 19:00:28 +02:00
if (
os . system (
2023-09-07 23:11:03 +02:00
" grep -q ' Explain in *a few (10~15) words* the purpose of the app \\ |Expliquez en *quelques* (10~15) mots ' %s /manifest.json 2>/dev/null " % self . path
2022-08-09 19:00:28 +02:00
)
== 0
) :
2022-12-03 20:13:41 +01:00
yield Error (
2022-08-09 19:00:28 +02:00
" Sounds like your manifest.json contains the default description string ( ' Explain in *a few (10~15 words) [...] ' ) ... Please replace it with an actual description. "
)
2021-01-10 18:51:58 +01:00
@test ( )
def remaining_replacebyyourapp ( self ) :
2021-01-17 15:32:20 +01:00
if os . system ( " grep -I -qr ' REPLACEBYYOURAPP ' %s 2>/dev/null " % self . path ) == 0 :
2022-12-03 20:13:41 +01:00
yield Error ( " You should replace all occurences of REPLACEBYYOURAPP. " )
2021-01-10 18:51:58 +01:00
2021-10-23 16:43:22 +02:00
@test ( )
def bad_encoding ( self ) :
2021-10-24 14:33:41 +02:00
cmd = (
2023-09-07 23:11:03 +02:00
" file --mime-encoding $(find %s / -type f) | grep ' iso-8859-1 \\ |unknown-8bit ' || true "
2021-10-24 14:33:41 +02:00
% self . path
)
bad_encoding_files = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
2021-10-23 16:43:22 +02:00
for file_ in bad_encoding_files :
2021-10-23 16:59:23 +02:00
if not file_ :
continue
2021-10-23 16:43:22 +02:00
file_ = file_ . split ( ) [ 0 ]
2022-12-03 19:13:36 +01:00
yield Error (
" %s appears to be encoded as latin-1 / iso-8859-1. Please convert it to utf-8 to avoid funky issues. Something like ' iconv -f iso-8859-1 -t utf-8 SOURCE > DEST ' should do the trick. "
2021-10-24 14:33:41 +02:00
% file_
)
2021-10-23 16:43:22 +02:00
2020-10-08 14:25:11 +02:00
#######################################
# _ _ _ #
# | | | | | | #
# | |__| | ___| |_ __ ___ _ __ ___ #
# | __ |/ _ \ | '_ \ / _ \ '__/ __| #
# | | | | __/ | |_) | __/ | \__ \ #
# |_| |_|\___|_| .__/ \___|_| |___/ #
# | | #
# |_| #
#######################################
@test ( )
def helpers_now_official ( app ) :
2023-09-07 23:11:03 +02:00
cmd = " grep -IhEro ' ynh_ \\ w+ * \\ ( * \\ ) ' ' %s /scripts ' | tr -d ' () ' " % app . path
2021-10-24 14:33:41 +02:00
custom_helpers = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
2020-10-08 14:25:11 +02:00
custom_helpers = [ c . split ( " __ " ) [ 0 ] for c in custom_helpers ]
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
for custom_helper in custom_helpers :
if custom_helper in official_helpers . keys ( ) :
2021-10-24 14:33:41 +02:00
yield Info (
" %s is now an official helper since version ' %s ' "
% ( custom_helper , official_helpers [ custom_helper ] or " ? " )
)
2020-03-31 21:15:08 +02:00
2023-11-25 15:15:01 +01:00
@test ( )
def git_clone_usage ( app ) :
cmd = f " grep -I ' git clone ' ' { app . path } ' /scripts/install ' { app . path } ' /scripts/_common.sh 2>/dev/null | grep -qv ' xxenv \ |rbenv \ |oracledb ' "
if os . system ( cmd ) == 0 :
yield Info ( " Using ' git clone ' is not recommended ... most forge do provide the ability to download a proper archive of the code for a specific commit. Please use the ' sources ' resource in the manifest.toml in combination with ynh_setup_source. " )
2020-10-08 14:25:11 +02:00
@test ( )
def helpers_version_requirement ( app ) :
2020-03-31 21:15:08 +02:00
2023-09-07 23:11:03 +02:00
cmd = " grep -IhEro ' ynh_ \\ w+ * \\ ( * \\ ) ' ' %s /scripts ' | tr -d ' () ' " % app . path
2021-10-24 14:33:41 +02:00
custom_helpers = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
2020-03-31 21:15:08 +02:00
custom_helpers = [ c . split ( " __ " ) [ 0 ] for c in custom_helpers ]
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
yunohost_version_req = (
app . manifest . get ( " requirements " , { } ) . get ( " yunohost " , " " ) . strip ( " >= " )
)
else :
yunohost_version_req = (
app . manifest . get ( " integration " , { } ) . get ( " yunohost " , " " ) . strip ( " >= " )
)
2020-10-08 14:25:11 +02:00
2023-09-07 23:11:03 +02:00
cmd = " grep -IhEro ' ynh_ \\ w+ ' ' %s /scripts ' " % app . path
2021-10-24 14:33:41 +02:00
helpers_used = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
2020-10-08 14:25:11 +02:00
helpers_used = sorted ( set ( helpers_used ) )
2020-03-31 21:15:08 +02:00
2021-10-24 14:33:41 +02:00
manifest_req = [ int ( i ) for i in yunohost_version_req . split ( " . " ) ] + [ 0 , 0 , 0 ]
2020-10-08 14:25:11 +02:00
def validate_version_requirement ( helper_req ) :
2021-10-24 14:33:41 +02:00
if helper_req == " " :
2020-10-08 14:25:11 +02:00
return True
2021-10-24 14:33:41 +02:00
helper_req = [ int ( i ) for i in helper_req . split ( " . " ) ]
for i in range ( 0 , len ( helper_req ) ) :
2020-10-08 14:25:11 +02:00
if helper_req [ i ] == manifest_req [ i ] :
2020-03-31 21:15:08 +02:00
continue
2020-10-08 14:25:11 +02:00
return helper_req [ i ] < = manifest_req [ i ]
return True
2020-03-31 21:15:08 +02:00
2020-10-08 14:25:11 +02:00
for helper in [ h for h in helpers_used if h in official_helpers . keys ( ) ] :
if helper in custom_helpers :
continue
helper_req = official_helpers [ helper ]
if not validate_version_requirement ( helper_req ) :
major_diff = manifest_req [ 0 ] > int ( helper_req [ 0 ] )
2021-10-24 14:33:41 +02:00
message = (
" Using official helper %s implies requiring at least version %s , but manifest only requires %s "
% ( helper , helper_req , yunohost_version_req )
)
2021-12-04 23:54:46 +01:00
yield Error ( message ) if major_diff else Warning ( message )
2020-03-31 21:15:08 +02:00
2022-11-27 18:16:33 +01:00
@test ( )
def helpers_deprecated_in_v2 ( app ) :
if app_packaging_format < = 1 :
return
2023-09-07 23:11:03 +02:00
cmd = f " grep -IhEro ' ynh_ \\ w+ ' ' { app . path } /scripts/install ' ' { app . path } /scripts/remove ' ' { app . path } /scripts/upgrade ' ' { app . path } /scripts/backup ' ' { app . path } /scripts/restore ' || true "
2022-11-27 18:16:33 +01:00
helpers_used = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
helpers_used = sorted ( set ( helpers_used ) )
deprecated_helpers_in_v2_ = { k : v for k , v in deprecated_helpers_in_v2 }
for helper in [ h for h in helpers_used if h in deprecated_helpers_in_v2_ . keys ( ) ] :
yield Warning ( f " Using helper { helper } is deprecated when using packaging v2 ... It is replaced by: { deprecated_helpers_in_v2_ [ helper ] } " )
2020-10-08 14:25:11 +02:00
@test ( )
2020-10-08 16:03:52 +02:00
def helper_consistency_apt_deps ( app ) :
2020-10-08 14:25:11 +02:00
"""
2021-09-23 22:28:00 +02:00
Check if ynh_install_app_dependencies is present in install / upgrade / restore
2020-10-08 14:25:11 +02:00
so dependencies are up to date after restoration or upgrade
"""
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
install_script = app . scripts [ " install " ]
2020-10-08 16:03:52 +02:00
if install_script . contains ( " ynh_install_app_dependencies " ) :
for name in [ " upgrade " , " restore " ] :
2021-10-24 14:33:41 +02:00
if app . scripts [ name ] . exists and not app . scripts [ name ] . contains (
" ynh_install_app_dependencies "
) :
yield Warning (
" ynh_install_app_dependencies should also be in %s script "
% name
)
2020-10-08 14:25:11 +02:00
2021-10-24 14:33:41 +02:00
cmd = (
' grep -IhEr " install_extra_app_dependencies " %s /scripts | grep -v " key " | grep -q " http:// " '
% app . path
)
2021-10-10 13:38:52 +02:00
if os . system ( cmd ) == 0 :
2021-12-04 23:54:46 +01:00
yield Warning (
2021-10-24 14:33:41 +02:00
" When installing dependencies from extra repository, please include a `--key` argument (yes, even if it ' s official debian repos such as backports - because systems like Raspbian do not ship Debian ' s key by default! "
)
2021-10-10 13:38:52 +02:00
2020-10-08 16:03:52 +02:00
@test ( )
def helper_consistency_service_add ( app ) :
2020-12-01 01:19:58 +01:00
occurences = {
2021-10-24 14:33:41 +02:00
" install " : app . scripts [ " install " ] . occurences ( " yunohost service add " )
if app . scripts [ " install " ] . exists
else [ ] ,
" upgrade " : app . scripts [ " upgrade " ] . occurences ( " yunohost service add " )
if app . scripts [ " upgrade " ] . exists
else [ ] ,
" restore " : app . scripts [ " restore " ] . occurences ( " yunohost service add " )
if app . scripts [ " restore " ] . exists
else [ ] ,
2020-12-01 01:19:58 +01:00
}
2021-10-24 14:33:41 +02:00
occurences = {
k : [ sub_v . replace ( ' " $app " ' , " $app " ) for sub_v in v ]
for k , v in occurences . items ( )
}
2020-12-01 01:19:58 +01:00
2021-10-24 14:33:41 +02:00
all_occurences = (
occurences [ " install " ] + occurences [ " upgrade " ] + occurences [ " restore " ]
)
2020-12-01 01:19:58 +01:00
found_inconsistency = False
found_legacy_logtype_option = False
for cmd in all_occurences :
2021-10-24 14:33:41 +02:00
if any (
cmd not in occurences_list for occurences_list in occurences . values ( )
) :
2020-12-01 01:19:58 +01:00
found_inconsistency = True
if " --log_type systemd " in cmd :
found_legacy_logtype_option = True
if found_inconsistency :
2021-10-24 14:33:41 +02:00
details = [
(
" %s : " % script
+ " " . join (
" \n " + cmd
for cmd in occurences [ script ] or [ " ...None?... " ]
)
)
for script in occurences . keys ( )
]
details = " \n " . join ( details )
yield Warning (
" Some inconsistencies were found in the ' yunohost service add ' commands between install, upgrade and restore: \n %s "
% details
)
2020-12-01 01:19:58 +01:00
if found_legacy_logtype_option :
2021-10-24 14:33:41 +02:00
yield Warning (
" Using option ' --log_type systemd ' with ' yunohost service add ' is not relevant anymore "
)
2020-12-01 01:19:58 +01:00
2021-10-24 14:33:41 +02:00
if occurences [ " install " ] and not app . scripts [ " remove " ] . contains (
" yunohost service remove "
) :
2020-12-01 01:19:58 +01:00
yield Error (
" You used ' yunohost service add ' in the install script, "
" but not ' yunohost service remove ' in the remove script. "
)
2020-10-08 14:25:11 +02:00
2020-11-09 18:21:31 +01:00
@test ( )
2021-10-06 14:31:23 +02:00
def references_to_superold_stuff ( app ) :
2021-10-24 14:33:41 +02:00
if any (
script . contains ( " jessie " )
for script in app . scripts . values ( )
if script . exists
) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" The app still contains references to jessie, which could probably be cleaned up... "
)
if any (
script . contains ( " /etc/php5 " ) or script . contains ( " php5-fpm " )
for script in app . scripts . values ( )
if script . exists
) :
yield Error (
" This app still has references to php5 (from the jessie era!!) which tends to indicate that it ' s not up to date with recent packaging practices. "
)
if any (
script . contains ( " /etc/php/7.0 " ) or script . contains ( " php7.0-fpm " )
for script in app . scripts . values ( )
if script . exists
) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" This app still has references to php7.0 (from the stretch era!!) which tends to indicate that it ' s not up to date with recent packaging practices. "
)
2023-11-24 18:10:07 +01:00
if any (
script . contains ( " /etc/php/7.3 " ) or script . contains ( " php7.3-fpm " )
for script in app . scripts . values ( )
if script . exists
) :
yield Error (
" This app still has references to php7.3 (from the buster era!!) which tends to indicate that it ' s not up to date with recent packaging practices. "
)
2020-10-08 14:25:11 +02:00
2021-01-09 18:45:40 +01:00
@test ( )
def conf_json_persistent_tweaking ( self ) :
2021-10-24 14:33:41 +02:00
if (
os . system (
" grep -q -nr ' /etc/ssowat/conf.json.persistent ' %s /*/* 2>/dev/null "
% self . path
)
== 0
) :
2021-09-23 22:28:00 +02:00
yield Error ( " Don ' t do black magic with /etc/ssowat/conf.json.persistent! " )
2021-01-09 18:45:40 +01:00
2021-12-04 19:02:58 +01:00
@test ( )
def bad_final_path_location ( self ) :
if os . system ( f " grep -q -nr ' ynh_webpath_register ' { self . path } /scripts/install 2>/dev/null " ) == 0 \
and os . system ( f " grep -q -nr ' final_path=/opt ' { self . path } /scripts/install { self . path } /scripts/_common.sh 2>/dev/null " ) == 0 :
2021-12-04 19:19:32 +01:00
yield Info ( " Web applications are not supposed to be installed in /opt/ ... They are supposed to be installed in /var/www/$app :/ " )
2021-12-04 19:02:58 +01:00
2021-03-11 02:59:34 +01:00
@test ( )
def app_data_in_unofficial_dir ( self ) :
2021-10-24 14:33:41 +02:00
allowed_locations = [
" /home/yunohost.app " ,
" /home/yunohost.conf " ,
" /home/yunohost.backup " ,
" /home/yunohost.multimedia " ,
]
cmd = (
" grep -IhEro ' /home/yunohost[^/ ]*/|/home/ \\ $app ' %s /scripts || true "
% self . path
)
home_locations = (
subprocess . check_output ( cmd , shell = True ) . decode ( " utf-8 " ) . strip ( ) . split ( " \n " )
)
forbidden_locations = set (
[
location
for location in home_locations
if location and location . rstrip ( " / " ) not in allowed_locations
]
)
2021-03-11 02:59:34 +01:00
if forbidden_locations :
2021-10-24 14:33:41 +02:00
yield Warning (
" The app seems to be storing data in the ' forbidden ' locations %s . The recommended pratice is rather to store data in /home/yunohost.app/$app or /home/yunohost.multimedia (depending on the use case) "
% " , " . join ( forbidden_locations )
)
2021-01-09 18:45:40 +01:00
2020-11-11 17:08:49 +01:00
class Configurations ( TestSuite ) :
def __init__ ( self , app ) :
2019-03-02 01:43:29 +01:00
2020-11-11 17:08:49 +01:00
self . app = app
2021-02-03 18:30:26 +01:00
self . test_suite_name = " Configuration files "
2020-10-15 21:29:17 +02:00
2020-11-11 17:08:49 +01:00
############################
# _____ __ #
# / ____| / _| #
# | | ___ _ __ | |_ #
# | | / _ \| '_ \| _| #
# | |___| (_) | | | | | #
# \_____\___/|_| |_|_| #
# #
############################
2020-10-15 21:29:17 +02:00
2020-10-15 21:38:14 +02:00
@test ( )
2023-01-16 23:40:33 +01:00
def tests_toml_exists ( self ) :
2020-11-11 17:08:49 +01:00
app = self . app
2020-10-15 21:38:14 +02:00
2023-01-16 23:40:33 +01:00
if app_packaging_format < = 1 :
check_process_file = app . path + " /check_process "
if not file_exists ( check_process_file ) :
yield Warning (
" You should add a ' check_process ' file to properly interface with the continuous integration system "
)
else :
tests_toml_file = app . path + " /tests.toml "
if not file_exists ( tests_toml_file ) :
yield Error (
" The ' check_process ' file that interfaces with the app CI has now been replaced with ' tests.toml ' format and is now mandatory for apps v2. "
)
2020-10-15 21:38:14 +02:00
@test ( )
2020-11-11 17:08:49 +01:00
def check_process_syntax ( self ) :
app = self . app
2020-10-15 21:38:14 +02:00
check_process_file = app . path + " /check_process "
if not file_exists ( check_process_file ) :
return
2020-11-10 13:46:47 +01:00
if os . system ( " grep -q ' Level 5=1 ' ' %s ' " % check_process_file ) == 0 :
2020-10-15 21:38:14 +02:00
yield Error ( " Do not force Level 5=1 in check_process... " )
2023-09-07 23:11:03 +02:00
elif os . system ( " grep -qE ' *Level \\ d= ' ' %s ' " % check_process_file ) == 0 :
2023-11-24 18:10:07 +01:00
yield Error (
2021-10-24 14:33:41 +02:00
" Setting Level x=y in check_process is obsolete / not relevant anymore "
)
2019-03-02 01:43:29 +01:00
2020-11-03 19:03:01 +01:00
@test ( )
2020-11-11 17:08:49 +01:00
def check_process_consistency ( self ) :
app = self . app
2020-11-03 19:03:01 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
args = app . manifest [ " arguments " ] . get ( " install " , [ ] )
else :
2023-07-25 18:00:42 +02:00
keyandargs = copy . deepcopy ( app . manifest [ " install " ] )
2022-11-27 18:16:33 +01:00
for key , infos in keyandargs . items ( ) :
infos [ " name " ] = key
args = keyandargs . values ( )
2020-11-03 19:03:01 +01:00
check_process_file = app . path + " /check_process "
if not file_exists ( check_process_file ) :
return
2021-10-24 14:33:41 +02:00
has_is_public_arg = any (
2022-11-27 18:16:33 +01:00
a [ " name " ] == " is_public " for a in args
2021-10-24 14:33:41 +02:00
)
2020-11-03 19:03:01 +01:00
if has_is_public_arg :
2021-10-24 14:33:41 +02:00
if (
os . system ( r " grep -q ' ^ \ s*setup_public=1 ' ' %s ' " % check_process_file )
!= 0
) :
yield Info (
" It looks like you forgot to enable setup_public test in check_process? "
)
2020-11-03 19:03:01 +01:00
2021-10-24 14:33:41 +02:00
if (
os . system ( r " grep -q ' ^ \ s*setup_private=1 ' ' %s ' " % check_process_file )
!= 0
) :
yield Info (
" It looks like you forgot to enable setup_private test in check_process? "
)
2020-11-03 19:03:01 +01:00
2021-10-24 14:33:41 +02:00
has_path_arg = any (
2022-11-27 18:16:33 +01:00
a [ " name " ] == " path " for a in args
2021-10-24 14:33:41 +02:00
)
2020-11-03 19:03:01 +01:00
if has_path_arg :
2021-10-24 14:33:41 +02:00
if (
os . system ( r " grep -q ' ^ \ s*setup_sub_dir=1 ' ' %s ' " % check_process_file )
!= 0
) :
yield Info (
" It looks like you forgot to enable setup_sub_dir test in check_process? "
)
2020-11-03 19:03:01 +01:00
2023-09-07 23:11:03 +02:00
if app . manifest . get ( " multi_instance " ) in [ True , 1 , " True " , " true " ] or app . manifest . get ( " integration " , { } ) . get ( " multi_instance " ) is True :
2021-10-24 14:33:41 +02:00
if (
os . system ( r " grep -q ' ^ \ s*multi_instance=1 ' ' %s ' " % check_process_file )
!= 0
) :
yield Info (
" It looks like you forgot to enable multi_instance test in check_process? "
)
2020-11-03 19:03:01 +01:00
if app . scripts [ " backup " ] . exists :
2021-10-24 14:33:41 +02:00
if (
os . system ( r " grep -q ' ^ \ s*backup_restore=1 ' ' %s ' " % check_process_file )
!= 0
) :
yield Info (
" It looks like you forgot to enable backup_restore test in check_process? "
)
2020-11-03 19:03:01 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
2020-11-11 17:08:49 +01:00
def misc_legacy_phpini ( self ) :
app = self . app
2020-10-08 14:25:11 +02:00
if file_exists ( app . path + " /conf/php-fpm.ini " ) :
2020-10-08 16:04:17 +02:00
yield Error (
2019-03-02 02:06:43 +01:00
" Using a separate php-fpm.ini file is deprecated. "
" Please merge your php-fpm directives directly in the pool file. "
" (c.f. https://github.com/YunoHost-Apps/nextcloud_ynh/issues/138 ) "
)
2019-03-02 01:43:29 +01:00
2022-12-03 20:13:41 +01:00
@test ( )
def encourage_extra_php_conf ( self ) :
app = self . app
if file_exists ( app . path + " /conf/php-fpm.conf " ) :
yield Info (
" For the php configuration, consider getting rid of php-fpm.conf "
" and using the --usage and --footprint option of ynh_add_fpm_config. "
" This will use an auto-generated php conf file. "
" Additionally you can provide a conf/extra_php-fpm.conf for custom PHP settings "
" that will automatically be appended to the autogenerated conf. "
" (Feel free to discuss this on the chat with other people, the whole thing "
" with --usage/--footprint is legitimately a bit unclear ;)) "
)
2020-10-08 14:25:11 +02:00
@test ( )
2020-11-11 17:08:49 +01:00
def misc_source_management ( self ) :
app = self . app
2020-10-08 14:25:11 +02:00
source_dir = os . path . join ( app . path , " sources " )
2021-10-24 14:33:41 +02:00
if (
os . path . exists ( source_dir )
and len (
[
name
for name in os . listdir ( source_dir )
if os . path . isfile ( os . path . join ( source_dir , name ) )
]
)
> 5
) :
2020-10-08 16:04:17 +02:00
yield Error (
2020-10-08 14:25:11 +02:00
" Upstream app sources shouldn ' t be stored in this ' sources ' folder of this git repository as a copy/paste \n "
2020-04-08 01:50:29 +02:00
" During installation, the package should download sources from upstream via ' ynh_setup_source ' . \n "
" See the helper documentation. "
2021-09-23 22:28:00 +02:00
" Original discussion happened here: "
2020-04-08 01:50:29 +02:00
" https://github.com/YunoHost/issues/issues/201#issuecomment-391549262 "
)
2020-10-08 14:25:11 +02:00
2020-11-28 18:43:15 +01:00
@test ( )
def src_file_checksum_type ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2020-11-28 18:43:15 +01:00
if not filename . endswith ( " .src " ) :
continue
try :
content = open ( app . path + " /conf/ " + filename ) . read ( )
except Exception as e :
2021-09-23 22:28:00 +02:00
yield Warning ( " Can ' t open/read %s : %s " % ( filename , e ) )
2020-11-28 18:43:15 +01:00
return
if " SOURCE_SUM_PRG=md5sum " in content :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" %s : Using md5sum checksum is not so great for "
" security. Consider using sha256sum instead. " % filename
)
2020-11-28 18:43:15 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
2020-11-11 17:08:49 +01:00
def systemd_config_specific_user ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2020-11-11 17:08:49 +01:00
# Ignore subdirs or filename not containing nginx in the name
if not filename . endswith ( " .service " ) :
continue
2020-11-28 18:31:54 +01:00
# Some apps only provide an override conf file, which is different
# from the full/base service config (c.f. ffsync)
if " override " in filename :
continue
2020-11-11 17:13:01 +01:00
try :
content = open ( app . path + " /conf/ " + filename ) . read ( )
except Exception as e :
yield Warning ( " Can ' t open/read %s : %s " % ( filename , e ) )
return
2021-12-31 02:14:49 +01:00
if ' [Unit] ' not in content :
continue
if re . findall ( r " ^ *Type=oneshot " , content , flags = re . MULTILINE ) :
Level = Info
else :
Level = Warning
2023-05-04 23:47:49 +02:00
matches = re . findall ( r " ^ *(User)=( \ S+) " , content , flags = re . MULTILINE )
2020-11-11 17:08:49 +01:00
if not any ( match [ 0 ] == " User " for match in matches ) :
2021-12-31 02:14:49 +01:00
yield Level (
2021-10-24 14:33:41 +02:00
" You should specify a ' User= ' directive in the systemd config ! "
)
2020-11-11 17:08:49 +01:00
return
if any ( match [ 1 ] in [ " root " , " www-data " ] for match in matches ) :
2021-12-31 02:14:49 +01:00
yield Level (
2021-10-24 14:33:41 +02:00
" DO NOT run the app ' s systemd service as root or www-data! Use a dedicated system user for this app! If your app requires administrator priviledges, you should consider adding the user to the sudoers (and restrict the commands it can use!) "
)
2021-09-29 17:37:23 +02:00
@test ( )
def systemd_config_harden_security ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2021-09-29 17:37:23 +02:00
# Ignore subdirs or filename not containing nginx in the name
if not filename . endswith ( " .service " ) :
continue
2021-10-24 14:33:41 +02:00
if (
os . system (
f " grep -q ' ^ *CapabilityBoundingSet= ' ' { app . path } /conf/ { filename } ' "
)
!= 0
or os . system ( f " grep -q ' ^ *Protect.*= ' ' { app . path } /conf/ { filename } ' " )
!= 0
or os . system (
f " grep -q ' ^ *SystemCallFilter= ' ' { app . path } /conf/ { filename } ' "
)
!= 0
or os . system ( f " grep -q ' ^ *PrivateTmp= ' ' { app . path } /conf/ { filename } ' " )
!= 0
) :
2021-09-29 17:37:23 +02:00
2021-10-24 14:33:41 +02:00
yield Info (
2022-12-04 03:24:45 +01:00
f " You are encouraged to harden the security of the systemd configuration { filename } . You can have a look at https://github.com/YunoHost/example_ynh/blob/master/conf/systemd.service#L14-L46 for a baseline. "
2021-10-24 14:33:41 +02:00
)
2021-09-29 17:37:23 +02:00
2020-11-11 17:08:49 +01:00
@test ( )
def php_config_specific_user ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2020-11-11 17:08:49 +01:00
# Ignore subdirs or filename not containing nginx in the name
if not filename . startswith ( " php " ) or not filename . endswith ( " .conf " ) :
continue
2020-11-11 17:13:01 +01:00
try :
content = open ( app . path + " /conf/ " + filename ) . read ( )
except Exception as e :
yield Warning ( " Can ' t open/read %s : %s " % ( filename , e ) )
return
2021-10-24 14:33:41 +02:00
matches = re . findall (
r " ^ *(user|group) = ( \ S+) " , content , flags = re . MULTILINE
)
2020-11-11 17:08:49 +01:00
if not any ( match [ 0 ] == " user " for match in matches ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" You should at least specify a ' user = ' directive in your PHP conf file "
)
2020-11-11 17:08:49 +01:00
return
2023-08-31 16:50:26 +02:00
if any ( match [ 1 ] == " root " or match == ( " user " , " www-data " ) for match in matches ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" DO NOT run the app PHP worker as root or www-data! Use a dedicated system user for this app! "
)
2020-11-11 17:08:49 +01:00
2023-07-04 15:33:02 +02:00
@test ( )
def nginx_http_host ( self ) :
app = self . app
if os . path . exists ( app . path + " /conf/nginx.conf " ) :
content = open ( app . path + " /conf/nginx.conf " ) . read ( )
if " $http_host " in content :
yield Info ( " In nginx.conf : please don ' t use $http_host but $host instead. C.f. https://github.com/yandex/gixy/blob/master/docs/en/plugins/hostspoofing.md " )
2021-11-20 22:25:11 +01:00
@test ( )
def nginx_https_redirect ( self ) :
app = self . app
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
# Ignore subdirs or filename not containing nginx in the name
if (
not os . path . isfile ( app . path + " /conf/ " + filename )
or " nginx " not in filename
) :
continue
content = open ( app . path + " /conf/ " + filename ) . read ( )
if " if ($scheme = http) " in content and " rewrite ^ https " in content :
2023-07-04 15:12:10 +02:00
level = Info if app_packaging_format < = 1 else Warning
yield level (
2021-11-20 22:25:11 +01:00
" Since Yunohost 4.3, the http->https redirect is handled by the core, "
" therefore having an if ($scheme = http) { rewrite ^ https://... } block "
" in the nginx config file is deprecated. (This helps with supporting Yunohost-behind-reverse-proxy use case) "
)
2020-11-11 17:08:49 +01:00
@test ( )
def misc_nginx_add_header ( self ) :
app = self . app
2020-10-08 14:25:11 +02:00
2020-04-08 01:50:29 +02:00
#
2019-03-09 18:38:37 +01:00
# Analyze nginx conf
# - Deprecated usage of 'add_header' in nginx conf
# - Spot path traversal issue vulnerability
2019-03-02 01:43:29 +01:00
#
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2019-03-09 18:38:37 +01:00
# Ignore subdirs or filename not containing nginx in the name
2021-10-24 14:33:41 +02:00
if (
not os . path . isfile ( app . path + " /conf/ " + filename )
or " nginx " not in filename
) :
2019-03-02 01:43:29 +01:00
continue
2019-03-09 18:38:37 +01:00
2020-10-08 14:25:11 +02:00
content = open ( app . path + " /conf/ " + filename ) . read ( )
2019-03-02 01:43:29 +01:00
if " location " in content and " add_header " in content :
2020-10-08 16:04:17 +02:00
yield Error (
2020-12-05 15:37:23 +01:00
" Do not use ' add_header ' in the NGINX conf. Use ' more_set_headers ' instead. "
2019-03-02 02:06:43 +01:00
" (See https://www.peterbe.com/plog/be-very-careful-with-your-add_header-in-nginx "
" and https://github.com/openresty/headers-more-nginx-module#more_set_headers ) "
)
2019-03-02 01:43:29 +01:00
2020-11-16 18:11:26 +01:00
@test ( )
def misc_nginx_more_set_headers ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2020-11-16 18:11:26 +01:00
# Ignore subdirs or filename not containing nginx in the name
2021-10-24 14:33:41 +02:00
if (
not os . path . isfile ( app . path + " /conf/ " + filename )
or " nginx " not in filename
) :
2020-11-16 18:11:26 +01:00
continue
content = open ( app . path + " /conf/ " + filename ) . read ( )
if " location " in content and " more_set_headers " in content :
lines = content . split ( " \n " )
2023-09-07 23:11:03 +02:00
more_set_headers_lines = [ zzz for zzz in lines if " more_set_headers " in zzz ]
2021-10-24 14:33:41 +02:00
2020-11-16 18:11:26 +01:00
def right_syntax ( line ) :
2021-10-24 14:33:41 +02:00
return re . search (
r " more_set_headers [ \" \ ' ][ \ w-]+ \ s?: .*[ \" \ ' ]; " , line
)
2020-11-16 18:11:26 +01:00
if any ( not right_syntax ( line ) for line in more_set_headers_lines ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-09-23 22:28:00 +02:00
" It looks like the syntax for the ' more_set_headers ' "
2020-12-05 15:52:38 +01:00
" instruction is incorrect in the NGINX conf (N.B. "
2021-09-23 22:28:00 +02:00
" : it ' s different than the ' add_header ' syntax!)... "
2020-11-16 18:11:26 +01:00
" The syntax should look like: "
2021-10-24 14:33:41 +02:00
' more_set_headers " Header-Name: value " '
2020-11-16 18:11:26 +01:00
)
2020-10-08 14:25:11 +02:00
@test ( )
2020-11-11 17:08:49 +01:00
def misc_nginx_path_traversal ( self ) :
app = self . app
2021-10-24 14:33:41 +02:00
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
2020-10-08 14:25:11 +02:00
# Ignore subdirs or filename not containing nginx in the name
2021-10-24 14:33:41 +02:00
if (
not os . path . isfile ( app . path + " /conf/ " + filename )
or " nginx " not in filename
) :
2020-10-08 14:25:11 +02:00
continue
2019-03-09 18:38:37 +01:00
#
# Path traversal issues
#
2019-03-20 18:32:53 +01:00
def find_location_with_alias ( locationblock ) :
if locationblock [ 0 ] [ 0 ] != " location " :
return
location = locationblock [ 0 ] [ - 1 ]
for line in locationblock [ 1 ] :
instruction = line [ 0 ]
if instruction == " alias " :
2019-03-20 20:32:30 +01:00
yield ( location , line )
2021-10-24 14:33:41 +02:00
elif (
isinstance ( instruction , list )
and instruction
and instruction [ 0 ] == " location "
) :
2019-03-20 18:32:53 +01:00
yield from find_location_with_alias ( instruction )
else :
continue
def find_path_traversal_issue ( nginxconf ) :
for block in nginxconf :
2019-03-20 20:32:30 +01:00
for location , alias in find_location_with_alias ( block ) :
2020-10-08 16:06:19 +02:00
# Ignore locations which are regexes..?
if location . startswith ( " ^ " ) and location . endswith ( " $ " ) :
continue
2019-03-20 20:32:30 +01:00
alias_path = alias [ - 1 ]
2021-10-10 16:38:01 +02:00
# Ugly hack to ignore cases where aliasing to a specific file (e.g. favicon.ico or foobar.html)
2021-10-10 16:39:12 +02:00
if " . " in alias_path [ - 5 : ] :
2021-10-10 16:38:01 +02:00
continue
2020-12-05 15:37:23 +01:00
# For path traversal issues to occur, both of those are needed:
2019-03-20 21:59:04 +01:00
# - location /foo { (*without* a / after foo)
# - alias /var/www/foo/ (*with* a / after foo)
#
# Note that we also consider a positive the case where
# the alias folder (e.g. /var/www/foo/) does not ends
2020-12-05 15:37:23 +01:00
# with / if __FINALPATH__ ain't used... that probably
2019-03-20 21:59:04 +01:00
# means that the app is not using the standard nginx
# helper, and therefore it is likely to be replaced by
# something ending with / ...
2021-10-24 14:33:41 +02:00
if not location . strip ( " ' " ) . endswith ( " / " ) and (
alias_path . endswith ( " / " )
or " __FINALPATH__ " not in alias_path
) :
2019-03-20 18:32:53 +01:00
yield location
2020-03-31 19:17:54 +02:00
do_path_traversal_check = False
try :
import pyparsing , six
2021-10-24 14:33:41 +02:00
2020-03-31 19:17:54 +02:00
do_path_traversal_check = True
2023-09-07 23:11:03 +02:00
except Exception :
2020-03-31 19:17:54 +02:00
# If inside a venv, try to magically install pyparsing
2021-10-24 14:33:41 +02:00
if " VIRTUAL_ENV " in os . environ :
2020-03-31 19:17:54 +02:00
try :
2020-06-10 17:34:34 +02:00
_print ( " (Trying to auto install pyparsing...) " )
2021-10-24 14:33:41 +02:00
subprocess . check_output (
" pip3 install pyparsing six " , shell = True
)
2020-03-31 19:17:54 +02:00
import pyparsing
2021-10-24 14:33:41 +02:00
2021-09-23 22:28:00 +02:00
_print ( " OK! " )
2020-03-31 19:17:54 +02:00
do_path_traversal_check = True
except Exception as e :
2020-06-10 17:34:34 +02:00
_print ( " Failed :[ : %s " % str ( e ) )
2020-03-31 19:17:54 +02:00
if not do_path_traversal_check :
2021-10-24 14:33:41 +02:00
_print (
" N.B.: The package linter needs you to run ' pip3 install pyparsing six ' if you want it to be able to check for path traversal issue in NGINX confs "
)
2020-03-31 19:17:54 +02:00
if do_path_traversal_check :
from lib . nginxparser import nginxparser
2021-10-24 14:33:41 +02:00
2020-03-31 20:06:03 +02:00
try :
2020-10-08 14:25:11 +02:00
nginxconf = nginxparser . load ( open ( app . path + " /conf/ " + filename ) )
2020-03-31 20:06:03 +02:00
except Exception as e :
2020-12-05 15:37:23 +01:00
_print ( " Could not parse NGINX conf...: " + str ( e ) )
2020-03-31 20:06:03 +02:00
nginxconf = [ ]
2020-03-31 19:17:54 +02:00
for location in find_path_traversal_issue ( nginxconf ) :
2020-10-08 16:04:17 +02:00
yield Error (
2020-12-05 15:37:23 +01:00
" The NGINX configuration (especially location %s ) "
2020-03-31 19:17:54 +02:00
" appears vulnerable to path traversal issues as explained in \n "
" https://www.acunetix.com/vulnerabilities/web/path-traversal-via-misconfigured-nginx-alias/ \n "
2020-12-05 15:37:23 +01:00
" To fix it, look at the first lines of the NGINX conf of the example app : \n "
2021-10-24 14:33:41 +02:00
" https://github.com/YunoHost/example_ynh/blob/master/conf/nginx.conf "
% location
2020-03-31 19:17:54 +02:00
)
2019-03-09 18:38:37 +01:00
2024-01-17 23:23:06 +01:00
@test ( )
def bind_public_ip ( self ) :
app = self . app
for filename in (
os . listdir ( app . path + " /conf " ) if os . path . exists ( app . path + " /conf " ) else [ ]
) :
try :
content = open ( app . path + " /conf/ " + filename ) . read ( )
except Exception as e :
yield Warning ( " Can ' t open/read %s : %s " % ( filename , e ) )
return
2024-01-17 23:37:25 +01:00
for number , line in enumerate ( content . split ( " \n " ) , 1 ) :
comment = ( " # " , " // " , " ; " )
2024-01-17 23:23:06 +01:00
if (
2024-01-17 23:37:25 +01:00
( " 0.0.0.0 " in line or " :: " in line )
2024-01-17 23:23:06 +01:00
and not line . strip ( ) . startswith ( comment )
) :
2024-01-18 03:06:27 +01:00
for ip in re . split ( " [ \t ,= ' \" () {} \ [ \ ]] " , line ) :
if ip == " :: " or " 0.0.0.0 " in ip :
yield Info (
f " { filename } : { number } : Binding to ' 0.0.0.0 ' or ' :: ' can result "
" in a security issue as the reverse proxy and the SSO can be "
" bypassed by knowing a public IP (typically an IPv6) and the "
" app port. lease be sure that this behavior is intentional. "
" Maybe use ' 127.0.0.1 ' or ' ::1 ' instead. "
)
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
#############################################
# __ __ _ __ _ #
# | \/ | (_)/ _| | | #
# | \ / | __ _ _ __ _| |_ ___ ___| |_ #
# | |\/| |/ _` | '_ \| | _/ _ \/ __| __| #
# | | | | (_| | | | | | || __/\__ \ |_ #
# |_| |_|\__,_|_| |_|_|_| \___||___/\__| #
# #
#############################################
2019-03-02 02:24:13 +01:00
2021-10-24 14:33:41 +02:00
class Manifest ( TestSuite ) :
2020-10-08 14:25:11 +02:00
def __init__ ( self , path ) :
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
self . path = path
2022-11-27 18:16:33 +01:00
self . test_suite_name = " manifest "
global app_packaging_format
manifest_path = os . path . join ( path , " manifest.toml " )
if os . path . exists ( manifest_path ) :
app_packaging_format = 2
else :
app_packaging_format = 1
manifest_path = os . path . join ( path , " manifest.json " )
assert os . path . exists ( manifest_path ) , " manifest.json is missing "
2020-10-08 14:25:11 +02:00
# Taken from https://stackoverflow.com/a/49518779
def check_for_duplicate_keys ( ordered_pairs ) :
dict_out = { }
for key , val in ordered_pairs :
if key in dict_out :
raise Exception ( " Duplicated key ' %s ' in %s " % ( key , ordered_pairs ) )
else :
dict_out [ key ] = val
return dict_out
2022-11-27 18:16:33 +01:00
self . raw_manifest = open ( manifest_path , encoding = " utf-8 " ) . read ( )
2020-10-08 14:25:11 +02:00
try :
2022-11-27 18:16:33 +01:00
if app_packaging_format == 1 :
self . manifest = json . loads (
self . raw_manifest , object_pairs_hook = check_for_duplicate_keys
)
else :
self . manifest = toml . loads ( self . raw_manifest )
2020-10-08 14:25:11 +02:00
except Exception as e :
2021-10-24 14:33:41 +02:00
print (
c . FAIL
+ " ✘ Looks like there ' s a syntax issue in your manifest? \n ---> %s " % e
)
2020-11-10 14:08:16 +01:00
sys . exit ( 1 )
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def mandatory_fields ( self ) :
2016-12-18 02:37:07 +01:00
2022-11-27 18:16:33 +01:00
base_fields = [
2021-10-24 14:33:41 +02:00
" packaging_format " ,
2022-11-27 18:16:33 +01:00
" id " ,
" name " ,
2021-10-24 14:33:41 +02:00
" description " ,
" version " ,
]
2016-12-18 02:37:07 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format == 1 :
fields = base_fields + [
" maintainer " ,
" requirements " ,
" multi_instance " ,
" services " ,
" arguments " ,
]
else :
fields = base_fields + [
" maintainers " ,
" upstream " ,
" integration " ,
" install " ,
" resources " ,
]
2019-03-02 01:43:29 +01:00
2021-10-24 14:33:41 +02:00
missing_fields = [
field for field in fields if field not in self . manifest . keys ( )
]
2020-10-08 15:01:36 +02:00
if missing_fields :
2022-11-27 18:16:33 +01:00
yield Critical (
2021-10-24 14:33:41 +02:00
" The following mandatory fields are missing: %s " % missing_fields
)
2020-10-08 15:01:36 +02:00
2022-11-27 18:16:33 +01:00
if app_packaging_format == 1 :
fields = ( " license " , " url " )
missing_fields = [
field for field in fields if field not in self . manifest . keys ( )
]
if missing_fields :
yield Error (
" The following mandatory fields are missing: %s " % missing_fields
)
else :
if " license " not in self . manifest . get ( " upstream " ) :
yield Error (
" The license key in the upstream section is missing "
)
2021-05-10 20:17:58 +02:00
@test ( )
def upstream_fields ( self ) :
if " upstream " not in self . manifest . keys ( ) :
2023-11-24 18:10:07 +01:00
yield Warning (
2021-10-24 14:33:41 +02:00
""" READMEs are to be automatically generated using https://github.com/YunoHost/apps/tree/master/tools/README-generator.
- You are encouraged to add an ' upstream ' section in the manifest , filled with the website , demo , repo , license of the upstream app , as shown here : https : / / github . com / YunoHost / example_ynh / blob / 7 b72b7334964b504e8c901637c73ce908204d38b / manifest . json #L11-L18 . (Not all infos are mandatory, you can remove irrelevant entries)"""
)
2021-05-10 20:17:58 +02:00
2021-08-07 16:56:07 +02:00
@test ( )
2022-08-15 18:37:42 +02:00
def upstream_placeholders ( self ) :
2021-08-07 16:56:07 +02:00
if " upstream " in self . manifest . keys ( ) :
2021-10-24 14:33:41 +02:00
if " yunohost.org " in self . manifest [ " upstream " ] . get ( " admindoc " , " " ) :
2022-12-03 20:13:41 +01:00
yield Error (
2021-10-24 14:33:41 +02:00
" The field ' admindoc ' should point to the **official** admin doc, not the YunoHost documentation. If there ' s no official admin doc, simply remove the admindoc key entirely. "
)
2022-12-19 00:20:38 +01:00
if " github.com " in self . manifest [ " upstream " ] . get ( " website " , " " ) :
yield Warning (
" The field ' website ' is not meant to point to a code repository ... this is to be handled by the ' code ' key ... If the app has no proper website, just remove the ' website ' key entirely "
)
2021-10-24 14:33:41 +02:00
if " yunohost.org " in self . manifest [ " upstream " ] . get ( " userdoc " , " " ) :
2022-08-15 18:37:42 +02:00
yield Warning (
2021-10-24 14:33:41 +02:00
" The field ' userdoc ' should point to the **official** user doc, not the YunoHost documentation. (The default auto-generated README already includes a link to the yunohost doc page for this app). If there ' s no official user doc, simply remove the userdoc key entirely. "
)
2022-08-27 15:48:34 +02:00
if " example.com " in self . manifest [ " upstream " ] . get ( " demo " , " " ) or " example.com " in self . manifest [ " upstream " ] . get ( " website " , " " ) :
2022-12-03 20:13:41 +01:00
yield Error ( " It seems like the upstream section still contains placeholder values such as ' example.com ' ... " )
2022-08-15 18:37:42 +02:00
2022-11-27 18:16:33 +01:00
@test ( )
def FIXMEs ( self ) :
if " FIXME " in self . raw_manifest :
yield Warning ( " There are still some FIXMEs remaining in the manifest " )
2021-05-10 20:17:58 +02:00
2020-10-08 14:25:11 +02:00
@test ( )
def yunohost_version_requirement ( self ) :
2016-12-18 02:37:07 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format > = 2 :
return
2020-10-08 14:25:11 +02:00
if not self . manifest . get ( " requirements " , { } ) . get ( " yunohost " , " " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" You should add a YunoHost version requirement in the manifest "
)
2019-03-02 01:43:29 +01:00
2020-11-03 17:45:17 +01:00
@test ( )
def yunohost_version_requirement_superold ( app ) :
2022-11-27 18:16:33 +01:00
if app_packaging_format > = 2 :
yunohost_version_req = (
app . manifest . get ( " integration " , { } ) . get ( " yunohost " , " " ) . strip ( " >= " )
)
else :
yunohost_version_req = (
app . manifest . get ( " requirements " , { } ) . get ( " yunohost " , " " ) . strip ( " >= " )
)
2022-08-13 17:36:18 +02:00
if yunohost_version_req . startswith ( " 2. " ) or yunohost_version_req . startswith ( " 3. " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
2022-08-13 17:36:18 +02:00
" Your app only requires YunoHost >= 2.x or 3.x, which tends to indicate that it may not be up to date with recommended packaging practices and helpers. "
2021-10-24 14:33:41 +02:00
)
2023-11-24 18:10:07 +01:00
elif yunohost_version_req . startswith ( " 4.0 " ) or yunohost_version_req . startswith ( " 4.1 " ) or yunohost_version_req . startswith ( " 4.2 " ) :
yield Critical (
" Your app only requires yunohost >= 4.0, 4.1 or 4.2, which tends to indicate that it may not be up to date with recommended packaging practices and helpers. "
2021-10-24 14:33:41 +02:00
)
2023-11-24 18:10:07 +01:00
#elif yunohost_version_req.startswith("4.3"):
# yield Warning(
# "Your app only requires yunohost >= 4.3, which tends to indicate that it may not be up to date with recommended packaging practices and helpers."
# )
2020-11-03 17:45:17 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def basic_fields_format ( self ) :
2022-11-27 18:16:33 +01:00
if self . manifest . get ( " packaging_format " ) != app_packaging_format :
yield Error ( f " packaging_format should be { app_packaging_format } " )
2021-10-24 14:33:41 +02:00
if not re . match ( " ^[a-z0-9]((_|-)?[a-z0-9])+$ " , self . manifest . get ( " id " ) ) :
2020-10-08 14:25:11 +02:00
yield Error ( " The app id is not a valid app id " )
2022-12-03 20:13:41 +01:00
elif self . manifest . get ( " id " ) . endswith ( " _ynh " ) :
yield Warning ( " The app id is not supposed to end with _ynh :| ... " )
2020-10-08 14:25:11 +02:00
if len ( self . manifest [ " name " ] ) > 22 :
2022-08-29 22:27:19 +02:00
yield Error ( " The app name is too long " )
2020-10-08 14:25:11 +02:00
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
if self . manifest . get ( " url " , " " ) . endswith ( " _ynh " ) :
yield Info (
" ' url ' is not meant to be the URL of the YunoHost package, "
" but rather the website or repo of the upstream app itself... "
)
if self . manifest [ " multi_instance " ] not in [ True , False , 0 , 1 ] :
yield Error (
" \" multi_instance \" field should be boolean ' true ' or ' false ' , and not string type "
)
return
keys = {
" yunohost " : ( lambda v : isinstance ( v , str ) and re . fullmatch ( r " ^>= \ s*[ \ d \ .]+ \ d$ " , v ) , " Expected something like ' >= 4.5.6 ' " ) ,
" architectures " : ( lambda v : v == " all " or ( isinstance ( v , list ) and all ( subv in [ " i386 " , " amd64 " , " armhf " , " arm64 " ] for subv in v ) ) , " ' all ' or a list of values in [ ' i386 ' , ' amd64 ' , ' armhf ' , ' arm64 ' ] " ) ,
" multi_instance " : ( lambda v : isinstance ( v , bool ) , " Expected a boolean (true or false, no quotes!) " ) ,
" ldap " : ( lambda v : isinstance ( v , bool ) or v == " not_relevant " , " Expected a boolean (true or false, no quotes!) or ' not_relevant ' " ) ,
" sso " : ( lambda v : isinstance ( v , bool ) or v == " not_relevant " , " Expected a boolean (true or false, no quotes!) or ' not_relevant ' " ) ,
" disk " : ( lambda v : isinstance ( v , str ) , " Expected a string " ) ,
" ram " : ( lambda v : isinstance ( v , dict ) and isinstance ( v . get ( " build " ) , str ) and isinstance ( v . get ( " runtime " ) , str ) , " Expected to find ram.build and ram.runtime with string values " ) ,
}
for key , validator in keys . items ( ) :
if key not in self . manifest . get ( " integration " , { } ) :
yield Error ( f " Missing key in the integration section: { key } " )
2023-06-10 23:50:05 +02:00
continue
2022-11-27 18:16:33 +01:00
value = self . manifest [ " integration " ] [ key ]
if not validator [ 0 ] ( value ) :
yield Error ( f " Error found with key { key } in the ' integration ' section: { validator [ 1 ] } , got: { value } " )
if not self . manifest . get ( " upstream " , { } ) . get ( " license " ) :
yield Error ( " Missing ' license ' key in the upstream section " )
2020-10-08 14:25:11 +02:00
@test ( )
def license ( self ) :
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
if " license " not in self . manifest :
return
2020-10-08 14:25:11 +02:00
2022-11-27 18:16:33 +01:00
if " upstream " in self . manifest and " license " in self . manifest [ " upstream " ] and self . manifest [ " upstream " ] [ " license " ] != self . manifest [ " license " ] :
yield Warning ( " The content of ' license ' in the ' upstream ' block should be the same as ' license ' (yes sorry, this is duplicate info, this is transitional for the manifest v2 ...) " )
# Turns out there may be multiple licenses... (c.f. Seafile)
licenses = self . manifest [ " license " ] . split ( " , " )
else :
# Turns out there may be multiple licenses... (c.f. Seafile)
licenses = self . manifest . get ( " upstream " , { } ) . get ( " license " , " " ) . split ( " , " )
2019-03-02 01:43:29 +01:00
2020-11-21 02:34:01 +01:00
for license in licenses :
2019-03-02 01:43:29 +01:00
2020-11-21 02:34:01 +01:00
license = license . strip ( )
2019-04-25 23:14:45 +02:00
2020-11-21 02:34:01 +01:00
if " nonfree " in license . replace ( " - " , " " ) :
2021-10-24 14:33:41 +02:00
yield Warning (
" ' non-free ' apps cannot be integrated in YunoHost ' s app catalog. "
)
2020-11-21 02:34:01 +01:00
return
2021-10-24 14:33:41 +02:00
code_license = ' <code property= " spdx:licenseId " > ' + license + " </code> "
2020-11-21 02:34:01 +01:00
if code_license not in spdx_licenses ( ) :
yield Warning (
2021-10-24 14:33:41 +02:00
" The license id ' %s ' is not registered in https://spdx.org/licenses/. "
% license
2020-11-21 02:34:01 +01:00
)
return
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def description ( self ) :
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
descr = self . manifest . get ( " description " , " " )
id = self . manifest [ " id " ] . lower ( )
name = self . manifest [ " name " ] . lower ( )
2019-03-02 01:43:29 +01:00
2020-10-08 14:25:11 +02:00
if isinstance ( descr , dict ) :
descr = descr . get ( " en " , " " )
if len ( descr ) < 5 or len ( descr ) > 150 :
2021-10-24 14:33:41 +02:00
yield Warning (
" The description of your app is either missing, too short or too long... Please describe in *consise* terms what the app is/does. "
)
2020-10-08 14:25:11 +02:00
if " for yunohost " in descr . lower ( ) :
2022-12-03 20:13:41 +01:00
yield Error (
2020-10-08 14:25:11 +02:00
" The ' description ' should explain what the app actually does. "
" No need to say that it is ' for YunoHost ' - this is a YunoHost app "
" so of course we know it is for YunoHost ;-). "
)
if descr . lower ( ) . startswith ( id ) or descr . lower ( ) . startswith ( name ) :
yield Warning (
" Try to avoid starting the description by ' $app is ' "
2020-12-05 15:37:23 +01:00
" ... explain what the app is/does directly! "
2020-10-08 14:25:11 +02:00
)
@test ( )
def version_format ( self ) :
2021-10-28 15:24:32 +02:00
if not re . match ( r " ^ " + VERSION_PATTERN + r " ~ynh[0-9]+$ " , self . manifest . get ( " version " , " " ) , re . VERBOSE ) :
2020-10-08 16:04:17 +02:00
yield Error (
2020-10-08 14:25:11 +02:00
" The ' version ' field should match the format <upstreamversion>~ynh<packageversion>. "
2020-12-05 15:37:23 +01:00
" For example: 4.3-2~ynh3. It is composed of the upstream version number (in the "
2020-10-08 14:25:11 +02:00
" example, 4.3-2) and an incremental number for each change in the package without "
" upstream change (in the example, 3). This incremental number can be reset to 1 "
" each time the upstream version changes. "
)
@test ( )
def install_args ( self ) :
2021-10-24 14:33:41 +02:00
recognized_types = (
2022-11-27 18:16:33 +01:00
" string " ,
" text " ,
" select " ,
" tags " ,
" email " ,
" url " ,
" date " ,
" time " ,
" color " ,
" password " ,
2021-10-24 14:33:41 +02:00
" path " ,
" boolean " ,
2022-11-27 18:16:33 +01:00
" domain " ,
2021-10-24 14:33:41 +02:00
" user " ,
2022-11-27 18:16:33 +01:00
" group " ,
" number " ,
" range " ,
2021-10-24 14:33:41 +02:00
" display_text " ,
2022-11-27 18:16:33 +01:00
" alert " ,
" markdown " ,
" file " ,
" app " ,
2021-10-24 14:33:41 +02:00
)
2019-03-09 20:26:30 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
args = self . manifest [ " arguments " ] . get ( " install " , [ ] )
else :
2023-07-25 18:00:42 +02:00
keyandargs = copy . deepcopy ( self . manifest [ " install " ] )
2022-11-27 18:16:33 +01:00
for key , infos in keyandargs . items ( ) :
infos [ " name " ] = key
args = keyandargs . values ( )
for argument in args :
2020-10-08 14:25:11 +02:00
if not isinstance ( argument . get ( " optional " , False ) , bool ) :
yield Warning (
2021-10-24 14:33:41 +02:00
" The key ' optional ' value for setting %s should be a boolean (true or false) "
% argument [ " name " ]
2020-10-08 14:25:11 +02:00
)
if " type " not in argument . keys ( ) :
yield Warning (
" You should specify the type of the argument ' %s ' . "
2021-10-24 14:33:41 +02:00
" You can use: %s . " % ( argument [ " name " ] , " , " . join ( recognized_types ) )
2020-10-08 14:25:11 +02:00
)
elif argument [ " type " ] not in recognized_types :
yield Warning (
" The type ' %s ' for argument ' %s ' is not recognized... "
2021-10-24 14:33:41 +02:00
" it probably doesn ' t behave as you expect? Choose among those instead: %s "
% ( argument [ " type " ] , argument [ " name " ] , " , " . join ( recognized_types ) )
2020-10-08 14:25:11 +02:00
)
2022-11-27 18:16:33 +01:00
elif argument [ " type " ] == " display_text " :
yield Info (
" Question type ' display_text ' is deprecated. You might want to use ' markdown ' or ' alert ' instead. "
)
2021-10-24 14:33:41 +02:00
elif argument [ " type " ] == " boolean " and argument . get (
" default " , True
) not in [ True , False ] :
2020-11-25 02:11:26 +01:00
yield Warning (
" Default value for boolean-type arguments should be a boolean... (in particular, make sure it ' s not a string!) "
)
2021-09-16 20:16:16 +02:00
elif argument [ " type " ] in [ " domain " , " user " , " password " ] :
if argument . get ( " default " ) :
2021-10-24 14:33:41 +02:00
yield Info (
" Default value for argument %s is superfluous, will be ignored "
% argument [ " name " ]
)
2021-09-16 20:16:16 +02:00
if argument . get ( " example " ) :
2021-10-24 14:33:41 +02:00
yield Info (
" Example value for argument %s is superfluous, will be ignored "
% argument [ " name " ]
)
2016-01-25 12:52:18 +01:00
2020-10-08 14:25:11 +02:00
if " choices " in argument . keys ( ) :
choices = [ c . lower ( ) for c in argument [ " choices " ] ]
if len ( choices ) == 2 :
2021-10-24 14:33:41 +02:00
if ( " true " in choices and " false " in choices ) or (
" yes " in choices and " no " in choices
) :
2020-10-08 14:25:11 +02:00
yield Warning (
" Argument %s : you might want to simply use a boolean-type argument. "
2021-10-24 14:33:41 +02:00
" No need to specify the choices list yourself. "
% argument [ " name " ]
2020-10-08 14:25:11 +02:00
)
2021-03-15 02:26:41 +01:00
@test ( )
2021-09-29 15:22:43 +02:00
def obsolete_or_missing_ask_strings ( self ) :
2021-03-15 02:26:41 +01:00
2021-10-24 14:33:41 +02:00
ask_string_managed_by_the_core = [
( " domain " , " domain " ) ,
( " path " , " path " ) ,
( " admin " , " user " ) ,
( " is_public " , " boolean " ) ,
( " password " , " password " ) ,
2022-11-27 18:16:33 +01:00
( " init_main_permission " , " group " ) ,
2021-10-24 14:33:41 +02:00
]
2021-03-15 02:26:41 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format < = 1 :
args = self . manifest [ " arguments " ] . get ( " install " , [ ] )
else :
2023-07-25 18:00:42 +02:00
keyandargs = copy . deepcopy ( self . manifest [ " install " ] )
2022-11-27 18:16:33 +01:00
for key , infos in keyandargs . items ( ) :
infos [ " name " ] = key
args = keyandargs . values ( )
for argument in args :
2021-03-15 02:26:41 +01:00
2021-10-24 14:33:41 +02:00
if (
argument . get ( " ask " )
and ( argument . get ( " name " ) , argument . get ( " type " ) )
in ask_string_managed_by_the_core
) :
2022-08-29 22:27:19 +02:00
yield Warning (
2021-10-24 14:33:41 +02:00
" ' ask ' string for argument %s is superfluous / will be ignored. Since 4.1, the core handles the ' ask ' string for some recurring arg name/type for consistency and easier i18n. See https://github.com/YunoHost/example_ynh/pull/142 "
% argument . get ( " name " )
)
2021-03-15 02:26:41 +01:00
2021-10-24 14:33:41 +02:00
elif (
not argument . get ( " ask " )
and ( argument . get ( " name " ) , argument . get ( " type " ) )
not in ask_string_managed_by_the_core
) :
yield Warning (
" You should add ' ask ' strings for argument %s "
% argument . get ( " name " )
)
2021-03-15 02:26:41 +01:00
2023-02-20 18:02:27 +01:00
@test ( )
def resource_consistency ( self ) :
if app_packaging_format < = 1 :
return
resources = self . manifest [ " resources " ]
if " database " in list ( resources . keys ( ) ) :
if " apt " not in list ( resources . keys ( ) ) :
yield Warning (
" Having an ' apt ' resource is mandatory when using a ' database ' resource, to also install postgresql/mysql if needed "
)
else :
if list ( resources . keys ( ) ) . index ( " database " ) < list (
resources . keys ( )
) . index ( " apt " ) :
yield Warning (
" The ' apt ' resource should be placed before the ' database ' resource, to install postgresql/mysql if needed *before* provisioning the database "
)
dbtype = resources [ " database " ] [ " type " ]
2023-09-06 13:53:54 +02:00
apt_packages = resources [ " apt " ] . get ( " packages " , [ ] )
if isinstance ( apt_packages , str ) :
2023-09-06 22:47:09 +02:00
apt_packages = [ value . strip ( ) for value in re . split ( ' |, ' , apt_packages ) ]
2023-09-06 13:53:54 +02:00
2023-02-20 18:02:27 +01:00
if dbtype == " mysql " and " mariadb-server " not in apt_packages :
yield Warning (
" When using a mysql database, you should add mariadb-server in apt dependencies. Even though it ' s currently installed by default in YunoHost installations, it might not be in the future ! "
)
if dbtype == " postgresql " and " postgresql " not in apt_packages :
yield Warning (
" When using a postgresql database, you should add postgresql in apt dependencies. "
)
2023-08-18 19:15:49 +02:00
main_perm = self . manifest [ " resources " ] . get ( ' permissions ' , { } ) . get ( ' main ' , { } )
if (
isinstance ( main_perm . get ( ' url ' ) , str )
and " init_main_permission " not in self . manifest [ ' install ' ]
and not main_perm . get ( ' allowed ' )
) :
yield Warning (
" You should add a ' init_main_permission ' question, or define `allowed` for main permission to have the app ready to be accessed right after installation. "
)
2023-07-25 18:00:42 +02:00
@test ( )
def manifest_schema ( self ) :
v = jsonschema . Draft7Validator ( manifest_v2_schema ( ) )
if app_packaging_format < = 1 :
return
for error in v . iter_errors ( self . manifest ) :
2024-01-17 03:48:26 +01:00
try :
error_path = ' > ' . join ( error . path )
except :
error_path = str ( error . path )
yield Info ( " Error validating manifest using schema: in key " + error_path + " \n " + error . message )
2023-07-25 18:00:42 +02:00
2023-02-20 18:02:27 +01:00
2020-11-17 01:13:05 +01:00
########################################
# _____ _ _ #
# / __ \ | | | | #
# | / \/ __ _| |_ __ _| | ___ __ _ #
# | | / _` | __/ _` | |/ _ \ / _` | #
# | \__/\ (_| | || (_| | | (_) | (_| | #
# \____/\__,_|\__\__,_|_|\___/ \__, | #
# __/ | #
# |___/ #
# #
########################################
class AppCatalog ( TestSuite ) :
def __init__ ( self , app_id ) :
self . app_id = app_id
2021-02-03 18:30:26 +01:00
self . test_suite_name = " Catalog infos "
2020-11-17 01:13:05 +01:00
self . _fetch_app_repo ( )
try :
2023-01-20 17:56:57 +01:00
self . app_list = toml . loads ( open ( " ./.apps/apps.toml " ) . read ( ) )
2020-11-17 01:13:05 +01:00
except Exception :
2023-01-20 17:56:57 +01:00
_print ( " Failed to read apps.toml :/ " )
2020-11-17 01:13:05 +01:00
sys . exit ( - 1 )
self . catalog_infos = self . app_list . get ( app_id , { } )
def _fetch_app_repo ( self ) :
flagfile = " ./.apps_git_clone_cache "
2021-10-24 14:33:41 +02:00
if (
os . path . exists ( " ./.apps " )
and os . path . exists ( flagfile )
and time . time ( ) - os . path . getmtime ( flagfile ) < 3600
) :
2020-11-17 01:13:05 +01:00
return
if not os . path . exists ( " ./.apps " ) :
2021-10-24 14:33:41 +02:00
subprocess . check_call (
[
" git " ,
" clone " ,
" https://github.com/YunoHost/apps " ,
" ./.apps " ,
" --quiet " ,
]
)
2020-11-17 01:13:05 +01:00
else :
subprocess . check_call ( [ " git " , " -C " , " ./.apps " , " fetch " , " --quiet " ] )
2021-10-24 14:33:41 +02:00
subprocess . check_call (
[ " git " , " -C " , " ./.apps " , " reset " , " origin/master " , " --hard " , " --quiet " ]
)
2020-11-17 01:13:05 +01:00
open ( flagfile , " w " ) . write ( " " )
@test ( )
def is_in_catalog ( self ) :
if not self . catalog_infos :
2020-11-21 00:33:09 +01:00
yield Critical ( " This app is not in YunoHost ' s application catalog " )
2020-11-17 01:13:05 +01:00
@test ( )
def revision_is_HEAD ( self ) :
if self . catalog_infos and self . catalog_infos . get ( " revision " , " HEAD " ) != " HEAD " :
2021-10-24 14:33:41 +02:00
yield Error (
" You should make sure that the revision used in YunoHost ' s apps catalog is HEAD... "
)
2020-11-17 01:13:05 +01:00
@test ( )
def state_is_working ( self ) :
2021-10-24 14:33:41 +02:00
if (
self . catalog_infos
and self . catalog_infos . get ( " state " , " working " ) != " working "
) :
yield Error (
" The application is not flagged as working in YunoHost ' s apps catalog "
)
2020-11-17 01:13:05 +01:00
@test ( )
def has_category ( self ) :
if self . catalog_infos and not self . catalog_infos . get ( " category " ) :
2021-10-24 14:33:41 +02:00
yield Warning (
" The application has no associated category in YunoHost ' s apps catalog "
)
2020-11-17 01:13:05 +01:00
@test ( )
def is_in_github_org ( self ) :
repo_org = " https://github.com/YunoHost-Apps/ %s _ynh " % ( self . app_id )
repo_brique = " https://github.com/labriqueinternet/ %s _ynh " % ( self . app_id )
if self . catalog_infos :
repo_url = self . catalog_infos [ " url " ]
if repo_url . lower ( ) not in [ repo_org . lower ( ) , repo_brique . lower ( ) ] :
if repo_url . lower ( ) . startswith ( " https://github.com/YunoHost-Apps/ " ) :
2021-10-24 14:33:41 +02:00
yield Warning (
" The URL for this app in the catalog should be %s " % repo_org
)
2020-11-17 01:13:05 +01:00
else :
2022-03-21 13:25:10 +01:00
yield Info (
2021-10-24 14:33:41 +02:00
" Consider adding your app to the YunoHost-Apps organization to allow the community to contribute more easily "
)
2020-11-17 01:13:05 +01:00
else :
2021-10-24 14:33:41 +02:00
2020-11-17 01:13:05 +01:00
def is_in_github_org ( ) :
2021-10-24 14:33:41 +02:00
return urlopen ( repo_org ) [ " code " ] != 404
2020-11-17 01:13:05 +01:00
def is_in_brique_org ( ) :
2021-10-24 14:33:41 +02:00
return urlopen ( repo_brique ) [ " code " ] != 404
2020-11-17 01:13:05 +01:00
if not is_in_github_org ( ) and not is_in_brique_org ( ) :
2022-03-21 13:25:10 +01:00
yield Info (
2021-10-24 14:33:41 +02:00
" Consider adding your app to the YunoHost-Apps organization to allow the community to contribute more easily "
)
2020-11-17 01:13:05 +01:00
@test ( )
def is_long_term_good_quality ( self ) :
#
# This analyzes the (git) history of apps.json in the past year and
2021-09-23 22:28:00 +02:00
# compute a score according to the time when the app was
2020-11-17 01:13:05 +01:00
# known + flagged working + level >= 5
#
def git ( cmd ) :
2021-10-24 14:33:41 +02:00
return (
subprocess . check_output ( [ " git " , " -C " , " ./.apps " ] + cmd )
. decode ( " utf-8 " )
. strip ( )
)
2020-11-17 01:13:05 +01:00
def _time_points_until_today ( ) :
# Prior to April 4th, 2019, we still had official.json and community.json
# Nowadays we only have apps.json
year = 2019
month = 6
day = 1
today = datetime . today ( )
date = datetime ( year , month , day )
while date < today :
yield date
day + = 14
if day > 15 :
day = 1
month + = 1
if month > 12 :
month = 1
year + = 1
date = datetime ( year , month , day )
def get_history ( N ) :
2023-09-07 23:11:03 +02:00
for t in list ( _time_points_until_today ( ) ) [ ( - 1 * N ) : ] :
2020-11-17 01:13:05 +01:00
# Fetch apps.json content at this date
2021-10-24 14:33:41 +02:00
commit = git (
[
" rev-list " ,
" -1 " ,
" --before= ' %s ' " % t . strftime ( " % b %d % Y " ) ,
" master " ,
]
)
2023-02-15 19:20:58 +01:00
if os . system ( f " git -C ./.apps cat-file -e { commit } :apps.json 2>/dev/null " ) == 0 :
2023-01-20 17:56:57 +01:00
raw_catalog_at_this_date = git ( [ " show " , f " { commit } :apps.json " ] )
loader = json
elif os . system ( f " git -C ./.apps cat-file -e { commit } :apps.toml " ) == 0 :
raw_catalog_at_this_date = git ( [ " show " , f " { commit } :apps.toml " ] )
loader = toml
else :
raise Exception ( " No apps.json/toml at this point in history? " )
2021-04-11 19:58:15 +02:00
try :
2023-01-20 17:56:57 +01:00
catalog_at_this_date = loader . loads ( raw_catalog_at_this_date )
2021-04-11 19:58:15 +02:00
# This can happen in stupid cases where there was a temporary syntax error in the json..
except json . decoder . JSONDecodeError :
2021-10-24 14:33:41 +02:00
_print (
2023-01-20 17:56:57 +01:00
" Failed to parse apps.json/toml history for at commit %s / %s ... ignoring "
2021-10-24 14:33:41 +02:00
% ( commit , t )
)
2021-04-11 19:58:15 +02:00
continue
2023-01-20 17:56:57 +01:00
yield ( t , catalog_at_this_date . get ( self . app_id ) )
2020-11-17 01:13:05 +01:00
# We'll check the history for last 12 months (*2 points per month)
N = 12 * 2
history = list ( get_history ( N ) )
# Must have been
# known
# + flagged as working
# + level > 5
# for the past 6 months
def good_quality ( infos ) :
2021-10-24 14:33:41 +02:00
return (
bool ( infos )
and isinstance ( infos , dict )
and infos . get ( " state " ) == " working "
2020-11-17 01:13:05 +01:00
and infos . get ( " level " , - 1 ) > = 5
2021-10-24 14:33:41 +02:00
)
2020-11-17 01:13:05 +01:00
score = sum ( [ good_quality ( infos ) for d , infos in history ] )
rel_score = int ( 100 * score / N )
2021-12-05 00:19:37 +01:00
if rel_score > 80 :
2020-12-05 15:37:23 +01:00
yield Success ( " The app is long-term good quality in the catalog! " )
2020-11-17 01:13:05 +01:00
2021-10-24 14:33:41 +02:00
2020-10-08 14:25:11 +02:00
##################################
# _____ _ _ #
# / ____| (_) | | #
# | (___ ___ _ __ _ _ __ | |_ #
# \___ \ / __| '__| | '_ \| __| #
# ____) | (__| | | | |_) | |_ #
# |_____/ \___|_| |_| .__/ \__| #
# | | #
# |_| #
##################################
class Script ( TestSuite ) :
2019-02-23 20:14:56 +01:00
def __init__ ( self , app_path , name ) :
self . name = name
2019-04-19 17:42:40 +02:00
self . app_path = app_path
2019-02-23 20:14:56 +01:00
self . path = app_path + " /scripts/ " + name
self . exists = file_exists ( self . path )
if not self . exists :
return
2019-03-02 01:19:40 +01:00
self . lines = list ( self . read_file ( ) )
2021-02-03 18:30:26 +01:00
self . test_suite_name = " scripts/ " + self . name
2019-02-23 20:14:56 +01:00
def read_file ( self ) :
with open ( self . path ) as f :
lines = f . readlines ( )
# Remove trailing spaces, empty lines and comment lines
lines = [ line . strip ( ) for line in lines ]
2021-10-24 14:33:41 +02:00
lines = [ line for line in lines if line and not line . startswith ( " # " ) ]
2019-02-23 20:14:56 +01:00
# Merge lines when ending with \
2021-10-24 14:33:41 +02:00
lines = " \n " . join ( lines ) . replace ( " \\ \n " , " " ) . split ( " \n " )
2019-02-23 20:14:56 +01:00
2019-04-19 16:58:57 +02:00
some_parsing_failed = False
2019-02-23 20:14:56 +01:00
for line in lines :
2019-02-27 13:47:26 +01:00
2019-02-23 20:14:56 +01:00
try :
line = shlex . split ( line , True )
yield line
except Exception as e :
2021-02-03 18:30:26 +01:00
2021-10-24 14:33:41 +02:00
ignore_pattern = [
" /etc/cron " ,
" admin_panel= " ,
' echo " ' ,
" __PRE_TAG " ,
" __URL_TAG " ,
" maintenance.$app.conf " ,
" mail_message= " ,
" maintenance.$app.html " ,
" > mail_to_send " ,
]
if str ( e ) == " No closing quotation " and any (
pattern in line for pattern in ignore_pattern
) :
2021-02-03 18:30:26 +01:00
continue
2019-04-19 16:58:57 +02:00
if not some_parsing_failed :
2021-10-24 14:33:41 +02:00
_print (
" Some lines could not be parsed in script %s . (That ' s probably not really critical) "
% self . name
)
2019-04-19 16:58:57 +02:00
some_parsing_failed = True
2021-02-03 18:30:26 +01:00
2020-10-08 14:25:11 +02:00
report_warning_not_reliable ( " %s : %s " % ( e , line ) )
2019-03-02 01:19:40 +01:00
2020-12-01 01:19:58 +01:00
def occurences ( self , command ) :
2021-10-24 14:33:41 +02:00
return [
line for line in [ " " . join ( line ) for line in self . lines ] if command in line
]
2020-12-01 01:19:58 +01:00
2019-02-23 20:14:56 +01:00
def contains ( self , command ) :
"""
Iterate on lines to check if command is contained in line
For instance , " app setting " is contained in " yunohost app setting $app ... "
"""
2021-10-24 14:33:41 +02:00
return any ( command in line for line in [ " " . join ( line ) for line in self . lines ] )
2019-02-23 20:14:56 +01:00
2020-03-31 04:30:11 +02:00
def containsregex ( self , regex ) :
"""
Iterate on lines to check if command is contained in line
For instance , " app setting " is contained in " yunohost app setting $app ... "
"""
2021-10-24 14:33:41 +02:00
return any (
re . search ( regex , line ) for line in [ " " . join ( line ) for line in self . lines ]
)
2020-03-31 05:45:51 +02:00
2020-10-08 14:25:11 +02:00
@test ( )
def error_handling ( self ) :
2019-03-02 02:10:54 +01:00
2022-11-27 18:16:33 +01:00
if app_packaging_format == 2 :
if self . contains ( " ynh_abort_if_errors " ) \
2023-09-07 23:11:03 +02:00
or self . contains ( " set -eu " ) \
or self . contains ( " set -u " ) :
2022-11-27 18:16:33 +01:00
yield Error ( " ynh_abort_if_errors or set -eu is now handled by YunoHost core in packaging v2, you should not have to add it to your script ! " )
return
2020-10-08 14:25:11 +02:00
if self . name in [ " backup " , " remove " , " _common.sh " ] :
2021-10-24 14:33:41 +02:00
present = (
self . contains ( " ynh_abort_if_errors " )
or self . contains ( " set -eu " )
or self . contains ( " set -u " )
)
2019-03-02 02:10:54 +01:00
else :
present = self . contains ( " ynh_abort_if_errors " )
2020-10-08 14:25:11 +02:00
if self . name in [ " remove " , " _common.sh " ] :
2019-03-02 02:10:54 +01:00
if present :
2020-10-08 14:25:11 +02:00
yield Error (
2021-09-23 22:28:00 +02:00
" Do not use ' set -eu ' or ' ynh_abort_if_errors ' in the remove or _common.sh scripts: "
2020-10-08 14:25:11 +02:00
" If a single instruction fails, it will stop the script and is "
" likely to leave the system in a broken state. "
2019-03-02 02:10:54 +01:00
)
elif not present :
2020-10-08 14:25:11 +02:00
yield Error (
" You should add ' ynh_abort_if_errors ' in this script, "
" c.f. https://github.com/YunoHost/issues/issues/419 "
2019-03-02 02:10:54 +01:00
)
2020-10-08 14:25:11 +02:00
# Skip this in common.sh, sometimes custom not-yet-official helpers need this
@test ( ignore = [ " _common.sh " ] )
def raw_apt_commands ( self ) :
2020-03-31 05:45:51 +02:00
2021-10-24 14:33:41 +02:00
if (
self . contains ( " ynh_package_install " )
or self . contains ( " apt install " )
or self . contains ( " apt-get install " )
) :
2022-08-29 22:27:19 +02:00
yield Error (
2019-03-02 02:10:54 +01:00
" You should not use `ynh_package_install` or `apt-get install`, "
" use `ynh_install_app_dependencies` instead "
)
2021-10-24 14:33:41 +02:00
if (
self . contains ( " ynh_package_remove " )
or self . contains ( " apt remove " )
or self . contains ( " apt-get remove " )
) :
2022-08-29 22:27:19 +02:00
yield Error (
2019-03-02 02:10:54 +01:00
" You should not use `ynh_package_remove` or `apt-get remove`, "
" use `ynh_remove_app_dependencies` instead "
)
2020-10-08 14:25:11 +02:00
@test ( )
def obsolete_helpers ( self ) :
2019-03-02 02:10:54 +01:00
if self . contains ( " yunohost app setting " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" Do not use ' yunohost app setting ' directly. Please use ' ynh_app_setting_(set,get,delete) ' instead. "
)
2019-03-02 02:10:54 +01:00
if self . contains ( " yunohost app checkurl " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" ' yunohost app checkurl ' is obsolete!!! Please use ' ynh_webpath_register ' instead. "
)
2019-03-02 02:10:54 +01:00
if self . contains ( " yunohost app checkport " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" ' yunohost app checkport ' is obsolete!!! Please use ' ynh_find_port ' instead. "
)
2019-03-02 02:10:54 +01:00
if self . contains ( " yunohost app initdb " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" ' yunohost app initdb ' is obsolete!!! Please use ' ynh_mysql_setup_db ' instead. "
)
2020-11-09 15:46:32 +01:00
if self . contains ( " yunohost tools port-available " ) :
2021-10-24 14:33:41 +02:00
yield Critical (
" ' yunohost tools port-available is obsolete!!! Please use ' ynh_port_available ' instead. "
)
if self . contains ( " yunohost app addaccess " ) or self . contains (
" yunohost app removeaccess "
) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" ' yunohost app addaccess/removeacces ' is obsolete. You should use the new permission system to manage accesses. "
)
if self . contains ( " yunohost app list -i " ) or self . contains (
" yunohost app list --installed "
) :
2022-08-29 22:27:19 +02:00
yield Error (
2020-11-16 17:49:29 +01:00
" Argument --installed ain ' t needed anymore when using "
" ' yunohost app list ' . It directly returns the list of installed "
2020-12-05 15:37:23 +01:00
" apps.. Also beware that option -f is obsolete as well... "
2020-11-16 17:49:29 +01:00
" Use grep -q ' id: $appname ' to check a specific app is installed "
)
2021-05-10 19:39:11 +02:00
if self . contains ( " --others_var " ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" Option --others_var is deprecated / irrelevant since 4.2, and will be removed in Bullseye. YunoHost now manages conf using ynh_add_config which automatically replace all __FOOBAR__ by $foobar "
)
2021-08-28 22:09:04 +02:00
if self . contains ( " ynh_webpath_available " ) :
2022-08-29 22:27:19 +02:00
yield Warning (
2021-10-24 14:33:41 +02:00
" Calling ' ynh_webpath_available ' is quite probably pointless: in the install script, just call ynh_webpath_register, and in the restore script, there ' s no need to check/register the webpath. (Also the helper always return exit code 0, so ' ynh_webpath_available || ynh_die ' is useless :/ "
)
2021-09-16 18:43:47 +02:00
if self . contains ( " ynh_print_ON " ) or self . contains ( " ynh_print_OFF " ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" Please refrain from using ' ynh_print_ON/OFF ' ... YunoHost already integrates a mecanism to automatically redact variables with names ending with : pwd, pass, passwd, password, passphrase, key, token, and any variable with ' secret ' in its name. Using ' ynh_print_ON/OFF ' is cumbersome and may have the unintended effect of defeating Yunohost ' s autoredacting mecanism ... If you noticed that Yunohost ' s mecanism doesn ' t work or cover your specific case, please contact the dev team about it. "
)
2021-10-06 14:33:30 +02:00
if self . contains ( " ynh_add_app_dependencies " ) :
2022-08-29 22:27:19 +02:00
yield Error (
2021-10-24 14:33:41 +02:00
" ynh_add_app_dependencies is supposed to be an internal helper and will soon be deprecated. Consider using ynh_install_app_dependencies or ynh_install_extra_app_dependencies instead. "
)
2021-11-14 18:21:16 +01:00
if self . contains ( " ynh_detect_arch " ) :
2023-11-24 18:10:07 +01:00
yield Warning (
2021-11-14 18:21:16 +01:00
" (Requires yunohost 4.3) Using ynh_detect_arch is deprecated, since Yunohost 4.3, an $YNH_ARCH variable is directly available in the global context. Its value directly corresponds to `dpkg --print-architecture` which returns a value among : amd64, i386, armhf, arm64 and armel (though armel is probably not used at all?) "
)
2021-05-10 19:39:11 +02:00
2022-08-15 19:38:24 +02:00
@test ( only = [ " install " ] )
def admin_has_to_finish_install ( self ) :
cmd = ' grep -B10 -IhEr " send_readme_to_admin " %s | grep -q db_pwd ' % self . path
if os . system ( cmd ) == 0 :
yield Warning ( " It looks like this app requires the admin to finish the install by entering DB credentials. Unless it ' s absolutely not easily automatizable, this should be handled automatically by the app install script using curl calls, or (CLI tools provided by the upstream maybe ?). " )
2021-05-10 21:12:07 +02:00
@test ( only = [ " install " , " upgrade " ] )
def deprecated_replace_string ( self ) :
2021-05-10 21:15:55 +02:00
cmd1 = " grep -Ec ' ynh_replace_string ' ' %s ' || true " % self . path
2023-09-07 23:11:03 +02:00
cmd2 = " grep -Ec ' ynh_replace_string.*__ \\ w+__ ' ' %s ' || true " % self . path
2021-05-10 21:12:07 +02:00
2021-10-24 14:33:41 +02:00
count1 = int ( subprocess . check_output ( cmd1 , shell = True ) . decode ( " utf-8 " ) . strip ( ) )
count2 = int ( subprocess . check_output ( cmd2 , shell = True ) . decode ( " utf-8 " ) . strip ( ) )
2021-05-10 21:12:07 +02:00
if count2 > 0 or count1 > = 5 :
2021-10-24 14:33:41 +02:00
yield Info (
" Please consider using ' ynh_add_config ' to handle config files instead of gazillions of manual cp + ' ynh_replace_string ' + chmod "
)
2020-10-08 14:25:11 +02:00
2021-11-13 19:24:56 +01:00
@test ( )
def bad_ynh_exec_syntax ( self ) :
cmd = ' grep -q -IhEro " ynh_exec_(err|warn|warn_less|quiet|fully_quiet) ( \\ " | \' ) " %s ' % self . path
if os . system ( cmd ) == 0 :
2022-08-29 22:27:19 +02:00
yield Warning ( " (Requires Yunohost 4.3) When using ynh_exec_*, please don ' t wrap your command between quotes (typically DONT write ynh_exec_warn_less ' foo --bar --baz ' ) " )
2021-11-13 19:24:56 +01:00
2022-08-15 18:23:42 +02:00
@test ( )
def ynh_setup_source_keep_with_absolute_path ( self ) :
cmd = ' grep -q -IhEro " ynh_setup_source.*keep.*final_path " %s ' % self . path
if os . system ( cmd ) == 0 :
yield Info ( " The --keep option of ynh_setup_source expects relative paths, not absolute path ... you do not need to prefix everything with ' $final_path ' in the --keep arg ... " )
2021-11-13 18:54:11 +01:00
@test ( )
def ynh_add_fpm_config_deprecated_package_option ( self ) :
if self . containsregex ( r ' ynh_add_fpm_config .*package=.* ' ) :
2023-02-15 19:19:43 +01:00
yield Error ( " (Requires Yunohost 4.3) Option --package for ynh_add_fpm_config is deprecated : please use ' ynh_install_app_dependencies ' with **all** your apt dependencies instead (no need to define a special ' extra_php_dependencies ' ). YunoHost will automatically install any phpX.Y-fpm / phpX.Y-common if needed. " )
2021-11-13 18:54:11 +01:00
2021-02-21 18:31:06 +01:00
@test ( )
def set_is_public_setting ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex ( r " ynh_app_setting_set .*is_public.* " ) :
2022-08-16 23:32:48 +02:00
if self . name == " upgrade " :
yield Error (
" permission system: it should not be needed to save is_public with ynh_app_setting_set ... this setting should only be used during installation to initialize the permission. The admin is likely to manually tweak the permission using YunoHost ' s interface later. "
)
else :
yield Warning (
" permission system: it should not be needed to save is_public with ynh_app_setting_set ... this setting should only be used during installation to initialize the permission. The admin is likely to manually tweak the permission using YunoHost ' s interface later. "
)
2021-02-21 18:31:06 +01:00
2023-06-19 16:16:33 +02:00
@test ( only = [ " _common.sh " ] )
def default_php_version_in_common ( self ) :
if self . contains ( " YNH_DEFAULT_PHP_VERSION " ) :
yield Warning ( " Do not use YNH_DEFAULT_PHP_VERSION in _common.sh ... _common.sh is usually sourced *before* the helpers, which define the version of YNH_DEFAULT_PHP_VERSION (hence it gets replaced with empty string). Instead, please explicitly state the PHP version in the package, e.g. dependencies= ' php8.2-cli php8.2-imagemagick ' " )
2021-02-21 18:31:06 +01:00
@test ( ignore = [ " install " , " _common.sh " ] )
def get_is_public_setting ( self ) :
2021-10-24 14:33:41 +02:00
if self . contains ( " is_public= " ) or self . contains ( " $is_public " ) :
2021-12-05 00:36:29 +01:00
yield Warning (
2021-10-24 14:33:41 +02:00
" permission system: there should be no need to fetch or use $is_public ... is_public should only be used during installation to initialize the permission. The admin is likely to manually tweak the permission using YunoHost ' s interface later. "
)
2021-02-21 18:31:06 +01:00
2021-12-05 00:36:29 +01:00
@test ( only = [ " upgrade " ] )
def temporarily_enable_visitors_during_upgrade ( self ) :
if self . containsregex ( " ynh_permission_update.*add.*visitors " ) and self . containsregex ( " ynh_permission_update.*remove.*visitors " ) :
2023-11-24 18:10:07 +01:00
yield Warning (
2021-12-05 00:36:29 +01:00
" permission system: since Yunohost 4.3, there should be no need to temporarily add ' visitors ' to the main permission. ynh_local_curl will temporarily enable visitors access if needed "
)
2021-02-21 18:31:06 +01:00
@test ( )
def set_legacy_permissions ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex (
2022-08-16 23:38:40 +02:00
r " ynh_app_setting_set .*protected_uris "
) or self . containsregex ( r " ynh_app_setting_set .*skipped_uris " ) :
yield Error (
" permission system: it looks like the app is still using super-legacy (un)protected/skipped_uris settings. This is now completely deprecated. Please check https://yunohost.org/packaging_apps_permissions for a documentation on how to migrate the app to the new permission system. "
)
elif self . containsregex (
2021-10-24 14:33:41 +02:00
r " ynh_app_setting_set .*protected_ "
) or self . containsregex ( r " ynh_app_setting_set .*skipped_ " ) :
yield Warning (
" permission system: it looks like the app is still using the legacy permission system (unprotected/protected/skipped uris/regexes setting). Please check https://yunohost.org/packaging_apps_permissions for a documentation on how to migrate the app to the new permission system. "
)
2021-02-21 18:31:06 +01:00
2020-11-29 07:02:19 +01:00
@test ( )
def normalize_url_path ( self ) :
if self . contains ( " ynh_normalize_url_path " ) :
2023-11-24 18:10:07 +01:00
yield Warning (
2021-10-24 14:33:41 +02:00
" You probably don ' t need to call ' ynh_normalize_url_path ' ... this is only relevant for upgrades from super-old versions (like 3 years ago or so...) "
)
2020-11-29 07:02:19 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def safe_rm ( self ) :
2021-10-24 14:33:41 +02:00
if (
self . contains ( " rm -r " )
or self . contains ( " rm -R " )
or self . contains ( " rm -fr " )
or self . contains ( " rm -fR " )
) :
yield Error (
" You should not be using ' rm -rf ' , please use ' ynh_secure_remove ' instead "
)
2020-10-08 14:25:11 +02:00
@test ( )
def nginx_restart ( self ) :
2021-10-24 14:33:41 +02:00
if self . contains ( " systemctl restart nginx " ) or self . contains (
" service nginx restart "
) :
2020-10-08 14:25:11 +02:00
yield Error (
2020-12-05 15:37:23 +01:00
" Restarting NGINX is quite dangerous (especially for web installs) "
2020-10-08 14:25:11 +02:00
" and should be avoided at all cost. Use ' reload ' instead. "
)
2021-01-07 18:37:51 +01:00
@test ( )
def raw_systemctl_start ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex ( r " systemctl start \" ?[^. ]+( \ .service)? \" ? \ s " ) :
yield Warning (
" Please do not use ' systemctl start ' to start services. Instead, you should use ' ynh_systemd_action ' which will display the service log in case it fails to start. You can also use ' --line_match ' to wait until some specific word appear in the log, signaling the service indeed fully started. "
)
2021-01-07 18:37:51 +01:00
2023-12-28 05:15:30 +01:00
@test ( )
def bad_line_match ( self ) :
if self . containsregex ( r ' --line_match=Started$ ' ) or self . containsregex ( r ' --line_match=Stopped$ ' ) :
yield Warning (
' Using --line_match= " Started " or " Stopped " in ynh_systemd_action is counter productive because it will match the systemd message and not the actual app message ... Please check the log of the service to find an actual, relevant message to match, or remove the --line_match option entirely '
)
2020-11-24 03:25:45 +01:00
@test ( )
def quiet_systemctl_enable ( self ) :
2021-10-24 14:33:41 +02:00
systemctl_enable = [
line
for line in [ " " . join ( line ) for line in self . lines ]
if re . search ( r " systemctl.*(enable|disable) " , line )
]
2020-11-24 03:25:45 +01:00
if any ( " -q " not in cmd for cmd in systemctl_enable ) :
2021-09-23 22:28:00 +02:00
message = " Please add --quiet to systemctl enable/disable commands to avoid unnecessary warnings when the script runs "
2021-08-07 17:14:18 +02:00
yield Warning ( message )
2020-11-24 03:25:45 +01:00
2020-11-24 03:45:10 +01:00
@test ( )
def quiet_wget ( self ) :
2021-10-24 14:33:41 +02:00
wget_cmds = [
line
for line in [ " " . join ( line ) for line in self . lines ]
if re . search ( r " ^wget " , line )
]
2020-11-24 03:45:10 +01:00
2021-10-24 14:33:41 +02:00
if any (
" -q " not in cmd and " --quiet " not in cmd and " 2> " not in cmd
for cmd in wget_cmds
) :
2020-11-24 03:45:10 +01:00
message = " Please redirect wget ' s stderr to stdout with 2>&1 to avoid unecessary warnings when the script runs (yes, wget is annoying and displays a warning even when things are going okay >_> ...) "
2021-08-07 17:14:18 +02:00
yield Warning ( message )
2020-11-24 03:45:10 +01:00
2020-10-08 14:25:11 +02:00
@test ( only = [ " install " ] )
def argument_fetching ( self ) :
if self . containsregex ( r " ^ \ w+ \ = \ $ \ { ?[0-9] " ) :
2020-11-21 00:33:09 +01:00
yield Critical (
2021-09-23 22:28:00 +02:00
" Do not fetch arguments from manifest using ' variable=$N ' (e.g. "
" domain=$1...) Instead, use ' name=$YNH_APP_ARG_NAME ' "
2020-10-08 14:25:11 +02:00
)
@test ( only = [ " install " ] )
def sources_list_tweaking ( self ) :
2021-10-24 14:33:41 +02:00
if self . contains ( " /etc/apt/sources.list " ) or (
os . path . exists ( self . app_path + " /scripts/_common.sh " )
and " /etc/apt/sources.list "
in open ( self . app_path + " /scripts/_common.sh " ) . read ( )
and " ynh_add_repo " not in open ( self . app_path + " /scripts/_common.sh " ) . read ( )
) :
2020-10-08 14:25:11 +02:00
yield Error (
" Manually messing with apt ' s sources.lists is strongly discouraged "
2020-12-05 15:37:23 +01:00
" and should be avoided. Please use ' ynh_install_extra_app_dependencies ' if you "
2020-10-08 14:25:11 +02:00
" need to install dependencies from a custom apt repo. "
)
2021-01-09 22:11:44 +01:00
@test ( )
def firewall_consistency ( self ) :
2021-10-24 14:33:41 +02:00
if self . contains ( " yunohost firewall allow " ) and not self . contains (
" --needs_exposed_ports "
) :
yield Warning (
" You used ' yunohost firewall allow ' to expose a port on the outside but did not use ' yunohost service add ' with ' --needs_exposed_ports ' ... If you are ABSOLUTELY SURE that the service needs to be exposed on THE OUTSIDE, then add ' --needs_exposed_ports ' to ' yunohost service add ' with the relevant port number. Otherwise, opening the port leads to a significant security risk and you should keep the damn port closed ! "
)
2021-01-09 22:11:44 +01:00
2021-10-24 14:33:41 +02:00
if self . contains ( " Configuring firewall " ) and not self . contains (
" yunohost firewall allow "
) :
yield Warning (
" Some message is talking about ' Configuring firewall ' but there ' s no mention of ' yunohost firewall allow ' ... If you ' re only finding an available port for *internal reverse proxy*, this has nothing to do with ' Configuring the firewall ' , so the message should be changed to avoid confusion... "
)
2021-01-09 22:11:44 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def exit_ynhdie ( self ) :
2020-10-08 16:40:04 +02:00
if self . contains ( r " \ bexit \ b " ) :
2021-10-24 14:33:41 +02:00
yield Error (
" ' exit ' command shouldn ' t be used. Please use ' ynh_die ' instead. "
)
2020-03-31 04:30:57 +02:00
2020-10-08 14:25:11 +02:00
@test ( )
def old_regenconf ( self ) :
2020-03-31 18:44:50 +02:00
if self . contains ( " yunohost service regen-conf " ) :
2022-12-03 20:13:41 +01:00
yield Error (
2021-10-24 14:33:41 +02:00
" ' yunohost service regen-conf ' has been replaced by ' yunohost tools regen-conf ' . "
)
2020-03-31 18:44:50 +02:00
2020-10-08 14:25:11 +02:00
@test ( )
2022-12-04 03:30:45 +01:00
def ssowatconf_or_nginx_reload ( self ) :
2020-03-31 04:30:57 +02:00
# Dirty hack to check only the 10 last lines for ssowatconf
# (the "bad" practice being using this at the very end of the script, but some apps legitimately need this in the middle of the script)
oldlines = list ( self . lines )
self . lines = self . lines [ - 10 : ]
2019-04-29 20:23:50 +02:00
if self . contains ( " yunohost app ssowatconf " ) :
2021-10-24 14:33:41 +02:00
yield Warning (
" You probably don ' t need to run ' yunohost app ssowatconf ' in the app self. It ' s supposed to be ran automatically after the script. "
)
2022-12-04 03:30:45 +01:00
if app_packaging_format > = 2 and self . name not in [ " change_url " , " restore " ] :
if self . contains ( " ynh_systemd_action --service_name=nginx --action=reload " ) :
yield Warning (
" You should not need to reload nginx at the end of the script ... it ' s already taken care of by ynh_add_nginx_config "
)
2020-03-31 04:30:57 +02:00
self . lines = oldlines
2019-03-02 02:10:54 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
def sed ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex (
r " sed \ s+(-i|--in-place) \ s+(-r \ s+)?s "
) or self . containsregex ( r " sed \ s+s \ S* \ s+(-i|--in-place) " ) :
yield Info (
" You should avoid using ' sed -i ' for substitutions, please use ' ynh_add_config ' instead "
)
2020-10-08 14:25:11 +02:00
@test ( )
def sudo ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex (
r " sudo \ w "
) : # \w is here to not match sudo -u, legit use because ynh_exec_as not official yet...
2021-05-10 19:52:40 +02:00
yield Warning (
2020-11-19 00:47:57 +01:00
" You should not need to use ' sudo ' , the script is being run as root. "
" (If you need to run a command using a specific user, use ' ynh_exec_as ' (or ' sudo -u ' )) "
2019-03-02 02:10:54 +01:00
)
2021-09-29 18:01:01 +02:00
@test ( )
def chownroot ( self ) :
2022-08-29 22:27:19 +02:00
if self . containsregex ( r " ^ \ s*chown.* root:?[^$]* .*final_path " ) \
2023-09-07 23:11:03 +02:00
and not self . contains ( ' chown root:root " $final_path " ' ) :
2022-08-29 22:27:19 +02:00
# (Mywebapp has a special case because of SFTP é_è)
2023-11-24 18:10:07 +01:00
yield Warning (
2021-09-29 18:01:01 +02:00
" Using ' chown root $final_path ' is usually symptomatic of misconfigured and wide-open ' other ' permissions ... Usually ynh_setup_source should now set sane default permissions on $final_path (if the app requires Yunohost >= 4.2) ... Otherwise, consider using ' chown $app ' , ' chown nobody ' or ' chmod ' to limit access to $final_path ... "
)
2020-10-08 14:25:11 +02:00
@test ( )
2020-10-08 16:04:17 +02:00
def chmod777 ( self ) :
2021-10-24 14:33:41 +02:00
if self . containsregex ( r " chmod .*777 " ) or self . containsregex ( r " chmod .*o \ +w " ) :
2020-10-08 14:25:11 +02:00
yield Warning (
2020-12-05 15:37:23 +01:00
" DO NOT use chmod 777 or chmod o+w that gives write permission to every users on the system!!! If you have permission issues, just make sure that the owner and/or group owner is right... "
2020-04-25 18:19:47 +02:00
)
2020-10-08 14:25:11 +02:00
@test ( )
def random ( self ) :
2019-03-02 02:10:54 +01:00
if self . contains ( " dd if=/dev/urandom " ) or self . contains ( " openssl rand " ) :
2020-10-08 16:04:17 +02:00
yield Error (
2021-09-23 22:28:00 +02:00
" Instead of ' dd if=/dev/urandom ' or ' openssl rand ' , you should use ' ynh_string_random ' "
2019-03-02 02:10:54 +01:00
)
2020-10-08 14:25:11 +02:00
@test ( only = [ " install " ] )
def progression ( self ) :
2020-11-19 00:47:57 +01:00
if not self . contains ( " ynh_script_progression " ) :
2020-10-08 14:25:11 +02:00
yield Warning (
2020-11-19 00:47:57 +01:00
" Please add a few messages for the user using ' ynh_script_progression ' "
" to explain what is going on (in friendly, not-too-technical terms) "
" during the installation. (and ideally in scripts remove, upgrade and restore too) "
2019-03-02 02:10:54 +01:00
)
2020-11-28 18:49:05 +01:00
@test ( only = [ " backup " ] )
def progression_in_backup ( self ) :
if self . contains ( " ynh_script_progression " ) :
2021-05-10 19:52:40 +02:00
yield Warning (
2020-11-28 18:49:05 +01:00
" We recommend to *not* use ' ynh_script_progression ' in backup "
" scripts because no actual work happens when running the script "
2021-09-23 22:28:00 +02:00
" : YunoHost only fetches the list of things to backup (apart "
2020-12-05 15:37:23 +01:00
" from the DB dumps which effectively happens during the script...). "
2020-11-28 18:49:05 +01:00
" Consider using a simple message like this instead: ' ynh_print_info \" Declaring files to be backed up... \" ' "
)
2020-10-08 14:25:11 +02:00
@test ( )
2020-11-04 15:51:24 +01:00
def progression_time ( self ) :
2020-10-08 14:25:11 +02:00
2021-09-23 22:28:00 +02:00
# Usage of ynh_script_progression with --time or --weight=1 all over the place...
2020-10-08 14:25:11 +02:00
if self . containsregex ( r " ynh_script_progression.*--time " ) :
2021-10-24 14:33:41 +02:00
yield Info (
" Using ' ynh_script_progression --time ' should only be for calibrating the weight (c.f. ' --weight ' ). It ' s not meant to be kept for production versions. "
)
2020-05-23 23:48:56 +02:00
2020-11-04 15:51:24 +01:00
@test ( ignore = [ " _common.sh " , " backup " ] )
def progression_meaningful_weights ( self ) :
def weight ( line ) :
2021-10-24 14:33:41 +02:00
match = re . search (
r " ynh_script_progression.*--weight=([0-9]+) " , " " . join ( line )
)
2020-11-04 15:51:24 +01:00
if match :
try :
return int ( match . groups ( ) [ 0 ] )
2023-09-07 23:11:03 +02:00
except Exception :
2020-11-04 15:51:24 +01:00
return - 1
else :
return 1
2021-10-24 14:33:41 +02:00
script_progress = [
line for line in self . lines if " ynh_script_progression " in line
]
2020-11-04 15:51:24 +01:00
weights = [ weight ( line ) for line in script_progress ]
if not weights :
return
if len ( weights ) > 3 and statistics . stdev ( weights ) > 50 :
2021-10-24 14:33:41 +02:00
yield Warning (
" To have a meaningful progress bar, try to keep the weights in the same range of values, for example [1,10], or [10,100]... otherwise, if you have super-huge weight differences, the progress bar rendering will be completely dominated by one or two steps... If these steps are really long, just try to indicated in the message that this will take a while. "
)
2019-03-09 19:36:52 +01:00
2020-10-08 14:25:11 +02:00
@test ( only = [ " install " , " _common.sh " ] )
def php_deps ( self ) :
if self . containsregex ( " dependencies.*php- " ) :
2022-10-02 22:13:17 +02:00
# (Stupid hack because some apps like roundcube depend on php-pear or php-php-gettext and there's no phpx.y-pear phpx.y-php-gettext>_> ...
if not self . contains ( " php-pear " ) or not self . contains ( " php-php-gettext " ) :
2021-12-05 00:18:31 +01:00
yield Warning (
" You should avoid having dependencies like ' php-foobar ' . Instead, specify the exact version you want like ' php7.0-foobar ' . Otherwise, the *wrong* version of the dependency may be installed if sury is also installed. Note that for Stretch/Buster/Bullseye/... transition, YunoHost will automatically patch your file so there ' s no need to care about that. "
)
2020-05-27 13:06:13 +02:00
2020-10-08 14:25:11 +02:00
@test ( only = [ " backup " ] )
2020-10-08 16:04:17 +02:00
def systemd_during_backup ( self ) :
2020-10-08 14:25:11 +02:00
if self . containsregex ( " ^ynh_systemd_action " ) :
2021-10-24 14:33:41 +02:00
yield Warning (
" Unless you really have a good reason to do so, starting/stopping services during backup has no benefit and leads to unecessary service interruptions when creating backups... As a ' reminder ' : apart from possibly database dumps (which usually do not require the service to be stopped) or other super-specific action, running the backup script is only a *declaration* of what needs to be backed up. The real copy and archive creation happens *after* the backup script is ran. "
)
2020-05-25 18:47:48 +02:00
2020-11-09 18:56:29 +01:00
@test ( only = [ " backup " ] )
def check_size_backup ( self ) :
if self . contains ( " CHECK_SIZE " ) :
2022-12-03 20:13:41 +01:00
yield Error (
2021-10-24 14:33:41 +02:00
" There ' s no need to ' CHECK_SIZE ' during backup... This check is handled by the core automatically. "
)
2020-11-09 18:56:29 +01:00
2020-10-08 14:25:11 +02:00
@test ( )
2020-10-08 16:04:17 +02:00
def helpers_sourcing_after_official ( self ) :
2021-10-24 14:33:41 +02:00
helpers_after_official = subprocess . check_output (
" head -n 30 ' %s ' | grep -A 10 ' ^ *source */usr/share/yunohost/helpers ' | grep ' ^ *source ' | tail -n +2 "
% self . path ,
shell = True ,
) . decode ( " utf-8 " )
helpers_after_official = (
helpers_after_official . replace ( " source " , " " ) . replace ( " " , " " ) . strip ( )
)
2020-04-08 01:43:08 +02:00
if helpers_after_official :
helpers_after_official = helpers_after_official . split ( " \n " )
2021-10-24 14:33:41 +02:00
yield Warning (
" Please avoid sourcing additional helpers after the official helpers (in this case file %s ) "
% " , " . join ( helpers_after_official )
)
2020-04-08 01:43:08 +02:00
2020-10-08 14:25:11 +02:00
@test ( only = [ " backup " , " restore " ] )
2020-10-08 16:04:17 +02:00
def helpers_sourcing_backuprestore ( self ) :
2020-10-08 14:25:11 +02:00
if self . contains ( " source _common.sh " ) or self . contains ( " source ./_common.sh " ) :
2022-12-03 20:13:41 +01:00
yield Error (
2021-10-24 14:33:41 +02:00
' In the context of backup and restore scripts, you should load _common.sh with " source ../settings/scripts/_common.sh " '
)
2020-04-30 04:50:19 +02:00
2023-01-08 19:28:55 +01:00
@test ( only = [ " _common.sh " ] )
def no_progress_in_common ( self ) :
if self . contains ( " ynh_script_progression " ) :
2023-11-24 18:10:07 +01:00
yield Warning (
2023-01-08 19:46:01 +01:00
" You should not use `ynh_script_progression` in _common.sh because it will produce warnings when trying to install the application. "
2023-01-08 19:28:55 +01:00
)
2020-11-17 01:13:05 +01:00
2019-01-28 23:33:27 +01:00
def main ( ) :
2020-06-10 17:34:34 +02:00
if len ( sys . argv ) < 2 :
2016-11-03 19:09:07 +01:00
print ( " Give one app package path. " )
exit ( )
2016-12-18 02:37:07 +01:00
2016-11-03 19:09:07 +01:00
app_path = sys . argv [ 1 ]
2020-06-10 17:34:34 +02:00
global output
output = " json " if " --json " in sys . argv else " plain "
2021-10-24 14:33:41 +02:00
_print (
"""
2021-02-03 18:30:26 +01:00
[ { blue } { bold } YunoHost App Package Linter { end } ]
2021-10-28 11:16:41 +02:00
App packaging documentation - https : / / yunohost . org / en / contribute / packaging_apps
2021-02-03 18:30:26 +01:00
App package example - https : / / github . com / YunoHost / example_ynh
2021-10-28 11:16:41 +02:00
Official helpers - https : / / yunohost . org / en / contribute / packaging_apps / helpers
2021-02-03 18:30:26 +01:00
Experimental helpers - https : / / github . com / YunoHost - Apps / Experimental_helpers
If you believe this linter returns false negative ( warnings / errors which shouldn ' t happen),
please report them on https : / / github . com / YunoHost / package_linter / issues
2021-10-24 14:33:41 +02:00
""" .format(
blue = c . OKBLUE , bold = c . BOLD , end = c . END
)
)
2021-02-03 18:30:26 +01:00
2020-11-17 01:13:05 +01:00
app = App ( app_path )
app . analyze ( )
2019-01-28 23:33:27 +01:00
2021-10-24 14:33:41 +02:00
if __name__ == " __main__ " :
2019-01-28 23:33:27 +01:00
main ( )