summaryrefslogtreecommitdiff
path: root/py/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'py/util.py')
-rwxr-xr-xpy/util.py41
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