From patchwork Sat Dec 5 12:09:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov via arch-projects X-Patchwork-Id: 1806 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 7074A1C8E83E8 for ; Sat, 5 Dec 2020 12:10:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=DKIMWL_WL_HIGH=-1.496, DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,HTML_MESSAGE=0.001, MAILING_LIST_MULTI=-1,SPF_HELO_PASS=-0.001,T_DMARC_POLICY_NONE=0.01 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-BL-Results: Received: from mail.archlinux.org (mail.archlinux.org [IPv6:2a01:4f9:c010:3052::1]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Sat, 5 Dec 2020 12:10:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=luna2; t=1607170195; bh=z/y07T22agelIsd4f0czhbfLy4btL9m3ERlr9Q9Uvxk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc; b=MPPZfzBdRC051gdDCgSDRwVuWaSWQT8oZsK9shLdY0y8Gs7MzciAWnhPsyIt+aQdl sVtr6Db+ysZNi+qs/Ufckvfi2ktGXXWCqEtbkULdKqlbvDiEUDAnpUlscl/2Eyc8CJ sVfsS3NEoZHcP9HGzN2hEKFa8LMu94X4gDjV1ZMbQwhLjU22ihUDSpHmLz358YxYEQ 6L6W7RAaIIvftbDle8GdYYAf+uLh92htLCGQ5NtMZyBfgcQtqUx1oKgHGM4jeV93Df 3aZDA2oZMEFgA8MXcStuUfYDonV2U1h9Rt/Y5nbCq5pVOlisj7gomWeSaXQ4c1CZ85 TTzNJuT9jmiWgswvywZe7Xy2JiEjvZu7r3PriiMlYlqE47s7COeSLbKw+nxz3pXy2t Mvdcm2uJ+DJIpfxY/NkBvgp42wFdihPFFZ7q3YppwhC7spw0DdaZLturObNLuXQLUE rvPKvxG4wxSNCgz+axmEIl4TZj9tftwqX2oYHNiBHErkDTTS2yxfoAo0sjcWD4E/3j yByf9C3IuYv/kQ17QVCOo0teCMHXPRhuxbPJFbWeM578m5eSL4zNKz8+6AruOwV43I 8fs2dcuIflwVJdUBPOrGI3MlD5Sea8EcXYfgo7C0V7iIyMdA7PID9ORkEH0XdwXy/y q2PDDaJuwNR9YQa+HEstccXI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=archlinux.org; s=dkim-rsa; t=1607170196; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=z/y07T22agelIsd4f0czhbfLy4btL9m3ERlr9Q9Uvxk=; b=kvDHVo2XX85uQG3CXj5va8j7XZI3O2arkO3folAEcqnacp3iS1F61aW/1o7Zi6YYEz30fp PgMJIrUn6BxH4u5yyNEP3FDGhIRnow5Tf16ny8SH3EwSN4uiDFe0/qdqGgRGoklzeiu2aH UBIPnEBR4DhAtdLNzbWuuKJGu/9L1vhTvCU7O7nkLMnMi6NNrk6USM/9sRIjwxqDvMUtW7 jKS8r1hhRlBziFK1xDVYnF0mtOwahh0fTLgvr59DuI5zZ8MBaHsQet7/MlrcYHR9s49SqT MGDtF0Que5HAVqpC1BWyVZbbmIMcZySJk3/KO+irMpiYQ5tQx7BFQNkiuM1LBjLsMal1lS OcMan0FEMSZNqfb22shY9RS1hcL1xZGM03uTux8UgV6iVbsvFeb0HX4ZtxRVm2siyz/ZFw 1Em05sncvCORu7aE355eSCzqSPoDUQgCYv5diIAOCaiJlY7pKvFhigjkuIRhWy+QFwJpRE IDjRr8JdI9jIvOtKKu2rlhVV46HchD66kLSwTHkTlNEolkB87C4VhQgFXDDU6YPIIfTaN4 KwwfQv9itQBJzgmhK7J+G4yRGqQTgNf+Tzkcu2FKJIO31Tji4lnb2GUPkDZLSkkMzkq5Tm XFjWudDF8vzwUcyJykUaTtf9oL3a79ZtziMDdeQ0YnqMdpbhcWaMM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=archlinux.org; s=dkim-ed25519; t=1607170196; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=z/y07T22agelIsd4f0czhbfLy4btL9m3ERlr9Q9Uvxk=; b=/wu782BkFqk2c5XdpWAPjLDlhq4xJN9mIDot0kbWSUSdnz7qlJDGp4mUw+vN2nhbtizTkV 2Pj/rWp3doSTGtAw== Date: Sat, 05 Dec 2020 12:09:39 +0000 To: "arch-projects@archlinux.org" Message-ID: MIME-Version: 1.0 X-Rspamd-Queue-Id: 2C654289987 X-Spamd-Result: default: False [0.10 / 15.00]; HAS_REPLYTO(0.00)[meganomic@pm.me]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[pm.me:s=protonmail]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_ALLOW(-0.20)[+ip4:185.70.40.0/24]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; HAS_PHPMAILER_SIG(0.00)[]; MIME_BASE64_TEXT_BOGUS(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; DKIM_TRACE(0.00)[pm.me:+]; MIME_BASE64_TEXT(0.10)[]; DMARC_POLICY_ALLOW(-0.50)[pm.me,quarantine]; NEURAL_HAM(-0.00)[-1.000]; TO_DN_EQ_ADDR_ALL(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; ASN(0.00)[asn:62371, ipnet:185.70.40.0/24, country:CH]; MID_RHS_MATCH_FROM(0.00)[]; RWL_MAILSPIKE_POSSIBLE(0.00)[185.70.40.18:from] X-Rspamd-Server: mail.archlinux.org Subject: [arch-projects] [namcap] [PATCH] Change from getopt to argparse X-BeenThere: arch-projects@archlinux.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Arch Linux projects development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: meganomic via arch-projects Reply-To: meganomic , Arch Linux projects development discussion Cc: meganomic Errors-To: arch-projects-bounces@archlinux.org Sender: "arch-projects" Authentication-Results: mail.archlinux.org; auth=pass smtp.auth=luna smtp.mailfrom=arch-projects-bounces@archlinux.org Simple change from using getopt to using argparse. Also fixes https://bugs.archlinux.org/task/56995 diff --git a/namcap.py b/namcap.py index a7f532a..bb2754e 100755 --- a/namcap.py +++ b/namcap.py @@ -19,10 +19,10 @@ # # -import getopt import os import sys import tarfile +import argparse import Namcap.depends import Namcap.tags @@ -33,22 +33,6 @@ def get_modules(): """Return all possible modules (rules)""" return Namcap.rules.all_rules -def usage(): - """Display usage information""" - print("") - print("Usage: " + sys.argv[0] + " [OPTIONS] packages") - print("") - print("Options are:") - print(" -L, --list : list available rules") - print(" -i : prints information (debug) responses from rules") - print(" -m : makes the output parseable (machine-readable)") - print(" -e rulelist, --exclude=rulelist : don't apply RULELIST rules to the package") - print(" -r rulelist, --rules=rulelist : only apply RULELIST rules to the package") - print(" -t tags : use a custom tag file") - print(" -v version : print version and exit") - - sys.exit(2) - def open_package(filename): try: tar = tarfile.open(filename, "r") @@ -61,17 +45,6 @@ def open_package(filename): return None return tar -def check_rules_exclude(optlist): - '''Check if the -r (--rules) and the -r (--exclude) options - are being used at same time''' - args_used = 0 - for i in optlist: - if '-r' in i or '-e' in i: - args_used += 1 - if '--rules' in i or '--exclude' in i: - args_used += 1 - return args_used - def show_messages(name, key, messages): for msg in messages: print("%s %s: %s" % (name, key, Namcap.tags.format_message(msg))) @@ -162,74 +135,57 @@ def process_pkgbuild(package, modules): # Main modules = get_modules() -info_reporting = 0 -machine_readable = False -filename = None - -# get our options and process them -try: - optlist, args = getopt.getopt(sys.argv[1:], "ihmr:e:t:Lv", - ["info", "help", "machine-readable", "rules=", - "exclude=", "tags=", "list", "version"]) -except getopt.GetoptError: - usage() - -active_modules = {} -# Verifying if we are using the -r and -r options at same time -if check_rules_exclude(optlist) > 1: - print("You cannot use '-r' (--rules) and '-e' (-exclude) options at same time") - usage() - -for i, k in optlist: - if i in ('-L', '--list'): +# Let's handle those options! +version = Namcap.version.get_version() + +parser = argparse.ArgumentParser() +parser.add_argument('-L', '--list', action='store_true', help='List available rules') +parser.add_argument('-i', '--info', action='store_const', const=1, default=0, help='Prints information (debug) responses from rules') +parser.add_argument('-m', '--machine-readable', action='store_true', help='Makes the output parseable (machine-readable)') +parser.add_argument('-t', '--tags', action='store', help='Use a custom tag file') +parser.add_argument('packages', nargs='+') +pargroup = parser.add_mutually_exclusive_group() +pargroup.add_argument('-e', '--exclude', action='store', metavar='RULELIST', help="Don't apply RULELIST rules to the package") +pargroup.add_argument('-r', '--rules', action='store', metavar='RULELIST', help='Only apply RULELIST rules to the packag') +parser.add_argument('-v', '--version', action='version', version=version) +args = parser.parse_args() + +# Do something with all these options +if (args.list): print("-"*20 + " Namcap rule list " + "-"*20) for j in sorted(modules): print("%-20s: %s" % (j, modules[j].description)) - sys.exit(2) - - if i in ('-r', '--rules'): - module_list = k.split(',') - for j in module_list: - if j in modules: - active_modules[j] = modules[j] - else: - print("Error: Rule '%s' does not exist" % j) - usage() - - # Used to exclude some rules from the check - if i in ('-e', '--exclude'): - module_list = k.split(',') - active_modules.update(modules) - for j in module_list: - if j in modules: - active_modules.pop(j) - else: - print("Error: Rule '%s' does not exist" % j) - usage() - - if i in ('-i', '--info'): - info_reporting = 1 - - if i in ('-h', '--help'): - usage() - if i in ('-m', '--machine-readable'): - machine_readable = True - - if i in ('-t', '--tags'): - filename = k - - if i in ('-v', '--version'): - print(Namcap.version.get_version()) - sys.exit(0) - -# If there are no args, print usage -if (args == []): - usage() + parser.exit(2) -Namcap.tags.load_tags(filename = filename, machine = machine_readable) +info_reporting = args.info +machine_readable = args.machine_readable +filename = args.tags -packages = args +if (args.rules): + module_list = args.rules + for j in module_list: + if j in modules: + active_modules[j] = modules[j] + else: + print("Error: Rule '%s' does not exist" % j) + parser.exit(2) + +if (args.exclude): + module_list = args.exclude + active_modules.update(modules) + for j in module_list: + if j in modules: + active_modules.pop(j) + else: + print("Error: Rule '%s' does not exist" % j) + parser.exit(2) + +packages = args.packages + +active_modules = {} + +Namcap.tags.load_tags(filename = filename, machine = machine_readable) # No rules selected? Then select them all! if len(active_modules) == 0: @@ -239,7 +195,7 @@ if len(active_modules) == 0: for package in packages: if not os.access(package, os.R_OK): print("Error: Problem reading %s" % package) - usage() + parser.print_usage() if os.path.isfile(package) and tarfile.is_tarfile(package): process_realpackage(package, active_modules)