From 45b320cf9c4a9f0c6d9fa5255ef02656f049d19a Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 18 Dec 2016 02:36:58 +0100 Subject: [PATCH 1/5] [fix] unexisting variable --- package_linter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package_linter.py b/package_linter.py index 7c20d1d..4c1784e 100755 --- a/package_linter.py +++ b/package_linter.py @@ -132,7 +132,8 @@ def check_script(path, script_name): if check_file_exist(script_path) == 0: return print (c.BOLD + c.HEADER + "\n>>>>", - scripts[i].upper(), "SCRIPT <<<<" + c.END) + script_name.upper(), "SCRIPT <<<<" + c.END) + script = read_file(script_path) check_script_header_presence(script) check_sudo_prefix_commands(script) From 78661a4c66ff9e45220796fe3f7be6ec6544751c Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 18 Dec 2016 02:37:07 +0100 Subject: [PATCH 2/5] [mod] more pythonic style --- package_linter.py | 163 +++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 75 deletions(-) diff --git a/package_linter.py b/package_linter.py index 4c1784e..544ac55 100755 --- a/package_linter.py +++ b/package_linter.py @@ -39,13 +39,12 @@ def check_files_exist(app_path): print (c.BOLD + c.HEADER + ">>>> MISSING FILES <<<<" + c.END) fname = ("manifest.json", "scripts/install", "scripts/remove", "scripts/upgrade", "scripts/backup", "scripts/restore", "LICENSE", "README.md") - i = 0 - while (i < len(fname)): - if (check_file_exist(app_path + "/" + fname[i])): - print_right(fname[i]) + + for i in fname: + if check_file_exist(app_path + "/" + i): + print_right(i) else: - print_wrong(fname[i]) - i += 1 + print_wrong(i) def check_file_exist(file_path): @@ -61,8 +60,9 @@ def read_file(file_path): def check_manifest(manifest): print (c.BOLD + c.HEADER + "\n>>>> MANIFEST <<<<" + c.END) """ - Check if there is no comma syntax issue - """ + Check if there is no comma syntax issue + """ + try: with open(manifest, encoding='utf-8') as data_file: manifest = json.loads(data_file.read()) @@ -71,27 +71,34 @@ def check_manifest(manifest): print_wrong( "Syntax (comma) or encoding issue with manifest.json. Can't check file.") return - i, fields = 0, ("name", "id", "packaging_format", "description", "url", - "license", "maintainer", "requirements", "multi_instance", "services", "arguments") - while (i < len(fields)): - if fields[i] in manifest: - print_right("\"" + fields[i] + "\" field is present") + + fields = ("name", "id", "packaging_format", "description", "url", + "license", "maintainer", "requirements", "multi_instance", "services", "arguments") + + for i in fields: + if i in manifest: + print_right("\"" + i + "\" field is present") else: - print_wrong("\"" + fields[i] + "\" field is missing") - i += 1 + print_wrong("\"" + i + "\" field is missing") + """ Check values in keys """ + pf = 1 + if "packaging_format" not in manifest: print_wrong("\"packaging_format\" key is missing") pf = 0 + if pf == 1 and isinstance(manifest["packaging_format"], int) != 1: print_wrong("\"packaging_format\": value isn't an integer type") pf = 0 + if pf == 1 and manifest["packaging_format"] != 1: print_wrong("\"packaging_format\" field: current format value is '1'") pf = 0 + if pf == 1: print_right("\"packaging_format\" field is good") @@ -100,41 +107,43 @@ def check_manifest(manifest): "You should specify 'free' or 'non-free' software package in the license field.") elif "license" in manifest: print_right("\"licence\" key value is good") + if "multi_instance" in manifest and manifest["multi_instance"] != 1 and manifest["multi_instance"] != 0: print_wrong( "\"multi_instance\" field must be boolean type values 'true' or 'false' and not string type") elif "multi_instance" in manifest: print_right("\"multi_instance\" field is good") + if "services" in manifest: services = ("nginx", "php5-fpm", "mysql", "uwsgi", "metronome", "postfix", "dovecot") # , "rspamd", "rmilter") - i = 0 - while (i < len(manifest["services"])): - if manifest["services"][i] not in services: - print_wrong(manifest["services"][i] + " service doesn't exist") - i += 1 + + for i in manifest["services"]: + if i not in services: + print_wrong(i + " service doesn't exist") + if "install" in manifest["arguments"]: types = ("domain", "path", "password", "user", "admin") - i = 0 - while (i < len(types)): - j = 0 - while (j < len(manifest["arguments"]["install"])): - if types[i] == manifest["arguments"]["install"][j]["name"]: - if "type" not in manifest["arguments"]["install"][j]: + + for number, i in enumerate(types): + for j in manifest["arguments"]["install"]: + if i == j["name"]: + if "type" not in j: print("You should specify the type of the key with", end=" ") - print(types[i - 1]) if i == 4 else print(types[i]) - j += 1 - i += 1 + print(types[number - 1]) if number == 4 else print(i) def check_script(path, script_name): script_path = path + "/scripts/" + script_name + if check_file_exist(script_path) == 0: return + print (c.BOLD + c.HEADER + "\n>>>>", script_name.upper(), "SCRIPT <<<<" + c.END) script = read_file(script_path) + check_script_header_presence(script) check_sudo_prefix_commands(script) check_verifications_done_before_modifying_system(script) @@ -154,21 +163,19 @@ def check_sudo_prefix_commands(script): """ cmd = ("rm", "chown", "chmod", "apt-get", "apt", "service", "yunohost", "find" "swapon", "mkswap", "useradd") # , "dd") cp, mkdir - i, ok = 0, 1 - while i < len(script): - j = 0 - while j < len(cmd): - if cmd[j] + " " in script[i] and "sudo " + cmd[j] + " " not in script[i] \ - and "yunohost service" not in script[i] and "-exec " + cmd[j] not in script[i] \ - and ".service" not in script[i] and script[i][0] != '#': + ok = True + + for i in script: + for j in cmd: + if j + " " in i and "sudo " + j + " " not in i \ + and "yunohost service" not in i and "-exec " + j not in i \ + and ".service" not in i and i[0] != '#': print(c.FAIL + "✘ Line ", i + 1, "you should add \"sudo\" before this command line:", c.END) - print(" " + script[i].replace(cmd[j], - c.BOLD + c.FAIL + cmd[j] + c.END)) - ok = 0 - j += 1 - i += 1 - if ok == 1: + print(" " + i.replace(j, + c.BOLD + c.FAIL + cmd[j] + c.END)) + ok = False + if ok: print_right("All commands are prefix with \"sudo\".") @@ -176,24 +183,25 @@ def check_verifications_done_before_modifying_system(script): """ Check if verifications are done before modifying the system """ - ex, i = 0, 0 - while i < len(script): - if "ynh_die" in script[i] or "exit" in script[i]: - ex = i - i += 1 + ex = 0 + for line_number, i in enumerate(script): + if "ynh_die" in i or "exit" in i: + ex = line_number + cmd = ("cp", "mkdir", "rm", "chown", "chmod", "apt-get", "apt", "service", "find", "sed", "mysql", "swapon", "mount", "dd", "mkswap", "useradd") # "yunohost" - i, ok = 0, 1 - while i < len(script): - if i >= ex: + + ok = True + + for line_number, i in enumerate(script): + if line_number >= ex: break - j = 0 - while (j < len(cmd)): - if cmd[j] in script[i] and script[i][0] != '#': - ok = 0 - j += 1 - i += 1 - if ok == 0: + + for j in cmd: + if j in i and i[0] != '#': + ok = False + + if not ok: print(c.FAIL + "✘ At line", ex + 1, "'ynh_die' or 'exit' command is executed with system modification before.") print("This system modification is an issue if a verification exit the script.") @@ -208,40 +216,45 @@ def check_non_helpers_usage(script): - 'yunohost app setting' –> ynh_app_setting_(set,get,delete) - 'exit' –> 'ynh_die' """ - i, ok = 0, 1 - while i < len(script): - if "yunohost app setting" in script[i]: - print_wrong("Line {}: 'yunohost app setting' command is deprecated, please use helpers ynh_app_setting_(set,get,delete).".format(i + 1)) - ok = 0 - i += 1 - if ok == 1: + ok = True + + for line_number, i in enumerate(script): + if "yunohost app setting" in i: + print_wrong("Line {}: 'yunohost app setting' command is deprecated, please use helpers ynh_app_setting_(set,get,delete).".format(line_number + 1)) + ok = False + + if ok: print_right("Only helpers are used") else: print_wrong("Helpers documentation: https://yunohost.org/#/packaging_apps_helpers\ncode: https://github.com/YunoHost/yunohost/…helpers") - i, ok = 0, 1 - while i < len(script): - if "exit" in script[i]: - print_wrong("Line {}: 'exit' command shouldn't be used. Use 'ynh_die' helper instead.".format(i + 1)) - ok = 0 - i += 1 - if ok == 1: + ok = True + + for line_number, i in enumerate(script): + if "exit" in i: + print_wrong("Line {}: 'exit' command shouldn't be used. Use 'ynh_die' helper instead.".format(line_number + 1)) + ok = False + + if ok: print_right("no 'exit' command found: 'ynh_die' helper is possibly used") if __name__ == '__main__': os.system("clear") + if len(sys.argv) != 2: print("Give one app package path.") exit() + app_path = sys.argv[1] header(app_path) check_files_exist(app_path) check_manifest(app_path + "/manifest.json") - i, scripts = 0, ["install", "remove", "upgrade", "backup", "restore"] + + scripts = ["install", "remove", "upgrade", "backup", "restore"] for (dirpath, dirnames, filenames) in os.walk(os.path.join(app_path, "scripts")): for filename in filenames: if filename not in scripts and filename[-4:] != ".swp": scripts.append(filename) - while i < len(scripts): - check_script(app_path, scripts[i]) - i += 1 + + for i in scripts: + check_script(app_path, i) From 6e4fc3ae54f0b31f7d71cba302abd4774e0f0c75 Mon Sep 17 00:00:00 2001 From: Moul Date: Mon, 19 Dec 2016 09:00:14 +0400 Subject: [PATCH 3/5] [fix] use 'enumerate' for line_nbr. Relicat. --- package_linter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package_linter.py b/package_linter.py index 544ac55..0ad6ff1 100755 --- a/package_linter.py +++ b/package_linter.py @@ -165,15 +165,15 @@ def check_sudo_prefix_commands(script): "service", "yunohost", "find" "swapon", "mkswap", "useradd") # , "dd") cp, mkdir ok = True - for i in script: + for line_nbr, i in enumerate(script): for j in cmd: if j + " " in i and "sudo " + j + " " not in i \ and "yunohost service" not in i and "-exec " + j not in i \ and ".service" not in i and i[0] != '#': - print(c.FAIL + "✘ Line ", i + 1, + print(c.FAIL + "✘ Line ", line_nbr + 1, "you should add \"sudo\" before this command line:", c.END) print(" " + i.replace(j, - c.BOLD + c.FAIL + cmd[j] + c.END)) + c.BOLD + c.FAIL + j + c.END)) ok = False if ok: print_right("All commands are prefix with \"sudo\".") From 664993985c983039b37996a03f2f08f115a6ae27 Mon Sep 17 00:00:00 2001 From: Moul Date: Mon, 19 Dec 2016 09:53:22 +0400 Subject: [PATCH 4/5] [enh] use more explicit variables names. --- package_linter.py | 80 +++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/package_linter.py b/package_linter.py index 0ad6ff1..b90b5d4 100755 --- a/package_linter.py +++ b/package_linter.py @@ -37,14 +37,14 @@ def check_files_exist(app_path): Check files exist """ print (c.BOLD + c.HEADER + ">>>> MISSING FILES <<<<" + c.END) - fname = ("manifest.json", "scripts/install", "scripts/remove", + fnames = ("manifest.json", "scripts/install", "scripts/remove", "scripts/upgrade", "scripts/backup", "scripts/restore", "LICENSE", "README.md") - for i in fname: - if check_file_exist(app_path + "/" + i): - print_right(i) + for fname in fnames: + if check_file_exist(app_path + "/" + fname): + print_right(fname) else: - print_wrong(i) + print_wrong(fname) def check_file_exist(file_path): @@ -75,11 +75,11 @@ def check_manifest(manifest): fields = ("name", "id", "packaging_format", "description", "url", "license", "maintainer", "requirements", "multi_instance", "services", "arguments") - for i in fields: - if i in manifest: - print_right("\"" + i + "\" field is present") + for field in fields: + if field in manifest: + print_right("\"" + field + "\" field is present") else: - print_wrong("\"" + i + "\" field is missing") + print_wrong("\"" + field + "\" field is missing") """ Check values in keys @@ -118,19 +118,19 @@ def check_manifest(manifest): services = ("nginx", "php5-fpm", "mysql", "uwsgi", "metronome", "postfix", "dovecot") # , "rspamd", "rmilter") - for i in manifest["services"]: - if i not in services: - print_wrong(i + " service doesn't exist") + for service in manifest["services"]: + if service not in services: + print_wrong(service + " service doesn't exist") if "install" in manifest["arguments"]: types = ("domain", "path", "password", "user", "admin") - for number, i in enumerate(types): - for j in manifest["arguments"]["install"]: - if i == j["name"]: - if "type" not in j: + for nbr, typ in enumerate(types): + for install_arg in manifest["arguments"]["install"]: + if typ == install_arg["name"]: + if "type" not in install_arg: print("You should specify the type of the key with", end=" ") - print(types[number - 1]) if number == 4 else print(i) + print(types[nbr - 1]) if nbr == 4 else print(typ) def check_script(path, script_name): @@ -161,19 +161,19 @@ def check_sudo_prefix_commands(script): """ Check if commands are prefix with "sudo" """ - cmd = ("rm", "chown", "chmod", "apt-get", "apt", + cmds = ("rm", "chown", "chmod", "apt-get", "apt", "service", "yunohost", "find" "swapon", "mkswap", "useradd") # , "dd") cp, mkdir ok = True - for line_nbr, i in enumerate(script): - for j in cmd: - if j + " " in i and "sudo " + j + " " not in i \ - and "yunohost service" not in i and "-exec " + j not in i \ - and ".service" not in i and i[0] != '#': + for line_nbr, line in enumerate(script): + for cmd in cmds: + if cmd + " " in line and "sudo " + cmd + " " not in line \ + and "yunohost service" not in line and "-exec " + cmd not in line \ + and ".service" not in line and line[0] != '#': print(c.FAIL + "✘ Line ", line_nbr + 1, "you should add \"sudo\" before this command line:", c.END) - print(" " + i.replace(j, - c.BOLD + c.FAIL + j + c.END)) + print(" " + line.replace(cmd, + c.BOLD + c.FAIL + cmd + c.END)) ok = False if ok: print_right("All commands are prefix with \"sudo\".") @@ -184,21 +184,21 @@ def check_verifications_done_before_modifying_system(script): Check if verifications are done before modifying the system """ ex = 0 - for line_number, i in enumerate(script): - if "ynh_die" in i or "exit" in i: + for line_number, line in enumerate(script): + if "ynh_die" in line or "exit" in line: ex = line_number - cmd = ("cp", "mkdir", "rm", "chown", "chmod", "apt-get", "apt", "service", + cmds = ("cp", "mkdir", "rm", "chown", "chmod", "apt-get", "apt", "service", "find", "sed", "mysql", "swapon", "mount", "dd", "mkswap", "useradd") # "yunohost" ok = True - for line_number, i in enumerate(script): - if line_number >= ex: + for line_nbr, line in enumerate(script): + if line_nbr >= ex: break - for j in cmd: - if j in i and i[0] != '#': + for cmd in cmds: + if cmd in line and line[0] != '#': ok = False if not ok: @@ -218,9 +218,9 @@ def check_non_helpers_usage(script): """ ok = True - for line_number, i in enumerate(script): - if "yunohost app setting" in i: - print_wrong("Line {}: 'yunohost app setting' command is deprecated, please use helpers ynh_app_setting_(set,get,delete).".format(line_number + 1)) + for line_nbr, line in enumerate(script): + if "yunohost app setting" in line: + print_wrong("Line {}: 'yunohost app setting' command is deprecated, please use helpers ynh_app_setting_(set,get,delete).".format(line_nbr + 1)) ok = False if ok: @@ -230,9 +230,9 @@ def check_non_helpers_usage(script): ok = True - for line_number, i in enumerate(script): - if "exit" in i: - print_wrong("Line {}: 'exit' command shouldn't be used. Use 'ynh_die' helper instead.".format(line_number + 1)) + for line_nbr, line in enumerate(script): + if "exit" in line: + print_wrong("Line {}: 'exit' command shouldn't be used. Use 'ynh_die' helper instead.".format(line_nbr + 1)) ok = False if ok: @@ -256,5 +256,5 @@ if __name__ == '__main__': if filename not in scripts and filename[-4:] != ".swp": scripts.append(filename) - for i in scripts: - check_script(app_path, i) + for script in scripts: + check_script(app_path, script) From b14023d2f3d5bbf3b719ef6cbee64a40139b5b55 Mon Sep 17 00:00:00 2001 From: Moul Date: Mon, 19 Dec 2016 10:09:32 +0400 Subject: [PATCH 5/5] [mod] call one time 'print()'. --- package_linter.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package_linter.py b/package_linter.py index b90b5d4..1b5902b 100755 --- a/package_linter.py +++ b/package_linter.py @@ -18,10 +18,10 @@ class c: def header(app_path): print(c.UNDERLINE + c.HEADER + c.BOLD + - "YUNOHOST APP PACKAGE LINTER\n" + c.END) - print("App packaging documentation: https://yunohost.org/#/packaging_apps") - print("App package example: https://github.com/YunoHost/example_ynh\n") - print("Checking " + c.BOLD + app_path + c.END + " package\n") + "YUNOHOST APP PACKAGE LINTER\n", c.END, + "App packaging documentation: https://yunohost.org/#/packaging_apps\n", + "App package example: https://github.com/YunoHost/example_ynh\n", + "Checking " + c.BOLD + app_path + c.END + " package\n") def print_right(str): @@ -203,9 +203,9 @@ def check_verifications_done_before_modifying_system(script): if not ok: print(c.FAIL + "✘ At line", ex + 1, - "'ynh_die' or 'exit' command is executed with system modification before.") - print("This system modification is an issue if a verification exit the script.") - print("You should move this verification before any system modification." + c.END) + "'ynh_die' or 'exit' command is executed with system modification before.\n", + "This system modification is an issue if a verification exit the script.\n", + "You should move this verification before any system modification." + c.END) else: print_right( "Verifications (with 'ynh_die' or 'exit' commands) are done before any system modification.")