From c6d08d84a9f2ab59330463a95df6a220ac228570 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Fri, 8 Sep 2023 09:21:56 +0200 Subject: [PATCH] fix main, tests of scripts (not done) --- python/nav/bin/collect_active_ip.py | 18 ++++++++++++------ python/nav/bin/emailreports.py | 6 ++++-- python/nav/bin/nav.py | 13 +++++++------ python/nav/bin/radiusparser.py | 18 ++++++++++-------- python/nav/bin/servicemon.py | 10 ++++++---- python/nav/bin/sortedstats_cacher.py | 10 +++++++--- python/nav/bin/start_arnold.py | 8 +++++--- setup.py | 8 -------- tests/docker/test.sh | 8 ++++---- tests/integration/bin_test.py | 7 +++++-- tests/integration/conftest.py | 3 ++- 11 files changed, 62 insertions(+), 47 deletions(-) diff --git a/python/nav/bin/collect_active_ip.py b/python/nav/bin/collect_active_ip.py index bb5cffb55c..625e9a3085 100755 --- a/python/nav/bin/collect_active_ip.py +++ b/python/nav/bin/collect_active_ip.py @@ -37,8 +37,11 @@ _logger = logging.getLogger('nav.ipcollector') -def main(days=None): +def main(args=None): """Controller""" + if args == None: + args = get_parser().parse_args() + days = args.days or None exit_if_already_running() init_generic_logging(logfile=LOGFILE, stderr=False) run(days) @@ -62,9 +65,9 @@ def run(days): _logger.info('Done in %.2f seconds', time.time() - starttime) -if __name__ == '__main__': - _parser = argparse.ArgumentParser() - _parser.add_argument( +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument( "-d", "--days", default=None, @@ -72,5 +75,8 @@ def run(days): help="days back in time to start collecting from", ) - _args = _parser.parse_args() - main(_args.days) + return parser + + +if __name__ == '__main__': + main() diff --git a/python/nav/bin/emailreports.py b/python/nav/bin/emailreports.py index b546b092c4..9734f0d2a2 100755 --- a/python/nav/bin/emailreports.py +++ b/python/nav/bin/emailreports.py @@ -33,8 +33,10 @@ _logger = logging.getLogger('emailreports') -def main(args): +def main(args=None): """Send all reports""" + if args == None: + args = get_parser().parse_args() init_generic_logging(logfile=LOGFILE, stderr=False) send_reports(args.period) @@ -50,4 +52,4 @@ def get_parser(): if __name__ == '__main__': - main(get_parser().parse_args()) + main() diff --git a/python/nav/bin/nav.py b/python/nav/bin/nav.py index 11f67f83f4..e032ba293b 100755 --- a/python/nav/bin/nav.py +++ b/python/nav/bin/nav.py @@ -22,10 +22,10 @@ import os.path import argparse import textwrap -from nav import colors +from .. import colors try: - from nav.startstop import ServiceRegistry, CommandFailedError, CrontabError + from ..startstop import ServiceRegistry, CommandFailedError, CrontabError except ImportError: print( "Fatal error: Could not find the nav.startstop module.\nIs your " @@ -45,10 +45,11 @@ sys.exit(1) -def main(args): +def main(args=None): """Main execution point""" - parser = make_argparser() - args = parser.parse_args() + if args == None: + parser = make_argparser() + args = parser.parse_args() try: args.func(args) except AttributeError: @@ -330,4 +331,4 @@ def command_config_install(args): # begin here # ############## if __name__ == '__main__': - main(sys.argv[1:]) + main() diff --git a/python/nav/bin/radiusparser.py b/python/nav/bin/radiusparser.py index cd9873df6c..2915f151df 100755 --- a/python/nav/bin/radiusparser.py +++ b/python/nav/bin/radiusparser.py @@ -52,7 +52,15 @@ my_logfile = "./radiusparser.log" # Location of this program's debug log file -def main(args): +def main(args=None): + # If script is already running, abort + if pid_running(): + sys.exit(1) + else: + print("Running instance of script not found. Starting...") + + if args == None: + args = sys.argv[1:] try: db_params = (dbhost, dbport, dbname, dbuser, dbpasswd) @@ -460,10 +468,4 @@ def iter_lines(file): ########### if __name__ == '__main__': - # If script is already running, abort - if pid_running(): - sys.exit(1) - else: - print("Running instance of script not found. Starting...") - - main(sys.argv[1:]) + main() diff --git a/python/nav/bin/servicemon.py b/python/nav/bin/servicemon.py index 169c095d41..08f828c0bd 100755 --- a/python/nav/bin/servicemon.py +++ b/python/nav/bin/servicemon.py @@ -139,8 +139,12 @@ def signalhandler(self, signum, _): _logger.info("Caught %s. Resuming operation.", signum) -def main(foreground): +def main(args=None): """Daemon main entry point""" + os.umask(0o0002) + if args == None: + args = parse_args() + foreground = args.foreground conf = config.serviceconf() pidfilename = conf.get("pidfile", "servicemon.pid") @@ -174,6 +178,4 @@ def parse_args(): if __name__ == '__main__': - os.umask(0o0002) - args = parse_args() - main(args.foreground) + main() diff --git a/python/nav/bin/sortedstats_cacher.py b/python/nav/bin/sortedstats_cacher.py index bce6709985..fd90607b19 100755 --- a/python/nav/bin/sortedstats_cacher.py +++ b/python/nav/bin/sortedstats_cacher.py @@ -18,7 +18,7 @@ _logger = logging.getLogger('nav.sortedstats_cacher') -def main(timeframe, config): +def run(timeframe, config): _logger.info("Running for timeframe %s", timeframe) reports = config.get_reports(timeframe) for report_name, report in reports.items(): @@ -48,11 +48,15 @@ def exit_if_running(pidfile): exit(1) -if __name__ == '__main__': +def main(): init_generic_logging(logfile=LOGFILE, stderr=False, read_config=True) timeframe = get_parser().parse_args().timeframe pidfile = f"sortedstats_cacher_{timeframe}.pid" exit_if_running(pidfile) writepidfile(pidfile) config = SortedStatsConfig() - main(timeframe, config) + run(timeframe, config) + + +if __name__ == '__main__': + main() diff --git a/python/nav/bin/start_arnold.py b/python/nav/bin/start_arnold.py index a834933a29..4cc7968d08 100755 --- a/python/nav/bin/start_arnold.py +++ b/python/nav/bin/start_arnold.py @@ -63,9 +63,12 @@ _logger = logging.getLogger('nav.start_arnold') -def main(args): +def main(args=None): """Main controller""" + if args == None: + args = parse_command_options() + init_generic_logging(logfile=LOG_FILE, stderr=False, read_config=True) if args.listblocktypes: @@ -316,5 +319,4 @@ def valid_profile(detention_profile_id): if __name__ == '__main__': - _args = parse_command_options() - main(_args) + main() diff --git a/setup.py b/setup.py index d3f92f131f..c6e74831b0 100644 --- a/setup.py +++ b/setup.py @@ -3,20 +3,12 @@ from setuptools.command.build import build -def find_scripts(): - for candidate in glob('bin/*'): - with open(candidate) as handle: - if handle.readline().startswith("#!"): - yield candidate - - # Ensure CSS files are built every time build is invoked build.sub_commands = [('build_sass', None)] + build.sub_commands setup( setup_requires=['libsass', 'setuptools_scm'], - scripts=list(find_scripts()), sass_manifests={ 'nav.web': { 'sass_path': 'sass', diff --git a/tests/docker/test.sh b/tests/docker/test.sh index 0fdcab38d1..34335eb3ab 100755 --- a/tests/docker/test.sh +++ b/tests/docker/test.sh @@ -2,10 +2,10 @@ # MAIN EXECUTION POINT cd "$WORKSPACE" -tox +tox -e integration-py37-django32 -- tests/integration/bin_test.py -# Code analysis steps -tox -e pylint -/count-lines-of-code.sh +# # Code analysis steps +# tox -e pylint +# /count-lines-of-code.sh echo "test.sh done" diff --git a/tests/integration/bin_test.py b/tests/integration/bin_test.py index 72a7fb3920..1c07a8908e 100644 --- a/tests/integration/bin_test.py +++ b/tests/integration/bin_test.py @@ -3,6 +3,9 @@ import subprocess +BINDIR = './python/nav/bin' + + def test_binary_runs(binary): """Verifies that a command runs with a zero exit code""" proc = subprocess.Popen(binary, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) @@ -23,7 +26,7 @@ def test_naventity_runs_without_error_with_arguments(localhost, snmpsim): Added in regards to: https://github.com/Uninett/nav/issues/2433 """ proc = subprocess.Popen( - ["./bin/naventity", localhost.ip, "-p", "1024"], + [BINDIR + "/naventity.py", localhost.ip, "-p", "1024"], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, ) @@ -45,7 +48,7 @@ def test_nav_runs_without_error_without_arguments(): Added in regards to: https://github.com/Uninett/nav/issues/2601 """ proc = subprocess.Popen( - ["./bin/nav"], + [BINDIR + "/nav.py"], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, ) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5f74f1d6a0..a0c4dd5061 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -86,7 +86,7 @@ def stop_gunicorn(): r'^# +-\*-\s*testargs:\s*(?P.*?)\s*(-\*-)?\s*$', re.MULTILINE ) NOTEST_PATTERN = re.compile(r'^# +-\*-\s*notest\s*(-\*-)?\s*$', re.MULTILINE) -BINDIR = './bin' +BINDIR = './python/nav/bin' def pytest_generate_tests(metafunc): @@ -119,6 +119,7 @@ def _is_excluded(filename): return ( filename.endswith('~') or filename.startswith('.') + or filename.startswith('__') or filename.startswith('Makefile') )