Use the zipapp module of python instead of using zip and manually adding the hashbang

This commit is contained in:
Juhani Krekelä 2018-09-08 19:06:56 +03:00
parent 8c0505a077
commit 1b2bc64144
2 changed files with 97 additions and 12 deletions

View File

@ -15,28 +15,19 @@ sshwot-export-known-hosts: $(SSHWOT_EXPORT_KNOWN_HOSTS_MAIN) $(SSHWOT_EXPORT_KNO
mkdir -p build/$@
cp $(SSHWOT_EXPORT_KNOWN_HOSTS_DEPS) build/$@/
cp $(SSHWOT_EXPORT_KNOWN_HOSTS_MAIN) build/$@/__main__.py
zip --quiet --junk-paths build/$@.zip build/$@/*.py
echo '#!/usr/bin/env python3' > $@
cat build/$@.zip >> $@
chmod +x $@
python3 -m zipapp --output $@ -p "/usr/bin/env python3" build/$@
sshwot-filter: $(SSHWOT_FILTER_MAIN) $(SSHWOT_FILTER_DEPS)
mkdir -p build/$@
cp $(SSHWOT_FILTER_DEPS) build/$@/
cp $(SSHWOT_FILTER_MAIN) build/$@/__main__.py
zip --quiet --junk-paths build/$@.zip build/$@/*.py
echo '#!/usr/bin/env python3' > $@
cat build/$@.zip >> $@
chmod +x $@
python3 -m zipapp --output $@ -p "/usr/bin/env python3" build/$@
sshwot-verify: $(SSHWOT_VERIFY_MAIN) $(SSHWOT_VERIFY_DEPS)
mkdir -p build/$@
cp $(SSHWOT_VERIFY_DEPS) build/$@/
cp $(SSHWOT_VERIFY_MAIN) build/$@/__main__.py
zip --quiet --junk-paths build/$@.zip build/$@/*.py
echo '#!/usr/bin/env python3' > $@
cat build/$@.zip >> $@
chmod +x $@
python3 -m zipapp --output $@ -p "/usr/bin/env python3" build/$@
.PHONY: all clean distclean buildclean

94
src/main-alter.py Normal file
View File

@ -0,0 +1,94 @@
import argparse
import sys
def usage(file):
print('usage: %s add-comment [-o outfile] [-p port] [-f fingerprint] host comment [infile]' % sys.argv[0])
def add_comment():
parser = argparse.ArgumentParser(
description = """Add a comment to entry / entries matching the
given criteria""",
# We want to provide help on --help, but the default thing
# also adds -h, which we don't want
add_help = False,
# Display our name with the add-comment
prog = '% add-comment' % sys.argv[0]
)
# --help to get help
parser.add_argument('--help',
action = 'help',
help = 'show this help message and exit'
)
# -o can be used to write the results to a file instead of stdout
parser.add_argument('-o',
# Given one argument, we open a file of that name and store it
# to outfile, which will be sys.stdout.buffer otherwise.
# We use .buffer since we're going to write binary data
action = 'store',
dest = 'outfile',
type = argparse.FileType('wb'),
default = sys.stdout.buffer,
# This is what will be displayed in the help after -o
metavar = 'outfile',
help = 'write the sshwot file to a given file instead of the stdout'
)
# -p/--port for port (default port is 22)
parser.add_argument('-p', '--port',
action = 'store',
dest = 'port',
# Automatically convert to integer
type = int,
help = 'the port associated with the given host'
)
# -f/--fingerprint for fingerprint
parser.add_argument('-f', '--fingerprint',
action = 'store',
dest = 'fingerprint',
help = 'the fingerprint to filter for'
)
# Host and comment are required
parser.add_argument('host',
help = 'the domain to filter for'
)
parser.add_argument('comment',
help = 'the comment to add to the entries'
)
# Input file
parser.add_argument('infile',
nargs = '?',
type = argparse.FileType('rb'),
# Default to stdin. .buffer is because we do binary I/O
default = sys.stdin.buffer,
# The text shown for these in the usage
help = 'a sshwot file to alter'
)
# This automatically parses the command line args for us. If it
# returns, we have correct arguments
args = parser.parse_args(sys.argv[2:])
print(args) #debg
def main():
if len(sys.argv) < 2:
usage(sys.stderr)
sys.exit(1)
if sys.argv[1] == 'add-comment':
add_comment()
else:
usage(sys.stderr)
sys.exit(1)
if __name__ == '__main__':
try:
main()
except Exception as err:
print('Error: %s' % err, file=sys.stderr)
sys.exit(1)