From b2cd618e62e66b2b41db5ac5fe85854ec0673302 Mon Sep 17 00:00:00 2001 From: "Travis A. Everett" Date: Tue, 19 Sep 2023 21:13:31 -0500 Subject: [PATCH] swing --- resholve | 84 ++++++++++++++++++------------------ tests/helpers.bash | 15 ++++++- tests/linux_parse_flatpak.sh | 1 + tests/parse_flatpak.sh | 6 --- 4 files changed, 57 insertions(+), 49 deletions(-) create mode 100644 tests/linux_parse_flatpak.sh delete mode 100644 tests/parse_flatpak.sh diff --git a/resholve b/resholve index c31e44c..d37d4fc 100755 --- a/resholve +++ b/resholve @@ -1395,10 +1395,8 @@ class CommandParser(theirparse.ArgumentParser): - version, variant, description of how they differ, platform """ - def __init__(self, name, variant="generic"): # , **kwargs): - theirparse.ArgumentParser.__init__( - self, prog="{:} ({:})".format(name, variant), add_help=False # , **kwargs - ) + def __init__(self, **kwargs): + theirparse.ArgumentParser.__init__(self, **kwargs) self.register("action", "invocations", InvocationsAction) # self.register("action", "invocation", InvocationAction) @@ -1428,6 +1426,10 @@ class CommandParser(theirparse.ArgumentParser): return ret +def make_command_parser(name, variant="generic", cls=CommandParser): + return cls(prog="{:} ({:})".format(name, variant), add_help=False) + + class FindParser(CommandParser): """ find's exec options (-exec -execdir -ok -okdir) @@ -1544,7 +1546,7 @@ TODO: class BuiltinCommandParsers(object): @staticmethod def _alias(): - generic = CommandParser("alias", "builtin") + generic = make_command_parser("alias", "builtin") generic.add_argument("-p", action="store_true") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" @@ -1553,7 +1555,7 @@ class BuiltinCommandParsers(object): @staticmethod def _builtin(): - generic = CommandParser("builtin", "builtin") + generic = make_command_parser("builtin", "builtin") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" ) @@ -1561,7 +1563,7 @@ class BuiltinCommandParsers(object): @staticmethod def _coproc(): - generic = CommandParser("coproc", "builtin") + generic = make_command_parser("coproc", "builtin") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" ) @@ -1569,7 +1571,7 @@ class BuiltinCommandParsers(object): @staticmethod def _command(): - generic = CommandParser("command", "builtin") + generic = make_command_parser("command", "builtin") generic.add_argument("-p", action="store_true") generic.add_argument("-v", action="store_true") generic.add_argument("-V", action="store_true") @@ -1580,7 +1582,7 @@ class BuiltinCommandParsers(object): @staticmethod def _eval(): - generic = CommandParser("eval", "builtin") + generic = make_command_parser("eval", "builtin") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" ) @@ -1601,7 +1603,7 @@ class BuiltinCommandParsers(object): -c execute COMMAND with an empty environment -l place a dash in the zeroth argument to COMMAND """ - generic = CommandParser("exec", "builtin") + generic = make_command_parser("exec", "builtin") generic.add_argument("-a") generic.add_argument("-c", action="store_true") generic.add_argument("-l", action="store_true") @@ -1612,7 +1614,7 @@ class BuiltinCommandParsers(object): @staticmethod def _source(): - generic = CommandParser("source", "builtin") + generic = make_command_parser("source", "builtin") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" ) @@ -1620,7 +1622,7 @@ class BuiltinCommandParsers(object): @staticmethod def _type(): - generic = CommandParser("type", "builtin") + generic = make_command_parser("type", "builtin") generic.add_argument("-p", action="store_true") generic.add_argument("-v", action="store_true") generic.add_argument("-V", action="store_true") @@ -1646,7 +1648,7 @@ class ExternalCommandParsers(object): --help display this help and exit --version output version information and exit """ - generic = CommandParser("chroot") + generic = make_command_parser("chroot") # bsd generic.add_argument("-g") generic.add_argument("-G") @@ -1676,7 +1678,7 @@ class ExternalCommandParsers(object): assignment-alikes should be stripped out already """ - generic = CommandParser("env") + generic = make_command_parser("env") generic.add_argument("-P") # bsd altpath # gnu generic.add_argument( @@ -1705,7 +1707,7 @@ class ExternalCommandParsers(object): """ coreutils src/install.c:execlp """ - generic = CommandParser("install") + generic = make_command_parser("install") generic.add_argument( "--strip-program", dest="commands", @@ -1720,7 +1722,7 @@ class ExternalCommandParsers(object): coreutils src/nice.c:execvp Usage: %s [OPTION] [COMMAND [ARG]...] """ - generic = CommandParser("nice") + generic = make_command_parser("nice") generic.add_argument("-n") generic.add_argument("--adjustment") generic.add_argument( @@ -1734,7 +1736,7 @@ class ExternalCommandParsers(object): coreutils src/nohup.c:execvp Usage: %s [OPTION] [COMMAND [ARG]...] """ - generic = CommandParser("nohup") + generic = make_command_parser("nohup") generic.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" ) @@ -1749,7 +1751,7 @@ class ExternalCommandParsers(object): two mutually-exclusive forms; if opts are present, the command is the first arg--if not, there's a combined context first. """ - opt_context = CommandParser("runcon", "opt context") + opt_context = make_command_parser("runcon", "opt context") context = opt_context.add_mutually_exclusive_group(required=True) context.add_argument("-c", "--compute", action="store_true") context.add_argument("-t", "--type") @@ -1761,7 +1763,7 @@ class ExternalCommandParsers(object): ) # these two forms are mutua - combined_context = CommandParser("runcon", "combined context") + combined_context = make_command_parser("runcon", "combined context") combined_context.add_argument("context") combined_context.add_argument( "commands", nargs=theirparse.REMAINDER, action="invocations" @@ -1773,7 +1775,7 @@ class ExternalCommandParsers(object): """ coreutils src/sort.c:execlp """ - generic = CommandParser("sort") + generic = make_command_parser("sort") generic.add_argument( "--compress-program", dest="commands", @@ -1788,7 +1790,7 @@ class ExternalCommandParsers(object): """ coreutils src/split.c:execl """ - generic = CommandParser("split") + generic = make_command_parser("split") generic.add_argument( "--filter", dest="commands", @@ -1804,7 +1806,7 @@ class ExternalCommandParsers(object): coreutils src/stdbuf.c:? Usage: %s [OPTION] [COMMAND [ARG]...] """ - generic = CommandParser("stdbuf") + generic = make_command_parser("stdbuf") generic.add_argument("-i", "--input") generic.add_argument("-o", "--output") generic.add_argument("-e", "--error") @@ -1819,7 +1821,7 @@ class ExternalCommandParsers(object): coreutils src/timeout.c:execvp Usage: %s [OPTION] [COMMAND [ARG]...] """ - generic = CommandParser("timeout") + generic = make_command_parser("timeout") generic.add_argument("-k", "--kill-after") generic.add_argument("-s", "--signal") generic.add_argument("-v", "--verbose", action="store_true") @@ -1834,7 +1836,7 @@ class ExternalCommandParsers(object): @staticmethod def _sqlite3(): """ """ - generic = CommandParser("sqlite3") + generic = make_command_parser("sqlite3") generic.add_argument( "-cmd", dest="commands", @@ -1846,7 +1848,7 @@ class ExternalCommandParsers(object): @staticmethod def _script(): - linux = CommandParser("script", "linux") + linux = make_command_parser("script", "linux") linux.add_argument("-a", "--append", action="store_true") linux.add_argument("-E", "--echo") linux.add_argument("-e", "--return", action="store_true") @@ -1871,7 +1873,7 @@ class ExternalCommandParsers(object): ) linux.add_argument("file", nargs="?") - bsd = CommandParser("script", "bsd") + bsd = make_command_parser("script", "bsd") bsd.add_argument("-d", action="store_true") bsd.add_argument("-k", action="store_true") bsd.add_argument("-p", action="store_true") @@ -1896,7 +1898,7 @@ class ExternalCommandParsers(object): requires the GnuSedParser hack-around, I'll split them to be a little clearer... """ - gnu = GnuSedParser("sed") + gnu = make_command_parser("sed", cls=GnuSedParser) # very unsure about this group part gscript = gnu.add_mutually_exclusive_group() gnu.add_argument("-a", action="store_true") # BSD-only @@ -1918,7 +1920,7 @@ class ExternalCommandParsers(object): gnu.add_argument("--version", action="store_true") gnu.add_argument("input_files", nargs="*") - bsd = CommandParser("sed") + bsd = make_command_parser("sed") # very unsure about this group part bscript = bsd.add_mutually_exclusive_group() bsd.add_argument("-a", action="store_true") # BSD-only @@ -1965,8 +1967,8 @@ class ExternalCommandParsers(object): -V --version """ - generic = CommandParser("awk") - assign_fallback = CommandParser("awk") + generic = make_command_parser("awk") + assign_fallback = make_command_parser("awk") def base(parser): # very unsure about this group part @@ -2029,7 +2031,7 @@ class ExternalCommandParsers(object): YES: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command] YES: sudo [-AbEHknPS] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [] """ - generic = CommandParser("sudo", "generic") + generic = make_command_parser("sudo", "generic") # 1. weed out sudo forms that don't need resolving # note: some of these long forms aren't known correct # because sudo re-uses a lot of flags @@ -2096,7 +2098,7 @@ class ExternalCommandParsers(object): if this doesn't work, we'll probably need N parsers... """ - generic = CommandParser("xargs") + generic = make_command_parser("xargs") # gnu generic.add_argument("-0", "--null", action="store_true") generic.add_argument("-a", "--arg-file") @@ -2145,7 +2147,7 @@ class ExternalCommandParsers(object): if this doesn't work, we'll probably need N parsers... """ - generic = CommandParser("rlwrap") + generic = make_command_parser("rlwrap") generic.add_argument("-a", "--always-readline", nargs="?") generic.add_argument("-A", "--ansi-colour-aware", action="store_true") generic.add_argument("-b", "--break-chars") @@ -2195,7 +2197,7 @@ class ExternalCommandParsers(object): -ok command ; -okdir command ; """ - generic = FindParser("find") + generic = make_command_parser("find", cls=FindParser) generic.add_argument("start") # boolean predicates, not quite like normal flags... # just capturing to ~ignore @@ -2226,7 +2228,7 @@ class ExternalCommandParsers(object): @staticmethod def _generic_shell(): - generic = CommandParser("generic_shell") + generic = make_command_parser("generic_shell") generic.add_argument( "-c", # rest are nonstandard; leaving breadcrumbs; @@ -2294,7 +2296,7 @@ class ExternalCommandParsers(object): -I, --use-compress-program=COMMAND Filter data through COMMAND. It must accept the -d option, for decompression. The argument can contain command line options. """ - generic = CommandParser("generic tar") + generic = make_command_parser("generic tar") """ TODO: I'm technically missing one here, but it is a bit harder than the rest. @@ -2387,7 +2389,7 @@ class ExternalCommandParsers(object): """ We need to sub-search expressions for ! commands """ - generic = CommandParser("dc") + generic = make_command_parser("dc") generic.add_argument("-e", "--expression", action="append") generic.add_argument("-f", "--file") generic.add_argument("-V", "--version", action="store_true") @@ -2400,7 +2402,7 @@ class ExternalCommandParsers(object): """ Based on Flatpak v1.14.4. """ - generic = CommandParser("flatpak") + generic = make_command_parser("flatpak") # flatpak --help generic.add_argument( @@ -2420,7 +2422,7 @@ class ExternalCommandParsers(object): ) subparsers = generic.add_subparsers() # flatpak update --help - update = subparsers.add_parser("update") + update = subparsers.add_parser("update", add_help=False) update.add_argument( "-h", "--help", "-u", "--user", "--system", action="store_true" ) @@ -3890,8 +3892,8 @@ class RecordCommandlike(object): return True def handle_external_generic(self, parsed, invocation): - # if not hasattr(parsed, "commands"): - # return True + if not hasattr(parsed, "commands"): + return True # TODO: shimming shells in here may be a bigger crime than # duplicating the logic in two functions would be? diff --git a/tests/helpers.bash b/tests/helpers.bash index 4393746..730a93c 100644 --- a/tests/helpers.bash +++ b/tests/helpers.bash @@ -171,6 +171,17 @@ require() { parsers() { cat parse_*.sh > parsed.sh - resholve --interpreter none --path "${PKG_PARSED}:${PKG_COREUTILS}" < parsed.sh > resolved.sh - bash -xe resolved.sh + case "$(uname -s)" in + Linux) + cat linux_parse_*.sh >> parsed.sh + ;; + Darwin) + : pass for now + ;; + *) + : pass for now + ;; + esac + resholve --interpreter none --path "${PKG_PARSED}:${PKG_COREUTILS}" parsed.sh + bash -xe parsed.sh.resolved } diff --git a/tests/linux_parse_flatpak.sh b/tests/linux_parse_flatpak.sh new file mode 100644 index 0000000..337562b --- /dev/null +++ b/tests/linux_parse_flatpak.sh @@ -0,0 +1 @@ +flatpak update diff --git a/tests/parse_flatpak.sh b/tests/parse_flatpak.sh deleted file mode 100644 index afe8cb1..0000000 --- a/tests/parse_flatpak.sh +++ /dev/null @@ -1,6 +0,0 @@ -if [[ $(uname -s) == "Darwin" ]]; then - : skip on darwin -else - flatpak update -v -fi -