1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#! /usr/bin/python3
import argparse
import os
import sys
sys.path.append("/usr/share/dh-python")
import dhpython.tools
class ArgumentParser(argparse.ArgumentParser):
""" Command-line argument parser for this application. """
default_description = "Tool to change shebang line of an executable script"
def __init__(self, *args, **kwargs):
""" Initialise this instance. """
if 'description' not in kwargs:
kwargs['description'] = self.default_description
super().__init__(*args, **kwargs)
self.setup_arguments()
def setup_arguments(self):
""" Set up arguments special to this program. """
self.add_argument(
'--interpreter',
action='store',
default=sys.executable,
metavar="PATH",
help=(
"set interpreter to PATH"
" (default %(default)s)"
),
)
self.add_argument(
'script_paths',
nargs='+',
metavar="SCRIPT",
help=(
"modify script file SCRIPT"
),
)
class FixShebangApplication:
""" Program application behaviour. """
argumentparser_class = ArgumentParser
def parse_commandline(self, argv):
""" Parse the command line for this application.
:param argv: Sequence of command-line arguments used to
invoke this application.
:return: ``None``.
The parsed argument collection is set on the `args`
attribute of this application.
"""
program_args = list(argv)
program_path = program_args.pop(0)
if program_path.endswith("__main__.py"):
# Module is actually a Python package invoked as main.
program_path = os.path.dirname(program_path)
program_name = os.path.basename(program_path)
parser = self.argumentparser_class(prog=program_name)
return parser.parse_args(program_args)
def initialise(self, argv):
""" Initialise this application.
:param argv: Sequence of command-line arguments used to
invoke this application.
:return: ``None``.
"""
self.args = self.parse_commandline(argv)
self.shebang_interpreter = self.args.interpreter
def run(self):
""" Run this application instance. """
for script_path in self.args.script_paths:
dhpython.tools.fix_shebang(script_path, self.shebang_interpreter)
def main(argv=None):
""" Run the program mainline code.
:param argv: Sequence of command-line arguments used to invoke
this process. Default: `sys.argv`.
:return: Exit status (integer) for this process.
"""
if argv is None:
argv = sys.argv
exit_status = os.EX_OK
app = FixShebangApplication()
try:
app.initialise(argv)
app.run()
except SystemExit as exc:
exit_status = exc.code
return exit_status
if __name__ == '__main__':
exit_status = main(sys.argv)
sys.exit(exit_status)
|