1#!/usr/bin/python 2# Copyright (c) 2023 Nordic Semiconductor ASA 3# 4# SPDX-License-Identifier: Apache-2.0 5 6""" 7Simply mock for bash script to use with unit tests. 8""" 9 10import sys 11import time 12from argparse import ArgumentParser 13 14from zen_of_python import zen_of_python 15 16 17def main() -> int: 18 parser = ArgumentParser() 19 parser.add_argument('--sleep', action='store', default=0, type=float) 20 parser.add_argument('--long-sleep', action='store_true') 21 parser.add_argument('--return-code', action='store', default=0, type=int) 22 parser.add_argument('--exception', action='store_true') 23 24 args = parser.parse_args() 25 26 if args.exception: 27 # simulate crashing application 28 raise Exception 29 30 if args.long_sleep: 31 # prints data and wait for certain time 32 for line in zen_of_python: 33 print(line, flush=True) 34 time.sleep(args.sleep) 35 else: 36 # prints lines with delay 37 for line in zen_of_python: 38 print(line, flush=True) 39 time.sleep(args.sleep) 40 41 print('End of script', flush=True) 42 print('Returns with code', args.return_code, flush=True) 43 time.sleep(1) # give a moment for external programs to collect all outputs 44 return args.return_code 45 46 47if __name__ == '__main__': 48 sys.exit(main()) 49