diff --git a/python/online/fxreader/pr34/commands_typed/status.py b/python/online/fxreader/pr34/commands_typed/status.py index 1d136c2..94d7977 100644 --- a/python/online/fxreader/pr34/commands_typed/status.py +++ b/python/online/fxreader/pr34/commands_typed/status.py @@ -1,5 +1,8 @@ import sys +import datetime +import time import os +import signal import io import json import subprocess @@ -17,7 +20,48 @@ from typing import ( logger = logging.getLogger(__name__) +def get_info( + sh: list[str], + timeout: int | float, +): + t1: list[str] = [] + + for sh_index, o in enumerate( + [ + *sh, + *[ + r""" + A=$(free -h | grep -P Mem: | grep -Po '[\w\.\d]+'); + echo -n $A | awk '{print $2, $7}'; + """, + r""" + date +'%Y-%m-%d %l:%M:%S %p'; + """, + ], + ] + ): + try: + t1.append( + subprocess.check_output( + o, + shell=True, + timeout=timeout, + ) + .decode('utf-8') + .strip() + ) + except Exception: + t1.append('fail %d' % sh_index) + + t3 = ' | '.join(t1).replace('\n\r', '') + + sys.stdout.write(t3) + sys.stdout.flush() + + def run(argv: list[str]): + logging.basicConfig(level=logging.INFO) + assert isinstance(argv, list) and all([isinstance(o, str) for o in argv]) class c1(optparse.IndentedHelpFormatter): @@ -81,6 +125,13 @@ def run(argv: list[str]): default=None, type=float, ) + add_option( + parser, + '--repeat_interval', + dest='repeat_interval', + default=None, + type=float, + ) add_option( parser, '--config', @@ -122,39 +173,37 @@ printf '% 3.0f%%' $(upower -d | grep -Po 'percentage:\\s+\\d+(\\.\\d+)?%' | grep options.sh.extend(config.get('sh', [])) - t1: list[str] = [] + last_ts = datetime.datetime.now() - for sh_index, o in enumerate( - [ - *options.sh, - *[ - r""" - A=$(free -h | grep -P Mem: | grep -Po '[\w\.\d]+'); - echo -n $A | awk '{print $2, $7}'; - """, - r""" - date +'%Y-%m-%d %l:%M:%S %p'; - """, - ], - ] - ): - try: - t1.append( - subprocess.check_output( - o, - shell=True, - timeout=timeout2, - ) - .decode('utf-8') - .strip() - ) - except Exception: - t1.append('fail %d' % sh_index) + shutdown: bool = False - t3 = ' | '.join(t1).replace('\n\r', '') + def on_signal(*args: Any, **kwargs: Any): + nonlocal shutdown - sys.stdout.write(t3) - sys.stdout.flush() + shutdown = True + + signal.signal(signal.SIGINT, on_signal) + signal.signal(signal.SIGTERM, on_signal) + + while not shutdown: + get_info( + timeout=timeout2, + sh=options.sh, + ) + + if not options.repeat_interval: + break + else: + sys.stdout.write('\n') + sys.stdout.flush() + + now_ts = datetime.datetime.now() + spent = (now_ts - last_ts).total_seconds() + + last_ts = last_ts + datetime.timedelta(seconds=options.repeat_interval) + + if spent < options.repeat_interval: + time.sleep(options.repeat_interval - spent) if __name__ == '__main__': diff --git a/releases/whl/online_fxreader_pr34-0.1.5.43-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.43-py3-none-any.whl new file mode 100644 index 0000000..af632a1 --- /dev/null +++ b/releases/whl/online_fxreader_pr34-0.1.5.43-py3-none-any.whl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:faeea76f81f8658b0fa511c3d3d20480ce3f22ceac0318704fe304018cb966e9 +size 82857