diff options
| author | Thomas Grothe <grothe.tr@gmail.com> | 2026-05-24 21:55:30 -0400 |
|---|---|---|
| committer | Thomas Grothe <grothe.tr@gmail.com> | 2026-05-24 21:55:30 -0400 |
| commit | a448247317db852faadda20078fcad0ad1831936 (patch) | |
| tree | 14da154f1203f42f72208b4d2a52953efababb9b /py/util.py | |
Diffstat (limited to 'py/util.py')
| -rwxr-xr-x | py/util.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/py/util.py b/py/util.py new file mode 100755 index 0000000..ad5fa62 --- /dev/null +++ b/py/util.py @@ -0,0 +1,41 @@ +from datetime import datetime +import os +import subprocess +from pathlib import Path + +#TODO bring code over from complete module + +datadir=Path.home() / '.local' / 'share' / 'pyutil' +logdir=datadir / 'log' + + +def cmd(cmdstr,v=False): + '''run cmdstr as a command in the shell, return the output''' + cmdarray = cmdstr.strip().split(' ') + log(f'runcmd: {cmdstr}') + #TODO handling pipe not yet working + proc = subprocess.run(cmdarray, stdout=subprocess.PIPE) + if proc.returncode == 0: + res = proc.stdout + else: + log(f'returncode = {proc.returncode}') + res = proc.stderr + return res + +def log(msg): + d = datetime.now().strftime('%Y%m%d') + fn = logdir / f'log-{d}.txt' + if not logdir.exists(): + os.makedirs(logdir, exist_ok=True) + with open(fn, 'a') as lf: + lf.write(f'{d}: {msg}\n') + lf.close() + +def tnow(): + ''' + return: current time, formatted as %Y%m%d-%H%M%S + ''' + return datetime.now().strftime('%Y%m%d-%H%M%S') + + +############################
\ No newline at end of file |
