Add do_tasks and taskme.

This commit is contained in:
voussoir 2019-06-25 15:32:03 -07:00
parent f44e46aab5
commit bcc9700d67
2 changed files with 54 additions and 0 deletions

32
do_tasks.py Normal file
View file

@ -0,0 +1,32 @@
'''
Execute the contents of all .task files forever.
'''
import os
import sys
import time
def main(args=None):
if args:
task_files = args
do_loop = False
else:
do_loop = True
while True:
if do_loop:
task_files = [f for f in os.listdir() if (os.path.isfile(f) and f.endswith('.task'))]
for task_file in task_files:
with open(task_file, 'r', encoding='utf-8') as handle:
task_content = handle.read()
task_content = task_content.strip()
print('TASK:', task_content)
status = os.system(task_content)
if status == 0:
os.remove(task_file)
if not do_loop:
break
time.sleep(10)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))

22
taskme.py Normal file
View file

@ -0,0 +1,22 @@
'''
Create a task file to be executed by do_tasks.
Usage:
taskme command arg1 arg2
'''
import sys
import time
import random
import os
if len(sys.argv) < 2:
raise ValueError()
task_command = ' '.join('"%s"' % arg for arg in sys.argv[1:])
task_command = f'cd /d {os.getcwd()} & {task_command}'
timestamp = f'{time.time()}'.replace('.', '').ljust(18, '0')
filename = f'C:\\tasks\\{timestamp}-{random.randint(1, 1000000)}.task'
with open(filename, 'w', encoding='utf-8') as handle:
handle.write(task_command)