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') ############################